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