Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/lib/vsprintf.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
| 8 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ |
| 9 | /* |
| 10 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
| 11 | */ |
| 12 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 13 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
| 15 | * - changed to provide snprintf and vsnprintf functions |
| 16 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> |
| 17 | * - scnprintf and vscnprintf |
| 18 | */ |
| 19 | |
| 20 | #include <stdarg.h> |
Rasmus Villemoes | ef27ac1 | 2019-03-07 16:27:03 -0800 | [diff] [blame] | 21 | #include <linux/build_bug.h> |
Stephen Boyd | 0d1d7a5 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 22 | #include <linux/clk.h> |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 23 | #include <linux/clk-provider.h> |
Rasmus Villemoes | 57f5677 | 2019-10-15 21:07:05 +0200 | [diff] [blame] | 24 | #include <linux/errname.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 25 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/types.h> |
| 27 | #include <linux/string.h> |
| 28 | #include <linux/ctype.h> |
| 29 | #include <linux/kernel.h> |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 30 | #include <linux/kallsyms.h> |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 31 | #include <linux/math64.h> |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 32 | #include <linux/uaccess.h> |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 33 | #include <linux/ioport.h> |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 34 | #include <linux/dcache.h> |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 35 | #include <linux/cred.h> |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 36 | #include <linux/rtc.h> |
Andy Shevchenko | 7daac5b | 2020-04-15 20:00:44 +0300 | [diff] [blame] | 37 | #include <linux/time.h> |
Andy Shevchenko | 2b1b0d6 | 2016-05-20 17:01:04 -0700 | [diff] [blame] | 38 | #include <linux/uuid.h> |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 39 | #include <linux/of.h> |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 40 | #include <net/addrconf.h> |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 41 | #include <linux/siphash.h> |
| 42 | #include <linux/compiler.h> |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 43 | #include <linux/property.h> |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 44 | #ifdef CONFIG_BLOCK |
| 45 | #include <linux/blkdev.h> |
| 46 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 48 | #include "../mm/internal.h" /* For the trace_print_flags arrays */ |
| 49 | |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 50 | #include <asm/page.h> /* for PAGE_SIZE */ |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 51 | #include <asm/byteorder.h> /* cpu_to_le16 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 53 | #include <linux/string_helpers.h> |
Alexey Dobriyan | 1dff46d | 2011-10-31 17:12:28 -0700 | [diff] [blame] | 54 | #include "kstrtox.h" |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 55 | |
Christophe Leroy | 83ed3e2 | 2022-02-17 09:49:59 +0100 | [diff] [blame] | 56 | /* Disable pointer hashing if requested */ |
| 57 | bool no_hash_pointers __ro_after_init; |
| 58 | EXPORT_SYMBOL_GPL(no_hash_pointers); |
| 59 | |
Richard Fitzgerald | 9e914f5 | 2021-05-14 17:12:04 +0100 | [diff] [blame] | 60 | static unsigned long long simple_strntoull(const char *startp, size_t max_chars, |
| 61 | char **endp, unsigned int base) |
| 62 | { |
| 63 | const char *cp; |
| 64 | unsigned long long result = 0ULL; |
| 65 | size_t prefix_chars; |
| 66 | unsigned int rv; |
| 67 | |
| 68 | cp = _parse_integer_fixup_radix(startp, &base); |
| 69 | prefix_chars = cp - startp; |
| 70 | if (prefix_chars < max_chars) { |
| 71 | rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars); |
| 72 | /* FIXME */ |
| 73 | cp += (rv & ~KSTRTOX_OVERFLOW); |
| 74 | } else { |
| 75 | /* Field too short for prefix + digit, skip over without converting */ |
| 76 | cp = startp + max_chars; |
| 77 | } |
| 78 | |
| 79 | if (endp) |
| 80 | *endp = (char *)cp; |
| 81 | |
| 82 | return result; |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * simple_strtoull - convert a string to an unsigned long long |
| 87 | * @cp: The start of the string |
| 88 | * @endp: A pointer to the end of the parsed string will be placed here |
| 89 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 90 | * |
Andy Shevchenko | e8cc2b9 | 2020-02-21 10:57:23 +0200 | [diff] [blame] | 91 | * This function has caveats. Please use kstrtoull instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 93 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Richard Fitzgerald | 9e914f5 | 2021-05-14 17:12:04 +0100 | [diff] [blame] | 95 | return simple_strntoull(cp, INT_MAX, endp, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | EXPORT_SYMBOL(simple_strtoull); |
| 98 | |
| 99 | /** |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 100 | * simple_strtoul - convert a string to an unsigned long |
| 101 | * @cp: The start of the string |
| 102 | * @endp: A pointer to the end of the parsed string will be placed here |
| 103 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 104 | * |
Andy Shevchenko | e8cc2b9 | 2020-02-21 10:57:23 +0200 | [diff] [blame] | 105 | * This function has caveats. Please use kstrtoul instead. |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 106 | */ |
| 107 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
| 108 | { |
| 109 | return simple_strtoull(cp, endp, base); |
| 110 | } |
| 111 | EXPORT_SYMBOL(simple_strtoul); |
| 112 | |
| 113 | /** |
| 114 | * simple_strtol - convert a string to a signed long |
| 115 | * @cp: The start of the string |
| 116 | * @endp: A pointer to the end of the parsed string will be placed here |
| 117 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 118 | * |
Andy Shevchenko | e8cc2b9 | 2020-02-21 10:57:23 +0200 | [diff] [blame] | 119 | * This function has caveats. Please use kstrtol instead. |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 120 | */ |
| 121 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
| 122 | { |
| 123 | if (*cp == '-') |
| 124 | return -simple_strtoul(cp + 1, endp, base); |
| 125 | |
| 126 | return simple_strtoul(cp, endp, base); |
| 127 | } |
| 128 | EXPORT_SYMBOL(simple_strtol); |
| 129 | |
Richard Fitzgerald | 9e914f5 | 2021-05-14 17:12:04 +0100 | [diff] [blame] | 130 | static long long simple_strntoll(const char *cp, size_t max_chars, char **endp, |
| 131 | unsigned int base) |
| 132 | { |
| 133 | /* |
| 134 | * simple_strntoull() safely handles receiving max_chars==0 in the |
| 135 | * case cp[0] == '-' && max_chars == 1. |
| 136 | * If max_chars == 0 we can drop through and pass it to simple_strntoull() |
| 137 | * and the content of *cp is irrelevant. |
| 138 | */ |
| 139 | if (*cp == '-' && max_chars > 0) |
| 140 | return -simple_strntoull(cp + 1, max_chars - 1, endp, base); |
| 141 | |
| 142 | return simple_strntoull(cp, max_chars, endp, base); |
| 143 | } |
| 144 | |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 145 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | * simple_strtoll - convert a string to a signed long long |
| 147 | * @cp: The start of the string |
| 148 | * @endp: A pointer to the end of the parsed string will be placed here |
| 149 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 150 | * |
Andy Shevchenko | e8cc2b9 | 2020-02-21 10:57:23 +0200 | [diff] [blame] | 151 | * This function has caveats. Please use kstrtoll instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 153 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Richard Fitzgerald | 9e914f5 | 2021-05-14 17:12:04 +0100 | [diff] [blame] | 155 | return simple_strntoll(cp, INT_MAX, endp, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Hans Verkuil | 98d5ce0 | 2010-04-23 13:18:04 -0400 | [diff] [blame] | 157 | EXPORT_SYMBOL(simple_strtoll); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 159 | static noinline_for_stack |
| 160 | int skip_atoi(const char **s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 162 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Rasmus Villemoes | 43e5b66 | 2015-02-12 15:01:42 -0800 | [diff] [blame] | 164 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | i = i*10 + *((*s)++) - '0'; |
Rasmus Villemoes | 43e5b66 | 2015-02-12 15:01:42 -0800 | [diff] [blame] | 166 | } while (isdigit(**s)); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return i; |
| 169 | } |
| 170 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 171 | /* |
| 172 | * Decimal conversion is by far the most typical, and is used for |
| 173 | * /proc and /sys data. This directly impacts e.g. top performance |
| 174 | * with many processes running. We optimize it for speed by emitting |
| 175 | * two characters at a time, using a 200 byte lookup table. This |
| 176 | * roughly halves the number of multiplications compared to computing |
| 177 | * the digits one at a time. Implementation strongly inspired by the |
| 178 | * previous version, which in turn used ideas described at |
| 179 | * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission |
| 180 | * from the author, Douglas W. Jones). |
| 181 | * |
| 182 | * It turns out there is precisely one 26 bit fixed-point |
| 183 | * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 |
| 184 | * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual |
| 185 | * range happens to be somewhat larger (x <= 1073741898), but that's |
| 186 | * irrelevant for our purpose. |
| 187 | * |
| 188 | * For dividing a number in the range [10^4, 10^6-1] by 100, we still |
| 189 | * need a 32x32->64 bit multiply, so we simply use the same constant. |
| 190 | * |
| 191 | * For dividing a number in the range [100, 10^4-1] by 100, there are |
| 192 | * several options. The simplest is (x * 0x147b) >> 19, which is valid |
| 193 | * for all x <= 43698. |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 194 | */ |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 195 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 196 | static const u16 decpair[100] = { |
| 197 | #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) |
| 198 | _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), |
| 199 | _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), |
| 200 | _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), |
| 201 | _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), |
| 202 | _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), |
| 203 | _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), |
| 204 | _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), |
| 205 | _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), |
| 206 | _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), |
| 207 | _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), |
| 208 | #undef _ |
| 209 | }; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 210 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 211 | /* |
| 212 | * This will print a single '0' even if r == 0, since we would |
Rasmus Villemoes | 675cf53 | 2015-04-16 12:43:42 -0700 | [diff] [blame] | 213 | * immediately jump to out_r where two 0s would be written but only |
| 214 | * one of them accounted for in buf. This is needed by ip4_string |
| 215 | * below. All other callers pass a non-zero value of r. |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 216 | */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 217 | static noinline_for_stack |
| 218 | char *put_dec_trunc8(char *buf, unsigned r) |
| 219 | { |
| 220 | unsigned q; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 221 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 222 | /* 1 <= r < 10^8 */ |
| 223 | if (r < 100) |
| 224 | goto out_r; |
George Spelvin | cb239d0 | 2012-10-04 17:12:30 -0700 | [diff] [blame] | 225 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 226 | /* 100 <= r < 10^8 */ |
| 227 | q = (r * (u64)0x28f5c29) >> 32; |
| 228 | *((u16 *)buf) = decpair[r - 100*q]; |
| 229 | buf += 2; |
| 230 | |
| 231 | /* 1 <= q < 10^6 */ |
| 232 | if (q < 100) |
| 233 | goto out_q; |
| 234 | |
| 235 | /* 100 <= q < 10^6 */ |
| 236 | r = (q * (u64)0x28f5c29) >> 32; |
| 237 | *((u16 *)buf) = decpair[q - 100*r]; |
| 238 | buf += 2; |
| 239 | |
| 240 | /* 1 <= r < 10^4 */ |
| 241 | if (r < 100) |
| 242 | goto out_r; |
| 243 | |
| 244 | /* 100 <= r < 10^4 */ |
| 245 | q = (r * 0x147b) >> 19; |
| 246 | *((u16 *)buf) = decpair[r - 100*q]; |
| 247 | buf += 2; |
| 248 | out_q: |
| 249 | /* 1 <= q < 100 */ |
| 250 | r = q; |
| 251 | out_r: |
| 252 | /* 1 <= r < 100 */ |
| 253 | *((u16 *)buf) = decpair[r]; |
Rasmus Villemoes | 675cf53 | 2015-04-16 12:43:42 -0700 | [diff] [blame] | 254 | buf += r < 10 ? 1 : 2; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 255 | return buf; |
| 256 | } |
| 257 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 258 | #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 |
| 259 | static noinline_for_stack |
| 260 | char *put_dec_full8(char *buf, unsigned r) |
| 261 | { |
| 262 | unsigned q; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 263 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 264 | /* 0 <= r < 10^8 */ |
| 265 | q = (r * (u64)0x28f5c29) >> 32; |
| 266 | *((u16 *)buf) = decpair[r - 100*q]; |
| 267 | buf += 2; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 268 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 269 | /* 0 <= q < 10^6 */ |
| 270 | r = (q * (u64)0x28f5c29) >> 32; |
| 271 | *((u16 *)buf) = decpair[q - 100*r]; |
| 272 | buf += 2; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 273 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 274 | /* 0 <= r < 10^4 */ |
| 275 | q = (r * 0x147b) >> 19; |
| 276 | *((u16 *)buf) = decpair[r - 100*q]; |
| 277 | buf += 2; |
| 278 | |
| 279 | /* 0 <= q < 100 */ |
| 280 | *((u16 *)buf) = decpair[q]; |
| 281 | buf += 2; |
| 282 | return buf; |
| 283 | } |
| 284 | |
| 285 | static noinline_for_stack |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 286 | char *put_dec(char *buf, unsigned long long n) |
| 287 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 288 | if (n >= 100*1000*1000) |
| 289 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); |
| 290 | /* 1 <= n <= 1.6e11 */ |
| 291 | if (n >= 100*1000*1000) |
| 292 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); |
| 293 | /* 1 <= n < 1e8 */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 294 | return put_dec_trunc8(buf, n); |
| 295 | } |
| 296 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 297 | #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 298 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 299 | static void |
| 300 | put_dec_full4(char *buf, unsigned r) |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 301 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 302 | unsigned q; |
| 303 | |
| 304 | /* 0 <= r < 10^4 */ |
| 305 | q = (r * 0x147b) >> 19; |
| 306 | *((u16 *)buf) = decpair[r - 100*q]; |
| 307 | buf += 2; |
| 308 | /* 0 <= q < 100 */ |
| 309 | *((u16 *)buf) = decpair[q]; |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Call put_dec_full4 on x % 10000, return x / 10000. |
| 314 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 |
| 315 | * holds for all x < 1,128,869,999. The largest value this |
| 316 | * helper will ever be asked to convert is 1,125,520,955. |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 317 | * (second call in the put_dec code, assuming n is all-ones). |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 318 | */ |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 319 | static noinline_for_stack |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 320 | unsigned put_dec_helper4(char *buf, unsigned x) |
| 321 | { |
| 322 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; |
| 323 | |
| 324 | put_dec_full4(buf, x - q * 10000); |
| 325 | return q; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* Based on code by Douglas W. Jones found at |
| 329 | * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> |
| 330 | * (with permission from the author). |
| 331 | * Performs no 64-bit division and hence should be fast on 32-bit machines. |
| 332 | */ |
| 333 | static |
| 334 | char *put_dec(char *buf, unsigned long long n) |
| 335 | { |
| 336 | uint32_t d3, d2, d1, q, h; |
| 337 | |
| 338 | if (n < 100*1000*1000) |
| 339 | return put_dec_trunc8(buf, n); |
| 340 | |
| 341 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ |
| 342 | h = (n >> 32); |
| 343 | d2 = (h ) & 0xffff; |
| 344 | d3 = (h >> 16); /* implicit "& 0xffff" */ |
| 345 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 346 | /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 |
| 347 | = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 348 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 349 | q = put_dec_helper4(buf, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 350 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 351 | q += 7671 * d3 + 9496 * d2 + 6 * d1; |
| 352 | q = put_dec_helper4(buf+4, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 353 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 354 | q += 4749 * d3 + 42 * d2; |
| 355 | q = put_dec_helper4(buf+8, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 356 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 357 | q += 281 * d3; |
| 358 | buf += 12; |
| 359 | if (q) |
| 360 | buf = put_dec_trunc8(buf, q); |
| 361 | else while (buf[-1] == '0') |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 362 | --buf; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 363 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 364 | return buf; |
| 365 | } |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 366 | |
| 367 | #endif |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 368 | |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Convert passed number to decimal string. |
| 371 | * Returns the length of string. On buffer overflow, returns 0. |
| 372 | * |
| 373 | * If speed is not important, use snprintf(). It's easy to read the code. |
| 374 | */ |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 375 | int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 376 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 377 | /* put_dec requires 2-byte alignment of the buffer. */ |
| 378 | char tmp[sizeof(num) * 3] __aligned(2); |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 379 | int idx, len; |
| 380 | |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 381 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
| 382 | if (num <= 9) { |
| 383 | tmp[0] = '0' + num; |
| 384 | len = 1; |
| 385 | } else { |
| 386 | len = put_dec(tmp, num) - tmp; |
| 387 | } |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 388 | |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 389 | if (len > size || width > size) |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 390 | return 0; |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 391 | |
| 392 | if (width > len) { |
| 393 | width = width - len; |
| 394 | for (idx = 0; idx < width; idx++) |
| 395 | buf[idx] = ' '; |
| 396 | } else { |
| 397 | width = 0; |
| 398 | } |
| 399 | |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 400 | for (idx = 0; idx < len; ++idx) |
Andrei Vagin | d1be35c | 2018-04-10 16:31:16 -0700 | [diff] [blame] | 401 | buf[idx + width] = tmp[len - idx - 1]; |
| 402 | |
| 403 | return len + width; |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 406 | #define SIGN 1 /* unsigned/signed, must be 1 */ |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 407 | #define LEFT 2 /* left justified */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | #define PLUS 4 /* show plus */ |
| 409 | #define SPACE 8 /* space if plus */ |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 410 | #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ |
Bjorn Helgaas | b89dc5d | 2010-03-05 10:47:31 -0700 | [diff] [blame] | 411 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
| 412 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Andy Shevchenko | b886690 | 2020-07-31 21:08:22 +0300 | [diff] [blame] | 414 | static_assert(ZEROPAD == ('0' - ' ')); |
| 415 | static_assert(SMALL == ' '); |
| 416 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 417 | enum format_type { |
| 418 | FORMAT_TYPE_NONE, /* Just a string part */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 419 | FORMAT_TYPE_WIDTH, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 420 | FORMAT_TYPE_PRECISION, |
| 421 | FORMAT_TYPE_CHAR, |
| 422 | FORMAT_TYPE_STR, |
| 423 | FORMAT_TYPE_PTR, |
| 424 | FORMAT_TYPE_PERCENT_CHAR, |
| 425 | FORMAT_TYPE_INVALID, |
| 426 | FORMAT_TYPE_LONG_LONG, |
| 427 | FORMAT_TYPE_ULONG, |
| 428 | FORMAT_TYPE_LONG, |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 429 | FORMAT_TYPE_UBYTE, |
| 430 | FORMAT_TYPE_BYTE, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 431 | FORMAT_TYPE_USHORT, |
| 432 | FORMAT_TYPE_SHORT, |
| 433 | FORMAT_TYPE_UINT, |
| 434 | FORMAT_TYPE_INT, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 435 | FORMAT_TYPE_SIZE_T, |
| 436 | FORMAT_TYPE_PTRDIFF |
| 437 | }; |
| 438 | |
| 439 | struct printf_spec { |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 440 | unsigned int type:8; /* format_type enum */ |
| 441 | signed int field_width:24; /* width of output field */ |
| 442 | unsigned int flags:8; /* flags to number() */ |
| 443 | unsigned int base:8; /* number base, 8, 10 or 16 only */ |
| 444 | signed int precision:16; /* # of digits/chars */ |
| 445 | } __packed; |
Rasmus Villemoes | ef27ac1 | 2019-03-07 16:27:03 -0800 | [diff] [blame] | 446 | static_assert(sizeof(struct printf_spec) == 8); |
| 447 | |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 448 | #define FIELD_WIDTH_MAX ((1 << 23) - 1) |
| 449 | #define PRECISION_MAX ((1 << 15) - 1) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 450 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 451 | static noinline_for_stack |
| 452 | char *number(char *buf, char *end, unsigned long long num, |
| 453 | struct printf_spec spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 455 | /* put_dec requires 2-byte alignment of the buffer. */ |
| 456 | char tmp[3 * sizeof(num)] __aligned(2); |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 457 | char sign; |
| 458 | char locase; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 459 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | int i; |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 461 | bool is_zero = num == 0LL; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 462 | int field_width = spec.field_width; |
| 463 | int precision = spec.precision; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 465 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
| 466 | * produces same digits or (maybe lowercased) letters */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 467 | locase = (spec.flags & SMALL); |
| 468 | if (spec.flags & LEFT) |
| 469 | spec.flags &= ~ZEROPAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | sign = 0; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 471 | if (spec.flags & SIGN) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 472 | if ((signed long long)num < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | sign = '-'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 474 | num = -(signed long long)num; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 475 | field_width--; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 476 | } else if (spec.flags & PLUS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | sign = '+'; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 478 | field_width--; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 479 | } else if (spec.flags & SPACE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | sign = ' '; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 481 | field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 484 | if (need_pfx) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 485 | if (spec.base == 16) |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 486 | field_width -= 2; |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 487 | else if (!is_zero) |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 488 | field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 490 | |
| 491 | /* generate full string in tmp[], in reverse order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | i = 0; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 493 | if (num < spec.base) |
Rasmus Villemoes | 3ea8d44 | 2015-04-15 16:17:08 -0700 | [diff] [blame] | 494 | tmp[i++] = hex_asc_upper[num] | locase; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 495 | else if (spec.base != 10) { /* 8 or 16 */ |
| 496 | int mask = spec.base - 1; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 497 | int shift = 3; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 498 | |
| 499 | if (spec.base == 16) |
| 500 | shift = 4; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 501 | do { |
Rasmus Villemoes | 3ea8d44 | 2015-04-15 16:17:08 -0700 | [diff] [blame] | 502 | tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 503 | num >>= shift; |
| 504 | } while (num); |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 505 | } else { /* base 10 */ |
| 506 | i = put_dec(tmp, num) - tmp; |
| 507 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 508 | |
| 509 | /* printing 100 using %2d gives "100", not "00" */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 510 | if (i > precision) |
| 511 | precision = i; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 512 | /* leading space padding */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 513 | field_width -= precision; |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 514 | if (!(spec.flags & (ZEROPAD | LEFT))) { |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 515 | while (--field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 516 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | *buf = ' '; |
| 518 | ++buf; |
| 519 | } |
| 520 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 521 | /* sign */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if (sign) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 523 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | *buf = sign; |
| 525 | ++buf; |
| 526 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 527 | /* "0x" / "0" prefix */ |
| 528 | if (need_pfx) { |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 529 | if (spec.base == 16 || !is_zero) { |
| 530 | if (buf < end) |
| 531 | *buf = '0'; |
| 532 | ++buf; |
| 533 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 534 | if (spec.base == 16) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 535 | if (buf < end) |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 536 | *buf = ('X' | locase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | ++buf; |
| 538 | } |
| 539 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 540 | /* zero or space padding */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 541 | if (!(spec.flags & LEFT)) { |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 542 | char c = ' ' + (spec.flags & ZEROPAD); |
Andy Shevchenko | b886690 | 2020-07-31 21:08:22 +0300 | [diff] [blame] | 543 | |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 544 | while (--field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 545 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | *buf = c; |
| 547 | ++buf; |
| 548 | } |
| 549 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 550 | /* hmm even more zero padding? */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 551 | while (i <= --precision) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 552 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | *buf = '0'; |
| 554 | ++buf; |
| 555 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 556 | /* actual digits of result */ |
| 557 | while (--i >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 558 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | *buf = tmp[i]; |
| 560 | ++buf; |
| 561 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 562 | /* trailing space padding */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 563 | while (--field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 564 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | *buf = ' '; |
| 566 | ++buf; |
| 567 | } |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | return buf; |
| 570 | } |
| 571 | |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 572 | static noinline_for_stack |
| 573 | char *special_hex_number(char *buf, char *end, unsigned long long num, int size) |
| 574 | { |
| 575 | struct printf_spec spec; |
| 576 | |
| 577 | spec.type = FORMAT_TYPE_PTR; |
| 578 | spec.field_width = 2 + 2 * size; /* 0x + hex */ |
| 579 | spec.flags = SPECIAL | SMALL | ZEROPAD; |
| 580 | spec.base = 16; |
| 581 | spec.precision = -1; |
| 582 | |
| 583 | return number(buf, end, num, spec); |
| 584 | } |
| 585 | |
Rasmus Villemoes | cfccde0 | 2016-01-15 16:58:28 -0800 | [diff] [blame] | 586 | static void move_right(char *buf, char *end, unsigned len, unsigned spaces) |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 587 | { |
| 588 | size_t size; |
| 589 | if (buf >= end) /* nowhere to put anything */ |
| 590 | return; |
| 591 | size = end - buf; |
| 592 | if (size <= spaces) { |
| 593 | memset(buf, ' ', size); |
| 594 | return; |
| 595 | } |
| 596 | if (len) { |
| 597 | if (len > size - spaces) |
| 598 | len = size - spaces; |
| 599 | memmove(buf + spaces, buf, len); |
| 600 | } |
| 601 | memset(buf, ' ', spaces); |
| 602 | } |
| 603 | |
Rasmus Villemoes | cfccde0 | 2016-01-15 16:58:28 -0800 | [diff] [blame] | 604 | /* |
| 605 | * Handle field width padding for a string. |
| 606 | * @buf: current buffer position |
| 607 | * @n: length of string |
| 608 | * @end: end of output buffer |
| 609 | * @spec: for field width and flags |
| 610 | * Returns: new buffer position after padding. |
| 611 | */ |
| 612 | static noinline_for_stack |
| 613 | char *widen_string(char *buf, int n, char *end, struct printf_spec spec) |
| 614 | { |
| 615 | unsigned spaces; |
| 616 | |
| 617 | if (likely(n >= spec.field_width)) |
| 618 | return buf; |
| 619 | /* we want to pad the sucker */ |
| 620 | spaces = spec.field_width - n; |
| 621 | if (!(spec.flags & LEFT)) { |
| 622 | move_right(buf - n, end, n, spaces); |
| 623 | return buf + spaces; |
| 624 | } |
| 625 | while (spaces--) { |
| 626 | if (buf < end) |
| 627 | *buf = ' '; |
| 628 | ++buf; |
| 629 | } |
| 630 | return buf; |
| 631 | } |
| 632 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 633 | /* Handle string from a well known address. */ |
| 634 | static char *string_nocheck(char *buf, char *end, const char *s, |
| 635 | struct printf_spec spec) |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 636 | { |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 637 | int len = 0; |
Youngmin Nam | b314dd4 | 2019-06-10 16:47:07 +0900 | [diff] [blame] | 638 | int lim = spec.precision; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 639 | |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 640 | while (lim--) { |
| 641 | char c = *s++; |
| 642 | if (!c) |
| 643 | break; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 644 | if (buf < end) |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 645 | *buf = c; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 646 | ++buf; |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 647 | ++len; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 648 | } |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 649 | return widen_string(buf, len, end, spec); |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Rasmus Villemoes | 57f5677 | 2019-10-15 21:07:05 +0200 | [diff] [blame] | 652 | static char *err_ptr(char *buf, char *end, void *ptr, |
| 653 | struct printf_spec spec) |
| 654 | { |
| 655 | int err = PTR_ERR(ptr); |
| 656 | const char *sym = errname(err); |
| 657 | |
| 658 | if (sym) |
| 659 | return string_nocheck(buf, end, sym, spec); |
| 660 | |
| 661 | /* |
| 662 | * Somebody passed ERR_PTR(-1234) or some other non-existing |
| 663 | * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to |
| 664 | * printing it as its decimal representation. |
| 665 | */ |
| 666 | spec.flags |= SIGN; |
| 667 | spec.base = 10; |
| 668 | return number(buf, end, err, spec); |
| 669 | } |
| 670 | |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 671 | /* Be careful: error messages must fit into the given buffer. */ |
| 672 | static char *error_string(char *buf, char *end, const char *s, |
| 673 | struct printf_spec spec) |
| 674 | { |
| 675 | /* |
| 676 | * Hard limit to avoid a completely insane messages. It actually |
| 677 | * works pretty well because most error messages are in |
| 678 | * the many pointer format modifiers. |
| 679 | */ |
| 680 | if (spec.precision == -1) |
| 681 | spec.precision = 2 * sizeof(void *); |
| 682 | |
| 683 | return string_nocheck(buf, end, s, spec); |
| 684 | } |
| 685 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 686 | /* |
Petr Mladek | 2ac5a3b | 2019-05-10 10:42:13 +0200 | [diff] [blame] | 687 | * Do not call any complex external code here. Nested printk()/vsprintf() |
| 688 | * might cause infinite loops. Failures might break printk() and would |
| 689 | * be hard to debug. |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 690 | */ |
| 691 | static const char *check_pointer_msg(const void *ptr) |
| 692 | { |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 693 | if (!ptr) |
| 694 | return "(null)"; |
| 695 | |
Petr Mladek | 2ac5a3b | 2019-05-10 10:42:13 +0200 | [diff] [blame] | 696 | if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 697 | return "(efault)"; |
| 698 | |
| 699 | return NULL; |
| 700 | } |
| 701 | |
| 702 | static int check_pointer(char **buf, char *end, const void *ptr, |
| 703 | struct printf_spec spec) |
| 704 | { |
| 705 | const char *err_msg; |
| 706 | |
| 707 | err_msg = check_pointer_msg(ptr); |
| 708 | if (err_msg) { |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 709 | *buf = error_string(*buf, end, err_msg, spec); |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 710 | return -EFAULT; |
| 711 | } |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 716 | static noinline_for_stack |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 717 | char *string(char *buf, char *end, const char *s, |
| 718 | struct printf_spec spec) |
| 719 | { |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 720 | if (check_pointer(&buf, end, s, spec)) |
| 721 | return buf; |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 722 | |
| 723 | return string_nocheck(buf, end, s, spec); |
| 724 | } |
| 725 | |
YueHaibing | ce9d3ec | 2019-04-27 00:46:30 +0800 | [diff] [blame] | 726 | static char *pointer_string(char *buf, char *end, |
| 727 | const void *ptr, |
| 728 | struct printf_spec spec) |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 729 | { |
| 730 | spec.base = 16; |
| 731 | spec.flags |= SMALL; |
| 732 | if (spec.field_width == -1) { |
| 733 | spec.field_width = 2 * sizeof(ptr); |
| 734 | spec.flags |= ZEROPAD; |
| 735 | } |
| 736 | |
| 737 | return number(buf, end, (unsigned long int)ptr, spec); |
| 738 | } |
| 739 | |
| 740 | /* Make pointers available for printing early in the boot sequence. */ |
| 741 | static int debug_boot_weak_hash __ro_after_init; |
| 742 | |
| 743 | static int __init debug_boot_weak_hash_enable(char *str) |
| 744 | { |
| 745 | debug_boot_weak_hash = 1; |
| 746 | pr_info("debug_boot_weak_hash enabled\n"); |
| 747 | return 0; |
| 748 | } |
| 749 | early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); |
| 750 | |
| 751 | static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); |
| 752 | static siphash_key_t ptr_key __read_mostly; |
| 753 | |
| 754 | static void enable_ptr_key_workfn(struct work_struct *work) |
| 755 | { |
| 756 | get_random_bytes(&ptr_key, sizeof(ptr_key)); |
| 757 | /* Needs to run from preemptible context */ |
| 758 | static_branch_disable(¬_filled_random_ptr_key); |
| 759 | } |
| 760 | |
| 761 | static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); |
| 762 | |
Jason A. Donenfeld | 849e7b7 | 2022-03-01 20:03:49 +0100 | [diff] [blame] | 763 | static int fill_random_ptr_key(struct notifier_block *nb, |
| 764 | unsigned long action, void *data) |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 765 | { |
| 766 | /* This may be in an interrupt handler. */ |
| 767 | queue_work(system_unbound_wq, &enable_ptr_key_work); |
Jason A. Donenfeld | 849e7b7 | 2022-03-01 20:03:49 +0100 | [diff] [blame] | 768 | return 0; |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Jason A. Donenfeld | 849e7b7 | 2022-03-01 20:03:49 +0100 | [diff] [blame] | 771 | static struct notifier_block random_ready = { |
| 772 | .notifier_call = fill_random_ptr_key |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 773 | }; |
| 774 | |
| 775 | static int __init initialize_ptr_random(void) |
| 776 | { |
| 777 | int key_size = sizeof(ptr_key); |
| 778 | int ret; |
| 779 | |
| 780 | /* Use hw RNG if available. */ |
| 781 | if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { |
| 782 | static_branch_disable(¬_filled_random_ptr_key); |
| 783 | return 0; |
| 784 | } |
| 785 | |
Jason A. Donenfeld | 849e7b7 | 2022-03-01 20:03:49 +0100 | [diff] [blame] | 786 | ret = register_random_ready_notifier(&random_ready); |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 787 | if (!ret) { |
| 788 | return 0; |
| 789 | } else if (ret == -EALREADY) { |
| 790 | /* This is in preemptible context */ |
| 791 | enable_ptr_key_workfn(&enable_ptr_key_work); |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | return ret; |
| 796 | } |
| 797 | early_initcall(initialize_ptr_random); |
| 798 | |
| 799 | /* Maps a pointer to a 32 bit unique identifier. */ |
Joel Fernandes (Google) | e4dcad2 | 2019-11-30 17:50:33 -0800 | [diff] [blame] | 800 | static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 801 | { |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 802 | unsigned long hashval; |
| 803 | |
Joel Fernandes (Google) | e4dcad2 | 2019-11-30 17:50:33 -0800 | [diff] [blame] | 804 | if (static_branch_unlikely(¬_filled_random_ptr_key)) |
| 805 | return -EAGAIN; |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 806 | |
| 807 | #ifdef CONFIG_64BIT |
| 808 | hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); |
| 809 | /* |
| 810 | * Mask off the first 32 bits, this makes explicit that we have |
| 811 | * modified the address (and 32 bits is plenty for a unique ID). |
| 812 | */ |
| 813 | hashval = hashval & 0xffffffff; |
| 814 | #else |
| 815 | hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); |
| 816 | #endif |
Joel Fernandes (Google) | e4dcad2 | 2019-11-30 17:50:33 -0800 | [diff] [blame] | 817 | *hashval_out = hashval; |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | int ptr_to_hashval(const void *ptr, unsigned long *hashval_out) |
| 822 | { |
| 823 | return __ptr_to_hashval(ptr, hashval_out); |
| 824 | } |
| 825 | |
| 826 | static char *ptr_to_id(char *buf, char *end, const void *ptr, |
| 827 | struct printf_spec spec) |
| 828 | { |
| 829 | const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; |
| 830 | unsigned long hashval; |
| 831 | int ret; |
| 832 | |
Ilya Dryomov | 7bd57fb | 2020-05-19 13:26:57 +0200 | [diff] [blame] | 833 | /* |
| 834 | * Print the real pointer value for NULL and error pointers, |
| 835 | * as they are not actual addresses. |
| 836 | */ |
| 837 | if (IS_ERR_OR_NULL(ptr)) |
| 838 | return pointer_string(buf, end, ptr, spec); |
| 839 | |
Joel Fernandes (Google) | e4dcad2 | 2019-11-30 17:50:33 -0800 | [diff] [blame] | 840 | /* When debugging early boot use non-cryptographically secure hash. */ |
| 841 | if (unlikely(debug_boot_weak_hash)) { |
| 842 | hashval = hash_long((unsigned long)ptr, 32); |
| 843 | return pointer_string(buf, end, (const void *)hashval, spec); |
| 844 | } |
| 845 | |
| 846 | ret = __ptr_to_hashval(ptr, &hashval); |
| 847 | if (ret) { |
| 848 | spec.field_width = 2 * sizeof(ptr); |
| 849 | /* string length must be less than default_width */ |
| 850 | return error_string(buf, end, str, spec); |
| 851 | } |
| 852 | |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 853 | return pointer_string(buf, end, (const void *)hashval, spec); |
| 854 | } |
| 855 | |
Christophe Leroy | 83ed3e2 | 2022-02-17 09:49:59 +0100 | [diff] [blame] | 856 | static char *default_pointer(char *buf, char *end, const void *ptr, |
| 857 | struct printf_spec spec) |
| 858 | { |
| 859 | /* |
| 860 | * default is to _not_ leak addresses, so hash before printing, |
| 861 | * unless no_hash_pointers is specified on the command line. |
| 862 | */ |
| 863 | if (unlikely(no_hash_pointers)) |
| 864 | return pointer_string(buf, end, ptr, spec); |
| 865 | |
| 866 | return ptr_to_id(buf, end, ptr, spec); |
| 867 | } |
| 868 | |
Petr Mladek | 6eea242 | 2019-04-17 13:53:41 +0200 | [diff] [blame] | 869 | int kptr_restrict __read_mostly; |
| 870 | |
| 871 | static noinline_for_stack |
| 872 | char *restricted_pointer(char *buf, char *end, const void *ptr, |
| 873 | struct printf_spec spec) |
| 874 | { |
| 875 | switch (kptr_restrict) { |
| 876 | case 0: |
Petr Mladek | 1ac2f97 | 2019-04-17 13:53:42 +0200 | [diff] [blame] | 877 | /* Handle as %p, hash and do _not_ leak addresses. */ |
Christophe Leroy | 83ed3e2 | 2022-02-17 09:49:59 +0100 | [diff] [blame] | 878 | return default_pointer(buf, end, ptr, spec); |
Petr Mladek | 6eea242 | 2019-04-17 13:53:41 +0200 | [diff] [blame] | 879 | case 1: { |
| 880 | const struct cred *cred; |
| 881 | |
| 882 | /* |
| 883 | * kptr_restrict==1 cannot be used in IRQ context |
| 884 | * because its test for CAP_SYSLOG would be meaningless. |
| 885 | */ |
| 886 | if (in_irq() || in_serving_softirq() || in_nmi()) { |
| 887 | if (spec.field_width == -1) |
| 888 | spec.field_width = 2 * sizeof(ptr); |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 889 | return error_string(buf, end, "pK-error", spec); |
Petr Mladek | 6eea242 | 2019-04-17 13:53:41 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* |
| 893 | * Only print the real pointer value if the current |
| 894 | * process has CAP_SYSLOG and is running with the |
| 895 | * same credentials it started with. This is because |
| 896 | * access to files is checked at open() time, but %pK |
| 897 | * checks permission at read() time. We don't want to |
| 898 | * leak pointer values if a binary opens a file using |
| 899 | * %pK and then elevates privileges before reading it. |
| 900 | */ |
| 901 | cred = current_cred(); |
| 902 | if (!has_capability_noaudit(current, CAP_SYSLOG) || |
| 903 | !uid_eq(cred->euid, cred->uid) || |
| 904 | !gid_eq(cred->egid, cred->gid)) |
| 905 | ptr = NULL; |
| 906 | break; |
| 907 | } |
| 908 | case 2: |
| 909 | default: |
| 910 | /* Always print 0's for %pK */ |
| 911 | ptr = NULL; |
| 912 | break; |
| 913 | } |
| 914 | |
| 915 | return pointer_string(buf, end, ptr, spec); |
| 916 | } |
| 917 | |
Geert Uytterhoeven | 9073dac | 2018-10-11 10:42:47 +0200 | [diff] [blame] | 918 | static noinline_for_stack |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 919 | char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, |
| 920 | const char *fmt) |
| 921 | { |
| 922 | const char *array[4], *s; |
| 923 | const struct dentry *p; |
| 924 | int depth; |
| 925 | int i, n; |
| 926 | |
| 927 | switch (fmt[1]) { |
| 928 | case '2': case '3': case '4': |
| 929 | depth = fmt[1] - '0'; |
| 930 | break; |
| 931 | default: |
| 932 | depth = 1; |
| 933 | } |
| 934 | |
| 935 | rcu_read_lock(); |
| 936 | for (i = 0; i < depth; i++, d = p) { |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 937 | if (check_pointer(&buf, end, d, spec)) { |
| 938 | rcu_read_unlock(); |
| 939 | return buf; |
| 940 | } |
| 941 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 942 | p = READ_ONCE(d->d_parent); |
| 943 | array[i] = READ_ONCE(d->d_name.name); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 944 | if (p == d) { |
| 945 | if (i) |
| 946 | array[i] = ""; |
| 947 | i++; |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | s = array[--i]; |
| 952 | for (n = 0; n != spec.precision; n++, buf++) { |
| 953 | char c = *s++; |
| 954 | if (!c) { |
| 955 | if (!i) |
| 956 | break; |
| 957 | c = '/'; |
| 958 | s = array[--i]; |
| 959 | } |
| 960 | if (buf < end) |
| 961 | *buf = c; |
| 962 | } |
| 963 | rcu_read_unlock(); |
Rasmus Villemoes | cfccde0 | 2016-01-15 16:58:28 -0800 | [diff] [blame] | 964 | return widen_string(buf, n, end, spec); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 965 | } |
| 966 | |
Jia He | 36594b3 | 2019-08-09 09:24:56 +0800 | [diff] [blame] | 967 | static noinline_for_stack |
| 968 | char *file_dentry_name(char *buf, char *end, const struct file *f, |
| 969 | struct printf_spec spec, const char *fmt) |
| 970 | { |
| 971 | if (check_pointer(&buf, end, f, spec)) |
| 972 | return buf; |
| 973 | |
| 974 | return dentry_name(buf, end, f->f_path.dentry, spec, fmt); |
| 975 | } |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 976 | #ifdef CONFIG_BLOCK |
| 977 | static noinline_for_stack |
| 978 | char *bdev_name(char *buf, char *end, struct block_device *bdev, |
| 979 | struct printf_spec spec, const char *fmt) |
| 980 | { |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 981 | struct gendisk *hd; |
| 982 | |
| 983 | if (check_pointer(&buf, end, bdev, spec)) |
| 984 | return buf; |
| 985 | |
| 986 | hd = bdev->bd_disk; |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 987 | buf = string(buf, end, hd->disk_name, spec); |
Christoph Hellwig | 700cd59 | 2020-09-03 07:41:04 +0200 | [diff] [blame] | 988 | if (bdev->bd_partno) { |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 989 | if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { |
| 990 | if (buf < end) |
| 991 | *buf = 'p'; |
| 992 | buf++; |
| 993 | } |
Christoph Hellwig | 700cd59 | 2020-09-03 07:41:04 +0200 | [diff] [blame] | 994 | buf = number(buf, end, bdev->bd_partno, spec); |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 995 | } |
| 996 | return buf; |
| 997 | } |
| 998 | #endif |
| 999 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1000 | static noinline_for_stack |
| 1001 | char *symbol_string(char *buf, char *end, void *ptr, |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 1002 | struct printf_spec spec, const char *fmt) |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1003 | { |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 1004 | unsigned long value; |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1005 | #ifdef CONFIG_KALLSYMS |
| 1006 | char sym[KSYM_SYMBOL_LEN]; |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 1007 | #endif |
| 1008 | |
| 1009 | if (fmt[1] == 'R') |
| 1010 | ptr = __builtin_extract_return_addr(ptr); |
| 1011 | value = (unsigned long)ptr; |
| 1012 | |
| 1013 | #ifdef CONFIG_KALLSYMS |
| 1014 | if (*fmt == 'B') |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1015 | sprint_backtrace(sym, value); |
Sakari Ailus | 9af7706 | 2019-10-03 15:32:14 +0300 | [diff] [blame] | 1016 | else if (*fmt != 's') |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1017 | sprint_symbol(sym, value); |
| 1018 | else |
Stephen Boyd | 4796dd2 | 2012-05-29 15:07:33 -0700 | [diff] [blame] | 1019 | sprint_symbol_no_offset(sym, value); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1020 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1021 | return string_nocheck(buf, end, sym, spec); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1022 | #else |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1023 | return special_hex_number(buf, end, value, sizeof(void *)); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1024 | #endif |
| 1025 | } |
| 1026 | |
Andy Shevchenko | abd4fe6 | 2018-02-16 23:07:05 +0200 | [diff] [blame] | 1027 | static const struct printf_spec default_str_spec = { |
| 1028 | .field_width = -1, |
| 1029 | .precision = -1, |
| 1030 | }; |
| 1031 | |
Andy Shevchenko | 5443397 | 2018-02-16 23:07:06 +0200 | [diff] [blame] | 1032 | static const struct printf_spec default_flag_spec = { |
| 1033 | .base = 16, |
| 1034 | .precision = -1, |
| 1035 | .flags = SPECIAL | SMALL, |
| 1036 | }; |
| 1037 | |
Andy Shevchenko | ce0b491 | 2018-02-16 23:07:04 +0200 | [diff] [blame] | 1038 | static const struct printf_spec default_dec_spec = { |
| 1039 | .base = 10, |
| 1040 | .precision = -1, |
| 1041 | }; |
| 1042 | |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 1043 | static const struct printf_spec default_dec02_spec = { |
| 1044 | .base = 10, |
| 1045 | .field_width = 2, |
| 1046 | .precision = -1, |
| 1047 | .flags = ZEROPAD, |
| 1048 | }; |
| 1049 | |
| 1050 | static const struct printf_spec default_dec04_spec = { |
| 1051 | .base = 10, |
| 1052 | .field_width = 4, |
| 1053 | .precision = -1, |
| 1054 | .flags = ZEROPAD, |
| 1055 | }; |
| 1056 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1057 | static noinline_for_stack |
| 1058 | char *resource_string(char *buf, char *end, struct resource *res, |
| 1059 | struct printf_spec spec, const char *fmt) |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1060 | { |
| 1061 | #ifndef IO_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 1062 | #define IO_RSRC_PRINTK_SIZE 6 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1063 | #endif |
| 1064 | |
| 1065 | #ifndef MEM_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 1066 | #define MEM_RSRC_PRINTK_SIZE 10 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1067 | #endif |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1068 | static const struct printf_spec io_spec = { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1069 | .base = 16, |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1070 | .field_width = IO_RSRC_PRINTK_SIZE, |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1071 | .precision = -1, |
| 1072 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 1073 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1074 | static const struct printf_spec mem_spec = { |
| 1075 | .base = 16, |
| 1076 | .field_width = MEM_RSRC_PRINTK_SIZE, |
| 1077 | .precision = -1, |
| 1078 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 1079 | }; |
Bjorn Helgaas | 0f4050c | 2010-03-05 10:47:42 -0700 | [diff] [blame] | 1080 | static const struct printf_spec bus_spec = { |
| 1081 | .base = 16, |
| 1082 | .field_width = 2, |
| 1083 | .precision = -1, |
| 1084 | .flags = SMALL | ZEROPAD, |
| 1085 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1086 | static const struct printf_spec str_spec = { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1087 | .field_width = -1, |
| 1088 | .precision = 10, |
| 1089 | .flags = LEFT, |
| 1090 | }; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1091 | |
| 1092 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
| 1093 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
| 1094 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
| 1095 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
Bjorn Helgaas | 9d7cca0 | 2010-03-05 10:47:47 -0700 | [diff] [blame] | 1096 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1097 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
| 1098 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
| 1099 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
| 1100 | |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1101 | char *p = sym, *pend = sym + sizeof(sym); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1102 | int decode = (fmt[0] == 'R') ? 1 : 0; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1103 | const struct printf_spec *specp; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1104 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1105 | if (check_pointer(&buf, end, res, spec)) |
| 1106 | return buf; |
| 1107 | |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1108 | *p++ = '['; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1109 | if (res->flags & IORESOURCE_IO) { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1110 | p = string_nocheck(p, pend, "io ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1111 | specp = &io_spec; |
| 1112 | } else if (res->flags & IORESOURCE_MEM) { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1113 | p = string_nocheck(p, pend, "mem ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1114 | specp = &mem_spec; |
| 1115 | } else if (res->flags & IORESOURCE_IRQ) { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1116 | p = string_nocheck(p, pend, "irq ", str_spec); |
Andy Shevchenko | ce0b491 | 2018-02-16 23:07:04 +0200 | [diff] [blame] | 1117 | specp = &default_dec_spec; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1118 | } else if (res->flags & IORESOURCE_DMA) { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1119 | p = string_nocheck(p, pend, "dma ", str_spec); |
Andy Shevchenko | ce0b491 | 2018-02-16 23:07:04 +0200 | [diff] [blame] | 1120 | specp = &default_dec_spec; |
Bjorn Helgaas | 0f4050c | 2010-03-05 10:47:42 -0700 | [diff] [blame] | 1121 | } else if (res->flags & IORESOURCE_BUS) { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1122 | p = string_nocheck(p, pend, "bus ", str_spec); |
Bjorn Helgaas | 0f4050c | 2010-03-05 10:47:42 -0700 | [diff] [blame] | 1123 | specp = &bus_spec; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1124 | } else { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1125 | p = string_nocheck(p, pend, "??? ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 1126 | specp = &mem_spec; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1127 | decode = 0; |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1128 | } |
Bjorn Helgaas | d19cb80 | 2014-02-26 11:25:56 -0700 | [diff] [blame] | 1129 | if (decode && res->flags & IORESOURCE_UNSET) { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1130 | p = string_nocheck(p, pend, "size ", str_spec); |
Bjorn Helgaas | d19cb80 | 2014-02-26 11:25:56 -0700 | [diff] [blame] | 1131 | p = number(p, pend, resource_size(res), *specp); |
| 1132 | } else { |
| 1133 | p = number(p, pend, res->start, *specp); |
| 1134 | if (res->start != res->end) { |
| 1135 | *p++ = '-'; |
| 1136 | p = number(p, pend, res->end, *specp); |
| 1137 | } |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 1138 | } |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1139 | if (decode) { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1140 | if (res->flags & IORESOURCE_MEM_64) |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1141 | p = string_nocheck(p, pend, " 64bit", str_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1142 | if (res->flags & IORESOURCE_PREFETCH) |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1143 | p = string_nocheck(p, pend, " pref", str_spec); |
Bjorn Helgaas | 9d7cca0 | 2010-03-05 10:47:47 -0700 | [diff] [blame] | 1144 | if (res->flags & IORESOURCE_WINDOW) |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1145 | p = string_nocheck(p, pend, " window", str_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1146 | if (res->flags & IORESOURCE_DISABLED) |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1147 | p = string_nocheck(p, pend, " disabled", str_spec); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1148 | } else { |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1149 | p = string_nocheck(p, pend, " flags ", str_spec); |
Andy Shevchenko | 5443397 | 2018-02-16 23:07:06 +0200 | [diff] [blame] | 1150 | p = number(p, pend, res->flags, default_flag_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1151 | } |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1152 | *p++ = ']'; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1153 | *p = '\0'; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1154 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1155 | return string_nocheck(buf, end, sym, spec); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1156 | } |
| 1157 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1158 | static noinline_for_stack |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1159 | char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
| 1160 | const char *fmt) |
| 1161 | { |
Steven Rostedt | 360603a | 2013-05-28 15:47:39 -0400 | [diff] [blame] | 1162 | int i, len = 1; /* if we pass '%ph[CDN]', field width remains |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1163 | negative value, fallback to the default */ |
| 1164 | char separator; |
| 1165 | |
| 1166 | if (spec.field_width == 0) |
| 1167 | /* nothing to print */ |
| 1168 | return buf; |
| 1169 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1170 | if (check_pointer(&buf, end, addr, spec)) |
| 1171 | return buf; |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1172 | |
| 1173 | switch (fmt[1]) { |
| 1174 | case 'C': |
| 1175 | separator = ':'; |
| 1176 | break; |
| 1177 | case 'D': |
| 1178 | separator = '-'; |
| 1179 | break; |
| 1180 | case 'N': |
| 1181 | separator = 0; |
| 1182 | break; |
| 1183 | default: |
| 1184 | separator = ' '; |
| 1185 | break; |
| 1186 | } |
| 1187 | |
| 1188 | if (spec.field_width > 0) |
| 1189 | len = min_t(int, spec.field_width, 64); |
| 1190 | |
Rasmus Villemoes | 9c98f23 | 2015-04-15 16:17:23 -0700 | [diff] [blame] | 1191 | for (i = 0; i < len; ++i) { |
| 1192 | if (buf < end) |
| 1193 | *buf = hex_asc_hi(addr[i]); |
| 1194 | ++buf; |
| 1195 | if (buf < end) |
| 1196 | *buf = hex_asc_lo(addr[i]); |
| 1197 | ++buf; |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1198 | |
Rasmus Villemoes | 9c98f23 | 2015-04-15 16:17:23 -0700 | [diff] [blame] | 1199 | if (separator && i != len - 1) { |
| 1200 | if (buf < end) |
| 1201 | *buf = separator; |
| 1202 | ++buf; |
| 1203 | } |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | return buf; |
| 1207 | } |
| 1208 | |
| 1209 | static noinline_for_stack |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1210 | char *bitmap_string(char *buf, char *end, unsigned long *bitmap, |
| 1211 | struct printf_spec spec, const char *fmt) |
| 1212 | { |
| 1213 | const int CHUNKSZ = 32; |
| 1214 | int nr_bits = max_t(int, spec.field_width, 0); |
| 1215 | int i, chunksz; |
| 1216 | bool first = true; |
| 1217 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1218 | if (check_pointer(&buf, end, bitmap, spec)) |
| 1219 | return buf; |
| 1220 | |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1221 | /* reused to print numbers */ |
| 1222 | spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; |
| 1223 | |
| 1224 | chunksz = nr_bits & (CHUNKSZ - 1); |
| 1225 | if (chunksz == 0) |
| 1226 | chunksz = CHUNKSZ; |
| 1227 | |
| 1228 | i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; |
| 1229 | for (; i >= 0; i -= CHUNKSZ) { |
| 1230 | u32 chunkmask, val; |
| 1231 | int word, bit; |
| 1232 | |
| 1233 | chunkmask = ((1ULL << chunksz) - 1); |
| 1234 | word = i / BITS_PER_LONG; |
| 1235 | bit = i % BITS_PER_LONG; |
| 1236 | val = (bitmap[word] >> bit) & chunkmask; |
| 1237 | |
| 1238 | if (!first) { |
| 1239 | if (buf < end) |
| 1240 | *buf = ','; |
| 1241 | buf++; |
| 1242 | } |
| 1243 | first = false; |
| 1244 | |
| 1245 | spec.field_width = DIV_ROUND_UP(chunksz, 4); |
| 1246 | buf = number(buf, end, val, spec); |
| 1247 | |
| 1248 | chunksz = CHUNKSZ; |
| 1249 | } |
| 1250 | return buf; |
| 1251 | } |
| 1252 | |
| 1253 | static noinline_for_stack |
| 1254 | char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, |
| 1255 | struct printf_spec spec, const char *fmt) |
| 1256 | { |
| 1257 | int nr_bits = max_t(int, spec.field_width, 0); |
| 1258 | /* current bit is 'cur', most recently seen range is [rbot, rtop] */ |
| 1259 | int cur, rbot, rtop; |
| 1260 | bool first = true; |
| 1261 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1262 | if (check_pointer(&buf, end, bitmap, spec)) |
| 1263 | return buf; |
| 1264 | |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1265 | rbot = cur = find_first_bit(bitmap, nr_bits); |
| 1266 | while (cur < nr_bits) { |
| 1267 | rtop = cur; |
| 1268 | cur = find_next_bit(bitmap, nr_bits, cur + 1); |
| 1269 | if (cur < nr_bits && cur <= rtop + 1) |
| 1270 | continue; |
| 1271 | |
| 1272 | if (!first) { |
| 1273 | if (buf < end) |
| 1274 | *buf = ','; |
| 1275 | buf++; |
| 1276 | } |
| 1277 | first = false; |
| 1278 | |
Andy Shevchenko | ce0b491 | 2018-02-16 23:07:04 +0200 | [diff] [blame] | 1279 | buf = number(buf, end, rbot, default_dec_spec); |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1280 | if (rbot < rtop) { |
| 1281 | if (buf < end) |
| 1282 | *buf = '-'; |
| 1283 | buf++; |
| 1284 | |
Andy Shevchenko | ce0b491 | 2018-02-16 23:07:04 +0200 | [diff] [blame] | 1285 | buf = number(buf, end, rtop, default_dec_spec); |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | rbot = cur; |
| 1289 | } |
| 1290 | return buf; |
| 1291 | } |
| 1292 | |
| 1293 | static noinline_for_stack |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1294 | char *mac_address_string(char *buf, char *end, u8 *addr, |
| 1295 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 1296 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1297 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 1298 | char *p = mac_addr; |
| 1299 | int i; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1300 | char separator; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1301 | bool reversed = false; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1302 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1303 | if (check_pointer(&buf, end, addr, spec)) |
| 1304 | return buf; |
| 1305 | |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1306 | switch (fmt[1]) { |
| 1307 | case 'F': |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1308 | separator = '-'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1309 | break; |
| 1310 | |
| 1311 | case 'R': |
| 1312 | reversed = true; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 1313 | /* fall through */ |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1314 | |
| 1315 | default: |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1316 | separator = ':'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1317 | break; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1318 | } |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 1319 | |
| 1320 | for (i = 0; i < 6; i++) { |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1321 | if (reversed) |
| 1322 | p = hex_byte_pack(p, addr[5 - i]); |
| 1323 | else |
| 1324 | p = hex_byte_pack(p, addr[i]); |
| 1325 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1326 | if (fmt[0] == 'M' && i != 5) |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1327 | *p++ = separator; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 1328 | } |
| 1329 | *p = '\0'; |
| 1330 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1331 | return string_nocheck(buf, end, mac_addr, spec); |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1334 | static noinline_for_stack |
| 1335 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1336 | { |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1337 | int i; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1338 | bool leading_zeros = (fmt[0] == 'i'); |
| 1339 | int index; |
| 1340 | int step; |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1341 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1342 | switch (fmt[2]) { |
| 1343 | case 'h': |
| 1344 | #ifdef __BIG_ENDIAN |
| 1345 | index = 0; |
| 1346 | step = 1; |
| 1347 | #else |
| 1348 | index = 3; |
| 1349 | step = -1; |
| 1350 | #endif |
| 1351 | break; |
| 1352 | case 'l': |
| 1353 | index = 3; |
| 1354 | step = -1; |
| 1355 | break; |
| 1356 | case 'n': |
| 1357 | case 'b': |
| 1358 | default: |
| 1359 | index = 0; |
| 1360 | step = 1; |
| 1361 | break; |
| 1362 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1363 | for (i = 0; i < 4; i++) { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 1364 | char temp[4] __aligned(2); /* hold each IP quad in reverse order */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1365 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1366 | if (leading_zeros) { |
| 1367 | if (digits < 3) |
| 1368 | *p++ = '0'; |
| 1369 | if (digits < 2) |
| 1370 | *p++ = '0'; |
| 1371 | } |
| 1372 | /* reverse the digits in the quad */ |
| 1373 | while (digits--) |
| 1374 | *p++ = temp[digits]; |
| 1375 | if (i < 3) |
| 1376 | *p++ = '.'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1377 | index += step; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1378 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1379 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1380 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1381 | return p; |
| 1382 | } |
| 1383 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1384 | static noinline_for_stack |
| 1385 | char *ip6_compressed_string(char *p, const char *addr) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1386 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1387 | int i, j, range; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1388 | unsigned char zerolength[8]; |
| 1389 | int longest = 1; |
| 1390 | int colonpos = -1; |
| 1391 | u16 word; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1392 | u8 hi, lo; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1393 | bool needcolon = false; |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1394 | bool useIPv4; |
| 1395 | struct in6_addr in6; |
| 1396 | |
| 1397 | memcpy(&in6, addr, sizeof(struct in6_addr)); |
| 1398 | |
| 1399 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1400 | |
| 1401 | memset(zerolength, 0, sizeof(zerolength)); |
| 1402 | |
| 1403 | if (useIPv4) |
| 1404 | range = 6; |
| 1405 | else |
| 1406 | range = 8; |
| 1407 | |
| 1408 | /* find position of longest 0 run */ |
| 1409 | for (i = 0; i < range; i++) { |
| 1410 | for (j = i; j < range; j++) { |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1411 | if (in6.s6_addr16[j] != 0) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1412 | break; |
| 1413 | zerolength[i]++; |
| 1414 | } |
| 1415 | } |
| 1416 | for (i = 0; i < range; i++) { |
| 1417 | if (zerolength[i] > longest) { |
| 1418 | longest = zerolength[i]; |
| 1419 | colonpos = i; |
| 1420 | } |
| 1421 | } |
Joe Perches | 29cf519 | 2011-06-09 11:23:37 -0700 | [diff] [blame] | 1422 | if (longest == 1) /* don't compress a single 0 */ |
| 1423 | colonpos = -1; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1424 | |
| 1425 | /* emit address */ |
| 1426 | for (i = 0; i < range; i++) { |
| 1427 | if (i == colonpos) { |
| 1428 | if (needcolon || i == 0) |
| 1429 | *p++ = ':'; |
| 1430 | *p++ = ':'; |
| 1431 | needcolon = false; |
| 1432 | i += longest - 1; |
| 1433 | continue; |
| 1434 | } |
| 1435 | if (needcolon) { |
| 1436 | *p++ = ':'; |
| 1437 | needcolon = false; |
| 1438 | } |
| 1439 | /* hex u16 without leading 0s */ |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1440 | word = ntohs(in6.s6_addr16[i]); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1441 | hi = word >> 8; |
| 1442 | lo = word & 0xff; |
| 1443 | if (hi) { |
| 1444 | if (hi > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1445 | p = hex_byte_pack(p, hi); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1446 | else |
| 1447 | *p++ = hex_asc_lo(hi); |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1448 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1449 | } |
André Goddard Rosa | b5ff992 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1450 | else if (lo > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1451 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1452 | else |
| 1453 | *p++ = hex_asc_lo(lo); |
| 1454 | needcolon = true; |
| 1455 | } |
| 1456 | |
| 1457 | if (useIPv4) { |
| 1458 | if (needcolon) |
| 1459 | *p++ = ':'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1460 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1461 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1462 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1463 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1464 | return p; |
| 1465 | } |
| 1466 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1467 | static noinline_for_stack |
| 1468 | char *ip6_string(char *p, const char *addr, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1469 | { |
| 1470 | int i; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1471 | |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1472 | for (i = 0; i < 8; i++) { |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1473 | p = hex_byte_pack(p, *addr++); |
| 1474 | p = hex_byte_pack(p, *addr++); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1475 | if (fmt[0] == 'I' && i != 7) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1476 | *p++ = ':'; |
| 1477 | } |
| 1478 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1479 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1480 | return p; |
| 1481 | } |
| 1482 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1483 | static noinline_for_stack |
| 1484 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, |
| 1485 | struct printf_spec spec, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1486 | { |
| 1487 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
| 1488 | |
| 1489 | if (fmt[0] == 'I' && fmt[2] == 'c') |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1490 | ip6_compressed_string(ip6_addr, addr); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1491 | else |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1492 | ip6_string(ip6_addr, addr, fmt); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1493 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1494 | return string_nocheck(buf, end, ip6_addr, spec); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1497 | static noinline_for_stack |
| 1498 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
| 1499 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1500 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1501 | char ip4_addr[sizeof("255.255.255.255")]; |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1502 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1503 | ip4_string(ip4_addr, addr, fmt); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1504 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1505 | return string_nocheck(buf, end, ip4_addr, spec); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1508 | static noinline_for_stack |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1509 | char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, |
| 1510 | struct printf_spec spec, const char *fmt) |
| 1511 | { |
| 1512 | bool have_p = false, have_s = false, have_f = false, have_c = false; |
| 1513 | char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + |
| 1514 | sizeof(":12345") + sizeof("/123456789") + |
| 1515 | sizeof("%1234567890")]; |
| 1516 | char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); |
| 1517 | const u8 *addr = (const u8 *) &sa->sin6_addr; |
| 1518 | char fmt6[2] = { fmt[0], '6' }; |
| 1519 | u8 off = 0; |
| 1520 | |
| 1521 | fmt++; |
| 1522 | while (isalpha(*++fmt)) { |
| 1523 | switch (*fmt) { |
| 1524 | case 'p': |
| 1525 | have_p = true; |
| 1526 | break; |
| 1527 | case 'f': |
| 1528 | have_f = true; |
| 1529 | break; |
| 1530 | case 's': |
| 1531 | have_s = true; |
| 1532 | break; |
| 1533 | case 'c': |
| 1534 | have_c = true; |
| 1535 | break; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | if (have_p || have_s || have_f) { |
| 1540 | *p = '['; |
| 1541 | off = 1; |
| 1542 | } |
| 1543 | |
| 1544 | if (fmt6[0] == 'I' && have_c) |
| 1545 | p = ip6_compressed_string(ip6_addr + off, addr); |
| 1546 | else |
| 1547 | p = ip6_string(ip6_addr + off, addr, fmt6); |
| 1548 | |
| 1549 | if (have_p || have_s || have_f) |
| 1550 | *p++ = ']'; |
| 1551 | |
| 1552 | if (have_p) { |
| 1553 | *p++ = ':'; |
| 1554 | p = number(p, pend, ntohs(sa->sin6_port), spec); |
| 1555 | } |
| 1556 | if (have_f) { |
| 1557 | *p++ = '/'; |
| 1558 | p = number(p, pend, ntohl(sa->sin6_flowinfo & |
| 1559 | IPV6_FLOWINFO_MASK), spec); |
| 1560 | } |
| 1561 | if (have_s) { |
| 1562 | *p++ = '%'; |
| 1563 | p = number(p, pend, sa->sin6_scope_id, spec); |
| 1564 | } |
| 1565 | *p = '\0'; |
| 1566 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1567 | return string_nocheck(buf, end, ip6_addr, spec); |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | static noinline_for_stack |
| 1571 | char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, |
| 1572 | struct printf_spec spec, const char *fmt) |
| 1573 | { |
| 1574 | bool have_p = false; |
| 1575 | char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; |
| 1576 | char *pend = ip4_addr + sizeof(ip4_addr); |
| 1577 | const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; |
| 1578 | char fmt4[3] = { fmt[0], '4', 0 }; |
| 1579 | |
| 1580 | fmt++; |
| 1581 | while (isalpha(*++fmt)) { |
| 1582 | switch (*fmt) { |
| 1583 | case 'p': |
| 1584 | have_p = true; |
| 1585 | break; |
| 1586 | case 'h': |
| 1587 | case 'l': |
| 1588 | case 'n': |
| 1589 | case 'b': |
| 1590 | fmt4[2] = *fmt; |
| 1591 | break; |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | p = ip4_string(ip4_addr, addr, fmt4); |
| 1596 | if (have_p) { |
| 1597 | *p++ = ':'; |
| 1598 | p = number(p, pend, ntohs(sa->sin_port), spec); |
| 1599 | } |
| 1600 | *p = '\0'; |
| 1601 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1602 | return string_nocheck(buf, end, ip4_addr, spec); |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static noinline_for_stack |
Petr Mladek | f00cc10 | 2019-04-17 13:53:44 +0200 | [diff] [blame] | 1606 | char *ip_addr_string(char *buf, char *end, const void *ptr, |
| 1607 | struct printf_spec spec, const char *fmt) |
| 1608 | { |
Petr Mladek | 0b74d4d | 2019-04-17 13:53:47 +0200 | [diff] [blame] | 1609 | char *err_fmt_msg; |
| 1610 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1611 | if (check_pointer(&buf, end, ptr, spec)) |
| 1612 | return buf; |
| 1613 | |
Petr Mladek | f00cc10 | 2019-04-17 13:53:44 +0200 | [diff] [blame] | 1614 | switch (fmt[1]) { |
| 1615 | case '6': |
| 1616 | return ip6_addr_string(buf, end, ptr, spec, fmt); |
| 1617 | case '4': |
| 1618 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
| 1619 | case 'S': { |
| 1620 | const union { |
| 1621 | struct sockaddr raw; |
| 1622 | struct sockaddr_in v4; |
| 1623 | struct sockaddr_in6 v6; |
| 1624 | } *sa = ptr; |
| 1625 | |
| 1626 | switch (sa->raw.sa_family) { |
| 1627 | case AF_INET: |
| 1628 | return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); |
| 1629 | case AF_INET6: |
| 1630 | return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); |
| 1631 | default: |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 1632 | return error_string(buf, end, "(einval)", spec); |
Petr Mladek | f00cc10 | 2019-04-17 13:53:44 +0200 | [diff] [blame] | 1633 | }} |
| 1634 | } |
| 1635 | |
Petr Mladek | 0b74d4d | 2019-04-17 13:53:47 +0200 | [diff] [blame] | 1636 | err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 1637 | return error_string(buf, end, err_fmt_msg, spec); |
Petr Mladek | f00cc10 | 2019-04-17 13:53:44 +0200 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | static noinline_for_stack |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1641 | char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
| 1642 | const char *fmt) |
| 1643 | { |
| 1644 | bool found = true; |
| 1645 | int count = 1; |
| 1646 | unsigned int flags = 0; |
| 1647 | int len; |
| 1648 | |
| 1649 | if (spec.field_width == 0) |
| 1650 | return buf; /* nothing to print */ |
| 1651 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1652 | if (check_pointer(&buf, end, addr, spec)) |
| 1653 | return buf; |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1654 | |
| 1655 | do { |
| 1656 | switch (fmt[count++]) { |
| 1657 | case 'a': |
| 1658 | flags |= ESCAPE_ANY; |
| 1659 | break; |
| 1660 | case 'c': |
| 1661 | flags |= ESCAPE_SPECIAL; |
| 1662 | break; |
| 1663 | case 'h': |
| 1664 | flags |= ESCAPE_HEX; |
| 1665 | break; |
| 1666 | case 'n': |
| 1667 | flags |= ESCAPE_NULL; |
| 1668 | break; |
| 1669 | case 'o': |
| 1670 | flags |= ESCAPE_OCTAL; |
| 1671 | break; |
| 1672 | case 'p': |
| 1673 | flags |= ESCAPE_NP; |
| 1674 | break; |
| 1675 | case 's': |
| 1676 | flags |= ESCAPE_SPACE; |
| 1677 | break; |
| 1678 | default: |
| 1679 | found = false; |
| 1680 | break; |
| 1681 | } |
| 1682 | } while (found); |
| 1683 | |
| 1684 | if (!flags) |
| 1685 | flags = ESCAPE_ANY_NP; |
| 1686 | |
| 1687 | len = spec.field_width < 0 ? 1 : spec.field_width; |
| 1688 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 1689 | /* |
| 1690 | * string_escape_mem() writes as many characters as it can to |
| 1691 | * the given buffer, and returns the total size of the output |
| 1692 | * had the buffer been big enough. |
| 1693 | */ |
| 1694 | buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1695 | |
| 1696 | return buf; |
| 1697 | } |
| 1698 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1699 | static char *va_format(char *buf, char *end, struct va_format *va_fmt, |
| 1700 | struct printf_spec spec, const char *fmt) |
Petr Mladek | 45c3e93 | 2019-04-17 13:53:45 +0200 | [diff] [blame] | 1701 | { |
| 1702 | va_list va; |
| 1703 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1704 | if (check_pointer(&buf, end, va_fmt, spec)) |
| 1705 | return buf; |
| 1706 | |
Petr Mladek | 45c3e93 | 2019-04-17 13:53:45 +0200 | [diff] [blame] | 1707 | va_copy(va, *va_fmt->va); |
| 1708 | buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); |
| 1709 | va_end(va); |
| 1710 | |
| 1711 | return buf; |
| 1712 | } |
| 1713 | |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1714 | static noinline_for_stack |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1715 | char *uuid_string(char *buf, char *end, const u8 *addr, |
| 1716 | struct printf_spec spec, const char *fmt) |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1717 | { |
Andy Shevchenko | 2b1b0d6 | 2016-05-20 17:01:04 -0700 | [diff] [blame] | 1718 | char uuid[UUID_STRING_LEN + 1]; |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1719 | char *p = uuid; |
| 1720 | int i; |
Christoph Hellwig | f9727a1 | 2017-05-17 10:02:48 +0200 | [diff] [blame] | 1721 | const u8 *index = uuid_index; |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1722 | bool uc = false; |
| 1723 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1724 | if (check_pointer(&buf, end, addr, spec)) |
| 1725 | return buf; |
| 1726 | |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1727 | switch (*(++fmt)) { |
| 1728 | case 'L': |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1729 | uc = true; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 1730 | /* fall through */ |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1731 | case 'l': |
Christoph Hellwig | f9727a1 | 2017-05-17 10:02:48 +0200 | [diff] [blame] | 1732 | index = guid_index; |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1733 | break; |
| 1734 | case 'B': |
| 1735 | uc = true; |
| 1736 | break; |
| 1737 | } |
| 1738 | |
| 1739 | for (i = 0; i < 16; i++) { |
Andy Shevchenko | aa4ea1c | 2016-05-20 17:00:54 -0700 | [diff] [blame] | 1740 | if (uc) |
| 1741 | p = hex_byte_pack_upper(p, addr[index[i]]); |
| 1742 | else |
| 1743 | p = hex_byte_pack(p, addr[index[i]]); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1744 | switch (i) { |
| 1745 | case 3: |
| 1746 | case 5: |
| 1747 | case 7: |
| 1748 | case 9: |
| 1749 | *p++ = '-'; |
| 1750 | break; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | *p = 0; |
| 1755 | |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 1756 | return string_nocheck(buf, end, uuid, spec); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1757 | } |
| 1758 | |
Andy Shevchenko | 5b17aec | 2016-01-15 16:59:20 -0800 | [diff] [blame] | 1759 | static noinline_for_stack |
Geert Uytterhoeven | 431bca2 | 2018-10-11 10:42:49 +0200 | [diff] [blame] | 1760 | char *netdev_bits(char *buf, char *end, const void *addr, |
| 1761 | struct printf_spec spec, const char *fmt) |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1762 | { |
Andy Shevchenko | 5b17aec | 2016-01-15 16:59:20 -0800 | [diff] [blame] | 1763 | unsigned long long num; |
| 1764 | int size; |
| 1765 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1766 | if (check_pointer(&buf, end, addr, spec)) |
| 1767 | return buf; |
| 1768 | |
Andy Shevchenko | 5b17aec | 2016-01-15 16:59:20 -0800 | [diff] [blame] | 1769 | switch (fmt[1]) { |
| 1770 | case 'F': |
| 1771 | num = *(const netdev_features_t *)addr; |
| 1772 | size = sizeof(netdev_features_t); |
| 1773 | break; |
| 1774 | default: |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 1775 | return error_string(buf, end, "(%pN?)", spec); |
Andy Shevchenko | 5b17aec | 2016-01-15 16:59:20 -0800 | [diff] [blame] | 1776 | } |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1777 | |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1778 | return special_hex_number(buf, end, num, size); |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1781 | static noinline_for_stack |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1782 | char *address_val(char *buf, char *end, const void *addr, |
| 1783 | struct printf_spec spec, const char *fmt) |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1784 | { |
| 1785 | unsigned long long num; |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1786 | int size; |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1787 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1788 | if (check_pointer(&buf, end, addr, spec)) |
| 1789 | return buf; |
| 1790 | |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1791 | switch (fmt[1]) { |
| 1792 | case 'd': |
| 1793 | num = *(const dma_addr_t *)addr; |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1794 | size = sizeof(dma_addr_t); |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1795 | break; |
| 1796 | case 'p': |
| 1797 | default: |
| 1798 | num = *(const phys_addr_t *)addr; |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1799 | size = sizeof(phys_addr_t); |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1800 | break; |
| 1801 | } |
| 1802 | |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1803 | return special_hex_number(buf, end, num, size); |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1804 | } |
| 1805 | |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1806 | static noinline_for_stack |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 1807 | char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) |
| 1808 | { |
| 1809 | int year = tm->tm_year + (r ? 0 : 1900); |
| 1810 | int mon = tm->tm_mon + (r ? 0 : 1); |
| 1811 | |
| 1812 | buf = number(buf, end, year, default_dec04_spec); |
| 1813 | if (buf < end) |
| 1814 | *buf = '-'; |
| 1815 | buf++; |
| 1816 | |
| 1817 | buf = number(buf, end, mon, default_dec02_spec); |
| 1818 | if (buf < end) |
| 1819 | *buf = '-'; |
| 1820 | buf++; |
| 1821 | |
| 1822 | return number(buf, end, tm->tm_mday, default_dec02_spec); |
| 1823 | } |
| 1824 | |
| 1825 | static noinline_for_stack |
| 1826 | char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) |
| 1827 | { |
| 1828 | buf = number(buf, end, tm->tm_hour, default_dec02_spec); |
| 1829 | if (buf < end) |
| 1830 | *buf = ':'; |
| 1831 | buf++; |
| 1832 | |
| 1833 | buf = number(buf, end, tm->tm_min, default_dec02_spec); |
| 1834 | if (buf < end) |
| 1835 | *buf = ':'; |
| 1836 | buf++; |
| 1837 | |
| 1838 | return number(buf, end, tm->tm_sec, default_dec02_spec); |
| 1839 | } |
| 1840 | |
| 1841 | static noinline_for_stack |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1842 | char *rtc_str(char *buf, char *end, const struct rtc_time *tm, |
| 1843 | struct printf_spec spec, const char *fmt) |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 1844 | { |
| 1845 | bool have_t = true, have_d = true; |
| 1846 | bool raw = false; |
| 1847 | int count = 2; |
| 1848 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1849 | if (check_pointer(&buf, end, tm, spec)) |
| 1850 | return buf; |
| 1851 | |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 1852 | switch (fmt[count]) { |
| 1853 | case 'd': |
| 1854 | have_t = false; |
| 1855 | count++; |
| 1856 | break; |
| 1857 | case 't': |
| 1858 | have_d = false; |
| 1859 | count++; |
| 1860 | break; |
| 1861 | } |
| 1862 | |
| 1863 | raw = fmt[count] == 'r'; |
| 1864 | |
| 1865 | if (have_d) |
| 1866 | buf = date_str(buf, end, tm, raw); |
| 1867 | if (have_d && have_t) { |
| 1868 | /* Respect ISO 8601 */ |
| 1869 | if (buf < end) |
| 1870 | *buf = 'T'; |
| 1871 | buf++; |
| 1872 | } |
| 1873 | if (have_t) |
| 1874 | buf = time_str(buf, end, tm, raw); |
| 1875 | |
| 1876 | return buf; |
| 1877 | } |
| 1878 | |
| 1879 | static noinline_for_stack |
Andy Shevchenko | 7daac5b | 2020-04-15 20:00:44 +0300 | [diff] [blame] | 1880 | char *time64_str(char *buf, char *end, const time64_t time, |
| 1881 | struct printf_spec spec, const char *fmt) |
| 1882 | { |
| 1883 | struct rtc_time rtc_time; |
| 1884 | struct tm tm; |
| 1885 | |
| 1886 | time64_to_tm(time, 0, &tm); |
| 1887 | |
| 1888 | rtc_time.tm_sec = tm.tm_sec; |
| 1889 | rtc_time.tm_min = tm.tm_min; |
| 1890 | rtc_time.tm_hour = tm.tm_hour; |
| 1891 | rtc_time.tm_mday = tm.tm_mday; |
| 1892 | rtc_time.tm_mon = tm.tm_mon; |
| 1893 | rtc_time.tm_year = tm.tm_year; |
| 1894 | rtc_time.tm_wday = tm.tm_wday; |
| 1895 | rtc_time.tm_yday = tm.tm_yday; |
| 1896 | |
| 1897 | rtc_time.tm_isdst = 0; |
| 1898 | |
| 1899 | return rtc_str(buf, end, &rtc_time, spec, fmt); |
| 1900 | } |
| 1901 | |
| 1902 | static noinline_for_stack |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 1903 | char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, |
| 1904 | const char *fmt) |
| 1905 | { |
| 1906 | switch (fmt[1]) { |
| 1907 | case 'R': |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1908 | return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); |
Andy Shevchenko | 7daac5b | 2020-04-15 20:00:44 +0300 | [diff] [blame] | 1909 | case 'T': |
| 1910 | return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 1911 | default: |
Andy Shevchenko | 7daac5b | 2020-04-15 20:00:44 +0300 | [diff] [blame] | 1912 | return error_string(buf, end, "(%pt?)", spec); |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | static noinline_for_stack |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1917 | char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, |
| 1918 | const char *fmt) |
| 1919 | { |
Petr Mladek | 0b74d4d | 2019-04-17 13:53:47 +0200 | [diff] [blame] | 1920 | if (!IS_ENABLED(CONFIG_HAVE_CLK)) |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 1921 | return error_string(buf, end, "(%pC?)", spec); |
Petr Mladek | 0b74d4d | 2019-04-17 13:53:47 +0200 | [diff] [blame] | 1922 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1923 | if (check_pointer(&buf, end, clk, spec)) |
| 1924 | return buf; |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1925 | |
| 1926 | switch (fmt[1]) { |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1927 | case 'n': |
| 1928 | default: |
| 1929 | #ifdef CONFIG_COMMON_CLK |
| 1930 | return string(buf, end, __clk_get_name(clk), spec); |
| 1931 | #else |
Geert Uytterhoeven | 4ca96aa | 2019-07-01 16:00:09 +0200 | [diff] [blame] | 1932 | return ptr_to_id(buf, end, clk, spec); |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1933 | #endif |
| 1934 | } |
| 1935 | } |
| 1936 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1937 | static |
| 1938 | char *format_flags(char *buf, char *end, unsigned long flags, |
| 1939 | const struct trace_print_flags *names) |
| 1940 | { |
| 1941 | unsigned long mask; |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1942 | |
| 1943 | for ( ; flags && names->name; names++) { |
| 1944 | mask = names->mask; |
| 1945 | if ((flags & mask) != mask) |
| 1946 | continue; |
| 1947 | |
Andy Shevchenko | abd4fe6 | 2018-02-16 23:07:05 +0200 | [diff] [blame] | 1948 | buf = string(buf, end, names->name, default_str_spec); |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1949 | |
| 1950 | flags &= ~mask; |
| 1951 | if (flags) { |
| 1952 | if (buf < end) |
| 1953 | *buf = '|'; |
| 1954 | buf++; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | if (flags) |
Andy Shevchenko | 5443397 | 2018-02-16 23:07:06 +0200 | [diff] [blame] | 1959 | buf = number(buf, end, flags, default_flag_spec); |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1960 | |
| 1961 | return buf; |
| 1962 | } |
| 1963 | |
| 1964 | static noinline_for_stack |
Petr Mladek | 0b74d4d | 2019-04-17 13:53:47 +0200 | [diff] [blame] | 1965 | char *flags_string(char *buf, char *end, void *flags_ptr, |
| 1966 | struct printf_spec spec, const char *fmt) |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1967 | { |
| 1968 | unsigned long flags; |
| 1969 | const struct trace_print_flags *names; |
| 1970 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 1971 | if (check_pointer(&buf, end, flags_ptr, spec)) |
| 1972 | return buf; |
| 1973 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1974 | switch (fmt[1]) { |
| 1975 | case 'p': |
| 1976 | flags = *(unsigned long *)flags_ptr; |
| 1977 | /* Remove zone id */ |
| 1978 | flags &= (1UL << NR_PAGEFLAGS) - 1; |
| 1979 | names = pageflag_names; |
| 1980 | break; |
| 1981 | case 'v': |
| 1982 | flags = *(unsigned long *)flags_ptr; |
| 1983 | names = vmaflag_names; |
| 1984 | break; |
| 1985 | case 'g': |
Andy Shevchenko | 30d497a | 2020-07-31 21:08:24 +0300 | [diff] [blame] | 1986 | flags = (__force unsigned long)(*(gfp_t *)flags_ptr); |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1987 | names = gfpflag_names; |
| 1988 | break; |
| 1989 | default: |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 1990 | return error_string(buf, end, "(%pG?)", spec); |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | return format_flags(buf, end, flags, names); |
| 1994 | } |
| 1995 | |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 1996 | static noinline_for_stack |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 1997 | char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, |
| 1998 | char *end) |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 1999 | { |
| 2000 | int depth; |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2001 | |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 2002 | /* Loop starting from the root node to the current node. */ |
| 2003 | for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { |
| 2004 | struct fwnode_handle *__fwnode = |
| 2005 | fwnode_get_nth_parent(fwnode, depth); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2006 | |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 2007 | buf = string(buf, end, fwnode_get_name_prefix(__fwnode), |
Andy Shevchenko | abd4fe6 | 2018-02-16 23:07:05 +0200 | [diff] [blame] | 2008 | default_str_spec); |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 2009 | buf = string(buf, end, fwnode_get_name(__fwnode), |
| 2010 | default_str_spec); |
| 2011 | |
| 2012 | fwnode_handle_put(__fwnode); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2013 | } |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 2014 | |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2015 | return buf; |
| 2016 | } |
| 2017 | |
| 2018 | static noinline_for_stack |
| 2019 | char *device_node_string(char *buf, char *end, struct device_node *dn, |
| 2020 | struct printf_spec spec, const char *fmt) |
| 2021 | { |
| 2022 | char tbuf[sizeof("xxxx") + 1]; |
| 2023 | const char *p; |
| 2024 | int ret; |
| 2025 | char *buf_start = buf; |
| 2026 | struct property *prop; |
| 2027 | bool has_mult, pass; |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2028 | |
| 2029 | struct printf_spec str_spec = spec; |
| 2030 | str_spec.field_width = -1; |
| 2031 | |
Sakari Ailus | 83abc5a | 2019-10-03 15:32:17 +0300 | [diff] [blame] | 2032 | if (fmt[0] != 'F') |
| 2033 | return error_string(buf, end, "(%pO?)", spec); |
| 2034 | |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2035 | if (!IS_ENABLED(CONFIG_OF)) |
Petr Mladek | c8c3b58 | 2019-04-17 13:53:50 +0200 | [diff] [blame] | 2036 | return error_string(buf, end, "(%pOF?)", spec); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2037 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 2038 | if (check_pointer(&buf, end, dn, spec)) |
| 2039 | return buf; |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2040 | |
| 2041 | /* simple case without anything any more format specifiers */ |
| 2042 | fmt++; |
| 2043 | if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) |
| 2044 | fmt = "f"; |
| 2045 | |
| 2046 | for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { |
Rob Herring | 6d0a70a | 2018-08-27 08:13:56 -0500 | [diff] [blame] | 2047 | int precision; |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2048 | if (pass) { |
| 2049 | if (buf < end) |
| 2050 | *buf = ':'; |
| 2051 | buf++; |
| 2052 | } |
| 2053 | |
| 2054 | switch (*fmt) { |
| 2055 | case 'f': /* full_name */ |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 2056 | buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, |
| 2057 | end); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2058 | break; |
| 2059 | case 'n': /* name */ |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 2060 | p = fwnode_get_name(of_fwnode_handle(dn)); |
Rob Herring | 6d0a70a | 2018-08-27 08:13:56 -0500 | [diff] [blame] | 2061 | precision = str_spec.precision; |
| 2062 | str_spec.precision = strchrnul(p, '@') - p; |
| 2063 | buf = string(buf, end, p, str_spec); |
| 2064 | str_spec.precision = precision; |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2065 | break; |
| 2066 | case 'p': /* phandle */ |
Andy Shevchenko | 09ceb8d | 2020-07-31 21:08:23 +0300 | [diff] [blame] | 2067 | buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2068 | break; |
| 2069 | case 'P': /* path-spec */ |
Sakari Ailus | a92eb76 | 2019-10-03 15:32:16 +0300 | [diff] [blame] | 2070 | p = fwnode_get_name(of_fwnode_handle(dn)); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2071 | if (!p[1]) |
| 2072 | p = "/"; |
| 2073 | buf = string(buf, end, p, str_spec); |
| 2074 | break; |
| 2075 | case 'F': /* flags */ |
| 2076 | tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; |
| 2077 | tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; |
| 2078 | tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; |
| 2079 | tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; |
| 2080 | tbuf[4] = 0; |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 2081 | buf = string_nocheck(buf, end, tbuf, str_spec); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2082 | break; |
| 2083 | case 'c': /* major compatible string */ |
| 2084 | ret = of_property_read_string(dn, "compatible", &p); |
| 2085 | if (!ret) |
| 2086 | buf = string(buf, end, p, str_spec); |
| 2087 | break; |
| 2088 | case 'C': /* full compatible string */ |
| 2089 | has_mult = false; |
| 2090 | of_property_for_each_string(dn, "compatible", prop, p) { |
| 2091 | if (has_mult) |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 2092 | buf = string_nocheck(buf, end, ",", str_spec); |
| 2093 | buf = string_nocheck(buf, end, "\"", str_spec); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2094 | buf = string(buf, end, p, str_spec); |
Petr Mladek | d529ac4 | 2019-04-17 13:53:43 +0200 | [diff] [blame] | 2095 | buf = string_nocheck(buf, end, "\"", str_spec); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2096 | |
| 2097 | has_mult = true; |
| 2098 | } |
| 2099 | break; |
| 2100 | default: |
| 2101 | break; |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | return widen_string(buf, buf - buf_start, end, spec); |
| 2106 | } |
| 2107 | |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 2108 | static noinline_for_stack |
| 2109 | char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, |
| 2110 | struct printf_spec spec, const char *fmt) |
Petr Mladek | 798cc27 | 2019-04-17 13:53:46 +0200 | [diff] [blame] | 2111 | { |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 2112 | struct printf_spec str_spec = spec; |
| 2113 | char *buf_start = buf; |
| 2114 | |
| 2115 | str_spec.field_width = -1; |
| 2116 | |
| 2117 | if (*fmt != 'w') |
| 2118 | return error_string(buf, end, "(%pf?)", spec); |
| 2119 | |
| 2120 | if (check_pointer(&buf, end, fwnode, spec)) |
| 2121 | return buf; |
| 2122 | |
| 2123 | fmt++; |
| 2124 | |
| 2125 | switch (*fmt) { |
| 2126 | case 'P': /* name */ |
| 2127 | buf = string(buf, end, fwnode_get_name(fwnode), str_spec); |
| 2128 | break; |
| 2129 | case 'f': /* full_name */ |
| 2130 | default: |
| 2131 | buf = fwnode_full_name_string(fwnode, buf, end); |
| 2132 | break; |
Petr Mladek | 798cc27 | 2019-04-17 13:53:46 +0200 | [diff] [blame] | 2133 | } |
| 2134 | |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 2135 | return widen_string(buf, buf - buf_start, end, spec); |
Petr Mladek | 798cc27 | 2019-04-17 13:53:46 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Timur Tabi | fc461a1 | 2021-02-14 10:13:48 -0600 | [diff] [blame] | 2138 | static int __init no_hash_pointers_enable(char *str) |
| 2139 | { |
| 2140 | no_hash_pointers = true; |
| 2141 | |
| 2142 | pr_warn("**********************************************************\n"); |
| 2143 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| 2144 | pr_warn("** **\n"); |
| 2145 | pr_warn("** This system shows unhashed kernel memory addresses **\n"); |
| 2146 | pr_warn("** via the console, logs, and other interfaces. This **\n"); |
| 2147 | pr_warn("** might reduce the security of your system. **\n"); |
| 2148 | pr_warn("** **\n"); |
| 2149 | pr_warn("** If you see this message and you are not debugging **\n"); |
| 2150 | pr_warn("** the kernel, report this immediately to your system **\n"); |
| 2151 | pr_warn("** administrator! **\n"); |
| 2152 | pr_warn("** **\n"); |
| 2153 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); |
| 2154 | pr_warn("**********************************************************\n"); |
| 2155 | |
| 2156 | return 0; |
| 2157 | } |
| 2158 | early_param("no_hash_pointers", no_hash_pointers_enable); |
| 2159 | |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 2160 | /* |
| 2161 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 2162 | * by an extra set of alphanumeric characters that are extended format |
| 2163 | * specifiers. |
| 2164 | * |
Joe Perches | 0b52376 | 2017-05-08 15:55:36 -0700 | [diff] [blame] | 2165 | * Please update scripts/checkpatch.pl when adding/removing conversion |
| 2166 | * characters. (Search for "check for vsprintf extension"). |
| 2167 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 2168 | * Right now we handle: |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 2169 | * |
Sergey Senozhatsky | cdb7e52 | 2018-04-14 12:00:05 +0900 | [diff] [blame] | 2170 | * - 'S' For symbolic direct pointers (or function descriptors) with offset |
| 2171 | * - 's' For symbolic direct pointers (or function descriptors) without offset |
Sakari Ailus | 9af7706 | 2019-10-03 15:32:14 +0300 | [diff] [blame] | 2172 | * - '[Ss]R' as above with __builtin_extract_return_addr() translation |
Sakari Ailus | 1586c5a | 2019-10-03 15:32:15 +0300 | [diff] [blame] | 2173 | * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of |
| 2174 | * %ps and %pS. Be careful when re-using these specifiers. |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 2175 | * - 'B' For backtraced symbolic direct pointers with offset |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 2176 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
| 2177 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 2178 | * - 'b[l]' For a bitmap, the number of bits is determined by the field |
| 2179 | * width which must be explicitly specified either as part of the |
| 2180 | * format string '%32b[l]' or through '%*b[l]', [l] selects |
| 2181 | * range-list format instead of hex format |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 2182 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 2183 | * usual colon-separated hex notation |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 2184 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 2185 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
Joe Perches | c8e0006 | 2010-01-11 00:44:14 -0800 | [diff] [blame] | 2186 | * with a dash-separated hex notation |
Andy Shevchenko | 7c59154 | 2012-10-04 17:12:33 -0700 | [diff] [blame] | 2187 | * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 2188 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
| 2189 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
| 2190 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 2191 | * [S][pfs] |
| 2192 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
| 2193 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 2194 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
| 2195 | * IPv6 omits the colons (01020304...0f) |
| 2196 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 2197 | * [S][pfs] |
| 2198 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
| 2199 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
| 2200 | * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order |
| 2201 | * - 'I[6S]c' for IPv6 addresses printed as specified by |
Alexander A. Klimov | 8eda94b | 2020-07-02 22:05:36 +0200 | [diff] [blame] | 2202 | * https://tools.ietf.org/html/rfc5952 |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 2203 | * - 'E[achnops]' For an escaped buffer, where rules are defined by combination |
| 2204 | * of the following flags (see string_escape_mem() for the |
| 2205 | * details): |
| 2206 | * a - ESCAPE_ANY |
| 2207 | * c - ESCAPE_SPECIAL |
| 2208 | * h - ESCAPE_HEX |
| 2209 | * n - ESCAPE_NULL |
| 2210 | * o - ESCAPE_OCTAL |
| 2211 | * p - ESCAPE_NP |
| 2212 | * s - ESCAPE_SPACE |
| 2213 | * By default ESCAPE_ANY_NP is used. |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 2214 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
| 2215 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 2216 | * Options for %pU are: |
| 2217 | * b big endian lower case hex (default) |
| 2218 | * B big endian UPPER case hex |
| 2219 | * l little endian lower case hex |
| 2220 | * L little endian UPPER case hex |
| 2221 | * big endian output byte order is: |
| 2222 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
| 2223 | * little endian output byte order is: |
| 2224 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] |
Joe Perches | 7db6f5f | 2010-06-27 01:02:33 +0000 | [diff] [blame] | 2225 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
| 2226 | * call vsnprintf(->format, *->va_list). |
| 2227 | * Implements a "recursive vsnprintf". |
| 2228 | * Do not use this feature without some mechanism to verify the |
| 2229 | * correctness of the format string and va_list arguments. |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 2230 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2231 | * - 'NF' For a netdev_features_t |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 2232 | * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with |
| 2233 | * a certain separator (' ' by default): |
| 2234 | * C colon |
| 2235 | * D dash |
| 2236 | * N no separator |
| 2237 | * The maximum supported length is 64 bytes of the input. Consider |
| 2238 | * to use print_hex_dump() for the larger input. |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 2239 | * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives |
| 2240 | * (default assumed to be phys_addr_t, passed by reference) |
Olof Johansson | c0d92a5 | 2013-11-12 15:09:50 -0800 | [diff] [blame] | 2241 | * - 'd[234]' For a dentry name (optionally 2-4 last components) |
| 2242 | * - 'D[234]' Same as 'd' but for a struct file |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 2243 | * - 'g' For block_device name (gendisk + partition number) |
Andy Shevchenko | 7daac5b | 2020-04-15 20:00:44 +0300 | [diff] [blame] | 2244 | * - 't[RT][dt][r]' For time and date as represented by: |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 2245 | * R struct rtc_time |
Andy Shevchenko | 7daac5b | 2020-04-15 20:00:44 +0300 | [diff] [blame] | 2246 | * T time64_t |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 2247 | * - 'C' For a clock, it prints the name (Common Clock Framework) or address |
| 2248 | * (legacy clock framework) of the clock |
| 2249 | * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address |
| 2250 | * (legacy clock framework) of the clock |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 2251 | * - 'G' For flags to be printed as a collection of symbolic strings that would |
| 2252 | * construct the specific value. Supported flags given by option: |
| 2253 | * p page flags (see struct page) given as pointer to unsigned long |
| 2254 | * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t |
| 2255 | * v vma flags (VM_*) given as pointer to unsigned long |
Geert Uytterhoeven | 94ac8f2 | 2018-10-08 13:08:48 +0200 | [diff] [blame] | 2256 | * - 'OF[fnpPcCF]' For a device tree object |
| 2257 | * Without any optional arguments prints the full_name |
| 2258 | * f device node full_name |
| 2259 | * n device node name |
| 2260 | * p device node phandle |
| 2261 | * P device node path spec (name + @unit) |
| 2262 | * F device node flags |
| 2263 | * c major compatible string |
| 2264 | * C full compatible string |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 2265 | * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer |
| 2266 | * Without an option prints the full name of the node |
| 2267 | * f full name |
| 2268 | * P node name, including a possible unit address |
Tobin C. Harding | 7b1924a | 2017-11-23 10:59:45 +1100 | [diff] [blame] | 2269 | * - 'x' For printing the address. Equivalent to "%lx". |
Daniel Borkmann | b2a5212 | 2020-05-15 12:11:18 +0200 | [diff] [blame] | 2270 | * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of |
| 2271 | * bpf_trace_printk() where [ku] prefix specifies either kernel (k) |
| 2272 | * or user (u) memory to probe, and: |
| 2273 | * s a string, equivalent to "%s" on direct vsnprintf() use |
Tobin C. Harding | 7b1924a | 2017-11-23 10:59:45 +1100 | [diff] [blame] | 2274 | * |
Tobin C. Harding | b3ed232 | 2017-12-20 08:17:15 +1100 | [diff] [blame] | 2275 | * ** When making changes please also update: |
| 2276 | * Documentation/core-api/printk-formats.rst |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 2277 | * |
Tobin C. Harding | ad67b74 | 2017-11-01 15:32:23 +1100 | [diff] [blame] | 2278 | * Note: The default behaviour (unadorned %p) is to hash the address, |
| 2279 | * rendering it useful as a unique identifier. |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 2280 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 2281 | static noinline_for_stack |
| 2282 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 2283 | struct printf_spec spec) |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 2284 | { |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 2285 | switch (*fmt) { |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 2286 | case 'S': |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 2287 | case 's': |
Sergey Senozhatsky | 04b8eb7 | 2017-12-06 13:36:49 +0900 | [diff] [blame] | 2288 | ptr = dereference_symbol_descriptor(ptr); |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 2289 | /* fall through */ |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 2290 | case 'B': |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 2291 | return symbol_string(buf, end, ptr, spec, fmt); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 2292 | case 'R': |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 2293 | case 'r': |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 2294 | return resource_string(buf, end, ptr, spec, fmt); |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 2295 | case 'h': |
| 2296 | return hex_string(buf, end, ptr, spec, fmt); |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 2297 | case 'b': |
| 2298 | switch (fmt[1]) { |
| 2299 | case 'l': |
| 2300 | return bitmap_list_string(buf, end, ptr, spec, fmt); |
| 2301 | default: |
| 2302 | return bitmap_string(buf, end, ptr, spec, fmt); |
| 2303 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 2304 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
| 2305 | case 'm': /* Contiguous: 000102030405 */ |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 2306 | /* [mM]F (FDDI) */ |
| 2307 | /* [mM]R (Reverse order; Bluetooth) */ |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 2308 | return mac_address_string(buf, end, ptr, spec, fmt); |
| 2309 | case 'I': /* Formatted IP supported |
| 2310 | * 4: 1.2.3.4 |
| 2311 | * 6: 0001:0203:...:0708 |
| 2312 | * 6c: 1::708 or 1::1.2.3.4 |
| 2313 | */ |
| 2314 | case 'i': /* Contiguous: |
| 2315 | * 4: 001.002.003.004 |
| 2316 | * 6: 000102...0f |
| 2317 | */ |
Petr Mladek | f00cc10 | 2019-04-17 13:53:44 +0200 | [diff] [blame] | 2318 | return ip_addr_string(buf, end, ptr, spec, fmt); |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 2319 | case 'E': |
| 2320 | return escaped_string(buf, end, ptr, spec, fmt); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 2321 | case 'U': |
| 2322 | return uuid_string(buf, end, ptr, spec, fmt); |
Joe Perches | 7db6f5f | 2010-06-27 01:02:33 +0000 | [diff] [blame] | 2323 | case 'V': |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 2324 | return va_format(buf, end, ptr, spec, fmt); |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 2325 | case 'K': |
Tobin C. Harding | 57e7344 | 2017-11-23 10:56:39 +1100 | [diff] [blame] | 2326 | return restricted_pointer(buf, end, ptr, spec); |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2327 | case 'N': |
Geert Uytterhoeven | 431bca2 | 2018-10-11 10:42:49 +0200 | [diff] [blame] | 2328 | return netdev_bits(buf, end, ptr, spec, fmt); |
Stepan Moskovchenko | 7d79921 | 2013-02-21 16:43:09 -0800 | [diff] [blame] | 2329 | case 'a': |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 2330 | return address_val(buf, end, ptr, spec, fmt); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 2331 | case 'd': |
| 2332 | return dentry_name(buf, end, ptr, spec, fmt); |
Andy Shevchenko | 4d42c44 | 2018-12-04 23:23:11 +0200 | [diff] [blame] | 2333 | case 't': |
| 2334 | return time_and_date(buf, end, ptr, spec, fmt); |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 2335 | case 'C': |
| 2336 | return clock(buf, end, ptr, spec, fmt); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 2337 | case 'D': |
Jia He | 36594b3 | 2019-08-09 09:24:56 +0800 | [diff] [blame] | 2338 | return file_dentry_name(buf, end, ptr, spec, fmt); |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 2339 | #ifdef CONFIG_BLOCK |
| 2340 | case 'g': |
| 2341 | return bdev_name(buf, end, ptr, spec, fmt); |
| 2342 | #endif |
| 2343 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 2344 | case 'G': |
Petr Mladek | 0b74d4d | 2019-04-17 13:53:47 +0200 | [diff] [blame] | 2345 | return flags_string(buf, end, ptr, spec, fmt); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2346 | case 'O': |
Sakari Ailus | 83abc5a | 2019-10-03 15:32:17 +0300 | [diff] [blame] | 2347 | return device_node_string(buf, end, ptr, spec, fmt + 1); |
Sakari Ailus | 3bd32d6 | 2019-10-03 15:32:18 +0300 | [diff] [blame] | 2348 | case 'f': |
| 2349 | return fwnode_string(buf, end, ptr, spec, fmt + 1); |
Tobin C. Harding | 7b1924a | 2017-11-23 10:59:45 +1100 | [diff] [blame] | 2350 | case 'x': |
| 2351 | return pointer_string(buf, end, ptr, spec); |
Rasmus Villemoes | 57f5677 | 2019-10-15 21:07:05 +0200 | [diff] [blame] | 2352 | case 'e': |
| 2353 | /* %pe with a non-ERR_PTR gets treated as plain %p */ |
| 2354 | if (!IS_ERR(ptr)) |
Christophe Leroy | 83ed3e2 | 2022-02-17 09:49:59 +0100 | [diff] [blame] | 2355 | return default_pointer(buf, end, ptr, spec); |
Rasmus Villemoes | 57f5677 | 2019-10-15 21:07:05 +0200 | [diff] [blame] | 2356 | return err_ptr(buf, end, ptr, spec); |
Daniel Borkmann | b2a5212 | 2020-05-15 12:11:18 +0200 | [diff] [blame] | 2357 | case 'u': |
| 2358 | case 'k': |
| 2359 | switch (fmt[1]) { |
| 2360 | case 's': |
| 2361 | return string(buf, end, ptr, spec); |
| 2362 | default: |
| 2363 | return error_string(buf, end, "(einval)", spec); |
| 2364 | } |
Christophe Leroy | 83ed3e2 | 2022-02-17 09:49:59 +0100 | [diff] [blame] | 2365 | default: |
| 2366 | return default_pointer(buf, end, ptr, spec); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 2367 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | /* |
| 2371 | * Helper function to decode printf style format. |
| 2372 | * Each call decode a token from the format and return the |
| 2373 | * number of characters read (or likely the delta where it wants |
| 2374 | * to go on the next call). |
| 2375 | * The decoded token is returned through the parameters |
| 2376 | * |
| 2377 | * 'h', 'l', or 'L' for integer fields |
| 2378 | * 'z' support added 23/7/1999 S.H. |
| 2379 | * 'z' changed to 'Z' --davidm 1/25/99 |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 2380 | * 'Z' changed to 'z' --adobriyan 2017-01-25 |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2381 | * 't' added for ptrdiff_t |
| 2382 | * |
| 2383 | * @fmt: the format string |
| 2384 | * @type of the token returned |
| 2385 | * @flags: various flags such as +, -, # tokens.. |
| 2386 | * @field_width: overwritten width |
| 2387 | * @base: base of the number (octal, hex, ...) |
| 2388 | * @precision: precision of a number |
| 2389 | * @qualifier: qualifier of a number (long, size_t, ...) |
| 2390 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 2391 | static noinline_for_stack |
| 2392 | int format_decode(const char *fmt, struct printf_spec *spec) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2393 | { |
| 2394 | const char *start = fmt; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2395 | char qualifier; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2396 | |
| 2397 | /* we finished early by reading the field width */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2398 | if (spec->type == FORMAT_TYPE_WIDTH) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2399 | if (spec->field_width < 0) { |
| 2400 | spec->field_width = -spec->field_width; |
| 2401 | spec->flags |= LEFT; |
| 2402 | } |
| 2403 | spec->type = FORMAT_TYPE_NONE; |
| 2404 | goto precision; |
| 2405 | } |
| 2406 | |
| 2407 | /* we finished early by reading the precision */ |
| 2408 | if (spec->type == FORMAT_TYPE_PRECISION) { |
| 2409 | if (spec->precision < 0) |
| 2410 | spec->precision = 0; |
| 2411 | |
| 2412 | spec->type = FORMAT_TYPE_NONE; |
| 2413 | goto qualifier; |
| 2414 | } |
| 2415 | |
| 2416 | /* By default */ |
| 2417 | spec->type = FORMAT_TYPE_NONE; |
| 2418 | |
| 2419 | for (; *fmt ; ++fmt) { |
| 2420 | if (*fmt == '%') |
| 2421 | break; |
| 2422 | } |
| 2423 | |
| 2424 | /* Return the current non-format string */ |
| 2425 | if (fmt != start || !*fmt) |
| 2426 | return fmt - start; |
| 2427 | |
| 2428 | /* Process flags */ |
| 2429 | spec->flags = 0; |
| 2430 | |
| 2431 | while (1) { /* this also skips first '%' */ |
| 2432 | bool found = true; |
| 2433 | |
| 2434 | ++fmt; |
| 2435 | |
| 2436 | switch (*fmt) { |
| 2437 | case '-': spec->flags |= LEFT; break; |
| 2438 | case '+': spec->flags |= PLUS; break; |
| 2439 | case ' ': spec->flags |= SPACE; break; |
| 2440 | case '#': spec->flags |= SPECIAL; break; |
| 2441 | case '0': spec->flags |= ZEROPAD; break; |
| 2442 | default: found = false; |
| 2443 | } |
| 2444 | |
| 2445 | if (!found) |
| 2446 | break; |
| 2447 | } |
| 2448 | |
| 2449 | /* get field width */ |
| 2450 | spec->field_width = -1; |
| 2451 | |
| 2452 | if (isdigit(*fmt)) |
| 2453 | spec->field_width = skip_atoi(&fmt); |
| 2454 | else if (*fmt == '*') { |
| 2455 | /* it's the next argument */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2456 | spec->type = FORMAT_TYPE_WIDTH; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2457 | return ++fmt - start; |
| 2458 | } |
| 2459 | |
| 2460 | precision: |
| 2461 | /* get the precision */ |
| 2462 | spec->precision = -1; |
| 2463 | if (*fmt == '.') { |
| 2464 | ++fmt; |
| 2465 | if (isdigit(*fmt)) { |
| 2466 | spec->precision = skip_atoi(&fmt); |
| 2467 | if (spec->precision < 0) |
| 2468 | spec->precision = 0; |
| 2469 | } else if (*fmt == '*') { |
| 2470 | /* it's the next argument */ |
Vegard Nossum | adf26f8 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2471 | spec->type = FORMAT_TYPE_PRECISION; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2472 | return ++fmt - start; |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | qualifier: |
| 2477 | /* get the conversion qualifier */ |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2478 | qualifier = 0; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 2479 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 2480 | *fmt == 'z' || *fmt == 't') { |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2481 | qualifier = *fmt++; |
| 2482 | if (unlikely(qualifier == *fmt)) { |
| 2483 | if (qualifier == 'l') { |
| 2484 | qualifier = 'L'; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2485 | ++fmt; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2486 | } else if (qualifier == 'h') { |
| 2487 | qualifier = 'H'; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2488 | ++fmt; |
| 2489 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | /* default base */ |
| 2494 | spec->base = 10; |
| 2495 | switch (*fmt) { |
| 2496 | case 'c': |
| 2497 | spec->type = FORMAT_TYPE_CHAR; |
| 2498 | return ++fmt - start; |
| 2499 | |
| 2500 | case 's': |
| 2501 | spec->type = FORMAT_TYPE_STR; |
| 2502 | return ++fmt - start; |
| 2503 | |
| 2504 | case 'p': |
| 2505 | spec->type = FORMAT_TYPE_PTR; |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 2506 | return ++fmt - start; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2507 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2508 | case '%': |
| 2509 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
| 2510 | return ++fmt - start; |
| 2511 | |
| 2512 | /* integer number formats - set up the flags and "break" */ |
| 2513 | case 'o': |
| 2514 | spec->base = 8; |
| 2515 | break; |
| 2516 | |
| 2517 | case 'x': |
| 2518 | spec->flags |= SMALL; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 2519 | /* fall through */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2520 | |
| 2521 | case 'X': |
| 2522 | spec->base = 16; |
| 2523 | break; |
| 2524 | |
| 2525 | case 'd': |
| 2526 | case 'i': |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 2527 | spec->flags |= SIGN; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2528 | case 'u': |
| 2529 | break; |
| 2530 | |
Ryan Mallon | 708d96fd | 2014-04-03 14:48:37 -0700 | [diff] [blame] | 2531 | case 'n': |
| 2532 | /* |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2533 | * Since %n poses a greater security risk than |
| 2534 | * utility, treat it as any other invalid or |
| 2535 | * unsupported format specifier. |
Ryan Mallon | 708d96fd | 2014-04-03 14:48:37 -0700 | [diff] [blame] | 2536 | */ |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 2537 | /* fall through */ |
Ryan Mallon | 708d96fd | 2014-04-03 14:48:37 -0700 | [diff] [blame] | 2538 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2539 | default: |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2540 | WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2541 | spec->type = FORMAT_TYPE_INVALID; |
| 2542 | return fmt - start; |
| 2543 | } |
| 2544 | |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2545 | if (qualifier == 'L') |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2546 | spec->type = FORMAT_TYPE_LONG_LONG; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2547 | else if (qualifier == 'l') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2548 | BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); |
| 2549 | spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 2550 | } else if (qualifier == 'z') { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2551 | spec->type = FORMAT_TYPE_SIZE_T; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2552 | } else if (qualifier == 't') { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2553 | spec->type = FORMAT_TYPE_PTRDIFF; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2554 | } else if (qualifier == 'H') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2555 | BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); |
| 2556 | spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2557 | } else if (qualifier == 'h') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2558 | BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); |
| 2559 | spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2560 | } else { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2561 | BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); |
| 2562 | spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | return ++fmt - start; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2568 | static void |
| 2569 | set_field_width(struct printf_spec *spec, int width) |
| 2570 | { |
| 2571 | spec->field_width = width; |
| 2572 | if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { |
| 2573 | spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | static void |
| 2578 | set_precision(struct printf_spec *spec, int prec) |
| 2579 | { |
| 2580 | spec->precision = prec; |
| 2581 | if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { |
| 2582 | spec->precision = clamp(prec, 0, PRECISION_MAX); |
| 2583 | } |
| 2584 | } |
| 2585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | /** |
| 2587 | * vsnprintf - Format a string and place it in a buffer |
| 2588 | * @buf: The buffer to place the result into |
| 2589 | * @size: The size of the buffer, including the trailing null space |
| 2590 | * @fmt: The format string to use |
| 2591 | * @args: Arguments for the format string |
| 2592 | * |
Rasmus Villemoes | d7ec9a0 | 2015-11-06 16:30:35 -0800 | [diff] [blame] | 2593 | * This function generally follows C99 vsnprintf, but has some |
| 2594 | * extensions and a few limitations: |
| 2595 | * |
mchehab@s-opensource.com | 6cc8913 | 2017-03-30 17:11:33 -0300 | [diff] [blame] | 2596 | * - ``%n`` is unsupported |
| 2597 | * - ``%p*`` is handled by pointer() |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2598 | * |
Jonathan Corbet | 27e7c0e | 2017-12-21 12:39:45 -0700 | [diff] [blame] | 2599 | * See pointer() or Documentation/core-api/printk-formats.rst for more |
Martin Kletzander | 5e4ee7b | 2015-11-06 16:30:17 -0800 | [diff] [blame] | 2600 | * extensive description. |
| 2601 | * |
mchehab@s-opensource.com | 6cc8913 | 2017-03-30 17:11:33 -0300 | [diff] [blame] | 2602 | * **Please update the documentation in both places when making changes** |
Andrew Morton | 80f548e | 2012-07-30 14:40:25 -0700 | [diff] [blame] | 2603 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | * The return value is the number of characters which would |
| 2605 | * be generated for the given input, excluding the trailing |
| 2606 | * '\0', as per ISO C99. If you want to have the exact |
| 2607 | * number of characters written into @buf as return value |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2608 | * (not including the trailing '\0'), use vscnprintf(). If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | * return is greater than or equal to @size, the resulting |
| 2610 | * string is truncated. |
| 2611 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2612 | * If you're not already dealing with a va_list consider using snprintf(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | */ |
| 2614 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 2615 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | unsigned long long num; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2617 | char *str, *end; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2618 | struct printf_spec spec = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2620 | /* Reject out-of-range values early. Large positive sizes are |
| 2621 | used for unknown buffer sizes. */ |
Rasmus Villemoes | 2aa2f9e | 2015-02-12 15:01:39 -0800 | [diff] [blame] | 2622 | if (WARN_ON_ONCE(size > INT_MAX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | |
| 2625 | str = buf; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2626 | end = buf + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2628 | /* Make sure end is always >= buf */ |
| 2629 | if (end < buf) { |
| 2630 | end = ((void *)-1); |
| 2631 | size = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | } |
| 2633 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2634 | while (*fmt) { |
| 2635 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2636 | int read = format_decode(fmt, &spec); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2637 | |
| 2638 | fmt += read; |
| 2639 | |
| 2640 | switch (spec.type) { |
| 2641 | case FORMAT_TYPE_NONE: { |
| 2642 | int copy = read; |
| 2643 | if (str < end) { |
| 2644 | if (copy > end - str) |
| 2645 | copy = end - str; |
| 2646 | memcpy(str, old_fmt, copy); |
| 2647 | } |
| 2648 | str += read; |
| 2649 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | } |
| 2651 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2652 | case FORMAT_TYPE_WIDTH: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2653 | set_field_width(&spec, va_arg(args, int)); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2654 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2656 | case FORMAT_TYPE_PRECISION: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2657 | set_precision(&spec, va_arg(args, int)); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2658 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2660 | case FORMAT_TYPE_CHAR: { |
| 2661 | char c; |
| 2662 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2663 | if (!(spec.flags & LEFT)) { |
| 2664 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2665 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | *str = ' '; |
| 2667 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2670 | } |
| 2671 | c = (unsigned char) va_arg(args, int); |
| 2672 | if (str < end) |
| 2673 | *str = c; |
| 2674 | ++str; |
| 2675 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2676 | if (str < end) |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2677 | *str = ' '; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2679 | } |
| 2680 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2683 | case FORMAT_TYPE_STR: |
| 2684 | str = string(str, end, va_arg(args, char *), spec); |
| 2685 | break; |
| 2686 | |
| 2687 | case FORMAT_TYPE_PTR: |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 2688 | str = pointer(fmt, str, end, va_arg(args, void *), |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2689 | spec); |
| 2690 | while (isalnum(*fmt)) |
| 2691 | fmt++; |
| 2692 | break; |
| 2693 | |
| 2694 | case FORMAT_TYPE_PERCENT_CHAR: |
| 2695 | if (str < end) |
| 2696 | *str = '%'; |
| 2697 | ++str; |
| 2698 | break; |
| 2699 | |
| 2700 | case FORMAT_TYPE_INVALID: |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2701 | /* |
| 2702 | * Presumably the arguments passed gcc's type |
| 2703 | * checking, but there is no safe or sane way |
| 2704 | * for us to continue parsing the format and |
| 2705 | * fetching from the va_list; the remaining |
| 2706 | * specifiers and arguments would be out of |
| 2707 | * sync. |
| 2708 | */ |
| 2709 | goto out; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2710 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2711 | default: |
| 2712 | switch (spec.type) { |
| 2713 | case FORMAT_TYPE_LONG_LONG: |
| 2714 | num = va_arg(args, long long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2716 | case FORMAT_TYPE_ULONG: |
| 2717 | num = va_arg(args, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2719 | case FORMAT_TYPE_LONG: |
| 2720 | num = va_arg(args, long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2722 | case FORMAT_TYPE_SIZE_T: |
Jason Gunthorpe | ef12496 | 2012-12-17 15:59:58 -0800 | [diff] [blame] | 2723 | if (spec.flags & SIGN) |
| 2724 | num = va_arg(args, ssize_t); |
| 2725 | else |
| 2726 | num = va_arg(args, size_t); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2727 | break; |
| 2728 | case FORMAT_TYPE_PTRDIFF: |
| 2729 | num = va_arg(args, ptrdiff_t); |
| 2730 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2731 | case FORMAT_TYPE_UBYTE: |
| 2732 | num = (unsigned char) va_arg(args, int); |
| 2733 | break; |
| 2734 | case FORMAT_TYPE_BYTE: |
| 2735 | num = (signed char) va_arg(args, int); |
| 2736 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2737 | case FORMAT_TYPE_USHORT: |
| 2738 | num = (unsigned short) va_arg(args, int); |
| 2739 | break; |
| 2740 | case FORMAT_TYPE_SHORT: |
| 2741 | num = (short) va_arg(args, int); |
| 2742 | break; |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 2743 | case FORMAT_TYPE_INT: |
| 2744 | num = (int) va_arg(args, int); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2745 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2747 | num = va_arg(args, unsigned int); |
| 2748 | } |
| 2749 | |
| 2750 | str = number(str, end, num, spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2753 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2754 | out: |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2755 | if (size > 0) { |
| 2756 | if (str < end) |
| 2757 | *str = '\0'; |
| 2758 | else |
Linus Torvalds | 0a6047e | 2006-06-28 17:09:34 -0700 | [diff] [blame] | 2759 | end[-1] = '\0'; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2760 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2761 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2762 | /* the trailing null byte doesn't count towards the total */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | return str-buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | EXPORT_SYMBOL(vsnprintf); |
| 2767 | |
| 2768 | /** |
| 2769 | * vscnprintf - Format a string and place it in a buffer |
| 2770 | * @buf: The buffer to place the result into |
| 2771 | * @size: The size of the buffer, including the trailing null space |
| 2772 | * @fmt: The format string to use |
| 2773 | * @args: Arguments for the format string |
| 2774 | * |
| 2775 | * The return value is the number of characters which have been written into |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2776 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | * returns 0. |
| 2778 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2779 | * If you're not already dealing with a va_list consider using scnprintf(). |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2780 | * |
| 2781 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | */ |
| 2783 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 2784 | { |
| 2785 | int i; |
| 2786 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2787 | i = vsnprintf(buf, size, fmt, args); |
| 2788 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2789 | if (likely(i < size)) |
| 2790 | return i; |
| 2791 | if (size != 0) |
| 2792 | return size - 1; |
| 2793 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | EXPORT_SYMBOL(vscnprintf); |
| 2796 | |
| 2797 | /** |
| 2798 | * snprintf - Format a string and place it in a buffer |
| 2799 | * @buf: The buffer to place the result into |
| 2800 | * @size: The size of the buffer, including the trailing null space |
| 2801 | * @fmt: The format string to use |
| 2802 | * @...: Arguments for the format string |
| 2803 | * |
| 2804 | * The return value is the number of characters which would be |
| 2805 | * generated for the given input, excluding the trailing null, |
| 2806 | * as per ISO C99. If the return is greater than or equal to |
| 2807 | * @size, the resulting string is truncated. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2808 | * |
| 2809 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2811 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | { |
| 2813 | va_list args; |
| 2814 | int i; |
| 2815 | |
| 2816 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2817 | i = vsnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | return i; |
| 2821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | EXPORT_SYMBOL(snprintf); |
| 2823 | |
| 2824 | /** |
| 2825 | * scnprintf - Format a string and place it in a buffer |
| 2826 | * @buf: The buffer to place the result into |
| 2827 | * @size: The size of the buffer, including the trailing null space |
| 2828 | * @fmt: The format string to use |
| 2829 | * @...: Arguments for the format string |
| 2830 | * |
| 2831 | * The return value is the number of characters written into @buf not including |
Changli Gao | b903c0b | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 2832 | * the trailing '\0'. If @size is == 0 the function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | */ |
| 2834 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2835 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | { |
| 2837 | va_list args; |
| 2838 | int i; |
| 2839 | |
| 2840 | va_start(args, fmt); |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2841 | i = vscnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2843 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2844 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | } |
| 2846 | EXPORT_SYMBOL(scnprintf); |
| 2847 | |
| 2848 | /** |
| 2849 | * vsprintf - Format a string and place it in a buffer |
| 2850 | * @buf: The buffer to place the result into |
| 2851 | * @fmt: The format string to use |
| 2852 | * @args: Arguments for the format string |
| 2853 | * |
| 2854 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2855 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | * buffer overflows. |
| 2857 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2858 | * If you're not already dealing with a va_list consider using sprintf(). |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2859 | * |
| 2860 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | */ |
| 2862 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 2863 | { |
| 2864 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 2865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | EXPORT_SYMBOL(vsprintf); |
| 2867 | |
| 2868 | /** |
| 2869 | * sprintf - Format a string and place it in a buffer |
| 2870 | * @buf: The buffer to place the result into |
| 2871 | * @fmt: The format string to use |
| 2872 | * @...: Arguments for the format string |
| 2873 | * |
| 2874 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2875 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | * buffer overflows. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2877 | * |
| 2878 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2880 | int sprintf(char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | { |
| 2882 | va_list args; |
| 2883 | int i; |
| 2884 | |
| 2885 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2886 | i = vsnprintf(buf, INT_MAX, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | return i; |
| 2890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2891 | EXPORT_SYMBOL(sprintf); |
| 2892 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2893 | #ifdef CONFIG_BINARY_PRINTF |
| 2894 | /* |
| 2895 | * bprintf service: |
| 2896 | * vbin_printf() - VA arguments to binary data |
| 2897 | * bstr_printf() - Binary data to text string |
| 2898 | */ |
| 2899 | |
| 2900 | /** |
| 2901 | * vbin_printf - Parse a format string and place args' binary value in a buffer |
| 2902 | * @bin_buf: The buffer to place args' binary value |
| 2903 | * @size: The size of the buffer(by words(32bits), not characters) |
| 2904 | * @fmt: The format string to use |
| 2905 | * @args: Arguments for the format string |
| 2906 | * |
| 2907 | * The format follows C99 vsnprintf, except %n is ignored, and its argument |
Masanari Iida | da3dae5 | 2014-09-09 01:27:23 +0900 | [diff] [blame] | 2908 | * is skipped. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2909 | * |
| 2910 | * The return value is the number of words(32bits) which would be generated for |
| 2911 | * the given input. |
| 2912 | * |
| 2913 | * NOTE: |
| 2914 | * If the return value is greater than @size, the resulting bin_buf is NOT |
| 2915 | * valid for bstr_printf(). |
| 2916 | */ |
| 2917 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) |
| 2918 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2919 | struct printf_spec spec = {0}; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2920 | char *str, *end; |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2921 | int width; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2922 | |
| 2923 | str = (char *)bin_buf; |
| 2924 | end = (char *)(bin_buf + size); |
| 2925 | |
| 2926 | #define save_arg(type) \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2927 | ({ \ |
| 2928 | unsigned long long value; \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2929 | if (sizeof(type) == 8) { \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2930 | unsigned long long val8; \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2931 | str = PTR_ALIGN(str, sizeof(u32)); \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2932 | val8 = va_arg(args, unsigned long long); \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2933 | if (str + sizeof(type) <= end) { \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2934 | *(u32 *)str = *(u32 *)&val8; \ |
| 2935 | *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2936 | } \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2937 | value = val8; \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2938 | } else { \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2939 | unsigned int val4; \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2940 | str = PTR_ALIGN(str, sizeof(type)); \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2941 | val4 = va_arg(args, int); \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2942 | if (str + sizeof(type) <= end) \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2943 | *(typeof(type) *)str = (type)(long)val4; \ |
| 2944 | value = (unsigned long long)val4; \ |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2945 | } \ |
| 2946 | str += sizeof(type); \ |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2947 | value; \ |
| 2948 | }) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2949 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2950 | while (*fmt) { |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2951 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2952 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2953 | fmt += read; |
| 2954 | |
| 2955 | switch (spec.type) { |
| 2956 | case FORMAT_TYPE_NONE: |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2957 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2958 | break; |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2959 | case FORMAT_TYPE_INVALID: |
| 2960 | goto out; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2961 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2962 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2963 | case FORMAT_TYPE_PRECISION: |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2964 | width = (int)save_arg(int); |
| 2965 | /* Pointers may require the width */ |
| 2966 | if (*fmt == 'p') |
| 2967 | set_field_width(&spec, width); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2968 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2969 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2970 | case FORMAT_TYPE_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2971 | save_arg(char); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2972 | break; |
| 2973 | |
| 2974 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2975 | const char *save_str = va_arg(args, char *); |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 2976 | const char *err_msg; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2977 | size_t len; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 2978 | |
Petr Mladek | 3e5903e | 2019-04-17 13:53:48 +0200 | [diff] [blame] | 2979 | err_msg = check_pointer_msg(save_str); |
| 2980 | if (err_msg) |
| 2981 | save_str = err_msg; |
| 2982 | |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 2983 | len = strlen(save_str) + 1; |
| 2984 | if (str + len < end) |
| 2985 | memcpy(str, save_str, len); |
| 2986 | str += len; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2987 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2988 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2989 | |
| 2990 | case FORMAT_TYPE_PTR: |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2991 | /* Dereferenced pointers must be done now */ |
| 2992 | switch (*fmt) { |
| 2993 | /* Dereference of functions is still OK */ |
| 2994 | case 'S': |
| 2995 | case 's': |
Steven Rostedt (VMware) | 1e6338c | 2018-04-03 14:38:53 -0400 | [diff] [blame] | 2996 | case 'x': |
| 2997 | case 'K': |
Rasmus Villemoes | 57f5677 | 2019-10-15 21:07:05 +0200 | [diff] [blame] | 2998 | case 'e': |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 2999 | save_arg(void *); |
| 3000 | break; |
| 3001 | default: |
| 3002 | if (!isalnum(*fmt)) { |
| 3003 | save_arg(void *); |
| 3004 | break; |
| 3005 | } |
| 3006 | str = pointer(fmt, str, end, va_arg(args, void *), |
| 3007 | spec); |
| 3008 | if (str + 1 < end) |
| 3009 | *str++ = '\0'; |
| 3010 | else |
| 3011 | end[-1] = '\0'; /* Must be nul terminated */ |
| 3012 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3013 | /* skip all alphanumeric pointer suffixes */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3014 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3015 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3016 | break; |
| 3017 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3018 | default: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3019 | switch (spec.type) { |
| 3020 | |
| 3021 | case FORMAT_TYPE_LONG_LONG: |
| 3022 | save_arg(long long); |
| 3023 | break; |
| 3024 | case FORMAT_TYPE_ULONG: |
| 3025 | case FORMAT_TYPE_LONG: |
| 3026 | save_arg(unsigned long); |
| 3027 | break; |
| 3028 | case FORMAT_TYPE_SIZE_T: |
| 3029 | save_arg(size_t); |
| 3030 | break; |
| 3031 | case FORMAT_TYPE_PTRDIFF: |
| 3032 | save_arg(ptrdiff_t); |
| 3033 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 3034 | case FORMAT_TYPE_UBYTE: |
| 3035 | case FORMAT_TYPE_BYTE: |
| 3036 | save_arg(char); |
| 3037 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3038 | case FORMAT_TYPE_USHORT: |
| 3039 | case FORMAT_TYPE_SHORT: |
| 3040 | save_arg(short); |
| 3041 | break; |
| 3042 | default: |
| 3043 | save_arg(int); |
| 3044 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3045 | } |
| 3046 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3047 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 3048 | out: |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3049 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3050 | #undef save_arg |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3051 | } |
| 3052 | EXPORT_SYMBOL_GPL(vbin_printf); |
| 3053 | |
| 3054 | /** |
| 3055 | * bstr_printf - Format a string from binary arguments and place it in a buffer |
| 3056 | * @buf: The buffer to place the result into |
| 3057 | * @size: The size of the buffer, including the trailing null space |
| 3058 | * @fmt: The format string to use |
| 3059 | * @bin_buf: Binary arguments for the format string |
| 3060 | * |
| 3061 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets |
| 3062 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is |
| 3063 | * a binary buffer that generated by vbin_printf. |
| 3064 | * |
| 3065 | * The format follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 3066 | * see vsnprintf comment for details. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3067 | * |
| 3068 | * The return value is the number of characters which would |
| 3069 | * be generated for the given input, excluding the trailing |
| 3070 | * '\0', as per ISO C99. If you want to have the exact |
| 3071 | * number of characters written into @buf as return value |
| 3072 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 3073 | * return is greater than or equal to @size, the resulting |
| 3074 | * string is truncated. |
| 3075 | */ |
| 3076 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
| 3077 | { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3078 | struct printf_spec spec = {0}; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3079 | char *str, *end; |
| 3080 | const char *args = (const char *)bin_buf; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3081 | |
Rasmus Villemoes | 762abb5 | 2015-11-06 16:30:23 -0800 | [diff] [blame] | 3082 | if (WARN_ON_ONCE(size > INT_MAX)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3083 | return 0; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3084 | |
| 3085 | str = buf; |
| 3086 | end = buf + size; |
| 3087 | |
| 3088 | #define get_arg(type) \ |
| 3089 | ({ \ |
| 3090 | typeof(type) value; \ |
| 3091 | if (sizeof(type) == 8) { \ |
| 3092 | args = PTR_ALIGN(args, sizeof(u32)); \ |
| 3093 | *(u32 *)&value = *(u32 *)args; \ |
| 3094 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ |
| 3095 | } else { \ |
| 3096 | args = PTR_ALIGN(args, sizeof(type)); \ |
| 3097 | value = *(typeof(type) *)args; \ |
| 3098 | } \ |
| 3099 | args += sizeof(type); \ |
| 3100 | value; \ |
| 3101 | }) |
| 3102 | |
| 3103 | /* Make sure end is always >= buf */ |
| 3104 | if (end < buf) { |
| 3105 | end = ((void *)-1); |
| 3106 | size = end - buf; |
| 3107 | } |
| 3108 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3109 | while (*fmt) { |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3110 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3111 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3112 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3113 | fmt += read; |
| 3114 | |
| 3115 | switch (spec.type) { |
| 3116 | case FORMAT_TYPE_NONE: { |
| 3117 | int copy = read; |
| 3118 | if (str < end) { |
| 3119 | if (copy > end - str) |
| 3120 | copy = end - str; |
| 3121 | memcpy(str, old_fmt, copy); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3122 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3123 | str += read; |
| 3124 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3125 | } |
| 3126 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 3127 | case FORMAT_TYPE_WIDTH: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 3128 | set_field_width(&spec, get_arg(int)); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3129 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3130 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3131 | case FORMAT_TYPE_PRECISION: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 3132 | set_precision(&spec, get_arg(int)); |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3133 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3134 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3135 | case FORMAT_TYPE_CHAR: { |
| 3136 | char c; |
| 3137 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3138 | if (!(spec.flags & LEFT)) { |
| 3139 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3140 | if (str < end) |
| 3141 | *str = ' '; |
| 3142 | ++str; |
| 3143 | } |
| 3144 | } |
| 3145 | c = (unsigned char) get_arg(char); |
| 3146 | if (str < end) |
| 3147 | *str = c; |
| 3148 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3149 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3150 | if (str < end) |
| 3151 | *str = ' '; |
| 3152 | ++str; |
| 3153 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3154 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3155 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3156 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3157 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3158 | const char *str_arg = args; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3159 | args += strlen(str_arg) + 1; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3160 | str = string(str, end, (char *)str_arg, spec); |
| 3161 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3162 | } |
| 3163 | |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 3164 | case FORMAT_TYPE_PTR: { |
| 3165 | bool process = false; |
| 3166 | int copy, len; |
| 3167 | /* Non function dereferences were already done */ |
| 3168 | switch (*fmt) { |
| 3169 | case 'S': |
| 3170 | case 's': |
Steven Rostedt (VMware) | 1e6338c | 2018-04-03 14:38:53 -0400 | [diff] [blame] | 3171 | case 'x': |
| 3172 | case 'K': |
Rasmus Villemoes | 57f5677 | 2019-10-15 21:07:05 +0200 | [diff] [blame] | 3173 | case 'e': |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 3174 | process = true; |
| 3175 | break; |
| 3176 | default: |
| 3177 | if (!isalnum(*fmt)) { |
| 3178 | process = true; |
| 3179 | break; |
| 3180 | } |
| 3181 | /* Pointer dereference was already processed */ |
| 3182 | if (str < end) { |
| 3183 | len = copy = strlen(args); |
| 3184 | if (copy > end - str) |
| 3185 | copy = end - str; |
| 3186 | memcpy(str, args, copy); |
| 3187 | str += len; |
Steven Rostedt (VMware) | 6216560 | 2018-10-05 10:08:03 -0400 | [diff] [blame] | 3188 | args += len + 1; |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 3189 | } |
| 3190 | } |
| 3191 | if (process) |
| 3192 | str = pointer(fmt, str, end, get_arg(void *), spec); |
| 3193 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3194 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3195 | fmt++; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3196 | break; |
Steven Rostedt (VMware) | 841a915 | 2017-12-28 20:40:25 -0500 | [diff] [blame] | 3197 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3198 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3199 | case FORMAT_TYPE_PERCENT_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3200 | if (str < end) |
| 3201 | *str = '%'; |
| 3202 | ++str; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3203 | break; |
| 3204 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 3205 | case FORMAT_TYPE_INVALID: |
| 3206 | goto out; |
| 3207 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3208 | default: { |
| 3209 | unsigned long long num; |
| 3210 | |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3211 | switch (spec.type) { |
| 3212 | |
| 3213 | case FORMAT_TYPE_LONG_LONG: |
| 3214 | num = get_arg(long long); |
| 3215 | break; |
| 3216 | case FORMAT_TYPE_ULONG: |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3217 | case FORMAT_TYPE_LONG: |
| 3218 | num = get_arg(unsigned long); |
| 3219 | break; |
| 3220 | case FORMAT_TYPE_SIZE_T: |
| 3221 | num = get_arg(size_t); |
| 3222 | break; |
| 3223 | case FORMAT_TYPE_PTRDIFF: |
| 3224 | num = get_arg(ptrdiff_t); |
| 3225 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 3226 | case FORMAT_TYPE_UBYTE: |
| 3227 | num = get_arg(unsigned char); |
| 3228 | break; |
| 3229 | case FORMAT_TYPE_BYTE: |
| 3230 | num = get_arg(signed char); |
| 3231 | break; |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3232 | case FORMAT_TYPE_USHORT: |
| 3233 | num = get_arg(unsigned short); |
| 3234 | break; |
| 3235 | case FORMAT_TYPE_SHORT: |
| 3236 | num = get_arg(short); |
| 3237 | break; |
| 3238 | case FORMAT_TYPE_UINT: |
| 3239 | num = get_arg(unsigned int); |
| 3240 | break; |
| 3241 | default: |
| 3242 | num = get_arg(int); |
| 3243 | } |
| 3244 | |
| 3245 | str = number(str, end, num, spec); |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3246 | } /* default: */ |
| 3247 | } /* switch(spec.type) */ |
| 3248 | } /* while(*fmt) */ |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3249 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 3250 | out: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3251 | if (size > 0) { |
| 3252 | if (str < end) |
| 3253 | *str = '\0'; |
| 3254 | else |
| 3255 | end[-1] = '\0'; |
| 3256 | } |
Frederic Weisbecker | fef20d9 | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 3257 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3258 | #undef get_arg |
| 3259 | |
| 3260 | /* the trailing null byte doesn't count towards the total */ |
| 3261 | return str - buf; |
| 3262 | } |
| 3263 | EXPORT_SYMBOL_GPL(bstr_printf); |
| 3264 | |
| 3265 | /** |
| 3266 | * bprintf - Parse a format string and place args' binary value in a buffer |
| 3267 | * @bin_buf: The buffer to place args' binary value |
| 3268 | * @size: The size of the buffer(by words(32bits), not characters) |
| 3269 | * @fmt: The format string to use |
| 3270 | * @...: Arguments for the format string |
| 3271 | * |
| 3272 | * The function returns the number of words(u32) written |
| 3273 | * into @bin_buf. |
| 3274 | */ |
| 3275 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) |
| 3276 | { |
| 3277 | va_list args; |
| 3278 | int ret; |
| 3279 | |
| 3280 | va_start(args, fmt); |
| 3281 | ret = vbin_printf(bin_buf, size, fmt, args); |
| 3282 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3283 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 3284 | return ret; |
| 3285 | } |
| 3286 | EXPORT_SYMBOL_GPL(bprintf); |
| 3287 | |
| 3288 | #endif /* CONFIG_BINARY_PRINTF */ |
| 3289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3290 | /** |
| 3291 | * vsscanf - Unformat a buffer into a list of arguments |
| 3292 | * @buf: input buffer |
| 3293 | * @fmt: format of buffer |
| 3294 | * @args: arguments |
| 3295 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3296 | int vsscanf(const char *buf, const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | { |
| 3298 | const char *str = buf; |
| 3299 | char *next; |
| 3300 | char digit; |
| 3301 | int num = 0; |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 3302 | u8 qualifier; |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3303 | unsigned int base; |
| 3304 | union { |
| 3305 | long long s; |
| 3306 | unsigned long long u; |
| 3307 | } val; |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 3308 | s16 field_width; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3309 | bool is_sign; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 3311 | while (*fmt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | /* skip any white space in format */ |
| 3313 | /* white space in format matchs any amount of |
| 3314 | * white space, including none, in the input. |
| 3315 | */ |
| 3316 | if (isspace(*fmt)) { |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3317 | fmt = skip_spaces(++fmt); |
| 3318 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | /* anything that is not a conversion must match exactly */ |
| 3322 | if (*fmt != '%' && *fmt) { |
| 3323 | if (*fmt++ != *str++) |
| 3324 | break; |
| 3325 | continue; |
| 3326 | } |
| 3327 | |
| 3328 | if (!*fmt) |
| 3329 | break; |
| 3330 | ++fmt; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | /* skip this conversion. |
| 3333 | * advance both strings to next white space |
| 3334 | */ |
| 3335 | if (*fmt == '*') { |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 3336 | if (!*str) |
| 3337 | break; |
Jessica Yu | f9310b2 | 2016-03-17 14:23:07 -0700 | [diff] [blame] | 3338 | while (!isspace(*fmt) && *fmt != '%' && *fmt) { |
| 3339 | /* '%*[' not yet supported, invalid format */ |
| 3340 | if (*fmt == '[') |
| 3341 | return num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | fmt++; |
Jessica Yu | f9310b2 | 2016-03-17 14:23:07 -0700 | [diff] [blame] | 3343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | while (!isspace(*str) && *str) |
| 3345 | str++; |
| 3346 | continue; |
| 3347 | } |
| 3348 | |
| 3349 | /* get field width */ |
| 3350 | field_width = -1; |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3351 | if (isdigit(*fmt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | field_width = skip_atoi(&fmt); |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3353 | if (field_width <= 0) |
| 3354 | break; |
| 3355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | |
| 3357 | /* get conversion qualifier */ |
| 3358 | qualifier = -1; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 3359 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 3360 | *fmt == 'z') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3361 | qualifier = *fmt++; |
| 3362 | if (unlikely(qualifier == *fmt)) { |
| 3363 | if (qualifier == 'h') { |
| 3364 | qualifier = 'H'; |
| 3365 | fmt++; |
| 3366 | } else if (qualifier == 'l') { |
| 3367 | qualifier = 'L'; |
| 3368 | fmt++; |
| 3369 | } |
| 3370 | } |
| 3371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 3373 | if (!*fmt) |
| 3374 | break; |
| 3375 | |
| 3376 | if (*fmt == 'n') { |
| 3377 | /* return number of characters read so far */ |
| 3378 | *va_arg(args, int *) = str - buf; |
| 3379 | ++fmt; |
| 3380 | continue; |
| 3381 | } |
| 3382 | |
| 3383 | if (!*str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | break; |
| 3385 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3386 | base = 10; |
Fabian Frederick | 3f623eb | 2014-06-04 16:11:52 -0700 | [diff] [blame] | 3387 | is_sign = false; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 3388 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3389 | switch (*fmt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | case 'c': |
| 3391 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3392 | char *s = (char *)va_arg(args, char*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | if (field_width == -1) |
| 3394 | field_width = 1; |
| 3395 | do { |
| 3396 | *s++ = *str++; |
| 3397 | } while (--field_width > 0 && *str); |
| 3398 | num++; |
| 3399 | } |
| 3400 | continue; |
| 3401 | case 's': |
| 3402 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3403 | char *s = (char *)va_arg(args, char *); |
| 3404 | if (field_width == -1) |
Alexey Dobriyan | 4be929b | 2010-05-24 14:33:03 -0700 | [diff] [blame] | 3405 | field_width = SHRT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | /* first, skip leading white space in buffer */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3407 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | |
| 3409 | /* now copy until next white space */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3410 | while (*str && !isspace(*str) && field_width--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | *s++ = *str++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | *s = '\0'; |
| 3413 | num++; |
| 3414 | } |
| 3415 | continue; |
Jessica Yu | f9310b2 | 2016-03-17 14:23:07 -0700 | [diff] [blame] | 3416 | /* |
| 3417 | * Warning: This implementation of the '[' conversion specifier |
| 3418 | * deviates from its glibc counterpart in the following ways: |
| 3419 | * (1) It does NOT support ranges i.e. '-' is NOT a special |
| 3420 | * character |
| 3421 | * (2) It cannot match the closing bracket ']' itself |
| 3422 | * (3) A field width is required |
| 3423 | * (4) '%*[' (discard matching input) is currently not supported |
| 3424 | * |
| 3425 | * Example usage: |
| 3426 | * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", |
| 3427 | * buf1, buf2, buf3); |
| 3428 | * if (ret < 3) |
| 3429 | * // etc.. |
| 3430 | */ |
| 3431 | case '[': |
| 3432 | { |
| 3433 | char *s = (char *)va_arg(args, char *); |
| 3434 | DECLARE_BITMAP(set, 256) = {0}; |
| 3435 | unsigned int len = 0; |
| 3436 | bool negate = (*fmt == '^'); |
| 3437 | |
| 3438 | /* field width is required */ |
| 3439 | if (field_width == -1) |
| 3440 | return num; |
| 3441 | |
| 3442 | if (negate) |
| 3443 | ++fmt; |
| 3444 | |
| 3445 | for ( ; *fmt && *fmt != ']'; ++fmt, ++len) |
| 3446 | set_bit((u8)*fmt, set); |
| 3447 | |
| 3448 | /* no ']' or no character set found */ |
| 3449 | if (!*fmt || !len) |
| 3450 | return num; |
| 3451 | ++fmt; |
| 3452 | |
| 3453 | if (negate) { |
| 3454 | bitmap_complement(set, set, 256); |
| 3455 | /* exclude null '\0' byte */ |
| 3456 | clear_bit(0, set); |
| 3457 | } |
| 3458 | |
| 3459 | /* match must be non-empty */ |
| 3460 | if (!test_bit((u8)*str, set)) |
| 3461 | return num; |
| 3462 | |
| 3463 | while (test_bit((u8)*str, set) && field_width--) |
| 3464 | *s++ = *str++; |
| 3465 | *s = '\0'; |
| 3466 | ++num; |
| 3467 | } |
| 3468 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | case 'o': |
| 3470 | base = 8; |
| 3471 | break; |
| 3472 | case 'x': |
| 3473 | case 'X': |
| 3474 | base = 16; |
| 3475 | break; |
| 3476 | case 'i': |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3477 | base = 0; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 3478 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | case 'd': |
Fabian Frederick | 3f623eb | 2014-06-04 16:11:52 -0700 | [diff] [blame] | 3480 | is_sign = true; |
Gustavo A. R. Silva | 6a9dc5f | 2020-08-24 15:36:14 -0500 | [diff] [blame] | 3481 | /* fall through */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | case 'u': |
| 3483 | break; |
| 3484 | case '%': |
| 3485 | /* looking for '%' in str */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3486 | if (*str++ != '%') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | return num; |
| 3488 | continue; |
| 3489 | default: |
| 3490 | /* invalid format; stop here */ |
| 3491 | return num; |
| 3492 | } |
| 3493 | |
| 3494 | /* have some sort of integer conversion. |
| 3495 | * first, skip white space in buffer. |
| 3496 | */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3497 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | |
| 3499 | digit = *str; |
| 3500 | if (is_sign && digit == '-') |
| 3501 | digit = *(str + 1); |
| 3502 | |
| 3503 | if (!digit |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3504 | || (base == 16 && !isxdigit(digit)) |
| 3505 | || (base == 10 && !isdigit(digit)) |
| 3506 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 3507 | || (base == 0 && !isdigit(digit))) |
| 3508 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3510 | if (is_sign) |
Richard Fitzgerald | 9e914f5 | 2021-05-14 17:12:04 +0100 | [diff] [blame] | 3511 | val.s = simple_strntoll(str, |
| 3512 | field_width >= 0 ? field_width : INT_MAX, |
| 3513 | &next, base); |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3514 | else |
Richard Fitzgerald | 9e914f5 | 2021-05-14 17:12:04 +0100 | [diff] [blame] | 3515 | val.u = simple_strntoull(str, |
| 3516 | field_width >= 0 ? field_width : INT_MAX, |
| 3517 | &next, base); |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3518 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3519 | switch (qualifier) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | case 'H': /* that's 'hh' in format */ |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3521 | if (is_sign) |
| 3522 | *va_arg(args, signed char *) = val.s; |
| 3523 | else |
| 3524 | *va_arg(args, unsigned char *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | break; |
| 3526 | case 'h': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3527 | if (is_sign) |
| 3528 | *va_arg(args, short *) = val.s; |
| 3529 | else |
| 3530 | *va_arg(args, unsigned short *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | break; |
| 3532 | case 'l': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3533 | if (is_sign) |
| 3534 | *va_arg(args, long *) = val.s; |
| 3535 | else |
| 3536 | *va_arg(args, unsigned long *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | break; |
| 3538 | case 'L': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3539 | if (is_sign) |
| 3540 | *va_arg(args, long long *) = val.s; |
| 3541 | else |
| 3542 | *va_arg(args, unsigned long long *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | case 'z': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3545 | *va_arg(args, size_t *) = val.u; |
| 3546 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | default: |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 3548 | if (is_sign) |
| 3549 | *va_arg(args, int *) = val.s; |
| 3550 | else |
| 3551 | *va_arg(args, unsigned int *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | break; |
| 3553 | } |
| 3554 | num++; |
| 3555 | |
| 3556 | if (!next) |
| 3557 | break; |
| 3558 | str = next; |
| 3559 | } |
Johannes Berg | c6b40d1 | 2007-05-08 00:27:20 -0700 | [diff] [blame] | 3560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | return num; |
| 3562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | EXPORT_SYMBOL(vsscanf); |
| 3564 | |
| 3565 | /** |
| 3566 | * sscanf - Unformat a buffer into a list of arguments |
| 3567 | * @buf: input buffer |
| 3568 | * @fmt: formatting of buffer |
| 3569 | * @...: resulting arguments |
| 3570 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3571 | int sscanf(const char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | { |
| 3573 | va_list args; |
| 3574 | int i; |
| 3575 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3576 | va_start(args, fmt); |
| 3577 | i = vsscanf(buf, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3580 | return i; |
| 3581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | EXPORT_SYMBOL(sscanf); |