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