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