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