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