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; 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 /* Set up default numeric format */ 2641 spec->base = 10; 2642 fmt.state = FORMAT_STATE_SIZE(int); 2643 static const struct format_state { 2644 unsigned char state; 2645 unsigned char flags_or_double_state; 2646 unsigned char modifier; 2647 unsigned char base; 2648 } lookup_state[256] = { 2649 // Qualifiers 2650 ['l'] = { FORMAT_STATE_SIZE(long), FORMAT_STATE_SIZE(long long), 1 }, 2651 ['L'] = { FORMAT_STATE_SIZE(long long), 0, 1 }, 2652 ['h'] = { FORMAT_STATE_SIZE(short), FORMAT_STATE_SIZE(char), 1 }, 2653 ['H'] = { FORMAT_STATE_SIZE(char), 0, 1 }, // Questionable, historic 2654 ['z'] = { FORMAT_STATE_SIZE(size_t), 0, 1 }, 2655 ['t'] = { FORMAT_STATE_SIZE(ptrdiff_t), 0, 1 }, 2656 2657 // Non-numeric formats 2658 ['c'] = { FORMAT_STATE_CHAR }, 2659 ['s'] = { FORMAT_STATE_STR }, 2660 ['p'] = { FORMAT_STATE_PTR }, 2661 ['%'] = { FORMAT_STATE_PERCENT_CHAR }, 2662 2663 // Numerics 2664 ['o'] = { 0, 0, 0, 8 }, 2665 ['x'] = { 0, SMALL, 0, 16 }, 2666 ['X'] = { 0, 0, 0, 16 }, 2667 ['d'] = { 0, SIGN, 0, 10 }, 2668 ['i'] = { 0, SIGN, 0, 10 }, 2669 ['u'] = { 0, 0, 0, 10, }, 2670 2671 /* 2672 * Since %n poses a greater security risk than 2673 * utility, treat it as any other invalid or 2674 * unsupported format specifier. 2675 */ 2676 }; 2677 2678 const struct format_state *p = lookup_state + (u8)*fmt.str; 2679 if (p->modifier) { 2680 fmt.state = p->state; 2681 if (p->flags_or_double_state && fmt.str[0] == fmt.str[1]) { 2682 fmt.state = p->flags_or_double_state; 2683 fmt.str++; 2684 } 2685 fmt.str++; 2686 p = lookup_state + *fmt.str; 2687 if (unlikely(p->modifier)) 2688 goto invalid; 2689 } 2690 if (p->base) { 2691 spec->base = p->base; 2692 spec->flags |= p->flags_or_double_state; 2693 fmt.str++; 2694 return fmt; 2695 } 2696 if (p->state) { 2697 fmt.state = p->state; 2698 fmt.str++; 2699 return fmt; 2700 } 2701 2702 invalid: 2703 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt.str); 2704 fmt.state = FORMAT_STATE_INVALID; 2705 return fmt; 2706 } 2707 2708 static void 2709 set_field_width(struct printf_spec *spec, int width) 2710 { 2711 spec->field_width = width; 2712 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 2713 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 2714 } 2715 } 2716 2717 static void 2718 set_precision(struct printf_spec *spec, int prec) 2719 { 2720 spec->precision = prec; 2721 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 2722 spec->precision = clamp(prec, 0, PRECISION_MAX); 2723 } 2724 } 2725 2726 /* 2727 * Turn a 1/2/4-byte value into a 64-bit one for printing: truncate 2728 * as necessary and deal with signedness. 2729 * 2730 * 'size' is the size of the value in bytes. 2731 */ 2732 static unsigned long long convert_num_spec(unsigned int val, int size, struct printf_spec spec) 2733 { 2734 unsigned int shift = 32 - size*8; 2735 2736 val <<= shift; 2737 if (!(spec.flags & SIGN)) 2738 return val >> shift; 2739 return (int)val >> shift; 2740 } 2741 2742 /** 2743 * vsnprintf - Format a string and place it in a buffer 2744 * @buf: The buffer to place the result into 2745 * @size: The size of the buffer, including the trailing null space 2746 * @fmt: The format string to use 2747 * @args: Arguments for the format string 2748 * 2749 * This function generally follows C99 vsnprintf, but has some 2750 * extensions and a few limitations: 2751 * 2752 * - ``%n`` is unsupported 2753 * - ``%p*`` is handled by pointer() 2754 * 2755 * See pointer() or Documentation/core-api/printk-formats.rst for more 2756 * extensive description. 2757 * 2758 * **Please update the documentation in both places when making changes** 2759 * 2760 * The return value is the number of characters which would 2761 * be generated for the given input, excluding the trailing 2762 * '\0', as per ISO C99. If you want to have the exact 2763 * number of characters written into @buf as return value 2764 * (not including the trailing '\0'), use vscnprintf(). If the 2765 * return is greater than or equal to @size, the resulting 2766 * string is truncated. 2767 * 2768 * If you're not already dealing with a va_list consider using snprintf(). 2769 */ 2770 int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) 2771 { 2772 unsigned long long num; 2773 char *str, *end; 2774 struct printf_spec spec = {0}; 2775 struct fmt fmt = { 2776 .str = fmt_str, 2777 .state = FORMAT_STATE_NONE, 2778 }; 2779 2780 /* Reject out-of-range values early. Large positive sizes are 2781 used for unknown buffer sizes. */ 2782 if (WARN_ON_ONCE(size > INT_MAX)) 2783 return 0; 2784 2785 str = buf; 2786 end = buf + size; 2787 2788 /* Make sure end is always >= buf */ 2789 if (end < buf) { 2790 end = ((void *)-1); 2791 size = end - buf; 2792 } 2793 2794 while (*fmt.str) { 2795 const char *old_fmt = fmt.str; 2796 2797 fmt = format_decode(fmt, &spec); 2798 2799 switch (fmt.state) { 2800 case FORMAT_STATE_NONE: { 2801 int read = fmt.str - old_fmt; 2802 if (str < end) { 2803 int copy = read; 2804 if (copy > end - str) 2805 copy = end - str; 2806 memcpy(str, old_fmt, copy); 2807 } 2808 str += read; 2809 continue; 2810 } 2811 2812 case FORMAT_STATE_WIDTH: 2813 set_field_width(&spec, va_arg(args, int)); 2814 continue; 2815 2816 case FORMAT_STATE_PRECISION: 2817 set_precision(&spec, va_arg(args, int)); 2818 continue; 2819 2820 case FORMAT_STATE_CHAR: { 2821 char c; 2822 2823 if (!(spec.flags & LEFT)) { 2824 while (--spec.field_width > 0) { 2825 if (str < end) 2826 *str = ' '; 2827 ++str; 2828 2829 } 2830 } 2831 c = (unsigned char) va_arg(args, int); 2832 if (str < end) 2833 *str = c; 2834 ++str; 2835 while (--spec.field_width > 0) { 2836 if (str < end) 2837 *str = ' '; 2838 ++str; 2839 } 2840 continue; 2841 } 2842 2843 case FORMAT_STATE_STR: 2844 str = string(str, end, va_arg(args, char *), spec); 2845 continue; 2846 2847 case FORMAT_STATE_PTR: 2848 str = pointer(fmt.str, str, end, va_arg(args, void *), 2849 spec); 2850 while (isalnum(*fmt.str)) 2851 fmt.str++; 2852 continue; 2853 2854 case FORMAT_STATE_PERCENT_CHAR: 2855 if (str < end) 2856 *str = '%'; 2857 ++str; 2858 continue; 2859 2860 case FORMAT_STATE_INVALID: 2861 /* 2862 * Presumably the arguments passed gcc's type 2863 * checking, but there is no safe or sane way 2864 * for us to continue parsing the format and 2865 * fetching from the va_list; the remaining 2866 * specifiers and arguments would be out of 2867 * sync. 2868 */ 2869 goto out; 2870 2871 case FORMAT_STATE_8BYTE: 2872 num = va_arg(args, long long); 2873 break; 2874 2875 default: 2876 num = convert_num_spec(va_arg(args, int), fmt.state, spec); 2877 break; 2878 } 2879 2880 str = number(str, end, num, spec); 2881 } 2882 2883 out: 2884 if (size > 0) { 2885 if (str < end) 2886 *str = '\0'; 2887 else 2888 end[-1] = '\0'; 2889 } 2890 2891 /* the trailing null byte doesn't count towards the total */ 2892 return str-buf; 2893 2894 } 2895 EXPORT_SYMBOL(vsnprintf); 2896 2897 /** 2898 * vscnprintf - Format a string and place it in a buffer 2899 * @buf: The buffer to place the result into 2900 * @size: The size of the buffer, including the trailing null space 2901 * @fmt: The format string to use 2902 * @args: Arguments for the format string 2903 * 2904 * The return value is the number of characters which have been written into 2905 * the @buf not including the trailing '\0'. If @size is == 0 the function 2906 * returns 0. 2907 * 2908 * If you're not already dealing with a va_list consider using scnprintf(). 2909 * 2910 * See the vsnprintf() documentation for format string extensions over C99. 2911 */ 2912 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 2913 { 2914 int i; 2915 2916 if (unlikely(!size)) 2917 return 0; 2918 2919 i = vsnprintf(buf, size, fmt, args); 2920 2921 if (likely(i < size)) 2922 return i; 2923 2924 return size - 1; 2925 } 2926 EXPORT_SYMBOL(vscnprintf); 2927 2928 /** 2929 * snprintf - Format a string and place it in a buffer 2930 * @buf: The buffer to place the result into 2931 * @size: The size of the buffer, including the trailing null space 2932 * @fmt: The format string to use 2933 * @...: Arguments for the format string 2934 * 2935 * The return value is the number of characters which would be 2936 * generated for the given input, excluding the trailing null, 2937 * as per ISO C99. If the return is greater than or equal to 2938 * @size, the resulting string is truncated. 2939 * 2940 * See the vsnprintf() documentation for format string extensions over C99. 2941 */ 2942 int snprintf(char *buf, size_t size, const char *fmt, ...) 2943 { 2944 va_list args; 2945 int i; 2946 2947 va_start(args, fmt); 2948 i = vsnprintf(buf, size, fmt, args); 2949 va_end(args); 2950 2951 return i; 2952 } 2953 EXPORT_SYMBOL(snprintf); 2954 2955 /** 2956 * scnprintf - Format a string and place it in a buffer 2957 * @buf: The buffer to place the result into 2958 * @size: The size of the buffer, including the trailing null space 2959 * @fmt: The format string to use 2960 * @...: Arguments for the format string 2961 * 2962 * The return value is the number of characters written into @buf not including 2963 * the trailing '\0'. If @size is == 0 the function returns 0. 2964 */ 2965 2966 int scnprintf(char *buf, size_t size, const char *fmt, ...) 2967 { 2968 va_list args; 2969 int i; 2970 2971 va_start(args, fmt); 2972 i = vscnprintf(buf, size, fmt, args); 2973 va_end(args); 2974 2975 return i; 2976 } 2977 EXPORT_SYMBOL(scnprintf); 2978 2979 /** 2980 * vsprintf - Format a string and place it in a buffer 2981 * @buf: The buffer to place the result into 2982 * @fmt: The format string to use 2983 * @args: Arguments for the format string 2984 * 2985 * The function returns the number of characters written 2986 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 2987 * buffer overflows. 2988 * 2989 * If you're not already dealing with a va_list consider using sprintf(). 2990 * 2991 * See the vsnprintf() documentation for format string extensions over C99. 2992 */ 2993 int vsprintf(char *buf, const char *fmt, va_list args) 2994 { 2995 return vsnprintf(buf, INT_MAX, fmt, args); 2996 } 2997 EXPORT_SYMBOL(vsprintf); 2998 2999 /** 3000 * sprintf - Format a string and place it in a buffer 3001 * @buf: The buffer to place the result into 3002 * @fmt: The format string to use 3003 * @...: Arguments for the format string 3004 * 3005 * The function returns the number of characters written 3006 * into @buf. Use snprintf() or scnprintf() in order to avoid 3007 * buffer overflows. 3008 * 3009 * See the vsnprintf() documentation for format string extensions over C99. 3010 */ 3011 int sprintf(char *buf, const char *fmt, ...) 3012 { 3013 va_list args; 3014 int i; 3015 3016 va_start(args, fmt); 3017 i = vsnprintf(buf, INT_MAX, fmt, args); 3018 va_end(args); 3019 3020 return i; 3021 } 3022 EXPORT_SYMBOL(sprintf); 3023 3024 #ifdef CONFIG_BINARY_PRINTF 3025 /* 3026 * bprintf service: 3027 * vbin_printf() - VA arguments to binary data 3028 * bstr_printf() - Binary data to text string 3029 */ 3030 3031 /** 3032 * vbin_printf - Parse a format string and place args' binary value in a buffer 3033 * @bin_buf: The buffer to place args' binary value 3034 * @size: The size of the buffer(by words(32bits), not characters) 3035 * @fmt: The format string to use 3036 * @args: Arguments for the format string 3037 * 3038 * The format follows C99 vsnprintf, except %n is ignored, and its argument 3039 * is skipped. 3040 * 3041 * The return value is the number of words(32bits) which would be generated for 3042 * the given input. 3043 * 3044 * NOTE: 3045 * If the return value is greater than @size, the resulting bin_buf is NOT 3046 * valid for bstr_printf(). 3047 */ 3048 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args) 3049 { 3050 struct fmt fmt = { 3051 .str = fmt_str, 3052 .state = FORMAT_STATE_NONE, 3053 }; 3054 struct printf_spec spec = {0}; 3055 char *str, *end; 3056 int width; 3057 3058 str = (char *)bin_buf; 3059 end = (char *)(bin_buf + size); 3060 3061 #define save_arg(type) \ 3062 ({ \ 3063 unsigned long long value; \ 3064 if (sizeof(type) == 8) { \ 3065 unsigned long long val8; \ 3066 str = PTR_ALIGN(str, sizeof(u32)); \ 3067 val8 = va_arg(args, unsigned long long); \ 3068 if (str + sizeof(type) <= end) { \ 3069 *(u32 *)str = *(u32 *)&val8; \ 3070 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 3071 } \ 3072 value = val8; \ 3073 } else { \ 3074 unsigned int val4; \ 3075 str = PTR_ALIGN(str, sizeof(type)); \ 3076 val4 = va_arg(args, int); \ 3077 if (str + sizeof(type) <= end) \ 3078 *(typeof(type) *)str = (type)(long)val4; \ 3079 value = (unsigned long long)val4; \ 3080 } \ 3081 str += sizeof(type); \ 3082 value; \ 3083 }) 3084 3085 while (*fmt.str) { 3086 fmt = format_decode(fmt, &spec); 3087 3088 switch (fmt.state) { 3089 case FORMAT_STATE_NONE: 3090 case FORMAT_STATE_PERCENT_CHAR: 3091 break; 3092 case FORMAT_STATE_INVALID: 3093 goto out; 3094 3095 case FORMAT_STATE_WIDTH: 3096 case FORMAT_STATE_PRECISION: 3097 width = (int)save_arg(int); 3098 /* Pointers may require the width */ 3099 if (*fmt.str == 'p') 3100 set_field_width(&spec, width); 3101 break; 3102 3103 case FORMAT_STATE_CHAR: 3104 save_arg(char); 3105 break; 3106 3107 case FORMAT_STATE_STR: { 3108 const char *save_str = va_arg(args, char *); 3109 const char *err_msg; 3110 size_t len; 3111 3112 err_msg = check_pointer_msg(save_str); 3113 if (err_msg) 3114 save_str = err_msg; 3115 3116 len = strlen(save_str) + 1; 3117 if (str + len < end) 3118 memcpy(str, save_str, len); 3119 str += len; 3120 break; 3121 } 3122 3123 case FORMAT_STATE_PTR: 3124 /* Dereferenced pointers must be done now */ 3125 switch (*fmt.str) { 3126 /* Dereference of functions is still OK */ 3127 case 'S': 3128 case 's': 3129 case 'x': 3130 case 'K': 3131 case 'e': 3132 save_arg(void *); 3133 break; 3134 default: 3135 if (!isalnum(*fmt.str)) { 3136 save_arg(void *); 3137 break; 3138 } 3139 str = pointer(fmt.str, str, end, va_arg(args, void *), 3140 spec); 3141 if (str + 1 < end) 3142 *str++ = '\0'; 3143 else 3144 end[-1] = '\0'; /* Must be nul terminated */ 3145 } 3146 /* skip all alphanumeric pointer suffixes */ 3147 while (isalnum(*fmt.str)) 3148 fmt.str++; 3149 break; 3150 3151 case FORMAT_STATE_8BYTE: 3152 save_arg(long long); 3153 break; 3154 case FORMAT_STATE_1BYTE: 3155 save_arg(char); 3156 break; 3157 case FORMAT_STATE_2BYTE: 3158 save_arg(short); 3159 break; 3160 default: 3161 save_arg(int); 3162 } 3163 } 3164 3165 out: 3166 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 3167 #undef save_arg 3168 } 3169 EXPORT_SYMBOL_GPL(vbin_printf); 3170 3171 /** 3172 * bstr_printf - Format a string from binary arguments and place it in a buffer 3173 * @buf: The buffer to place the result into 3174 * @size: The size of the buffer, including the trailing null space 3175 * @fmt: The format string to use 3176 * @bin_buf: Binary arguments for the format string 3177 * 3178 * This function like C99 vsnprintf, but the difference is that vsnprintf gets 3179 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 3180 * a binary buffer that generated by vbin_printf. 3181 * 3182 * The format follows C99 vsnprintf, but has some extensions: 3183 * see vsnprintf comment for details. 3184 * 3185 * The return value is the number of characters which would 3186 * be generated for the given input, excluding the trailing 3187 * '\0', as per ISO C99. If you want to have the exact 3188 * number of characters written into @buf as return value 3189 * (not including the trailing '\0'), use vscnprintf(). If the 3190 * return is greater than or equal to @size, the resulting 3191 * string is truncated. 3192 */ 3193 int bstr_printf(char *buf, size_t size, const char *fmt_str, const u32 *bin_buf) 3194 { 3195 struct fmt fmt = { 3196 .str = fmt_str, 3197 .state = FORMAT_STATE_NONE, 3198 }; 3199 struct printf_spec spec = {0}; 3200 char *str, *end; 3201 const char *args = (const char *)bin_buf; 3202 3203 if (WARN_ON_ONCE(size > INT_MAX)) 3204 return 0; 3205 3206 str = buf; 3207 end = buf + size; 3208 3209 #define get_arg(type) \ 3210 ({ \ 3211 typeof(type) value; \ 3212 if (sizeof(type) == 8) { \ 3213 args = PTR_ALIGN(args, sizeof(u32)); \ 3214 *(u32 *)&value = *(u32 *)args; \ 3215 *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 3216 } else { \ 3217 args = PTR_ALIGN(args, sizeof(type)); \ 3218 value = *(typeof(type) *)args; \ 3219 } \ 3220 args += sizeof(type); \ 3221 value; \ 3222 }) 3223 3224 /* Make sure end is always >= buf */ 3225 if (end < buf) { 3226 end = ((void *)-1); 3227 size = end - buf; 3228 } 3229 3230 while (*fmt.str) { 3231 const char *old_fmt = fmt.str; 3232 unsigned long long num; 3233 3234 fmt = format_decode(fmt, &spec); 3235 switch (fmt.state) { 3236 case FORMAT_STATE_NONE: { 3237 int read = fmt.str - old_fmt; 3238 if (str < end) { 3239 int copy = read; 3240 if (copy > end - str) 3241 copy = end - str; 3242 memcpy(str, old_fmt, copy); 3243 } 3244 str += read; 3245 continue; 3246 } 3247 3248 case FORMAT_STATE_WIDTH: 3249 set_field_width(&spec, get_arg(int)); 3250 continue; 3251 3252 case FORMAT_STATE_PRECISION: 3253 set_precision(&spec, get_arg(int)); 3254 continue; 3255 3256 case FORMAT_STATE_CHAR: { 3257 char c; 3258 3259 if (!(spec.flags & LEFT)) { 3260 while (--spec.field_width > 0) { 3261 if (str < end) 3262 *str = ' '; 3263 ++str; 3264 } 3265 } 3266 c = (unsigned char) get_arg(char); 3267 if (str < end) 3268 *str = c; 3269 ++str; 3270 while (--spec.field_width > 0) { 3271 if (str < end) 3272 *str = ' '; 3273 ++str; 3274 } 3275 continue; 3276 } 3277 3278 case FORMAT_STATE_STR: { 3279 const char *str_arg = args; 3280 args += strlen(str_arg) + 1; 3281 str = string(str, end, (char *)str_arg, spec); 3282 continue; 3283 } 3284 3285 case FORMAT_STATE_PTR: { 3286 bool process = false; 3287 int copy, len; 3288 /* Non function dereferences were already done */ 3289 switch (*fmt.str) { 3290 case 'S': 3291 case 's': 3292 case 'x': 3293 case 'K': 3294 case 'e': 3295 process = true; 3296 break; 3297 default: 3298 if (!isalnum(*fmt.str)) { 3299 process = true; 3300 break; 3301 } 3302 /* Pointer dereference was already processed */ 3303 if (str < end) { 3304 len = copy = strlen(args); 3305 if (copy > end - str) 3306 copy = end - str; 3307 memcpy(str, args, copy); 3308 str += len; 3309 args += len + 1; 3310 } 3311 } 3312 if (process) 3313 str = pointer(fmt.str, str, end, get_arg(void *), spec); 3314 3315 while (isalnum(*fmt.str)) 3316 fmt.str++; 3317 continue; 3318 } 3319 3320 case FORMAT_STATE_PERCENT_CHAR: 3321 if (str < end) 3322 *str = '%'; 3323 ++str; 3324 continue; 3325 3326 case FORMAT_STATE_INVALID: 3327 goto out; 3328 3329 case FORMAT_STATE_8BYTE: 3330 num = get_arg(long long); 3331 break; 3332 case FORMAT_STATE_2BYTE: 3333 num = convert_num_spec(get_arg(short), fmt.state, spec); 3334 break; 3335 case FORMAT_STATE_1BYTE: 3336 num = convert_num_spec(get_arg(char), fmt.state, spec); 3337 break; 3338 default: 3339 num = convert_num_spec(get_arg(int), fmt.state, spec); 3340 break; 3341 } 3342 3343 str = number(str, end, num, spec); 3344 } /* while(*fmt.str) */ 3345 3346 out: 3347 if (size > 0) { 3348 if (str < end) 3349 *str = '\0'; 3350 else 3351 end[-1] = '\0'; 3352 } 3353 3354 #undef get_arg 3355 3356 /* the trailing null byte doesn't count towards the total */ 3357 return str - buf; 3358 } 3359 EXPORT_SYMBOL_GPL(bstr_printf); 3360 3361 #endif /* CONFIG_BINARY_PRINTF */ 3362 3363 /** 3364 * vsscanf - Unformat a buffer into a list of arguments 3365 * @buf: input buffer 3366 * @fmt: format of buffer 3367 * @args: arguments 3368 */ 3369 int vsscanf(const char *buf, const char *fmt, va_list args) 3370 { 3371 const char *str = buf; 3372 char *next; 3373 char digit; 3374 int num = 0; 3375 u8 qualifier; 3376 unsigned int base; 3377 union { 3378 long long s; 3379 unsigned long long u; 3380 } val; 3381 s16 field_width; 3382 bool is_sign; 3383 3384 while (*fmt) { 3385 /* skip any white space in format */ 3386 /* white space in format matches any amount of 3387 * white space, including none, in the input. 3388 */ 3389 if (isspace(*fmt)) { 3390 fmt = skip_spaces(++fmt); 3391 str = skip_spaces(str); 3392 } 3393 3394 /* anything that is not a conversion must match exactly */ 3395 if (*fmt != '%' && *fmt) { 3396 if (*fmt++ != *str++) 3397 break; 3398 continue; 3399 } 3400 3401 if (!*fmt) 3402 break; 3403 ++fmt; 3404 3405 /* skip this conversion. 3406 * advance both strings to next white space 3407 */ 3408 if (*fmt == '*') { 3409 if (!*str) 3410 break; 3411 while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3412 /* '%*[' not yet supported, invalid format */ 3413 if (*fmt == '[') 3414 return num; 3415 fmt++; 3416 } 3417 while (!isspace(*str) && *str) 3418 str++; 3419 continue; 3420 } 3421 3422 /* get field width */ 3423 field_width = -1; 3424 if (isdigit(*fmt)) { 3425 field_width = skip_atoi(&fmt); 3426 if (field_width <= 0) 3427 break; 3428 } 3429 3430 /* get conversion qualifier */ 3431 qualifier = -1; 3432 if (*fmt == 'h' || _tolower(*fmt) == 'l' || 3433 *fmt == 'z') { 3434 qualifier = *fmt++; 3435 if (unlikely(qualifier == *fmt)) { 3436 if (qualifier == 'h') { 3437 qualifier = 'H'; 3438 fmt++; 3439 } else if (qualifier == 'l') { 3440 qualifier = 'L'; 3441 fmt++; 3442 } 3443 } 3444 } 3445 3446 if (!*fmt) 3447 break; 3448 3449 if (*fmt == 'n') { 3450 /* return number of characters read so far */ 3451 *va_arg(args, int *) = str - buf; 3452 ++fmt; 3453 continue; 3454 } 3455 3456 if (!*str) 3457 break; 3458 3459 base = 10; 3460 is_sign = false; 3461 3462 switch (*fmt++) { 3463 case 'c': 3464 { 3465 char *s = (char *)va_arg(args, char*); 3466 if (field_width == -1) 3467 field_width = 1; 3468 do { 3469 *s++ = *str++; 3470 } while (--field_width > 0 && *str); 3471 num++; 3472 } 3473 continue; 3474 case 's': 3475 { 3476 char *s = (char *)va_arg(args, char *); 3477 if (field_width == -1) 3478 field_width = SHRT_MAX; 3479 /* first, skip leading white space in buffer */ 3480 str = skip_spaces(str); 3481 3482 /* now copy until next white space */ 3483 while (*str && !isspace(*str) && field_width--) 3484 *s++ = *str++; 3485 *s = '\0'; 3486 num++; 3487 } 3488 continue; 3489 /* 3490 * Warning: This implementation of the '[' conversion specifier 3491 * deviates from its glibc counterpart in the following ways: 3492 * (1) It does NOT support ranges i.e. '-' is NOT a special 3493 * character 3494 * (2) It cannot match the closing bracket ']' itself 3495 * (3) A field width is required 3496 * (4) '%*[' (discard matching input) is currently not supported 3497 * 3498 * Example usage: 3499 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3500 * buf1, buf2, buf3); 3501 * if (ret < 3) 3502 * // etc.. 3503 */ 3504 case '[': 3505 { 3506 char *s = (char *)va_arg(args, char *); 3507 DECLARE_BITMAP(set, 256) = {0}; 3508 unsigned int len = 0; 3509 bool negate = (*fmt == '^'); 3510 3511 /* field width is required */ 3512 if (field_width == -1) 3513 return num; 3514 3515 if (negate) 3516 ++fmt; 3517 3518 for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3519 __set_bit((u8)*fmt, set); 3520 3521 /* no ']' or no character set found */ 3522 if (!*fmt || !len) 3523 return num; 3524 ++fmt; 3525 3526 if (negate) { 3527 bitmap_complement(set, set, 256); 3528 /* exclude null '\0' byte */ 3529 __clear_bit(0, set); 3530 } 3531 3532 /* match must be non-empty */ 3533 if (!test_bit((u8)*str, set)) 3534 return num; 3535 3536 while (test_bit((u8)*str, set) && field_width--) 3537 *s++ = *str++; 3538 *s = '\0'; 3539 ++num; 3540 } 3541 continue; 3542 case 'o': 3543 base = 8; 3544 break; 3545 case 'x': 3546 case 'X': 3547 base = 16; 3548 break; 3549 case 'i': 3550 base = 0; 3551 fallthrough; 3552 case 'd': 3553 is_sign = true; 3554 fallthrough; 3555 case 'u': 3556 break; 3557 case '%': 3558 /* looking for '%' in str */ 3559 if (*str++ != '%') 3560 return num; 3561 continue; 3562 default: 3563 /* invalid format; stop here */ 3564 return num; 3565 } 3566 3567 /* have some sort of integer conversion. 3568 * first, skip white space in buffer. 3569 */ 3570 str = skip_spaces(str); 3571 3572 digit = *str; 3573 if (is_sign && digit == '-') { 3574 if (field_width == 1) 3575 break; 3576 3577 digit = *(str + 1); 3578 } 3579 3580 if (!digit 3581 || (base == 16 && !isxdigit(digit)) 3582 || (base == 10 && !isdigit(digit)) 3583 || (base == 8 && !isodigit(digit)) 3584 || (base == 0 && !isdigit(digit))) 3585 break; 3586 3587 if (is_sign) 3588 val.s = simple_strntoll(str, &next, base, 3589 field_width >= 0 ? field_width : INT_MAX); 3590 else 3591 val.u = simple_strntoull(str, &next, base, 3592 field_width >= 0 ? field_width : INT_MAX); 3593 3594 switch (qualifier) { 3595 case 'H': /* that's 'hh' in format */ 3596 if (is_sign) 3597 *va_arg(args, signed char *) = val.s; 3598 else 3599 *va_arg(args, unsigned char *) = val.u; 3600 break; 3601 case 'h': 3602 if (is_sign) 3603 *va_arg(args, short *) = val.s; 3604 else 3605 *va_arg(args, unsigned short *) = val.u; 3606 break; 3607 case 'l': 3608 if (is_sign) 3609 *va_arg(args, long *) = val.s; 3610 else 3611 *va_arg(args, unsigned long *) = val.u; 3612 break; 3613 case 'L': 3614 if (is_sign) 3615 *va_arg(args, long long *) = val.s; 3616 else 3617 *va_arg(args, unsigned long long *) = val.u; 3618 break; 3619 case 'z': 3620 *va_arg(args, size_t *) = val.u; 3621 break; 3622 default: 3623 if (is_sign) 3624 *va_arg(args, int *) = val.s; 3625 else 3626 *va_arg(args, unsigned int *) = val.u; 3627 break; 3628 } 3629 num++; 3630 3631 if (!next) 3632 break; 3633 str = next; 3634 } 3635 3636 return num; 3637 } 3638 EXPORT_SYMBOL(vsscanf); 3639 3640 /** 3641 * sscanf - Unformat a buffer into a list of arguments 3642 * @buf: input buffer 3643 * @fmt: formatting of buffer 3644 * @...: resulting arguments 3645 */ 3646 int sscanf(const char *buf, const char *fmt, ...) 3647 { 3648 va_list args; 3649 int i; 3650 3651 va_start(args, fmt); 3652 i = vsscanf(buf, fmt, args); 3653 va_end(args); 3654 3655 return i; 3656 } 3657 EXPORT_SYMBOL(sscanf); 3658