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