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