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