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 *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 1997 const char *fmt) 1998 { 1999 switch (fmt[1]) { 2000 case 'R': 2001 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 2002 case 'T': 2003 return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); 2004 default: 2005 return error_string(buf, end, "(%pt?)", spec); 2006 } 2007 } 2008 2009 static noinline_for_stack 2010 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 2011 const char *fmt) 2012 { 2013 if (!IS_ENABLED(CONFIG_HAVE_CLK)) 2014 return error_string(buf, end, "(%pC?)", spec); 2015 2016 if (check_pointer(&buf, end, clk, spec)) 2017 return buf; 2018 2019 #ifdef CONFIG_COMMON_CLK 2020 return string(buf, end, __clk_get_name(clk), spec); 2021 #else 2022 return ptr_to_id(buf, end, clk, spec); 2023 #endif 2024 } 2025 2026 static 2027 char *format_flags(char *buf, char *end, unsigned long flags, 2028 const struct trace_print_flags *names) 2029 { 2030 unsigned long mask; 2031 2032 for ( ; flags && names->name; names++) { 2033 mask = names->mask; 2034 if ((flags & mask) != mask) 2035 continue; 2036 2037 buf = string(buf, end, names->name, default_str_spec); 2038 2039 flags &= ~mask; 2040 if (flags) { 2041 if (buf < end) 2042 *buf = '|'; 2043 buf++; 2044 } 2045 } 2046 2047 if (flags) 2048 buf = number(buf, end, flags, default_flag_spec); 2049 2050 return buf; 2051 } 2052 2053 struct page_flags_fields { 2054 int width; 2055 int shift; 2056 int mask; 2057 const struct printf_spec *spec; 2058 const char *name; 2059 }; 2060 2061 static const struct page_flags_fields pff[] = { 2062 {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, 2063 &default_dec_spec, "section"}, 2064 {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, 2065 &default_dec_spec, "node"}, 2066 {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, 2067 &default_dec_spec, "zone"}, 2068 {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, 2069 &default_flag_spec, "lastcpupid"}, 2070 {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, 2071 &default_flag_spec, "kasantag"}, 2072 }; 2073 2074 static 2075 char *format_page_flags(char *buf, char *end, unsigned long flags) 2076 { 2077 unsigned long main_flags = flags & PAGEFLAGS_MASK; 2078 bool append = false; 2079 int i; 2080 2081 buf = number(buf, end, flags, default_flag_spec); 2082 if (buf < end) 2083 *buf = '('; 2084 buf++; 2085 2086 /* Page flags from the main area. */ 2087 if (main_flags) { 2088 buf = format_flags(buf, end, main_flags, pageflag_names); 2089 append = true; 2090 } 2091 2092 /* Page flags from the fields area */ 2093 for (i = 0; i < ARRAY_SIZE(pff); i++) { 2094 /* Skip undefined fields. */ 2095 if (!pff[i].width) 2096 continue; 2097 2098 /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */ 2099 if (append) { 2100 if (buf < end) 2101 *buf = '|'; 2102 buf++; 2103 } 2104 2105 buf = string(buf, end, pff[i].name, default_str_spec); 2106 if (buf < end) 2107 *buf = '='; 2108 buf++; 2109 buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask, 2110 *pff[i].spec); 2111 2112 append = true; 2113 } 2114 if (buf < end) 2115 *buf = ')'; 2116 buf++; 2117 2118 return buf; 2119 } 2120 2121 static noinline_for_stack 2122 char *flags_string(char *buf, char *end, void *flags_ptr, 2123 struct printf_spec spec, const char *fmt) 2124 { 2125 unsigned long flags; 2126 const struct trace_print_flags *names; 2127 2128 if (check_pointer(&buf, end, flags_ptr, spec)) 2129 return buf; 2130 2131 switch (fmt[1]) { 2132 case 'p': 2133 return format_page_flags(buf, end, *(unsigned long *)flags_ptr); 2134 case 'v': 2135 flags = *(unsigned long *)flags_ptr; 2136 names = vmaflag_names; 2137 break; 2138 case 'g': 2139 flags = (__force unsigned long)(*(gfp_t *)flags_ptr); 2140 names = gfpflag_names; 2141 break; 2142 default: 2143 return error_string(buf, end, "(%pG?)", spec); 2144 } 2145 2146 return format_flags(buf, end, flags, names); 2147 } 2148 2149 static noinline_for_stack 2150 char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 2151 char *end) 2152 { 2153 int depth; 2154 2155 /* Loop starting from the root node to the current node. */ 2156 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 2157 /* 2158 * Only get a reference for other nodes (i.e. parent nodes). 2159 * fwnode refcount may be 0 here. 2160 */ 2161 struct fwnode_handle *__fwnode = depth ? 2162 fwnode_get_nth_parent(fwnode, depth) : fwnode; 2163 2164 buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 2165 default_str_spec); 2166 buf = string(buf, end, fwnode_get_name(__fwnode), 2167 default_str_spec); 2168 2169 if (depth) 2170 fwnode_handle_put(__fwnode); 2171 } 2172 2173 return buf; 2174 } 2175 2176 static noinline_for_stack 2177 char *device_node_string(char *buf, char *end, struct device_node *dn, 2178 struct printf_spec spec, const char *fmt) 2179 { 2180 char tbuf[sizeof("xxxx") + 1]; 2181 const char *p; 2182 int ret; 2183 char *buf_start = buf; 2184 struct property *prop; 2185 bool has_mult, pass; 2186 2187 struct printf_spec str_spec = spec; 2188 str_spec.field_width = -1; 2189 2190 if (fmt[0] != 'F') 2191 return error_string(buf, end, "(%pO?)", spec); 2192 2193 if (!IS_ENABLED(CONFIG_OF)) 2194 return error_string(buf, end, "(%pOF?)", spec); 2195 2196 if (check_pointer(&buf, end, dn, spec)) 2197 return buf; 2198 2199 /* simple case without anything any more format specifiers */ 2200 fmt++; 2201 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 2202 fmt = "f"; 2203 2204 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 2205 int precision; 2206 if (pass) { 2207 if (buf < end) 2208 *buf = ':'; 2209 buf++; 2210 } 2211 2212 switch (*fmt) { 2213 case 'f': /* full_name */ 2214 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 2215 end); 2216 break; 2217 case 'n': /* name */ 2218 p = fwnode_get_name(of_fwnode_handle(dn)); 2219 precision = str_spec.precision; 2220 str_spec.precision = strchrnul(p, '@') - p; 2221 buf = string(buf, end, p, str_spec); 2222 str_spec.precision = precision; 2223 break; 2224 case 'p': /* phandle */ 2225 buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); 2226 break; 2227 case 'P': /* path-spec */ 2228 p = fwnode_get_name(of_fwnode_handle(dn)); 2229 if (!p[1]) 2230 p = "/"; 2231 buf = string(buf, end, p, str_spec); 2232 break; 2233 case 'F': /* flags */ 2234 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 2235 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 2236 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 2237 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 2238 tbuf[4] = 0; 2239 buf = string_nocheck(buf, end, tbuf, str_spec); 2240 break; 2241 case 'c': /* major compatible string */ 2242 ret = of_property_read_string(dn, "compatible", &p); 2243 if (!ret) 2244 buf = string(buf, end, p, str_spec); 2245 break; 2246 case 'C': /* full compatible string */ 2247 has_mult = false; 2248 of_property_for_each_string(dn, "compatible", prop, p) { 2249 if (has_mult) 2250 buf = string_nocheck(buf, end, ",", str_spec); 2251 buf = string_nocheck(buf, end, "\"", str_spec); 2252 buf = string(buf, end, p, str_spec); 2253 buf = string_nocheck(buf, end, "\"", str_spec); 2254 2255 has_mult = true; 2256 } 2257 break; 2258 default: 2259 break; 2260 } 2261 } 2262 2263 return widen_string(buf, buf - buf_start, end, spec); 2264 } 2265 2266 static noinline_for_stack 2267 char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 2268 struct printf_spec spec, const char *fmt) 2269 { 2270 struct printf_spec str_spec = spec; 2271 char *buf_start = buf; 2272 2273 str_spec.field_width = -1; 2274 2275 if (*fmt != 'w') 2276 return error_string(buf, end, "(%pf?)", spec); 2277 2278 if (check_pointer(&buf, end, fwnode, spec)) 2279 return buf; 2280 2281 fmt++; 2282 2283 switch (*fmt) { 2284 case 'P': /* name */ 2285 buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 2286 break; 2287 case 'f': /* full_name */ 2288 default: 2289 buf = fwnode_full_name_string(fwnode, buf, end); 2290 break; 2291 } 2292 2293 return widen_string(buf, buf - buf_start, end, spec); 2294 } 2295 2296 static noinline_for_stack 2297 char *resource_or_range(const char *fmt, char *buf, char *end, void *ptr, 2298 struct printf_spec spec) 2299 { 2300 if (*fmt == 'r' && fmt[1] == 'a') 2301 return range_string(buf, end, ptr, spec, fmt); 2302 return resource_string(buf, end, ptr, spec, fmt); 2303 } 2304 2305 void __init hash_pointers_finalize(bool slub_debug) 2306 { 2307 switch (hash_pointers_mode) { 2308 case HASH_PTR_ALWAYS: 2309 no_hash_pointers = false; 2310 break; 2311 case HASH_PTR_NEVER: 2312 no_hash_pointers = true; 2313 break; 2314 case HASH_PTR_AUTO: 2315 default: 2316 no_hash_pointers = slub_debug; 2317 break; 2318 } 2319 2320 if (!no_hash_pointers) 2321 return; 2322 2323 pr_warn("**********************************************************\n"); 2324 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 2325 pr_warn("** **\n"); 2326 pr_warn("** This system shows unhashed kernel memory addresses **\n"); 2327 pr_warn("** via the console, logs, and other interfaces. This **\n"); 2328 pr_warn("** might reduce the security of your system. **\n"); 2329 pr_warn("** **\n"); 2330 pr_warn("** If you see this message and you are not debugging **\n"); 2331 pr_warn("** the kernel, report this immediately to your system **\n"); 2332 pr_warn("** administrator! **\n"); 2333 pr_warn("** **\n"); 2334 pr_warn("** Use hash_pointers=always to force this mode off **\n"); 2335 pr_warn("** **\n"); 2336 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 2337 pr_warn("**********************************************************\n"); 2338 } 2339 2340 static int __init hash_pointers_mode_parse(char *str) 2341 { 2342 if (!str) { 2343 pr_warn("Hash pointers mode empty; falling back to auto.\n"); 2344 hash_pointers_mode = HASH_PTR_AUTO; 2345 } else if (strncmp(str, "auto", 4) == 0) { 2346 pr_info("Hash pointers mode set to auto.\n"); 2347 hash_pointers_mode = HASH_PTR_AUTO; 2348 } else if (strncmp(str, "never", 5) == 0) { 2349 pr_info("Hash pointers mode set to never.\n"); 2350 hash_pointers_mode = HASH_PTR_NEVER; 2351 } else if (strncmp(str, "always", 6) == 0) { 2352 pr_info("Hash pointers mode set to always.\n"); 2353 hash_pointers_mode = HASH_PTR_ALWAYS; 2354 } else { 2355 pr_warn("Unknown hash_pointers mode '%s' specified; assuming auto.\n", str); 2356 hash_pointers_mode = HASH_PTR_AUTO; 2357 } 2358 2359 return 0; 2360 } 2361 early_param("hash_pointers", hash_pointers_mode_parse); 2362 2363 static int __init no_hash_pointers_enable(char *str) 2364 { 2365 return hash_pointers_mode_parse("never"); 2366 } 2367 early_param("no_hash_pointers", no_hash_pointers_enable); 2368 2369 /* 2370 * Show a '%p' thing. A kernel extension is that the '%p' is followed 2371 * by an extra set of alphanumeric characters that are extended format 2372 * specifiers. 2373 * 2374 * Please update scripts/checkpatch.pl when adding/removing conversion 2375 * characters. (Search for "check for vsprintf extension"). 2376 * 2377 * Right now we handle: 2378 * 2379 * - 'S' For symbolic direct pointers (or function descriptors) with offset 2380 * - 's' For symbolic direct pointers (or function descriptors) without offset 2381 * - '[Ss]R' as above with __builtin_extract_return_addr() translation 2382 * - 'S[R]b' as above with module build ID (for use in backtraces) 2383 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 2384 * %ps and %pS. Be careful when re-using these specifiers. 2385 * - 'B' For backtraced symbolic direct pointers with offset 2386 * - 'Bb' as above with module build ID (for use in backtraces) 2387 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2388 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2389 * - 'ra' For struct ranges, e.g., [range 0x0000000000000000 - 0x00000000000000ff] 2390 * - 'b[l]' For a bitmap, the number of bits is determined by the field 2391 * width which must be explicitly specified either as part of the 2392 * format string '%32b[l]' or through '%*b[l]', [l] selects 2393 * range-list format instead of hex format 2394 * - 'M' For a 6-byte MAC address, it prints the address in the 2395 * usual colon-separated hex notation 2396 * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2397 * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2398 * with a dash-separated hex notation 2399 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 2400 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 2401 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 2402 * IPv6 uses colon separated network-order 16 bit hex with leading 0's 2403 * [S][pfs] 2404 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 2405 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 2406 * - 'i' [46] for 'raw' IPv4/IPv6 addresses 2407 * IPv6 omits the colons (01020304...0f) 2408 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 2409 * [S][pfs] 2410 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 2411 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 2412 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 2413 * - 'I[6S]c' for IPv6 addresses printed as specified by 2414 * https://tools.ietf.org/html/rfc5952 2415 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 2416 * of the following flags (see string_escape_mem() for the 2417 * details): 2418 * a - ESCAPE_ANY 2419 * c - ESCAPE_SPECIAL 2420 * h - ESCAPE_HEX 2421 * n - ESCAPE_NULL 2422 * o - ESCAPE_OCTAL 2423 * p - ESCAPE_NP 2424 * s - ESCAPE_SPACE 2425 * By default ESCAPE_ANY_NP is used. 2426 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 2427 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 2428 * Options for %pU are: 2429 * b big endian lower case hex (default) 2430 * B big endian UPPER case hex 2431 * l little endian lower case hex 2432 * L little endian UPPER case hex 2433 * big endian output byte order is: 2434 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 2435 * little endian output byte order is: 2436 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 2437 * - 'V' For a struct va_format which contains a format string * and va_list *, 2438 * call vsnprintf(->format, *->va_list). 2439 * Implements a "recursive vsnprintf". 2440 * Do not use this feature without some mechanism to verify the 2441 * correctness of the format string and va_list arguments. 2442 * - 'K' For a kernel pointer that should be hidden from unprivileged users. 2443 * Use only for procfs, sysfs and similar files, not printk(); please 2444 * read the documentation (path below) first. 2445 * - 'NF' For a netdev_features_t 2446 * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value. 2447 * - '4c[h[R]lb]' For generic FourCC code with raw numerical value. Both are 2448 * displayed in the big-endian format. This is the opposite of V4L2 or 2449 * DRM FourCCs. 2450 * The additional specifiers define what endianness is used to load 2451 * the stored bytes. The data might be interpreted using the host, 2452 * reversed host byte order, little-endian, or big-endian. 2453 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 2454 * a certain separator (' ' by default): 2455 * C colon 2456 * D dash 2457 * N no separator 2458 * The maximum supported length is 64 bytes of the input. Consider 2459 * to use print_hex_dump() for the larger input. 2460 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2461 * (default assumed to be phys_addr_t, passed by reference) 2462 * - 'd[234]' For a dentry name (optionally 2-4 last components) 2463 * - 'D[234]' Same as 'd' but for a struct file 2464 * - 'g' For block_device name (gendisk + partition number) 2465 * - 't[RT][dt][r][s]' For time and date as represented by: 2466 * R struct rtc_time 2467 * T time64_t 2468 * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2469 * (legacy clock framework) of the clock 2470 * - 'G' For flags to be printed as a collection of symbolic strings that would 2471 * construct the specific value. Supported flags given by option: 2472 * p page flags (see struct page) given as pointer to unsigned long 2473 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2474 * v vma flags (VM_*) given as pointer to unsigned long 2475 * - 'OF[fnpPcCF]' For a device tree object 2476 * Without any optional arguments prints the full_name 2477 * f device node full_name 2478 * n device node name 2479 * p device node phandle 2480 * P device node path spec (name + @unit) 2481 * F device node flags 2482 * c major compatible string 2483 * C full compatible string 2484 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 2485 * Without an option prints the full name of the node 2486 * f full name 2487 * P node name, including a possible unit address 2488 * - 'x' For printing the address unmodified. Equivalent to "%lx". 2489 * Please read the documentation (path below) before using! 2490 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 2491 * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 2492 * or user (u) memory to probe, and: 2493 * s a string, equivalent to "%s" on direct vsnprintf() use 2494 * 2495 * ** When making changes please also update: 2496 * Documentation/core-api/printk-formats.rst 2497 * 2498 * Note: The default behaviour (unadorned %p) is to hash the address, 2499 * rendering it useful as a unique identifier. 2500 * 2501 * There is also a '%pA' format specifier, but it is only intended to be used 2502 * from Rust code to format core::fmt::Arguments. Do *not* use it from C. 2503 * See rust/kernel/print.rs for details. 2504 */ 2505 static noinline_for_stack 2506 char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2507 struct printf_spec spec) 2508 { 2509 switch (*fmt) { 2510 case 'S': 2511 case 's': 2512 ptr = dereference_symbol_descriptor(ptr); 2513 fallthrough; 2514 case 'B': 2515 return symbol_string(buf, end, ptr, spec, fmt); 2516 case 'R': 2517 case 'r': 2518 return resource_or_range(fmt, buf, end, ptr, spec); 2519 case 'h': 2520 return hex_string(buf, end, ptr, spec, fmt); 2521 case 'b': 2522 switch (fmt[1]) { 2523 case 'l': 2524 return bitmap_list_string(buf, end, ptr, spec, fmt); 2525 default: 2526 return bitmap_string(buf, end, ptr, spec, fmt); 2527 } 2528 case 'M': /* Colon separated: 00:01:02:03:04:05 */ 2529 case 'm': /* Contiguous: 000102030405 */ 2530 /* [mM]F (FDDI) */ 2531 /* [mM]R (Reverse order; Bluetooth) */ 2532 return mac_address_string(buf, end, ptr, spec, fmt); 2533 case 'I': /* Formatted IP supported 2534 * 4: 1.2.3.4 2535 * 6: 0001:0203:...:0708 2536 * 6c: 1::708 or 1::1.2.3.4 2537 */ 2538 case 'i': /* Contiguous: 2539 * 4: 001.002.003.004 2540 * 6: 000102...0f 2541 */ 2542 return ip_addr_string(buf, end, ptr, spec, fmt); 2543 case 'E': 2544 return escaped_string(buf, end, ptr, spec, fmt); 2545 case 'U': 2546 return uuid_string(buf, end, ptr, spec, fmt); 2547 case 'V': 2548 return va_format(buf, end, ptr, spec); 2549 case 'K': 2550 return restricted_pointer(buf, end, ptr, spec); 2551 case 'N': 2552 return netdev_bits(buf, end, ptr, spec, fmt); 2553 case '4': 2554 return fourcc_string(buf, end, ptr, spec, fmt); 2555 case 'a': 2556 return address_val(buf, end, ptr, spec, fmt); 2557 case 'd': 2558 return dentry_name(buf, end, ptr, spec, fmt); 2559 case 't': 2560 return time_and_date(buf, end, ptr, spec, fmt); 2561 case 'C': 2562 return clock(buf, end, ptr, spec, fmt); 2563 case 'D': 2564 return file_dentry_name(buf, end, ptr, spec, fmt); 2565 #ifdef CONFIG_BLOCK 2566 case 'g': 2567 return bdev_name(buf, end, ptr, spec, fmt); 2568 #endif 2569 2570 case 'G': 2571 return flags_string(buf, end, ptr, spec, fmt); 2572 case 'O': 2573 return device_node_string(buf, end, ptr, spec, fmt + 1); 2574 case 'f': 2575 return fwnode_string(buf, end, ptr, spec, fmt + 1); 2576 case 'A': 2577 if (!IS_ENABLED(CONFIG_RUST)) { 2578 WARN_ONCE(1, "Please remove %%pA from non-Rust code\n"); 2579 return error_string(buf, end, "(%pA?)", spec); 2580 } 2581 return rust_fmt_argument(buf, end, ptr); 2582 case 'x': 2583 return pointer_string(buf, end, ptr, spec); 2584 case 'e': 2585 /* %pe with a non-ERR_PTR gets treated as plain %p */ 2586 if (!IS_ERR(ptr)) 2587 return default_pointer(buf, end, ptr, spec); 2588 return err_ptr(buf, end, ptr, spec); 2589 case 'u': 2590 case 'k': 2591 switch (fmt[1]) { 2592 case 's': 2593 return string(buf, end, ptr, spec); 2594 default: 2595 return error_string(buf, end, "(einval)", spec); 2596 } 2597 default: 2598 return default_pointer(buf, end, ptr, spec); 2599 } 2600 } 2601 2602 struct fmt { 2603 const char *str; 2604 unsigned char state; // enum format_state 2605 unsigned char size; // size of numbers 2606 }; 2607 2608 #define SPEC_CHAR(x, flag) [(x)-32] = flag 2609 static unsigned char spec_flag(unsigned char c) 2610 { 2611 static const unsigned char spec_flag_array[] = { 2612 SPEC_CHAR(' ', SPACE), 2613 SPEC_CHAR('#', SPECIAL), 2614 SPEC_CHAR('+', PLUS), 2615 SPEC_CHAR('-', LEFT), 2616 SPEC_CHAR('0', ZEROPAD), 2617 }; 2618 c -= 32; 2619 return (c < sizeof(spec_flag_array)) ? spec_flag_array[c] : 0; 2620 } 2621 2622 /* 2623 * Helper function to decode printf style format. 2624 * Each call decode a token from the format and return the 2625 * number of characters read (or likely the delta where it wants 2626 * to go on the next call). 2627 * The decoded token is returned through the parameters 2628 * 2629 * 'h', 'l', or 'L' for integer fields 2630 * 'z' support added 23/7/1999 S.H. 2631 * 'z' changed to 'Z' --davidm 1/25/99 2632 * 'Z' changed to 'z' --adobriyan 2017-01-25 2633 * 't' added for ptrdiff_t 2634 * 2635 * @fmt: the format string 2636 * @type of the token returned 2637 * @flags: various flags such as +, -, # tokens.. 2638 * @field_width: overwritten width 2639 * @base: base of the number (octal, hex, ...) 2640 * @precision: precision of a number 2641 * @qualifier: qualifier of a number (long, size_t, ...) 2642 */ 2643 static noinline_for_stack 2644 struct fmt format_decode(struct fmt fmt, struct printf_spec *spec) 2645 { 2646 const char *start = fmt.str; 2647 char flag; 2648 2649 /* we finished early by reading the field width */ 2650 if (unlikely(fmt.state == FORMAT_STATE_WIDTH)) { 2651 if (spec->field_width < 0) { 2652 spec->field_width = -spec->field_width; 2653 spec->flags |= LEFT; 2654 } 2655 fmt.state = FORMAT_STATE_NONE; 2656 goto precision; 2657 } 2658 2659 /* we finished early by reading the precision */ 2660 if (unlikely(fmt.state == FORMAT_STATE_PRECISION)) { 2661 if (spec->precision < 0) 2662 spec->precision = 0; 2663 2664 fmt.state = FORMAT_STATE_NONE; 2665 goto qualifier; 2666 } 2667 2668 /* By default */ 2669 fmt.state = FORMAT_STATE_NONE; 2670 2671 for (; *fmt.str ; fmt.str++) { 2672 if (*fmt.str == '%') 2673 break; 2674 } 2675 2676 /* Return the current non-format string */ 2677 if (fmt.str != start || !*fmt.str) 2678 return fmt; 2679 2680 /* Process flags. This also skips the first '%' */ 2681 spec->flags = 0; 2682 do { 2683 /* this also skips first '%' */ 2684 flag = spec_flag(*++fmt.str); 2685 spec->flags |= flag; 2686 } while (flag); 2687 2688 /* get field width */ 2689 spec->field_width = -1; 2690 2691 if (isdigit(*fmt.str)) 2692 spec->field_width = skip_atoi(&fmt.str); 2693 else if (unlikely(*fmt.str == '*')) { 2694 /* it's the next argument */ 2695 fmt.state = FORMAT_STATE_WIDTH; 2696 fmt.str++; 2697 return fmt; 2698 } 2699 2700 precision: 2701 /* get the precision */ 2702 spec->precision = -1; 2703 if (unlikely(*fmt.str == '.')) { 2704 fmt.str++; 2705 if (isdigit(*fmt.str)) { 2706 spec->precision = skip_atoi(&fmt.str); 2707 if (spec->precision < 0) 2708 spec->precision = 0; 2709 } else if (*fmt.str == '*') { 2710 /* it's the next argument */ 2711 fmt.state = FORMAT_STATE_PRECISION; 2712 fmt.str++; 2713 return fmt; 2714 } 2715 } 2716 2717 qualifier: 2718 /* Set up default numeric format */ 2719 spec->base = 10; 2720 fmt.state = FORMAT_STATE_NUM; 2721 fmt.size = sizeof(int); 2722 static const struct format_state { 2723 unsigned char state; 2724 unsigned char size; 2725 unsigned char flags_or_double_size; 2726 unsigned char base; 2727 } lookup_state[256] = { 2728 // Length 2729 ['l'] = { 0, sizeof(long), sizeof(long long) }, 2730 ['L'] = { 0, sizeof(long long) }, 2731 ['h'] = { 0, sizeof(short), sizeof(char) }, 2732 ['H'] = { 0, sizeof(char) }, // Questionable historical 2733 ['z'] = { 0, sizeof(size_t) }, 2734 ['t'] = { 0, sizeof(ptrdiff_t) }, 2735 2736 // Non-numeric formats 2737 ['c'] = { FORMAT_STATE_CHAR }, 2738 ['s'] = { FORMAT_STATE_STR }, 2739 ['p'] = { FORMAT_STATE_PTR }, 2740 ['%'] = { FORMAT_STATE_PERCENT_CHAR }, 2741 2742 // Numerics 2743 ['o'] = { FORMAT_STATE_NUM, 0, 0, 8 }, 2744 ['x'] = { FORMAT_STATE_NUM, 0, SMALL, 16 }, 2745 ['X'] = { FORMAT_STATE_NUM, 0, 0, 16 }, 2746 ['d'] = { FORMAT_STATE_NUM, 0, SIGN, 10 }, 2747 ['i'] = { FORMAT_STATE_NUM, 0, SIGN, 10 }, 2748 ['u'] = { FORMAT_STATE_NUM, 0, 0, 10, }, 2749 2750 /* 2751 * Since %n poses a greater security risk than 2752 * utility, treat it as any other invalid or 2753 * unsupported format specifier. 2754 */ 2755 }; 2756 2757 const struct format_state *p = lookup_state + (u8)*fmt.str; 2758 if (p->size) { 2759 fmt.size = p->size; 2760 if (p->flags_or_double_size && fmt.str[0] == fmt.str[1]) { 2761 fmt.size = p->flags_or_double_size; 2762 fmt.str++; 2763 } 2764 fmt.str++; 2765 p = lookup_state + *fmt.str; 2766 } 2767 if (p->state) { 2768 if (p->base) 2769 spec->base = p->base; 2770 spec->flags |= p->flags_or_double_size; 2771 fmt.state = p->state; 2772 fmt.str++; 2773 return fmt; 2774 } 2775 2776 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt.str); 2777 fmt.state = FORMAT_STATE_INVALID; 2778 return fmt; 2779 } 2780 2781 static void 2782 set_field_width(struct printf_spec *spec, int width) 2783 { 2784 spec->field_width = width; 2785 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 2786 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 2787 } 2788 } 2789 2790 static void 2791 set_precision(struct printf_spec *spec, int prec) 2792 { 2793 spec->precision = prec; 2794 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 2795 spec->precision = clamp(prec, 0, PRECISION_MAX); 2796 } 2797 } 2798 2799 /* 2800 * Turn a 1/2/4-byte value into a 64-bit one for printing: truncate 2801 * as necessary and deal with signedness. 2802 * 2803 * 'size' is the size of the value in bytes. 2804 */ 2805 static unsigned long long convert_num_spec(unsigned int val, int size, struct printf_spec spec) 2806 { 2807 unsigned int shift = 32 - size*8; 2808 2809 val <<= shift; 2810 if (!(spec.flags & SIGN)) 2811 return val >> shift; 2812 return (int)val >> shift; 2813 } 2814 2815 /** 2816 * vsnprintf - Format a string and place it in a buffer 2817 * @buf: The buffer to place the result into 2818 * @size: The size of the buffer, including the trailing null space 2819 * @fmt_str: The format string to use 2820 * @args: Arguments for the format string 2821 * 2822 * This function generally follows C99 vsnprintf, but has some 2823 * extensions and a few limitations: 2824 * 2825 * - ``%n`` is unsupported 2826 * - ``%p*`` is handled by pointer() 2827 * 2828 * See pointer() or Documentation/core-api/printk-formats.rst for more 2829 * extensive description. 2830 * 2831 * **Please update the documentation in both places when making changes** 2832 * 2833 * The return value is the number of characters which would 2834 * be generated for the given input, excluding the trailing 2835 * '\0', as per ISO C99. If you want to have the exact 2836 * number of characters written into @buf as return value 2837 * (not including the trailing '\0'), use vscnprintf(). If the 2838 * return is greater than or equal to @size, the resulting 2839 * string is truncated. 2840 * 2841 * If you're not already dealing with a va_list consider using snprintf(). 2842 */ 2843 int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args) 2844 { 2845 char *str, *end; 2846 struct printf_spec spec = {0}; 2847 struct fmt fmt = { 2848 .str = fmt_str, 2849 .state = FORMAT_STATE_NONE, 2850 }; 2851 2852 /* Reject out-of-range values early. Large positive sizes are 2853 used for unknown buffer sizes. */ 2854 if (WARN_ON_ONCE(size > INT_MAX)) 2855 return 0; 2856 2857 str = buf; 2858 end = buf + size; 2859 2860 /* Make sure end is always >= buf */ 2861 if (end < buf) { 2862 end = ((void *)-1); 2863 size = end - buf; 2864 } 2865 2866 while (*fmt.str) { 2867 const char *old_fmt = fmt.str; 2868 2869 fmt = format_decode(fmt, &spec); 2870 2871 switch (fmt.state) { 2872 case FORMAT_STATE_NONE: { 2873 int read = fmt.str - old_fmt; 2874 if (str < end) { 2875 int copy = read; 2876 if (copy > end - str) 2877 copy = end - str; 2878 memcpy(str, old_fmt, copy); 2879 } 2880 str += read; 2881 continue; 2882 } 2883 2884 case FORMAT_STATE_NUM: { 2885 unsigned long long num; 2886 if (fmt.size <= sizeof(int)) 2887 num = convert_num_spec(va_arg(args, int), fmt.size, spec); 2888 else 2889 num = va_arg(args, long long); 2890 str = number(str, end, num, spec); 2891 continue; 2892 } 2893 2894 case FORMAT_STATE_WIDTH: 2895 set_field_width(&spec, va_arg(args, int)); 2896 continue; 2897 2898 case FORMAT_STATE_PRECISION: 2899 set_precision(&spec, va_arg(args, int)); 2900 continue; 2901 2902 case FORMAT_STATE_CHAR: { 2903 char c; 2904 2905 if (!(spec.flags & LEFT)) { 2906 while (--spec.field_width > 0) { 2907 if (str < end) 2908 *str = ' '; 2909 ++str; 2910 2911 } 2912 } 2913 c = (unsigned char) va_arg(args, int); 2914 if (str < end) 2915 *str = c; 2916 ++str; 2917 while (--spec.field_width > 0) { 2918 if (str < end) 2919 *str = ' '; 2920 ++str; 2921 } 2922 continue; 2923 } 2924 2925 case FORMAT_STATE_STR: 2926 str = string(str, end, va_arg(args, char *), spec); 2927 continue; 2928 2929 case FORMAT_STATE_PTR: 2930 str = pointer(fmt.str, str, end, va_arg(args, void *), 2931 spec); 2932 while (isalnum(*fmt.str)) 2933 fmt.str++; 2934 continue; 2935 2936 case FORMAT_STATE_PERCENT_CHAR: 2937 if (str < end) 2938 *str = '%'; 2939 ++str; 2940 continue; 2941 2942 default: 2943 /* 2944 * Presumably the arguments passed gcc's type 2945 * checking, but there is no safe or sane way 2946 * for us to continue parsing the format and 2947 * fetching from the va_list; the remaining 2948 * specifiers and arguments would be out of 2949 * sync. 2950 */ 2951 goto out; 2952 } 2953 } 2954 2955 out: 2956 if (size > 0) { 2957 if (str < end) 2958 *str = '\0'; 2959 else 2960 end[-1] = '\0'; 2961 } 2962 2963 /* the trailing null byte doesn't count towards the total */ 2964 return str-buf; 2965 2966 } 2967 EXPORT_SYMBOL(vsnprintf); 2968 2969 /** 2970 * vscnprintf - Format a string and place it in a buffer 2971 * @buf: The buffer to place the result into 2972 * @size: The size of the buffer, including the trailing null space 2973 * @fmt: The format string to use 2974 * @args: Arguments for the format string 2975 * 2976 * The return value is the number of characters which have been written into 2977 * the @buf not including the trailing '\0'. If @size is == 0 the function 2978 * returns 0. 2979 * 2980 * If you're not already dealing with a va_list consider using scnprintf(). 2981 * 2982 * See the vsnprintf() documentation for format string extensions over C99. 2983 */ 2984 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 2985 { 2986 int i; 2987 2988 if (unlikely(!size)) 2989 return 0; 2990 2991 i = vsnprintf(buf, size, fmt, args); 2992 2993 if (likely(i < size)) 2994 return i; 2995 2996 return size - 1; 2997 } 2998 EXPORT_SYMBOL(vscnprintf); 2999 3000 /** 3001 * snprintf - Format a string and place it in a buffer 3002 * @buf: The buffer to place the result into 3003 * @size: The size of the buffer, including the trailing null space 3004 * @fmt: The format string to use 3005 * @...: Arguments for the format string 3006 * 3007 * The return value is the number of characters which would be 3008 * generated for the given input, excluding the trailing null, 3009 * as per ISO C99. If the return is greater than or equal to 3010 * @size, the resulting string is truncated. 3011 * 3012 * See the vsnprintf() documentation for format string extensions over C99. 3013 */ 3014 int snprintf(char *buf, size_t size, const char *fmt, ...) 3015 { 3016 va_list args; 3017 int i; 3018 3019 va_start(args, fmt); 3020 i = vsnprintf(buf, size, fmt, args); 3021 va_end(args); 3022 3023 return i; 3024 } 3025 EXPORT_SYMBOL(snprintf); 3026 3027 /** 3028 * scnprintf - Format a string and place it in a buffer 3029 * @buf: The buffer to place the result into 3030 * @size: The size of the buffer, including the trailing null space 3031 * @fmt: The format string to use 3032 * @...: Arguments for the format string 3033 * 3034 * The return value is the number of characters written into @buf not including 3035 * the trailing '\0'. If @size is == 0 the function returns 0. 3036 */ 3037 3038 int scnprintf(char *buf, size_t size, const char *fmt, ...) 3039 { 3040 va_list args; 3041 int i; 3042 3043 va_start(args, fmt); 3044 i = vscnprintf(buf, size, fmt, args); 3045 va_end(args); 3046 3047 return i; 3048 } 3049 EXPORT_SYMBOL(scnprintf); 3050 3051 /** 3052 * vsprintf - Format a string and place it in a buffer 3053 * @buf: The buffer to place the result into 3054 * @fmt: The format string to use 3055 * @args: Arguments for the format string 3056 * 3057 * The function returns the number of characters written 3058 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 3059 * buffer overflows. 3060 * 3061 * If you're not already dealing with a va_list consider using sprintf(). 3062 * 3063 * See the vsnprintf() documentation for format string extensions over C99. 3064 */ 3065 int vsprintf(char *buf, const char *fmt, va_list args) 3066 { 3067 return vsnprintf(buf, INT_MAX, fmt, args); 3068 } 3069 EXPORT_SYMBOL(vsprintf); 3070 3071 /** 3072 * sprintf - Format a string and place it in a buffer 3073 * @buf: The buffer to place the result into 3074 * @fmt: The format string to use 3075 * @...: Arguments for the format string 3076 * 3077 * The function returns the number of characters written 3078 * into @buf. Use snprintf() or scnprintf() in order to avoid 3079 * buffer overflows. 3080 * 3081 * See the vsnprintf() documentation for format string extensions over C99. 3082 */ 3083 int sprintf(char *buf, const char *fmt, ...) 3084 { 3085 va_list args; 3086 int i; 3087 3088 va_start(args, fmt); 3089 i = vsnprintf(buf, INT_MAX, fmt, args); 3090 va_end(args); 3091 3092 return i; 3093 } 3094 EXPORT_SYMBOL(sprintf); 3095 3096 #ifdef CONFIG_BINARY_PRINTF 3097 /* 3098 * bprintf service: 3099 * vbin_printf() - VA arguments to binary data 3100 * bstr_printf() - Binary data to text string 3101 */ 3102 3103 /** 3104 * vbin_printf - Parse a format string and place args' binary value in a buffer 3105 * @bin_buf: The buffer to place args' binary value 3106 * @size: The size of the buffer(by words(32bits), not characters) 3107 * @fmt_str: The format string to use 3108 * @args: Arguments for the format string 3109 * 3110 * The format follows C99 vsnprintf, except %n is ignored, and its argument 3111 * is skipped. 3112 * 3113 * The return value is the number of words(32bits) which would be generated for 3114 * the given input. 3115 * 3116 * NOTE: 3117 * If the return value is greater than @size, the resulting bin_buf is NOT 3118 * valid for bstr_printf(). 3119 */ 3120 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args) 3121 { 3122 struct fmt fmt = { 3123 .str = fmt_str, 3124 .state = FORMAT_STATE_NONE, 3125 }; 3126 struct printf_spec spec = {0}; 3127 char *str, *end; 3128 int width; 3129 3130 str = (char *)bin_buf; 3131 end = (char *)(bin_buf + size); 3132 3133 #define save_arg(type) \ 3134 ({ \ 3135 unsigned long long value; \ 3136 if (sizeof(type) == 8) { \ 3137 unsigned long long val8; \ 3138 str = PTR_ALIGN(str, sizeof(u32)); \ 3139 val8 = va_arg(args, unsigned long long); \ 3140 if (str + sizeof(type) <= end) { \ 3141 *(u32 *)str = *(u32 *)&val8; \ 3142 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 3143 } \ 3144 value = val8; \ 3145 } else { \ 3146 unsigned int val4; \ 3147 str = PTR_ALIGN(str, sizeof(type)); \ 3148 val4 = va_arg(args, int); \ 3149 if (str + sizeof(type) <= end) \ 3150 *(typeof(type) *)str = (type)(long)val4; \ 3151 value = (unsigned long long)val4; \ 3152 } \ 3153 str += sizeof(type); \ 3154 value; \ 3155 }) 3156 3157 while (*fmt.str) { 3158 fmt = format_decode(fmt, &spec); 3159 3160 switch (fmt.state) { 3161 case FORMAT_STATE_NONE: 3162 case FORMAT_STATE_PERCENT_CHAR: 3163 break; 3164 case FORMAT_STATE_INVALID: 3165 goto out; 3166 3167 case FORMAT_STATE_WIDTH: 3168 case FORMAT_STATE_PRECISION: 3169 width = (int)save_arg(int); 3170 /* Pointers may require the width */ 3171 if (*fmt.str == 'p') 3172 set_field_width(&spec, width); 3173 break; 3174 3175 case FORMAT_STATE_CHAR: 3176 save_arg(char); 3177 break; 3178 3179 case FORMAT_STATE_STR: { 3180 const char *save_str = va_arg(args, char *); 3181 const char *err_msg; 3182 size_t len; 3183 3184 err_msg = check_pointer_msg(save_str); 3185 if (err_msg) 3186 save_str = err_msg; 3187 3188 len = strlen(save_str) + 1; 3189 if (str + len < end) 3190 memcpy(str, save_str, len); 3191 str += len; 3192 break; 3193 } 3194 3195 case FORMAT_STATE_PTR: 3196 /* Dereferenced pointers must be done now */ 3197 switch (*fmt.str) { 3198 /* Dereference of functions is still OK */ 3199 case 'S': 3200 case 's': 3201 case 'x': 3202 case 'K': 3203 case 'e': 3204 save_arg(void *); 3205 break; 3206 default: 3207 if (!isalnum(*fmt.str)) { 3208 save_arg(void *); 3209 break; 3210 } 3211 str = pointer(fmt.str, str, end, va_arg(args, void *), 3212 spec); 3213 if (str + 1 < end) 3214 *str++ = '\0'; 3215 else 3216 end[-1] = '\0'; /* Must be nul terminated */ 3217 } 3218 /* skip all alphanumeric pointer suffixes */ 3219 while (isalnum(*fmt.str)) 3220 fmt.str++; 3221 break; 3222 3223 case FORMAT_STATE_NUM: 3224 if (fmt.size > sizeof(int)) { 3225 save_arg(long long); 3226 } else { 3227 save_arg(int); 3228 } 3229 } 3230 } 3231 3232 out: 3233 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 3234 #undef save_arg 3235 } 3236 EXPORT_SYMBOL_GPL(vbin_printf); 3237 3238 /** 3239 * bstr_printf - Format a string from binary arguments and place it in a buffer 3240 * @buf: The buffer to place the result into 3241 * @size: The size of the buffer, including the trailing null space 3242 * @fmt_str: The format string to use 3243 * @bin_buf: Binary arguments for the format string 3244 * 3245 * This function like C99 vsnprintf, but the difference is that vsnprintf gets 3246 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 3247 * a binary buffer that generated by vbin_printf. 3248 * 3249 * The format follows C99 vsnprintf, but has some extensions: 3250 * see vsnprintf comment for details. 3251 * 3252 * The return value is the number of characters which would 3253 * be generated for the given input, excluding the trailing 3254 * '\0', as per ISO C99. If you want to have the exact 3255 * number of characters written into @buf as return value 3256 * (not including the trailing '\0'), use vscnprintf(). If the 3257 * return is greater than or equal to @size, the resulting 3258 * string is truncated. 3259 */ 3260 int bstr_printf(char *buf, size_t size, const char *fmt_str, const u32 *bin_buf) 3261 { 3262 struct fmt fmt = { 3263 .str = fmt_str, 3264 .state = FORMAT_STATE_NONE, 3265 }; 3266 struct printf_spec spec = {0}; 3267 char *str, *end; 3268 const char *args = (const char *)bin_buf; 3269 3270 if (WARN_ON_ONCE(size > INT_MAX)) 3271 return 0; 3272 3273 str = buf; 3274 end = buf + size; 3275 3276 #define get_arg(type) \ 3277 ({ \ 3278 typeof(type) value; \ 3279 if (sizeof(type) == 8) { \ 3280 args = PTR_ALIGN(args, sizeof(u32)); \ 3281 *(u32 *)&value = *(u32 *)args; \ 3282 *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 3283 } else { \ 3284 args = PTR_ALIGN(args, sizeof(type)); \ 3285 value = *(typeof(type) *)args; \ 3286 } \ 3287 args += sizeof(type); \ 3288 value; \ 3289 }) 3290 3291 /* Make sure end is always >= buf */ 3292 if (end < buf) { 3293 end = ((void *)-1); 3294 size = end - buf; 3295 } 3296 3297 while (*fmt.str) { 3298 const char *old_fmt = fmt.str; 3299 unsigned long long num; 3300 3301 fmt = format_decode(fmt, &spec); 3302 switch (fmt.state) { 3303 case FORMAT_STATE_NONE: { 3304 int read = fmt.str - old_fmt; 3305 if (str < end) { 3306 int copy = read; 3307 if (copy > end - str) 3308 copy = end - str; 3309 memcpy(str, old_fmt, copy); 3310 } 3311 str += read; 3312 continue; 3313 } 3314 3315 case FORMAT_STATE_WIDTH: 3316 set_field_width(&spec, get_arg(int)); 3317 continue; 3318 3319 case FORMAT_STATE_PRECISION: 3320 set_precision(&spec, get_arg(int)); 3321 continue; 3322 3323 case FORMAT_STATE_CHAR: { 3324 char c; 3325 3326 if (!(spec.flags & LEFT)) { 3327 while (--spec.field_width > 0) { 3328 if (str < end) 3329 *str = ' '; 3330 ++str; 3331 } 3332 } 3333 c = (unsigned char) get_arg(char); 3334 if (str < end) 3335 *str = c; 3336 ++str; 3337 while (--spec.field_width > 0) { 3338 if (str < end) 3339 *str = ' '; 3340 ++str; 3341 } 3342 continue; 3343 } 3344 3345 case FORMAT_STATE_STR: { 3346 const char *str_arg = args; 3347 args += strlen(str_arg) + 1; 3348 str = string(str, end, (char *)str_arg, spec); 3349 continue; 3350 } 3351 3352 case FORMAT_STATE_PTR: { 3353 bool process = false; 3354 int copy, len; 3355 /* Non function dereferences were already done */ 3356 switch (*fmt.str) { 3357 case 'S': 3358 case 's': 3359 case 'x': 3360 case 'K': 3361 case 'e': 3362 process = true; 3363 break; 3364 default: 3365 if (!isalnum(*fmt.str)) { 3366 process = true; 3367 break; 3368 } 3369 /* Pointer dereference was already processed */ 3370 if (str < end) { 3371 len = copy = strlen(args); 3372 if (copy > end - str) 3373 copy = end - str; 3374 memcpy(str, args, copy); 3375 str += len; 3376 args += len + 1; 3377 } 3378 } 3379 if (process) 3380 str = pointer(fmt.str, str, end, get_arg(void *), spec); 3381 3382 while (isalnum(*fmt.str)) 3383 fmt.str++; 3384 continue; 3385 } 3386 3387 case FORMAT_STATE_PERCENT_CHAR: 3388 if (str < end) 3389 *str = '%'; 3390 ++str; 3391 continue; 3392 3393 case FORMAT_STATE_INVALID: 3394 goto out; 3395 3396 case FORMAT_STATE_NUM: 3397 if (fmt.size > sizeof(int)) { 3398 num = get_arg(long long); 3399 } else { 3400 num = convert_num_spec(get_arg(int), fmt.size, spec); 3401 } 3402 str = number(str, end, num, spec); 3403 continue; 3404 } 3405 } /* while(*fmt.str) */ 3406 3407 out: 3408 if (size > 0) { 3409 if (str < end) 3410 *str = '\0'; 3411 else 3412 end[-1] = '\0'; 3413 } 3414 3415 #undef get_arg 3416 3417 /* the trailing null byte doesn't count towards the total */ 3418 return str - buf; 3419 } 3420 EXPORT_SYMBOL_GPL(bstr_printf); 3421 3422 #endif /* CONFIG_BINARY_PRINTF */ 3423 3424 /** 3425 * vsscanf - Unformat a buffer into a list of arguments 3426 * @buf: input buffer 3427 * @fmt: format of buffer 3428 * @args: arguments 3429 */ 3430 int vsscanf(const char *buf, const char *fmt, va_list args) 3431 { 3432 const char *str = buf; 3433 char *next; 3434 char digit; 3435 int num = 0; 3436 u8 qualifier; 3437 unsigned int base; 3438 union { 3439 long long s; 3440 unsigned long long u; 3441 } val; 3442 s16 field_width; 3443 bool is_sign; 3444 3445 while (*fmt) { 3446 /* skip any white space in format */ 3447 /* white space in format matches any amount of 3448 * white space, including none, in the input. 3449 */ 3450 if (isspace(*fmt)) { 3451 fmt = skip_spaces(++fmt); 3452 str = skip_spaces(str); 3453 } 3454 3455 /* anything that is not a conversion must match exactly */ 3456 if (*fmt != '%' && *fmt) { 3457 if (*fmt++ != *str++) 3458 break; 3459 continue; 3460 } 3461 3462 if (!*fmt) 3463 break; 3464 ++fmt; 3465 3466 /* skip this conversion. 3467 * advance both strings to next white space 3468 */ 3469 if (*fmt == '*') { 3470 if (!*str) 3471 break; 3472 while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3473 /* '%*[' not yet supported, invalid format */ 3474 if (*fmt == '[') 3475 return num; 3476 fmt++; 3477 } 3478 while (!isspace(*str) && *str) 3479 str++; 3480 continue; 3481 } 3482 3483 /* get field width */ 3484 field_width = -1; 3485 if (isdigit(*fmt)) { 3486 field_width = skip_atoi(&fmt); 3487 if (field_width <= 0) 3488 break; 3489 } 3490 3491 /* get conversion qualifier */ 3492 qualifier = -1; 3493 if (*fmt == 'h' || _tolower(*fmt) == 'l' || 3494 *fmt == 'z') { 3495 qualifier = *fmt++; 3496 if (unlikely(qualifier == *fmt)) { 3497 if (qualifier == 'h') { 3498 qualifier = 'H'; 3499 fmt++; 3500 } else if (qualifier == 'l') { 3501 qualifier = 'L'; 3502 fmt++; 3503 } 3504 } 3505 } 3506 3507 if (!*fmt) 3508 break; 3509 3510 if (*fmt == 'n') { 3511 /* return number of characters read so far */ 3512 *va_arg(args, int *) = str - buf; 3513 ++fmt; 3514 continue; 3515 } 3516 3517 if (!*str) 3518 break; 3519 3520 base = 10; 3521 is_sign = false; 3522 3523 switch (*fmt++) { 3524 case 'c': 3525 { 3526 char *s = (char *)va_arg(args, char*); 3527 if (field_width == -1) 3528 field_width = 1; 3529 do { 3530 *s++ = *str++; 3531 } while (--field_width > 0 && *str); 3532 num++; 3533 } 3534 continue; 3535 case 's': 3536 { 3537 char *s = (char *)va_arg(args, char *); 3538 if (field_width == -1) 3539 field_width = SHRT_MAX; 3540 /* first, skip leading white space in buffer */ 3541 str = skip_spaces(str); 3542 3543 /* now copy until next white space */ 3544 while (*str && !isspace(*str) && field_width--) 3545 *s++ = *str++; 3546 *s = '\0'; 3547 num++; 3548 } 3549 continue; 3550 /* 3551 * Warning: This implementation of the '[' conversion specifier 3552 * deviates from its glibc counterpart in the following ways: 3553 * (1) It does NOT support ranges i.e. '-' is NOT a special 3554 * character 3555 * (2) It cannot match the closing bracket ']' itself 3556 * (3) A field width is required 3557 * (4) '%*[' (discard matching input) is currently not supported 3558 * 3559 * Example usage: 3560 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3561 * buf1, buf2, buf3); 3562 * if (ret < 3) 3563 * // etc.. 3564 */ 3565 case '[': 3566 { 3567 char *s = (char *)va_arg(args, char *); 3568 DECLARE_BITMAP(set, 256) = {0}; 3569 unsigned int len = 0; 3570 bool negate = (*fmt == '^'); 3571 3572 /* field width is required */ 3573 if (field_width == -1) 3574 return num; 3575 3576 if (negate) 3577 ++fmt; 3578 3579 for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3580 __set_bit((u8)*fmt, set); 3581 3582 /* no ']' or no character set found */ 3583 if (!*fmt || !len) 3584 return num; 3585 ++fmt; 3586 3587 if (negate) { 3588 bitmap_complement(set, set, 256); 3589 /* exclude null '\0' byte */ 3590 __clear_bit(0, set); 3591 } 3592 3593 /* match must be non-empty */ 3594 if (!test_bit((u8)*str, set)) 3595 return num; 3596 3597 while (test_bit((u8)*str, set) && field_width--) 3598 *s++ = *str++; 3599 *s = '\0'; 3600 ++num; 3601 } 3602 continue; 3603 case 'o': 3604 base = 8; 3605 break; 3606 case 'x': 3607 case 'X': 3608 base = 16; 3609 break; 3610 case 'i': 3611 base = 0; 3612 fallthrough; 3613 case 'd': 3614 is_sign = true; 3615 fallthrough; 3616 case 'u': 3617 break; 3618 case '%': 3619 /* looking for '%' in str */ 3620 if (*str++ != '%') 3621 return num; 3622 continue; 3623 default: 3624 /* invalid format; stop here */ 3625 return num; 3626 } 3627 3628 /* have some sort of integer conversion. 3629 * first, skip white space in buffer. 3630 */ 3631 str = skip_spaces(str); 3632 3633 digit = *str; 3634 if (is_sign && digit == '-') { 3635 if (field_width == 1) 3636 break; 3637 3638 digit = *(str + 1); 3639 } 3640 3641 if (!digit 3642 || (base == 16 && !isxdigit(digit)) 3643 || (base == 10 && !isdigit(digit)) 3644 || (base == 8 && !isodigit(digit)) 3645 || (base == 0 && !isdigit(digit))) 3646 break; 3647 3648 if (is_sign) 3649 val.s = simple_strntoll(str, &next, base, 3650 field_width >= 0 ? field_width : INT_MAX); 3651 else 3652 val.u = simple_strntoull(str, &next, base, 3653 field_width >= 0 ? field_width : INT_MAX); 3654 3655 switch (qualifier) { 3656 case 'H': /* that's 'hh' in format */ 3657 if (is_sign) 3658 *va_arg(args, signed char *) = val.s; 3659 else 3660 *va_arg(args, unsigned char *) = val.u; 3661 break; 3662 case 'h': 3663 if (is_sign) 3664 *va_arg(args, short *) = val.s; 3665 else 3666 *va_arg(args, unsigned short *) = val.u; 3667 break; 3668 case 'l': 3669 if (is_sign) 3670 *va_arg(args, long *) = val.s; 3671 else 3672 *va_arg(args, unsigned long *) = val.u; 3673 break; 3674 case 'L': 3675 if (is_sign) 3676 *va_arg(args, long long *) = val.s; 3677 else 3678 *va_arg(args, unsigned long long *) = val.u; 3679 break; 3680 case 'z': 3681 *va_arg(args, size_t *) = val.u; 3682 break; 3683 default: 3684 if (is_sign) 3685 *va_arg(args, int *) = val.s; 3686 else 3687 *va_arg(args, unsigned int *) = val.u; 3688 break; 3689 } 3690 num++; 3691 3692 if (!next) 3693 break; 3694 str = next; 3695 } 3696 3697 return num; 3698 } 3699 EXPORT_SYMBOL(vsscanf); 3700 3701 /** 3702 * sscanf - Unformat a buffer into a list of arguments 3703 * @buf: input buffer 3704 * @fmt: formatting of buffer 3705 * @...: resulting arguments 3706 */ 3707 int sscanf(const char *buf, const char *fmt, ...) 3708 { 3709 va_list args; 3710 int i; 3711 3712 va_start(args, fmt); 3713 i = vsscanf(buf, fmt, args); 3714 va_end(args); 3715 3716 return i; 3717 } 3718 EXPORT_SYMBOL(sscanf); 3719