linux/lib/vsprintf.c
<<
>>
Prefs
   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/module.h>       /* for KSYM_SYMBOL_LEN */
  21#include <linux/types.h>
  22#include <linux/string.h>
  23#include <linux/ctype.h>
  24#include <linux/kernel.h>
  25#include <linux/kallsyms.h>
  26#include <linux/math64.h>
  27#include <linux/uaccess.h>
  28#include <linux/ioport.h>
  29#include <linux/cpumask.h>
  30#include <linux/dcache.h>
  31#include <linux/cred.h>
  32#include <linux/uuid.h>
  33#include <net/addrconf.h>
  34
  35#include <asm/page.h>           /* for PAGE_SIZE */
  36#include <asm/sections.h>       /* for dereference_function_descriptor() */
  37
  38#include "kstrtox.h"
  39
  40/**
  41 * simple_strtoull - convert a string to an unsigned long long
  42 * @cp: The start of the string
  43 * @endp: A pointer to the end of the parsed string will be placed here
  44 * @base: The number base to use
  45 *
  46 * This function is obsolete. Please use kstrtoull instead.
  47 */
  48unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  49{
  50        unsigned long long result;
  51        unsigned int rv;
  52
  53        cp = _parse_integer_fixup_radix(cp, &base);
  54        rv = _parse_integer(cp, base, &result);
  55        /* FIXME */
  56        cp += (rv & ~KSTRTOX_OVERFLOW);
  57
  58        if (endp)
  59                *endp = (char *)cp;
  60
  61        return result;
  62}
  63EXPORT_SYMBOL(simple_strtoull);
  64
  65/**
  66 * simple_strtoul - convert a string to an unsigned long
  67 * @cp: The start of the string
  68 * @endp: A pointer to the end of the parsed string will be placed here
  69 * @base: The number base to use
  70 *
  71 * This function is obsolete. Please use kstrtoul instead.
  72 */
  73unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  74{
  75        return simple_strtoull(cp, endp, base);
  76}
  77EXPORT_SYMBOL(simple_strtoul);
  78
  79/**
  80 * simple_strtol - convert a string to a signed long
  81 * @cp: The start of the string
  82 * @endp: A pointer to the end of the parsed string will be placed here
  83 * @base: The number base to use
  84 *
  85 * This function is obsolete. Please use kstrtol instead.
  86 */
  87long simple_strtol(const char *cp, char **endp, unsigned int base)
  88{
  89        if (*cp == '-')
  90                return -simple_strtoul(cp + 1, endp, base);
  91
  92        return simple_strtoul(cp, endp, base);
  93}
  94EXPORT_SYMBOL(simple_strtol);
  95
  96/**
  97 * simple_strtoll - convert a string to a signed long long
  98 * @cp: The start of the string
  99 * @endp: A pointer to the end of the parsed string will be placed here
 100 * @base: The number base to use
 101 *
 102 * This function is obsolete. Please use kstrtoll instead.
 103 */
 104long long simple_strtoll(const char *cp, char **endp, unsigned int base)
 105{
 106        if (*cp == '-')
 107                return -simple_strtoull(cp + 1, endp, base);
 108
 109        return simple_strtoull(cp, endp, base);
 110}
 111EXPORT_SYMBOL(simple_strtoll);
 112
 113static noinline_for_stack
 114int skip_atoi(const char **s)
 115{
 116        int i = 0;
 117
 118        while (isdigit(**s))
 119                i = i*10 + *((*s)++) - '0';
 120
 121        return i;
 122}
 123
 124/* Decimal conversion is by far the most typical, and is used
 125 * for /proc and /sys data. This directly impacts e.g. top performance
 126 * with many processes running. We optimize it for speed
 127 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
 128 * (with permission from the author, Douglas W. Jones).
 129 */
 130
 131#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
 132/* Formats correctly any integer in [0, 999999999] */
 133static noinline_for_stack
 134char *put_dec_full9(char *buf, unsigned q)
 135{
 136        unsigned r;
 137
 138        /*
 139         * Possible ways to approx. divide by 10
 140         * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
 141         * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
 142         * (x * 0x6667) >> 18     x <      43699
 143         * (x * 0x3334) >> 17     x <      16389
 144         * (x * 0x199a) >> 16     x <      16389
 145         * (x * 0x0ccd) >> 15     x <      16389
 146         * (x * 0x0667) >> 14     x <       2739
 147         * (x * 0x0334) >> 13     x <       1029
 148         * (x * 0x019a) >> 12     x <       1029
 149         * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
 150         * (x * 0x0067) >> 10     x <        179
 151         * (x * 0x0034) >>  9     x <         69 same
 152         * (x * 0x001a) >>  8     x <         69 same
 153         * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
 154         * (x * 0x0007) >>  6     x <         19
 155         * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
 156         */
 157        r      = (q * (uint64_t)0x1999999a) >> 32;
 158        *buf++ = (q - 10 * r) + '0'; /* 1 */
 159        q      = (r * (uint64_t)0x1999999a) >> 32;
 160        *buf++ = (r - 10 * q) + '0'; /* 2 */
 161        r      = (q * (uint64_t)0x1999999a) >> 32;
 162        *buf++ = (q - 10 * r) + '0'; /* 3 */
 163        q      = (r * (uint64_t)0x1999999a) >> 32;
 164        *buf++ = (r - 10 * q) + '0'; /* 4 */
 165        r      = (q * (uint64_t)0x1999999a) >> 32;
 166        *buf++ = (q - 10 * r) + '0'; /* 5 */
 167        /* Now value is under 10000, can avoid 64-bit multiply */
 168        q      = (r * 0x199a) >> 16;
 169        *buf++ = (r - 10 * q)  + '0'; /* 6 */
 170        r      = (q * 0xcd) >> 11;
 171        *buf++ = (q - 10 * r)  + '0'; /* 7 */
 172        q      = (r * 0xcd) >> 11;
 173        *buf++ = (r - 10 * q) + '0'; /* 8 */
 174        *buf++ = q + '0'; /* 9 */
 175        return buf;
 176}
 177#endif
 178
 179/* Similar to above but do not pad with zeros.
 180 * Code can be easily arranged to print 9 digits too, but our callers
 181 * always call put_dec_full9() instead when the number has 9 decimal digits.
 182 */
 183static noinline_for_stack
 184char *put_dec_trunc8(char *buf, unsigned r)
 185{
 186        unsigned q;
 187
 188        /* Copy of previous function's body with added early returns */
 189        while (r >= 10000) {
 190                q = r + '0';
 191                r  = (r * (uint64_t)0x1999999a) >> 32;
 192                *buf++ = q - 10*r;
 193        }
 194
 195        q      = (r * 0x199a) >> 16;    /* r <= 9999 */
 196        *buf++ = (r - 10 * q)  + '0';
 197        if (q == 0)
 198                return buf;
 199        r      = (q * 0xcd) >> 11;      /* q <= 999 */
 200        *buf++ = (q - 10 * r)  + '0';
 201        if (r == 0)
 202                return buf;
 203        q      = (r * 0xcd) >> 11;      /* r <= 99 */
 204        *buf++ = (r - 10 * q) + '0';
 205        if (q == 0)
 206                return buf;
 207        *buf++ = q + '0';                /* q <= 9 */
 208        return buf;
 209}
 210
 211/* There are two algorithms to print larger numbers.
 212 * One is generic: divide by 1000000000 and repeatedly print
 213 * groups of (up to) 9 digits. It's conceptually simple,
 214 * but requires a (unsigned long long) / 1000000000 division.
 215 *
 216 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
 217 * manipulates them cleverly and generates groups of 4 decimal digits.
 218 * It so happens that it does NOT require long long division.
 219 *
 220 * If long is > 32 bits, division of 64-bit values is relatively easy,
 221 * and we will use the first algorithm.
 222 * If long long is > 64 bits (strange architecture with VERY large long long),
 223 * second algorithm can't be used, and we again use the first one.
 224 *
 225 * Else (if long is 32 bits and long long is 64 bits) we use second one.
 226 */
 227
 228#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
 229
 230/* First algorithm: generic */
 231
 232static
 233char *put_dec(char *buf, unsigned long long n)
 234{
 235        if (n >= 100*1000*1000) {
 236                while (n >= 1000*1000*1000)
 237                        buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
 238                if (n >= 100*1000*1000)
 239                        return put_dec_full9(buf, n);
 240        }
 241        return put_dec_trunc8(buf, n);
 242}
 243
 244#else
 245
 246/* Second algorithm: valid only for 64-bit long longs */
 247
 248/* See comment in put_dec_full9 for choice of constants */
 249static noinline_for_stack
 250void put_dec_full4(char *buf, unsigned q)
 251{
 252        unsigned r;
 253        r      = (q * 0xccd) >> 15;
 254        buf[0] = (q - 10 * r) + '0';
 255        q      = (r * 0xcd) >> 11;
 256        buf[1] = (r - 10 * q)  + '0';
 257        r      = (q * 0xcd) >> 11;
 258        buf[2] = (q - 10 * r)  + '0';
 259        buf[3] = r + '0';
 260}
 261
 262/*
 263 * Call put_dec_full4 on x % 10000, return x / 10000.
 264 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
 265 * holds for all x < 1,128,869,999.  The largest value this
 266 * helper will ever be asked to convert is 1,125,520,955.
 267 * (d1 in the put_dec code, assuming n is all-ones).
 268 */
 269static
 270unsigned put_dec_helper4(char *buf, unsigned x)
 271{
 272        uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
 273
 274        put_dec_full4(buf, x - q * 10000);
 275        return q;
 276}
 277
 278/* Based on code by Douglas W. Jones found at
 279 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
 280 * (with permission from the author).
 281 * Performs no 64-bit division and hence should be fast on 32-bit machines.
 282 */
 283static
 284char *put_dec(char *buf, unsigned long long n)
 285{
 286        uint32_t d3, d2, d1, q, h;
 287
 288        if (n < 100*1000*1000)
 289                return put_dec_trunc8(buf, n);
 290
 291        d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
 292        h   = (n >> 32);
 293        d2  = (h      ) & 0xffff;
 294        d3  = (h >> 16); /* implicit "& 0xffff" */
 295
 296        q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
 297        q = put_dec_helper4(buf, q);
 298
 299        q += 7671 * d3 + 9496 * d2 + 6 * d1;
 300        q = put_dec_helper4(buf+4, q);
 301
 302        q += 4749 * d3 + 42 * d2;
 303        q = put_dec_helper4(buf+8, q);
 304
 305        q += 281 * d3;
 306        buf += 12;
 307        if (q)
 308                buf = put_dec_trunc8(buf, q);
 309        else while (buf[-1] == '0')
 310                --buf;
 311
 312        return buf;
 313}
 314
 315#endif
 316
 317/*
 318 * Convert passed number to decimal string.
 319 * Returns the length of string.  On buffer overflow, returns 0.
 320 *
 321 * If speed is not important, use snprintf(). It's easy to read the code.
 322 */
 323int num_to_str(char *buf, int size, unsigned long long num)
 324{
 325        char tmp[sizeof(num) * 3];
 326        int idx, len;
 327
 328        /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
 329        if (num <= 9) {
 330                tmp[0] = '0' + num;
 331                len = 1;
 332        } else {
 333                len = put_dec(tmp, num) - tmp;
 334        }
 335
 336        if (len > size)
 337                return 0;
 338        for (idx = 0; idx < len; ++idx)
 339                buf[idx] = tmp[len - idx - 1];
 340        return len;
 341}
 342
 343#define ZEROPAD 1               /* pad with zero */
 344#define SIGN    2               /* unsigned/signed long */
 345#define PLUS    4               /* show plus */
 346#define SPACE   8               /* space if plus */
 347#define LEFT    16              /* left justified */
 348#define SMALL   32              /* use lowercase in hex (must be 32 == 0x20) */
 349#define SPECIAL 64              /* prefix hex with "0x", octal with "0" */
 350
 351enum format_type {
 352        FORMAT_TYPE_NONE, /* Just a string part */
 353        FORMAT_TYPE_WIDTH,
 354        FORMAT_TYPE_PRECISION,
 355        FORMAT_TYPE_CHAR,
 356        FORMAT_TYPE_STR,
 357        FORMAT_TYPE_PTR,
 358        FORMAT_TYPE_PERCENT_CHAR,
 359        FORMAT_TYPE_INVALID,
 360        FORMAT_TYPE_LONG_LONG,
 361        FORMAT_TYPE_ULONG,
 362        FORMAT_TYPE_LONG,
 363        FORMAT_TYPE_UBYTE,
 364        FORMAT_TYPE_BYTE,
 365        FORMAT_TYPE_USHORT,
 366        FORMAT_TYPE_SHORT,
 367        FORMAT_TYPE_UINT,
 368        FORMAT_TYPE_INT,
 369        FORMAT_TYPE_NRCHARS,
 370        FORMAT_TYPE_SIZE_T,
 371        FORMAT_TYPE_PTRDIFF
 372};
 373
 374struct printf_spec {
 375        u8      type;           /* format_type enum */
 376        u8      flags;          /* flags to number() */
 377        u8      base;           /* number base, 8, 10 or 16 only */
 378        u8      qualifier;      /* number qualifier, one of 'hHlLtzZ' */
 379        s16     field_width;    /* width of output field */
 380        s16     precision;      /* # of digits/chars */
 381};
 382
 383static noinline_for_stack
 384char *number(char *buf, char *end, unsigned long long num,
 385             struct printf_spec spec)
 386{
 387        /* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
 388        static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
 389
 390        char tmp[66];
 391        char sign;
 392        char locase;
 393        int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
 394        int i;
 395        bool is_zero = num == 0LL;
 396
 397        /* locase = 0 or 0x20. ORing digits or letters with 'locase'
 398         * produces same digits or (maybe lowercased) letters */
 399        locase = (spec.flags & SMALL);
 400        if (spec.flags & LEFT)
 401                spec.flags &= ~ZEROPAD;
 402        sign = 0;
 403        if (spec.flags & SIGN) {
 404                if ((signed long long)num < 0) {
 405                        sign = '-';
 406                        num = -(signed long long)num;
 407                        spec.field_width--;
 408                } else if (spec.flags & PLUS) {
 409                        sign = '+';
 410                        spec.field_width--;
 411                } else if (spec.flags & SPACE) {
 412                        sign = ' ';
 413                        spec.field_width--;
 414                }
 415        }
 416        if (need_pfx) {
 417                if (spec.base == 16)
 418                        spec.field_width -= 2;
 419                else if (!is_zero)
 420                        spec.field_width--;
 421        }
 422
 423        /* generate full string in tmp[], in reverse order */
 424        i = 0;
 425        if (num < spec.base)
 426                tmp[i++] = digits[num] | locase;
 427        /* Generic code, for any base:
 428        else do {
 429                tmp[i++] = (digits[do_div(num,base)] | locase);
 430        } while (num != 0);
 431        */
 432        else if (spec.base != 10) { /* 8 or 16 */
 433                int mask = spec.base - 1;
 434                int shift = 3;
 435
 436                if (spec.base == 16)
 437                        shift = 4;
 438                do {
 439                        tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
 440                        num >>= shift;
 441                } while (num);
 442        } else { /* base 10 */
 443                i = put_dec(tmp, num) - tmp;
 444        }
 445
 446        /* printing 100 using %2d gives "100", not "00" */
 447        if (i > spec.precision)
 448                spec.precision = i;
 449        /* leading space padding */
 450        spec.field_width -= spec.precision;
 451        if (!(spec.flags & (ZEROPAD+LEFT))) {
 452                while (--spec.field_width >= 0) {
 453                        if (buf < end)
 454                                *buf = ' ';
 455                        ++buf;
 456                }
 457        }
 458        /* sign */
 459        if (sign) {
 460                if (buf < end)
 461                        *buf = sign;
 462                ++buf;
 463        }
 464        /* "0x" / "0" prefix */
 465        if (need_pfx) {
 466                if (spec.base == 16 || !is_zero) {
 467                        if (buf < end)
 468                                *buf = '0';
 469                        ++buf;
 470                }
 471                if (spec.base == 16) {
 472                        if (buf < end)
 473                                *buf = ('X' | locase);
 474                        ++buf;
 475                }
 476        }
 477        /* zero or space padding */
 478        if (!(spec.flags & LEFT)) {
 479                char c = (spec.flags & ZEROPAD) ? '0' : ' ';
 480                while (--spec.field_width >= 0) {
 481                        if (buf < end)
 482                                *buf = c;
 483                        ++buf;
 484                }
 485        }
 486        /* hmm even more zero padding? */
 487        while (i <= --spec.precision) {
 488                if (buf < end)
 489                        *buf = '0';
 490                ++buf;
 491        }
 492        /* actual digits of result */
 493        while (--i >= 0) {
 494                if (buf < end)
 495                        *buf = tmp[i];
 496                ++buf;
 497        }
 498        /* trailing space padding */
 499        while (--spec.field_width >= 0) {
 500                if (buf < end)
 501                        *buf = ' ';
 502                ++buf;
 503        }
 504
 505        return buf;
 506}
 507
 508static noinline_for_stack
 509char *string(char *buf, char *end, const char *s, struct printf_spec spec)
 510{
 511        int len, i;
 512
 513        if ((unsigned long)s < PAGE_SIZE)
 514                s = "(null)";
 515
 516        len = strnlen(s, spec.precision);
 517
 518        if (!(spec.flags & LEFT)) {
 519                while (len < spec.field_width--) {
 520                        if (buf < end)
 521                                *buf = ' ';
 522                        ++buf;
 523                }
 524        }
 525        for (i = 0; i < len; ++i) {
 526                if (buf < end)
 527                        *buf = *s;
 528                ++buf; ++s;
 529        }
 530        while (len < spec.field_width--) {
 531                if (buf < end)
 532                        *buf = ' ';
 533                ++buf;
 534        }
 535
 536        return buf;
 537}
 538
 539static void widen(char *buf, char *end, unsigned len, unsigned spaces)
 540{
 541        size_t size;
 542        if (buf >= end) /* nowhere to put anything */
 543                return;
 544        size = end - buf;
 545        if (size <= spaces) {
 546                memset(buf, ' ', size);
 547                return;
 548        }
 549        if (len) {
 550                if (len > size - spaces)
 551                        len = size - spaces;
 552                memmove(buf + spaces, buf, len);
 553        }
 554        memset(buf, ' ', spaces);
 555}
 556
 557static noinline_for_stack
 558char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
 559                  const char *fmt)
 560{
 561        const char *array[4], *s;
 562        const struct dentry *p;
 563        int depth;
 564        int i, n;
 565
 566        switch (fmt[1]) {
 567                case '2': case '3': case '4':
 568                        depth = fmt[1] - '0';
 569                        break;
 570                default:
 571                        depth = 1;
 572        }
 573
 574        rcu_read_lock();
 575        for (i = 0; i < depth; i++, d = p) {
 576                p = ACCESS_ONCE(d->d_parent);
 577                array[i] = ACCESS_ONCE(d->d_name.name);
 578                if (p == d) {
 579                        if (i)
 580                                array[i] = "";
 581                        i++;
 582                        break;
 583                }
 584        }
 585        s = array[--i];
 586        for (n = 0; n != spec.precision; n++, buf++) {
 587                char c = *s++;
 588                if (!c) {
 589                        if (!i)
 590                                break;
 591                        c = '/';
 592                        s = array[--i];
 593                }
 594                if (buf < end)
 595                        *buf = c;
 596        }
 597        rcu_read_unlock();
 598        if (n < spec.field_width) {
 599                /* we want to pad the sucker */
 600                unsigned spaces = spec.field_width - n;
 601                if (!(spec.flags & LEFT)) {
 602                        widen(buf - n, end, n, spaces);
 603                        return buf + spaces;
 604                }
 605                while (spaces--) {
 606                        if (buf < end)
 607                                *buf = ' ';
 608                        ++buf;
 609                }
 610        }
 611        return buf;
 612}
 613
 614static noinline_for_stack
 615char *symbol_string(char *buf, char *end, void *ptr,
 616                    struct printf_spec spec, const char *fmt)
 617{
 618        unsigned long value;
 619#ifdef CONFIG_KALLSYMS
 620        char sym[KSYM_SYMBOL_LEN];
 621#endif
 622
 623        if (fmt[1] == 'R')
 624                ptr = __builtin_extract_return_addr(ptr);
 625        value = (unsigned long)ptr;
 626
 627#ifdef CONFIG_KALLSYMS
 628        if (*fmt == 'B')
 629                sprint_backtrace(sym, value);
 630        else if (*fmt != 'f' && *fmt != 's')
 631                sprint_symbol(sym, value);
 632        else
 633                sprint_symbol_no_offset(sym, value);
 634
 635        return string(buf, end, sym, spec);
 636#else
 637        spec.field_width = 2 * sizeof(void *);
 638        spec.flags |= SPECIAL | SMALL | ZEROPAD;
 639        spec.base = 16;
 640
 641        return number(buf, end, value, spec);
 642#endif
 643}
 644
 645static noinline_for_stack
 646char *resource_string(char *buf, char *end, struct resource *res,
 647                      struct printf_spec spec, const char *fmt)
 648{
 649#ifndef IO_RSRC_PRINTK_SIZE
 650#define IO_RSRC_PRINTK_SIZE     6
 651#endif
 652
 653#ifndef MEM_RSRC_PRINTK_SIZE
 654#define MEM_RSRC_PRINTK_SIZE    10
 655#endif
 656        static const struct printf_spec io_spec = {
 657                .base = 16,
 658                .field_width = IO_RSRC_PRINTK_SIZE,
 659                .precision = -1,
 660                .flags = SPECIAL | SMALL | ZEROPAD,
 661        };
 662        static const struct printf_spec mem_spec = {
 663                .base = 16,
 664                .field_width = MEM_RSRC_PRINTK_SIZE,
 665                .precision = -1,
 666                .flags = SPECIAL | SMALL | ZEROPAD,
 667        };
 668        static const struct printf_spec bus_spec = {
 669                .base = 16,
 670                .field_width = 2,
 671                .precision = -1,
 672                .flags = SMALL | ZEROPAD,
 673        };
 674        static const struct printf_spec dec_spec = {
 675                .base = 10,
 676                .precision = -1,
 677                .flags = 0,
 678        };
 679        static const struct printf_spec str_spec = {
 680                .field_width = -1,
 681                .precision = 10,
 682                .flags = LEFT,
 683        };
 684        static const struct printf_spec flag_spec = {
 685                .base = 16,
 686                .precision = -1,
 687                .flags = SPECIAL | SMALL,
 688        };
 689
 690        /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
 691         * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
 692#define RSRC_BUF_SIZE           ((2 * sizeof(resource_size_t)) + 4)
 693#define FLAG_BUF_SIZE           (2 * sizeof(res->flags))
 694#define DECODED_BUF_SIZE        sizeof("[mem - 64bit pref window disabled]")
 695#define RAW_BUF_SIZE            sizeof("[mem - flags 0x]")
 696        char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
 697                     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
 698
 699        char *p = sym, *pend = sym + sizeof(sym);
 700        int decode = (fmt[0] == 'R') ? 1 : 0;
 701        const struct printf_spec *specp;
 702
 703        *p++ = '[';
 704        if (res->flags & IORESOURCE_IO) {
 705                p = string(p, pend, "io  ", str_spec);
 706                specp = &io_spec;
 707        } else if (res->flags & IORESOURCE_MEM) {
 708                p = string(p, pend, "mem ", str_spec);
 709                specp = &mem_spec;
 710        } else if (res->flags & IORESOURCE_IRQ) {
 711                p = string(p, pend, "irq ", str_spec);
 712                specp = &dec_spec;
 713        } else if (res->flags & IORESOURCE_DMA) {
 714                p = string(p, pend, "dma ", str_spec);
 715                specp = &dec_spec;
 716        } else if (res->flags & IORESOURCE_BUS) {
 717                p = string(p, pend, "bus ", str_spec);
 718                specp = &bus_spec;
 719        } else {
 720                p = string(p, pend, "??? ", str_spec);
 721                specp = &mem_spec;
 722                decode = 0;
 723        }
 724        if (decode && res->flags & IORESOURCE_UNSET) {
 725                p = string(p, pend, "size ", str_spec);
 726                p = number(p, pend, resource_size(res), *specp);
 727        } else {
 728                p = number(p, pend, res->start, *specp);
 729                if (res->start != res->end) {
 730                        *p++ = '-';
 731                        p = number(p, pend, res->end, *specp);
 732                }
 733        }
 734        if (decode) {
 735                if (res->flags & IORESOURCE_MEM_64)
 736                        p = string(p, pend, " 64bit", str_spec);
 737                if (res->flags & IORESOURCE_PREFETCH)
 738                        p = string(p, pend, " pref", str_spec);
 739                if (res->flags & IORESOURCE_WINDOW)
 740                        p = string(p, pend, " window", str_spec);
 741                if (res->flags & IORESOURCE_DISABLED)
 742                        p = string(p, pend, " disabled", str_spec);
 743        } else {
 744                p = string(p, pend, " flags ", str_spec);
 745                p = number(p, pend, res->flags, flag_spec);
 746        }
 747        *p++ = ']';
 748        *p = '\0';
 749
 750        return string(buf, end, sym, spec);
 751}
 752
 753static noinline_for_stack
 754char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
 755                 const char *fmt)
 756{
 757        int i, len = 1;         /* if we pass '%ph[CDN]', field witdh remains
 758                                   negative value, fallback to the default */
 759        char separator;
 760
 761        if (spec.field_width == 0)
 762                /* nothing to print */
 763                return buf;
 764
 765        if (ZERO_OR_NULL_PTR(addr))
 766                /* NULL pointer */
 767                return string(buf, end, NULL, spec);
 768
 769        switch (fmt[1]) {
 770        case 'C':
 771                separator = ':';
 772                break;
 773        case 'D':
 774                separator = '-';
 775                break;
 776        case 'N':
 777                separator = 0;
 778                break;
 779        default:
 780                separator = ' ';
 781                break;
 782        }
 783
 784        if (spec.field_width > 0)
 785                len = min_t(int, spec.field_width, 64);
 786
 787        for (i = 0; i < len; ++i) {
 788                if (buf < end)
 789                        *buf = hex_asc_hi(addr[i]);
 790                ++buf;
 791                if (buf < end)
 792                        *buf = hex_asc_lo(addr[i]);
 793                ++buf;
 794
 795                if (separator && i != len - 1) {
 796                        if (buf < end)
 797                                *buf = separator;
 798                        ++buf;
 799                }
 800        }
 801
 802        return buf;
 803}
 804
 805static noinline_for_stack
 806char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
 807                    struct printf_spec spec, const char *fmt)
 808{
 809        const int CHUNKSZ = 32;
 810        int nr_bits = max_t(int, spec.field_width, 0);
 811        int i, chunksz;
 812        bool first = true;
 813
 814        /* reused to print numbers */
 815        spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
 816
 817        chunksz = nr_bits & (CHUNKSZ - 1);
 818        if (chunksz == 0)
 819                chunksz = CHUNKSZ;
 820
 821        i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
 822        for (; i >= 0; i -= CHUNKSZ) {
 823                u32 chunkmask, val;
 824                int word, bit;
 825
 826                chunkmask = ((1ULL << chunksz) - 1);
 827                word = i / BITS_PER_LONG;
 828                bit = i % BITS_PER_LONG;
 829                val = (bitmap[word] >> bit) & chunkmask;
 830
 831                if (!first) {
 832                        if (buf < end)
 833                                *buf = ',';
 834                        buf++;
 835                }
 836                first = false;
 837
 838                spec.field_width = DIV_ROUND_UP(chunksz, 4);
 839                buf = number(buf, end, val, spec);
 840
 841                chunksz = CHUNKSZ;
 842        }
 843        return buf;
 844}
 845
 846static noinline_for_stack
 847char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
 848                         struct printf_spec spec, const char *fmt)
 849{
 850        int nr_bits = max_t(int, spec.field_width, 0);
 851        /* current bit is 'cur', most recently seen range is [rbot, rtop] */
 852        int cur, rbot, rtop;
 853        bool first = true;
 854
 855        /* reused to print numbers */
 856        spec = (struct printf_spec){ .base = 10 };
 857
 858        rbot = cur = find_first_bit(bitmap, nr_bits);
 859        while (cur < nr_bits) {
 860                rtop = cur;
 861                cur = find_next_bit(bitmap, nr_bits, cur + 1);
 862                if (cur < nr_bits && cur <= rtop + 1)
 863                        continue;
 864
 865                if (!first) {
 866                        if (buf < end)
 867                                *buf = ',';
 868                        buf++;
 869                }
 870                first = false;
 871
 872                buf = number(buf, end, rbot, spec);
 873                if (rbot < rtop) {
 874                        if (buf < end)
 875                                *buf = '-';
 876                        buf++;
 877
 878                        buf = number(buf, end, rtop, spec);
 879                }
 880
 881                rbot = cur;
 882        }
 883        return buf;
 884}
 885
 886static noinline_for_stack
 887char *mac_address_string(char *buf, char *end, u8 *addr,
 888                         struct printf_spec spec, const char *fmt)
 889{
 890        char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
 891        char *p = mac_addr;
 892        int i;
 893        char separator;
 894        bool reversed = false;
 895
 896        switch (fmt[1]) {
 897        case 'F':
 898                separator = '-';
 899                break;
 900
 901        case 'R':
 902                reversed = true;
 903                /* fall through */
 904
 905        default:
 906                separator = ':';
 907                break;
 908        }
 909
 910        for (i = 0; i < 6; i++) {
 911                if (reversed)
 912                        p = hex_byte_pack(p, addr[5 - i]);
 913                else
 914                        p = hex_byte_pack(p, addr[i]);
 915
 916                if (fmt[0] == 'M' && i != 5)
 917                        *p++ = separator;
 918        }
 919        *p = '\0';
 920
 921        return string(buf, end, mac_addr, spec);
 922}
 923
 924static noinline_for_stack
 925char *ip4_string(char *p, const u8 *addr, const char *fmt)
 926{
 927        int i;
 928        bool leading_zeros = (fmt[0] == 'i');
 929        int index;
 930        int step;
 931
 932        switch (fmt[2]) {
 933        case 'h':
 934#ifdef __BIG_ENDIAN
 935                index = 0;
 936                step = 1;
 937#else
 938                index = 3;
 939                step = -1;
 940#endif
 941                break;
 942        case 'l':
 943                index = 3;
 944                step = -1;
 945                break;
 946        case 'n':
 947        case 'b':
 948        default:
 949                index = 0;
 950                step = 1;
 951                break;
 952        }
 953        for (i = 0; i < 4; i++) {
 954                char temp[3];   /* hold each IP quad in reverse order */
 955                int digits = put_dec_trunc8(temp, addr[index]) - temp;
 956                if (leading_zeros) {
 957                        if (digits < 3)
 958                                *p++ = '0';
 959                        if (digits < 2)
 960                                *p++ = '0';
 961                }
 962                /* reverse the digits in the quad */
 963                while (digits--)
 964                        *p++ = temp[digits];
 965                if (i < 3)
 966                        *p++ = '.';
 967                index += step;
 968        }
 969        *p = '\0';
 970
 971        return p;
 972}
 973
 974static noinline_for_stack
 975char *ip6_compressed_string(char *p, const char *addr)
 976{
 977        int i, j, range;
 978        unsigned char zerolength[8];
 979        int longest = 1;
 980        int colonpos = -1;
 981        u16 word;
 982        u8 hi, lo;
 983        bool needcolon = false;
 984        bool useIPv4;
 985        struct in6_addr in6;
 986
 987        memcpy(&in6, addr, sizeof(struct in6_addr));
 988
 989        useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
 990
 991        memset(zerolength, 0, sizeof(zerolength));
 992
 993        if (useIPv4)
 994                range = 6;
 995        else
 996                range = 8;
 997
 998        /* find position of longest 0 run */
 999        for (i = 0; i < range; i++) {
1000                for (j = i; j < range; j++) {
1001                        if (in6.s6_addr16[j] != 0)
1002                                break;
1003                        zerolength[i]++;
1004                }
1005        }
1006        for (i = 0; i < range; i++) {
1007                if (zerolength[i] > longest) {
1008                        longest = zerolength[i];
1009                        colonpos = i;
1010                }
1011        }
1012        if (longest == 1)               /* don't compress a single 0 */
1013                colonpos = -1;
1014
1015        /* emit address */
1016        for (i = 0; i < range; i++) {
1017                if (i == colonpos) {
1018                        if (needcolon || i == 0)
1019                                *p++ = ':';
1020                        *p++ = ':';
1021                        needcolon = false;
1022                        i += longest - 1;
1023                        continue;
1024                }
1025                if (needcolon) {
1026                        *p++ = ':';
1027                        needcolon = false;
1028                }
1029                /* hex u16 without leading 0s */
1030                word = ntohs(in6.s6_addr16[i]);
1031                hi = word >> 8;
1032                lo = word & 0xff;
1033                if (hi) {
1034                        if (hi > 0x0f)
1035                                p = hex_byte_pack(p, hi);
1036                        else
1037                                *p++ = hex_asc_lo(hi);
1038                        p = hex_byte_pack(p, lo);
1039                }
1040                else if (lo > 0x0f)
1041                        p = hex_byte_pack(p, lo);
1042                else
1043                        *p++ = hex_asc_lo(lo);
1044                needcolon = true;
1045        }
1046
1047        if (useIPv4) {
1048                if (needcolon)
1049                        *p++ = ':';
1050                p = ip4_string(p, &in6.s6_addr[12], "I4");
1051        }
1052        *p = '\0';
1053
1054        return p;
1055}
1056
1057static noinline_for_stack
1058char *ip6_string(char *p, const char *addr, const char *fmt)
1059{
1060        int i;
1061
1062        for (i = 0; i < 8; i++) {
1063                p = hex_byte_pack(p, *addr++);
1064                p = hex_byte_pack(p, *addr++);
1065                if (fmt[0] == 'I' && i != 7)
1066                        *p++ = ':';
1067        }
1068        *p = '\0';
1069
1070        return p;
1071}
1072
1073static noinline_for_stack
1074char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1075                      struct printf_spec spec, const char *fmt)
1076{
1077        char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1078
1079        if (fmt[0] == 'I' && fmt[2] == 'c')
1080                ip6_compressed_string(ip6_addr, addr);
1081        else
1082                ip6_string(ip6_addr, addr, fmt);
1083
1084        return string(buf, end, ip6_addr, spec);
1085}
1086
1087static noinline_for_stack
1088char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1089                      struct printf_spec spec, const char *fmt)
1090{
1091        char ip4_addr[sizeof("255.255.255.255")];
1092
1093        ip4_string(ip4_addr, addr, fmt);
1094
1095        return string(buf, end, ip4_addr, spec);
1096}
1097
1098static noinline_for_stack
1099char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1100                         struct printf_spec spec, const char *fmt)
1101{
1102        bool have_p = false, have_s = false, have_f = false, have_c = false;
1103        char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1104                      sizeof(":12345") + sizeof("/123456789") +
1105                      sizeof("%1234567890")];
1106        char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1107        const u8 *addr = (const u8 *) &sa->sin6_addr;
1108        char fmt6[2] = { fmt[0], '6' };
1109        u8 off = 0;
1110
1111        fmt++;
1112        while (isalpha(*++fmt)) {
1113                switch (*fmt) {
1114                case 'p':
1115                        have_p = true;
1116                        break;
1117                case 'f':
1118                        have_f = true;
1119                        break;
1120                case 's':
1121                        have_s = true;
1122                        break;
1123                case 'c':
1124                        have_c = true;
1125                        break;
1126                }
1127        }
1128
1129        if (have_p || have_s || have_f) {
1130                *p = '[';
1131                off = 1;
1132        }
1133
1134        if (fmt6[0] == 'I' && have_c)
1135                p = ip6_compressed_string(ip6_addr + off, addr);
1136        else
1137                p = ip6_string(ip6_addr + off, addr, fmt6);
1138
1139        if (have_p || have_s || have_f)
1140                *p++ = ']';
1141
1142        if (have_p) {
1143                *p++ = ':';
1144                p = number(p, pend, ntohs(sa->sin6_port), spec);
1145        }
1146        if (have_f) {
1147                *p++ = '/';
1148                p = number(p, pend, ntohl(sa->sin6_flowinfo &
1149                                          IPV6_FLOWINFO_MASK), spec);
1150        }
1151        if (have_s) {
1152                *p++ = '%';
1153                p = number(p, pend, sa->sin6_scope_id, spec);
1154        }
1155        *p = '\0';
1156
1157        return string(buf, end, ip6_addr, spec);
1158}
1159
1160static noinline_for_stack
1161char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1162                         struct printf_spec spec, const char *fmt)
1163{
1164        bool have_p = false;
1165        char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1166        char *pend = ip4_addr + sizeof(ip4_addr);
1167        const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1168        char fmt4[3] = { fmt[0], '4', 0 };
1169
1170        fmt++;
1171        while (isalpha(*++fmt)) {
1172                switch (*fmt) {
1173                case 'p':
1174                        have_p = true;
1175                        break;
1176                case 'h':
1177                case 'l':
1178                case 'n':
1179                case 'b':
1180                        fmt4[2] = *fmt;
1181                        break;
1182                }
1183        }
1184
1185        p = ip4_string(ip4_addr, addr, fmt4);
1186        if (have_p) {
1187                *p++ = ':';
1188                p = number(p, pend, ntohs(sa->sin_port), spec);
1189        }
1190        *p = '\0';
1191
1192        return string(buf, end, ip4_addr, spec);
1193}
1194
1195static noinline_for_stack
1196char *uuid_string(char *buf, char *end, const u8 *addr,
1197                  struct printf_spec spec, const char *fmt)
1198{
1199        char uuid[UUID_STRING_LEN + 1];
1200        char *p = uuid;
1201        int i;
1202        const u8 *index = uuid_index;
1203        bool uc = false;
1204
1205        switch (*(++fmt)) {
1206        case 'L':
1207                uc = true;              /* fall-through */
1208        case 'l':
1209                index = guid_index;
1210                break;
1211        case 'B':
1212                uc = true;
1213                break;
1214        }
1215
1216        for (i = 0; i < 16; i++) {
1217                p = hex_byte_pack(p, addr[index[i]]);
1218                switch (i) {
1219                case 3:
1220                case 5:
1221                case 7:
1222                case 9:
1223                        *p++ = '-';
1224                        break;
1225                }
1226        }
1227
1228        *p = 0;
1229
1230        if (uc) {
1231                p = uuid;
1232                do {
1233                        *p = toupper(*p);
1234                } while (*(++p));
1235        }
1236
1237        return string(buf, end, uuid, spec);
1238}
1239
1240static
1241char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1242                      struct printf_spec spec)
1243{
1244        spec.flags |= SPECIAL | SMALL | ZEROPAD;
1245        if (spec.field_width == -1)
1246                spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1247        spec.base = 16;
1248
1249        return number(buf, end, *(const netdev_features_t *)addr, spec);
1250}
1251
1252int kptr_restrict __read_mostly;
1253
1254/*
1255 * Show a '%p' thing.  A kernel extension is that the '%p' is followed
1256 * by an extra set of alphanumeric characters that are extended format
1257 * specifiers.
1258 *
1259 * Right now we handle:
1260 *
1261 * - 'F' For symbolic function descriptor pointers with offset
1262 * - 'f' For simple symbolic function names without offset
1263 * - 'S' For symbolic direct pointers with offset
1264 * - 's' For symbolic direct pointers without offset
1265 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1266 * - 'B' For backtraced symbolic direct pointers with offset
1267 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1268 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1269 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1270 *       width which must be explicitly specified either as part of the
1271 *       format string '%32b[l]' or through '%*b[l]', [l] selects
1272 *       range-list format instead of hex format
1273 * - 'M' For a 6-byte MAC address, it prints the address in the
1274 *       usual colon-separated hex notation
1275 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1276 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1277 *       with a dash-separated hex notation
1278 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1279 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1280 *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1281 *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
1282 *       [S][pfs]
1283 *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1284 *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1285 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1286 *       IPv6 omits the colons (01020304...0f)
1287 *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1288 *       [S][pfs]
1289 *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1290 *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1291 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1292 * - 'I[6S]c' for IPv6 addresses printed as specified by
1293 *       http://tools.ietf.org/html/rfc5952
1294 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1295 *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1296 *       Options for %pU are:
1297 *         b big endian lower case hex (default)
1298 *         B big endian UPPER case hex
1299 *         l little endian lower case hex
1300 *         L little endian UPPER case hex
1301 *           big endian output byte order is:
1302 *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1303 *           little endian output byte order is:
1304 *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1305 * - 'V' For a struct va_format which contains a format string * and va_list *,
1306 *       call vsnprintf(->format, *->va_list).
1307 *       Implements a "recursive vsnprintf".
1308 *       Do not use this feature without some mechanism to verify the
1309 *       correctness of the format string and va_list arguments.
1310 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1311 * - 'NF' For a netdev_features_t
1312 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1313 *            a certain separator (' ' by default):
1314 *              C colon
1315 *              D dash
1316 *              N no separator
1317 *            The maximum supported length is 64 bytes of the input. Consider
1318 *            to use print_hex_dump() for the larger input.
1319 * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
1320 * - 'c' For a cpumask list
1321 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1322 * - 'D[234]' Same as 'd' but for a struct file
1323 *
1324 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1325 * function pointers are really function descriptors, which contain a
1326 * pointer to the real address.
1327 */
1328static noinline_for_stack
1329char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1330              struct printf_spec spec)
1331{
1332        int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1333
1334        if (!ptr && *fmt != 'K') {
1335                /*
1336                 * Print (null) with the same width as a pointer so it makes
1337                 * tabular output look nice.
1338                 */
1339                if (spec.field_width == -1)
1340                        spec.field_width = default_width;
1341                return string(buf, end, "(null)", spec);
1342        }
1343
1344        switch (*fmt) {
1345        case 'F':
1346        case 'f':
1347                ptr = dereference_function_descriptor(ptr);
1348                /* Fallthrough */
1349        case 'S':
1350        case 's':
1351        case 'B':
1352                return symbol_string(buf, end, ptr, spec, fmt);
1353        case 'R':
1354        case 'r':
1355                return resource_string(buf, end, ptr, spec, fmt);
1356        case 'h':
1357                return hex_string(buf, end, ptr, spec, fmt);
1358        case 'b':
1359                switch (fmt[1]) {
1360                case 'l':
1361                        return bitmap_list_string(buf, end, ptr, spec, fmt);
1362                default:
1363                        return bitmap_string(buf, end, ptr, spec, fmt);
1364                }
1365        case 'M':                       /* Colon separated: 00:01:02:03:04:05 */
1366        case 'm':                       /* Contiguous: 000102030405 */
1367                                        /* [mM]F (FDDI) */
1368                                        /* [mM]R (Reverse order; Bluetooth) */
1369                return mac_address_string(buf, end, ptr, spec, fmt);
1370        case 'I':                       /* Formatted IP supported
1371                                         * 4:   1.2.3.4
1372                                         * 6:   0001:0203:...:0708
1373                                         * 6c:  1::708 or 1::1.2.3.4
1374                                         */
1375        case 'i':                       /* Contiguous:
1376                                         * 4:   001.002.003.004
1377                                         * 6:   000102...0f
1378                                         */
1379                switch (fmt[1]) {
1380                case '6':
1381                        return ip6_addr_string(buf, end, ptr, spec, fmt);
1382                case '4':
1383                        return ip4_addr_string(buf, end, ptr, spec, fmt);
1384                case 'S': {
1385                        const union {
1386                                struct sockaddr         raw;
1387                                struct sockaddr_in      v4;
1388                                struct sockaddr_in6     v6;
1389                        } *sa = ptr;
1390
1391                        switch (sa->raw.sa_family) {
1392                        case AF_INET:
1393                                return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1394                        case AF_INET6:
1395                                return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1396                        default:
1397                                return string(buf, end, "(invalid address)", spec);
1398                        }}
1399                }
1400                break;
1401        case 'U':
1402                return uuid_string(buf, end, ptr, spec, fmt);
1403        case 'V':
1404                {
1405                        va_list va;
1406
1407                        va_copy(va, *((struct va_format *)ptr)->va);
1408                        buf += vsnprintf(buf, end > buf ? end - buf : 0,
1409                                         ((struct va_format *)ptr)->fmt, va);
1410                        va_end(va);
1411                        return buf;
1412                }
1413        case 'K':
1414                /*
1415                 * %pK cannot be used in IRQ context because its test
1416                 * for CAP_SYSLOG would be meaningless.
1417                 */
1418                if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1419                                      in_nmi())) {
1420                        if (spec.field_width == -1)
1421                                spec.field_width = default_width;
1422                        return string(buf, end, "pK-error", spec);
1423                }
1424
1425                switch (kptr_restrict) {
1426                case 0:
1427                        /* Always print %pK values */
1428                        break;
1429                case 1: {
1430                        /*
1431                         * Only print the real pointer value if the current
1432                         * process has CAP_SYSLOG and is running with the
1433                         * same credentials it started with. This is because
1434                         * access to files is checked at open() time, but %pK
1435                         * checks permission at read() time. We don't want to
1436                         * leak pointer values if a binary opens a file using
1437                         * %pK and then elevates privileges before reading it.
1438                         */
1439                        const struct cred *cred = current_cred();
1440
1441                        if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1442                            !uid_eq(cred->euid, cred->uid) ||
1443                            !gid_eq(cred->egid, cred->gid))
1444                                ptr = NULL;
1445                        break;
1446                }
1447                case 2:
1448                default:
1449                        /* Always print 0's for %pK */
1450                        ptr = NULL;
1451                        break;
1452                }
1453                break;
1454
1455        case 'N':
1456                switch (fmt[1]) {
1457                case 'F':
1458                        return netdev_feature_string(buf, end, ptr, spec);
1459                }
1460                break;
1461        case 'a':
1462                spec.flags |= SPECIAL | SMALL | ZEROPAD;
1463                spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1464                spec.base = 16;
1465                return number(buf, end,
1466                              (unsigned long long) *((phys_addr_t *)ptr), spec);
1467        case 'c':
1468                return buf + cpulist_scnprintf(buf, end - buf, ptr);
1469        case 'd':
1470                return dentry_name(buf, end, ptr, spec, fmt);
1471        case 'D':
1472                return dentry_name(buf, end,
1473                                   ((const struct file *)ptr)->f_path.dentry,
1474                                   spec, fmt);
1475        }
1476        spec.flags |= SMALL;
1477        if (spec.field_width == -1) {
1478                spec.field_width = default_width;
1479                spec.flags |= ZEROPAD;
1480        }
1481        spec.base = 16;
1482
1483        return number(buf, end, (unsigned long) ptr, spec);
1484}
1485
1486/*
1487 * Helper function to decode printf style format.
1488 * Each call decode a token from the format and return the
1489 * number of characters read (or likely the delta where it wants
1490 * to go on the next call).
1491 * The decoded token is returned through the parameters
1492 *
1493 * 'h', 'l', or 'L' for integer fields
1494 * 'z' support added 23/7/1999 S.H.
1495 * 'z' changed to 'Z' --davidm 1/25/99
1496 * 't' added for ptrdiff_t
1497 *
1498 * @fmt: the format string
1499 * @type of the token returned
1500 * @flags: various flags such as +, -, # tokens..
1501 * @field_width: overwritten width
1502 * @base: base of the number (octal, hex, ...)
1503 * @precision: precision of a number
1504 * @qualifier: qualifier of a number (long, size_t, ...)
1505 */
1506static noinline_for_stack
1507int format_decode(const char *fmt, struct printf_spec *spec)
1508{
1509        const char *start = fmt;
1510
1511        /* we finished early by reading the field width */
1512        if (spec->type == FORMAT_TYPE_WIDTH) {
1513                if (spec->field_width < 0) {
1514                        spec->field_width = -spec->field_width;
1515                        spec->flags |= LEFT;
1516                }
1517                spec->type = FORMAT_TYPE_NONE;
1518                goto precision;
1519        }
1520
1521        /* we finished early by reading the precision */
1522        if (spec->type == FORMAT_TYPE_PRECISION) {
1523                if (spec->precision < 0)
1524                        spec->precision = 0;
1525
1526                spec->type = FORMAT_TYPE_NONE;
1527                goto qualifier;
1528        }
1529
1530        /* By default */
1531        spec->type = FORMAT_TYPE_NONE;
1532
1533        for (; *fmt ; ++fmt) {
1534                if (*fmt == '%')
1535                        break;
1536        }
1537
1538        /* Return the current non-format string */
1539        if (fmt != start || !*fmt)
1540                return fmt - start;
1541
1542        /* Process flags */
1543        spec->flags = 0;
1544
1545        while (1) { /* this also skips first '%' */
1546                bool found = true;
1547
1548                ++fmt;
1549
1550                switch (*fmt) {
1551                case '-': spec->flags |= LEFT;    break;
1552                case '+': spec->flags |= PLUS;    break;
1553                case ' ': spec->flags |= SPACE;   break;
1554                case '#': spec->flags |= SPECIAL; break;
1555                case '0': spec->flags |= ZEROPAD; break;
1556                default:  found = false;
1557                }
1558
1559                if (!found)
1560                        break;
1561        }
1562
1563        /* get field width */
1564        spec->field_width = -1;
1565
1566        if (isdigit(*fmt))
1567                spec->field_width = skip_atoi(&fmt);
1568        else if (*fmt == '*') {
1569                /* it's the next argument */
1570                spec->type = FORMAT_TYPE_WIDTH;
1571                return ++fmt - start;
1572        }
1573
1574precision:
1575        /* get the precision */
1576        spec->precision = -1;
1577        if (*fmt == '.') {
1578                ++fmt;
1579                if (isdigit(*fmt)) {
1580                        spec->precision = skip_atoi(&fmt);
1581                        if (spec->precision < 0)
1582                                spec->precision = 0;
1583                } else if (*fmt == '*') {
1584                        /* it's the next argument */
1585                        spec->type = FORMAT_TYPE_PRECISION;
1586                        return ++fmt - start;
1587                }
1588        }
1589
1590qualifier:
1591        /* get the conversion qualifier */
1592        spec->qualifier = -1;
1593        if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1594            _tolower(*fmt) == 'z' || *fmt == 't') {
1595                spec->qualifier = *fmt++;
1596                if (unlikely(spec->qualifier == *fmt)) {
1597                        if (spec->qualifier == 'l') {
1598                                spec->qualifier = 'L';
1599                                ++fmt;
1600                        } else if (spec->qualifier == 'h') {
1601                                spec->qualifier = 'H';
1602                                ++fmt;
1603                        }
1604                }
1605        }
1606
1607        /* default base */
1608        spec->base = 10;
1609        switch (*fmt) {
1610        case 'c':
1611                spec->type = FORMAT_TYPE_CHAR;
1612                return ++fmt - start;
1613
1614        case 's':
1615                spec->type = FORMAT_TYPE_STR;
1616                return ++fmt - start;
1617
1618        case 'p':
1619                spec->type = FORMAT_TYPE_PTR;
1620                return fmt - start;
1621                /* skip alnum */
1622
1623        case 'n':
1624                spec->type = FORMAT_TYPE_NRCHARS;
1625                return ++fmt - start;
1626
1627        case '%':
1628                spec->type = FORMAT_TYPE_PERCENT_CHAR;
1629                return ++fmt - start;
1630
1631        /* integer number formats - set up the flags and "break" */
1632        case 'o':
1633                spec->base = 8;
1634                break;
1635
1636        case 'x':
1637                spec->flags |= SMALL;
1638
1639        case 'X':
1640                spec->base = 16;
1641                break;
1642
1643        case 'd':
1644        case 'i':
1645                spec->flags |= SIGN;
1646        case 'u':
1647                break;
1648
1649        default:
1650                spec->type = FORMAT_TYPE_INVALID;
1651                return fmt - start;
1652        }
1653
1654        if (spec->qualifier == 'L')
1655                spec->type = FORMAT_TYPE_LONG_LONG;
1656        else if (spec->qualifier == 'l') {
1657                if (spec->flags & SIGN)
1658                        spec->type = FORMAT_TYPE_LONG;
1659                else
1660                        spec->type = FORMAT_TYPE_ULONG;
1661        } else if (_tolower(spec->qualifier) == 'z') {
1662                spec->type = FORMAT_TYPE_SIZE_T;
1663        } else if (spec->qualifier == 't') {
1664                spec->type = FORMAT_TYPE_PTRDIFF;
1665        } else if (spec->qualifier == 'H') {
1666                if (spec->flags & SIGN)
1667                        spec->type = FORMAT_TYPE_BYTE;
1668                else
1669                        spec->type = FORMAT_TYPE_UBYTE;
1670        } else if (spec->qualifier == 'h') {
1671                if (spec->flags & SIGN)
1672                        spec->type = FORMAT_TYPE_SHORT;
1673                else
1674                        spec->type = FORMAT_TYPE_USHORT;
1675        } else {
1676                if (spec->flags & SIGN)
1677                        spec->type = FORMAT_TYPE_INT;
1678                else
1679                        spec->type = FORMAT_TYPE_UINT;
1680        }
1681
1682        return ++fmt - start;
1683}
1684
1685/**
1686 * vsnprintf - Format a string and place it in a buffer
1687 * @buf: The buffer to place the result into
1688 * @size: The size of the buffer, including the trailing null space
1689 * @fmt: The format string to use
1690 * @args: Arguments for the format string
1691 *
1692 * This function follows C99 vsnprintf, but has some extensions:
1693 * %pS output the name of a text symbol with offset
1694 * %ps output the name of a text symbol without offset
1695 * %pF output the name of a function pointer with its offset
1696 * %pf output the name of a function pointer without its offset
1697 * %pB output the name of a backtrace symbol with its offset
1698 * %pR output the address range in a struct resource with decoded flags
1699 * %pr output the address range in a struct resource with raw flags
1700 * %pb output the bitmap with field width as the number of bits
1701 * %pbl output the bitmap as range list with field width as the number of bits
1702 * %pM output a 6-byte MAC address with colons
1703 * %pMR output a 6-byte MAC address with colons in reversed order
1704 * %pMF output a 6-byte MAC address with dashes
1705 * %pm output a 6-byte MAC address without colons
1706 * %pmR output a 6-byte MAC address without colons in reversed order
1707 * %pI4 print an IPv4 address without leading zeros
1708 * %pi4 print an IPv4 address with leading zeros
1709 * %pI6 print an IPv6 address with colons
1710 * %pi6 print an IPv6 address without colons
1711 * %pI6c print an IPv6 address as specified by RFC 5952
1712 * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1713 * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1714 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1715 *   case.
1716 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1717 *           bytes of the input)
1718 * %pc print a cpumask as comma-separated list
1719 * %n is ignored
1720 *
1721 * ** Please update Documentation/printk-formats.txt when making changes **
1722 *
1723 * The return value is the number of characters which would
1724 * be generated for the given input, excluding the trailing
1725 * '\0', as per ISO C99. If you want to have the exact
1726 * number of characters written into @buf as return value
1727 * (not including the trailing '\0'), use vscnprintf(). If the
1728 * return is greater than or equal to @size, the resulting
1729 * string is truncated.
1730 *
1731 * If you're not already dealing with a va_list consider using snprintf().
1732 */
1733int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1734{
1735        unsigned long long num;
1736        char *str, *end;
1737        struct printf_spec spec = {0};
1738
1739        /* Reject out-of-range values early.  Large positive sizes are
1740           used for unknown buffer sizes. */
1741        if (WARN_ON_ONCE((int) size < 0))
1742                return 0;
1743
1744        str = buf;
1745        end = buf + size;
1746
1747        /* Make sure end is always >= buf */
1748        if (end < buf) {
1749                end = ((void *)-1);
1750                size = end - buf;
1751        }
1752
1753        while (*fmt) {
1754                const char *old_fmt = fmt;
1755                int read = format_decode(fmt, &spec);
1756
1757                fmt += read;
1758
1759                switch (spec.type) {
1760                case FORMAT_TYPE_NONE: {
1761                        int copy = read;
1762                        if (str < end) {
1763                                if (copy > end - str)
1764                                        copy = end - str;
1765                                memcpy(str, old_fmt, copy);
1766                        }
1767                        str += read;
1768                        break;
1769                }
1770
1771                case FORMAT_TYPE_WIDTH:
1772                        spec.field_width = va_arg(args, int);
1773                        break;
1774
1775                case FORMAT_TYPE_PRECISION:
1776                        spec.precision = va_arg(args, int);
1777                        break;
1778
1779                case FORMAT_TYPE_CHAR: {
1780                        char c;
1781
1782                        if (!(spec.flags & LEFT)) {
1783                                while (--spec.field_width > 0) {
1784                                        if (str < end)
1785                                                *str = ' ';
1786                                        ++str;
1787
1788                                }
1789                        }
1790                        c = (unsigned char) va_arg(args, int);
1791                        if (str < end)
1792                                *str = c;
1793                        ++str;
1794                        while (--spec.field_width > 0) {
1795                                if (str < end)
1796                                        *str = ' ';
1797                                ++str;
1798                        }
1799                        break;
1800                }
1801
1802                case FORMAT_TYPE_STR:
1803                        str = string(str, end, va_arg(args, char *), spec);
1804                        break;
1805
1806                case FORMAT_TYPE_PTR:
1807                        str = pointer(fmt+1, str, end, va_arg(args, void *),
1808                                      spec);
1809                        while (isalnum(*fmt))
1810                                fmt++;
1811                        break;
1812
1813                case FORMAT_TYPE_PERCENT_CHAR:
1814                        if (str < end)
1815                                *str = '%';
1816                        ++str;
1817                        break;
1818
1819                case FORMAT_TYPE_INVALID:
1820                        if (str < end)
1821                                *str = '%';
1822                        ++str;
1823                        break;
1824
1825                case FORMAT_TYPE_NRCHARS: {
1826                        u8 qualifier = spec.qualifier;
1827
1828                        if (qualifier == 'l') {
1829                                long *ip = va_arg(args, long *);
1830                                *ip = (str - buf);
1831                        } else if (_tolower(qualifier) == 'z') {
1832                                size_t *ip = va_arg(args, size_t *);
1833                                *ip = (str - buf);
1834                        } else {
1835                                int *ip = va_arg(args, int *);
1836                                *ip = (str - buf);
1837                        }
1838                        break;
1839                }
1840
1841                default:
1842                        switch (spec.type) {
1843                        case FORMAT_TYPE_LONG_LONG:
1844                                num = va_arg(args, long long);
1845                                break;
1846                        case FORMAT_TYPE_ULONG:
1847                                num = va_arg(args, unsigned long);
1848                                break;
1849                        case FORMAT_TYPE_LONG:
1850                                num = va_arg(args, long);
1851                                break;
1852                        case FORMAT_TYPE_SIZE_T:
1853                                if (spec.flags & SIGN)
1854                                        num = va_arg(args, ssize_t);
1855                                else
1856                                        num = va_arg(args, size_t);
1857                                break;
1858                        case FORMAT_TYPE_PTRDIFF:
1859                                num = va_arg(args, ptrdiff_t);
1860                                break;
1861                        case FORMAT_TYPE_UBYTE:
1862                                num = (unsigned char) va_arg(args, int);
1863                                break;
1864                        case FORMAT_TYPE_BYTE:
1865                                num = (signed char) va_arg(args, int);
1866                                break;
1867                        case FORMAT_TYPE_USHORT:
1868                                num = (unsigned short) va_arg(args, int);
1869                                break;
1870                        case FORMAT_TYPE_SHORT:
1871                                num = (short) va_arg(args, int);
1872                                break;
1873                        case FORMAT_TYPE_INT:
1874                                num = (int) va_arg(args, int);
1875                                break;
1876                        default:
1877                                num = va_arg(args, unsigned int);
1878                        }
1879
1880                        str = number(str, end, num, spec);
1881                }
1882        }
1883
1884        if (size > 0) {
1885                if (str < end)
1886                        *str = '\0';
1887                else
1888                        end[-1] = '\0';
1889        }
1890
1891        /* the trailing null byte doesn't count towards the total */
1892        return str-buf;
1893
1894}
1895EXPORT_SYMBOL(vsnprintf);
1896
1897/**
1898 * vscnprintf - Format a string and place it in a buffer
1899 * @buf: The buffer to place the result into
1900 * @size: The size of the buffer, including the trailing null space
1901 * @fmt: The format string to use
1902 * @args: Arguments for the format string
1903 *
1904 * The return value is the number of characters which have been written into
1905 * the @buf not including the trailing '\0'. If @size is == 0 the function
1906 * returns 0.
1907 *
1908 * If you're not already dealing with a va_list consider using scnprintf().
1909 *
1910 * See the vsnprintf() documentation for format string extensions over C99.
1911 */
1912int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1913{
1914        int i;
1915
1916        i = vsnprintf(buf, size, fmt, args);
1917
1918        if (likely(i < size))
1919                return i;
1920        if (size != 0)
1921                return size - 1;
1922        return 0;
1923}
1924EXPORT_SYMBOL(vscnprintf);
1925
1926/**
1927 * snprintf - Format a string and place it in a buffer
1928 * @buf: The buffer to place the result into
1929 * @size: The size of the buffer, including the trailing null space
1930 * @fmt: The format string to use
1931 * @...: Arguments for the format string
1932 *
1933 * The return value is the number of characters which would be
1934 * generated for the given input, excluding the trailing null,
1935 * as per ISO C99.  If the return is greater than or equal to
1936 * @size, the resulting string is truncated.
1937 *
1938 * See the vsnprintf() documentation for format string extensions over C99.
1939 */
1940int snprintf(char *buf, size_t size, const char *fmt, ...)
1941{
1942        va_list args;
1943        int i;
1944
1945        va_start(args, fmt);
1946        i = vsnprintf(buf, size, fmt, args);
1947        va_end(args);
1948
1949        return i;
1950}
1951EXPORT_SYMBOL(snprintf);
1952
1953/**
1954 * scnprintf - Format a string and place it in a buffer
1955 * @buf: The buffer to place the result into
1956 * @size: The size of the buffer, including the trailing null space
1957 * @fmt: The format string to use
1958 * @...: Arguments for the format string
1959 *
1960 * The return value is the number of characters written into @buf not including
1961 * the trailing '\0'. If @size is == 0 the function returns 0.
1962 */
1963
1964int scnprintf(char *buf, size_t size, const char *fmt, ...)
1965{
1966        va_list args;
1967        int i;
1968
1969        va_start(args, fmt);
1970        i = vscnprintf(buf, size, fmt, args);
1971        va_end(args);
1972
1973        return i;
1974}
1975EXPORT_SYMBOL(scnprintf);
1976
1977/**
1978 * vsprintf - Format a string and place it in a buffer
1979 * @buf: The buffer to place the result into
1980 * @fmt: The format string to use
1981 * @args: Arguments for the format string
1982 *
1983 * The function returns the number of characters written
1984 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1985 * buffer overflows.
1986 *
1987 * If you're not already dealing with a va_list consider using sprintf().
1988 *
1989 * See the vsnprintf() documentation for format string extensions over C99.
1990 */
1991int vsprintf(char *buf, const char *fmt, va_list args)
1992{
1993        return vsnprintf(buf, INT_MAX, fmt, args);
1994}
1995EXPORT_SYMBOL(vsprintf);
1996
1997/**
1998 * sprintf - Format a string and place it in a buffer
1999 * @buf: The buffer to place the result into
2000 * @fmt: The format string to use
2001 * @...: Arguments for the format string
2002 *
2003 * The function returns the number of characters written
2004 * into @buf. Use snprintf() or scnprintf() in order to avoid
2005 * buffer overflows.
2006 *
2007 * See the vsnprintf() documentation for format string extensions over C99.
2008 */
2009int sprintf(char *buf, const char *fmt, ...)
2010{
2011        va_list args;
2012        int i;
2013
2014        va_start(args, fmt);
2015        i = vsnprintf(buf, INT_MAX, fmt, args);
2016        va_end(args);
2017
2018        return i;
2019}
2020EXPORT_SYMBOL(sprintf);
2021
2022#ifdef CONFIG_BINARY_PRINTF
2023/*
2024 * bprintf service:
2025 * vbin_printf() - VA arguments to binary data
2026 * bstr_printf() - Binary data to text string
2027 */
2028
2029/**
2030 * vbin_printf - Parse a format string and place args' binary value in a buffer
2031 * @bin_buf: The buffer to place args' binary value
2032 * @size: The size of the buffer(by words(32bits), not characters)
2033 * @fmt: The format string to use
2034 * @args: Arguments for the format string
2035 *
2036 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2037 * is skiped.
2038 *
2039 * The return value is the number of words(32bits) which would be generated for
2040 * the given input.
2041 *
2042 * NOTE:
2043 * If the return value is greater than @size, the resulting bin_buf is NOT
2044 * valid for bstr_printf().
2045 */
2046int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2047{
2048        struct printf_spec spec = {0};
2049        char *str, *end;
2050
2051        str = (char *)bin_buf;
2052        end = (char *)(bin_buf + size);
2053
2054#define save_arg(type)                                                  \
2055do {                                                                    \
2056        if (sizeof(type) == 8) {                                        \
2057                unsigned long long value;                               \
2058                str = PTR_ALIGN(str, sizeof(u32));                      \
2059                value = va_arg(args, unsigned long long);               \
2060                if (str + sizeof(type) <= end) {                        \
2061                        *(u32 *)str = *(u32 *)&value;                   \
2062                        *(u32 *)(str + 4) = *((u32 *)&value + 1);       \
2063                }                                                       \
2064        } else {                                                        \
2065                unsigned long value;                                    \
2066                str = PTR_ALIGN(str, sizeof(type));                     \
2067                value = va_arg(args, int);                              \
2068                if (str + sizeof(type) <= end)                          \
2069                        *(typeof(type) *)str = (type)value;             \
2070        }                                                               \
2071        str += sizeof(type);                                            \
2072} while (0)
2073
2074        while (*fmt) {
2075                int read = format_decode(fmt, &spec);
2076
2077                fmt += read;
2078
2079                switch (spec.type) {
2080                case FORMAT_TYPE_NONE:
2081                case FORMAT_TYPE_INVALID:
2082                case FORMAT_TYPE_PERCENT_CHAR:
2083                        break;
2084
2085                case FORMAT_TYPE_WIDTH:
2086                case FORMAT_TYPE_PRECISION:
2087                        save_arg(int);
2088                        break;
2089
2090                case FORMAT_TYPE_CHAR:
2091                        save_arg(char);
2092                        break;
2093
2094                case FORMAT_TYPE_STR: {
2095                        const char *save_str = va_arg(args, char *);
2096                        size_t len;
2097
2098                        if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2099                                        || (unsigned long)save_str < PAGE_SIZE)
2100                                save_str = "(null)";
2101                        len = strlen(save_str) + 1;
2102                        if (str + len < end)
2103                                memcpy(str, save_str, len);
2104                        str += len;
2105                        break;
2106                }
2107
2108                case FORMAT_TYPE_PTR:
2109                        save_arg(void *);
2110                        /* skip all alphanumeric pointer suffixes */
2111                        while (isalnum(*fmt))
2112                                fmt++;
2113                        break;
2114
2115                case FORMAT_TYPE_NRCHARS: {
2116                        /* skip %n 's argument */
2117                        u8 qualifier = spec.qualifier;
2118                        void *skip_arg;
2119                        if (qualifier == 'l')
2120                                skip_arg = va_arg(args, long *);
2121                        else if (_tolower(qualifier) == 'z')
2122                                skip_arg = va_arg(args, size_t *);
2123                        else
2124                                skip_arg = va_arg(args, int *);
2125                        break;
2126                }
2127
2128                default:
2129                        switch (spec.type) {
2130
2131                        case FORMAT_TYPE_LONG_LONG:
2132                                save_arg(long long);
2133                                break;
2134                        case FORMAT_TYPE_ULONG:
2135                        case FORMAT_TYPE_LONG:
2136                                save_arg(unsigned long);
2137                                break;
2138                        case FORMAT_TYPE_SIZE_T:
2139                                save_arg(size_t);
2140                                break;
2141                        case FORMAT_TYPE_PTRDIFF:
2142                                save_arg(ptrdiff_t);
2143                                break;
2144                        case FORMAT_TYPE_UBYTE:
2145                        case FORMAT_TYPE_BYTE:
2146                                save_arg(char);
2147                                break;
2148                        case FORMAT_TYPE_USHORT:
2149                        case FORMAT_TYPE_SHORT:
2150                                save_arg(short);
2151                                break;
2152                        default:
2153                                save_arg(int);
2154                        }
2155                }
2156        }
2157
2158        return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2159#undef save_arg
2160}
2161EXPORT_SYMBOL_GPL(vbin_printf);
2162
2163/**
2164 * bstr_printf - Format a string from binary arguments and place it in a buffer
2165 * @buf: The buffer to place the result into
2166 * @size: The size of the buffer, including the trailing null space
2167 * @fmt: The format string to use
2168 * @bin_buf: Binary arguments for the format string
2169 *
2170 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2171 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2172 * a binary buffer that generated by vbin_printf.
2173 *
2174 * The format follows C99 vsnprintf, but has some extensions:
2175 *  see vsnprintf comment for details.
2176 *
2177 * The return value is the number of characters which would
2178 * be generated for the given input, excluding the trailing
2179 * '\0', as per ISO C99. If you want to have the exact
2180 * number of characters written into @buf as return value
2181 * (not including the trailing '\0'), use vscnprintf(). If the
2182 * return is greater than or equal to @size, the resulting
2183 * string is truncated.
2184 */
2185int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2186{
2187        struct printf_spec spec = {0};
2188        char *str, *end;
2189        const char *args = (const char *)bin_buf;
2190
2191        if (WARN_ON_ONCE((int) size < 0))
2192                return 0;
2193
2194        str = buf;
2195        end = buf + size;
2196
2197#define get_arg(type)                                                   \
2198({                                                                      \
2199        typeof(type) value;                                             \
2200        if (sizeof(type) == 8) {                                        \
2201                args = PTR_ALIGN(args, sizeof(u32));                    \
2202                *(u32 *)&value = *(u32 *)args;                          \
2203                *((u32 *)&value + 1) = *(u32 *)(args + 4);              \
2204        } else {                                                        \
2205                args = PTR_ALIGN(args, sizeof(type));                   \
2206                value = *(typeof(type) *)args;                          \
2207        }                                                               \
2208        args += sizeof(type);                                           \
2209        value;                                                          \
2210})
2211
2212        /* Make sure end is always >= buf */
2213        if (end < buf) {
2214                end = ((void *)-1);
2215                size = end - buf;
2216        }
2217
2218        while (*fmt) {
2219                const char *old_fmt = fmt;
2220                int read = format_decode(fmt, &spec);
2221
2222                fmt += read;
2223
2224                switch (spec.type) {
2225                case FORMAT_TYPE_NONE: {
2226                        int copy = read;
2227                        if (str < end) {
2228                                if (copy > end - str)
2229                                        copy = end - str;
2230                                memcpy(str, old_fmt, copy);
2231                        }
2232                        str += read;
2233                        break;
2234                }
2235
2236                case FORMAT_TYPE_WIDTH:
2237                        spec.field_width = get_arg(int);
2238                        break;
2239
2240                case FORMAT_TYPE_PRECISION:
2241                        spec.precision = get_arg(int);
2242                        break;
2243
2244                case FORMAT_TYPE_CHAR: {
2245                        char c;
2246
2247                        if (!(spec.flags & LEFT)) {
2248                                while (--spec.field_width > 0) {
2249                                        if (str < end)
2250                                                *str = ' ';
2251                                        ++str;
2252                                }
2253                        }
2254                        c = (unsigned char) get_arg(char);
2255                        if (str < end)
2256                                *str = c;
2257                        ++str;
2258                        while (--spec.field_width > 0) {
2259                                if (str < end)
2260                                        *str = ' ';
2261                                ++str;
2262                        }
2263                        break;
2264                }
2265
2266                case FORMAT_TYPE_STR: {
2267                        const char *str_arg = args;
2268                        args += strlen(str_arg) + 1;
2269                        str = string(str, end, (char *)str_arg, spec);
2270                        break;
2271                }
2272
2273                case FORMAT_TYPE_PTR:
2274                        str = pointer(fmt+1, str, end, get_arg(void *), spec);
2275                        while (isalnum(*fmt))
2276                                fmt++;
2277                        break;
2278
2279                case FORMAT_TYPE_PERCENT_CHAR:
2280                case FORMAT_TYPE_INVALID:
2281                        if (str < end)
2282                                *str = '%';
2283                        ++str;
2284                        break;
2285
2286                case FORMAT_TYPE_NRCHARS:
2287                        /* skip */
2288                        break;
2289
2290                default: {
2291                        unsigned long long num;
2292
2293                        switch (spec.type) {
2294
2295                        case FORMAT_TYPE_LONG_LONG:
2296                                num = get_arg(long long);
2297                                break;
2298                        case FORMAT_TYPE_ULONG:
2299                        case FORMAT_TYPE_LONG:
2300                                num = get_arg(unsigned long);
2301                                break;
2302                        case FORMAT_TYPE_SIZE_T:
2303                                num = get_arg(size_t);
2304                                break;
2305                        case FORMAT_TYPE_PTRDIFF:
2306                                num = get_arg(ptrdiff_t);
2307                                break;
2308                        case FORMAT_TYPE_UBYTE:
2309                                num = get_arg(unsigned char);
2310                                break;
2311                        case FORMAT_TYPE_BYTE:
2312                                num = get_arg(signed char);
2313                                break;
2314                        case FORMAT_TYPE_USHORT:
2315                                num = get_arg(unsigned short);
2316                                break;
2317                        case FORMAT_TYPE_SHORT:
2318                                num = get_arg(short);
2319                                break;
2320                        case FORMAT_TYPE_UINT:
2321                                num = get_arg(unsigned int);
2322                                break;
2323                        default:
2324                                num = get_arg(int);
2325                        }
2326
2327                        str = number(str, end, num, spec);
2328                } /* default: */
2329                } /* switch(spec.type) */
2330        } /* while(*fmt) */
2331
2332        if (size > 0) {
2333                if (str < end)
2334                        *str = '\0';
2335                else
2336                        end[-1] = '\0';
2337        }
2338
2339#undef get_arg
2340
2341        /* the trailing null byte doesn't count towards the total */
2342        return str - buf;
2343}
2344EXPORT_SYMBOL_GPL(bstr_printf);
2345
2346/**
2347 * bprintf - Parse a format string and place args' binary value in a buffer
2348 * @bin_buf: The buffer to place args' binary value
2349 * @size: The size of the buffer(by words(32bits), not characters)
2350 * @fmt: The format string to use
2351 * @...: Arguments for the format string
2352 *
2353 * The function returns the number of words(u32) written
2354 * into @bin_buf.
2355 */
2356int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2357{
2358        va_list args;
2359        int ret;
2360
2361        va_start(args, fmt);
2362        ret = vbin_printf(bin_buf, size, fmt, args);
2363        va_end(args);
2364
2365        return ret;
2366}
2367EXPORT_SYMBOL_GPL(bprintf);
2368
2369#endif /* CONFIG_BINARY_PRINTF */
2370
2371/**
2372 * vsscanf - Unformat a buffer into a list of arguments
2373 * @buf:        input buffer
2374 * @fmt:        format of buffer
2375 * @args:       arguments
2376 */
2377int vsscanf(const char *buf, const char *fmt, va_list args)
2378{
2379        const char *str = buf;
2380        char *next;
2381        char digit;
2382        int num = 0;
2383        u8 qualifier;
2384        unsigned int base;
2385        union {
2386                long long s;
2387                unsigned long long u;
2388        } val;
2389        s16 field_width;
2390        bool is_sign;
2391
2392        while (*fmt) {
2393                /* skip any white space in format */
2394                /* white space in format matchs any amount of
2395                 * white space, including none, in the input.
2396                 */
2397                if (isspace(*fmt)) {
2398                        fmt = skip_spaces(++fmt);
2399                        str = skip_spaces(str);
2400                }
2401
2402                /* anything that is not a conversion must match exactly */
2403                if (*fmt != '%' && *fmt) {
2404                        if (*fmt++ != *str++)
2405                                break;
2406                        continue;
2407                }
2408
2409                if (!*fmt)
2410                        break;
2411                ++fmt;
2412
2413                /* skip this conversion.
2414                 * advance both strings to next white space
2415                 */
2416                if (*fmt == '*') {
2417                        if (!*str)
2418                                break;
2419                        while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2420                                /* '%*[' not yet supported, invalid format */
2421                                if (*fmt == '[')
2422                                        return num;
2423                                fmt++;
2424                        }
2425                        while (!isspace(*str) && *str)
2426                                str++;
2427                        continue;
2428                }
2429
2430                /* get field width */
2431                field_width = -1;
2432                if (isdigit(*fmt)) {
2433                        field_width = skip_atoi(&fmt);
2434                        if (field_width <= 0)
2435                                break;
2436                }
2437
2438                /* get conversion qualifier */
2439                qualifier = -1;
2440                if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2441                    _tolower(*fmt) == 'z') {
2442                        qualifier = *fmt++;
2443                        if (unlikely(qualifier == *fmt)) {
2444                                if (qualifier == 'h') {
2445                                        qualifier = 'H';
2446                                        fmt++;
2447                                } else if (qualifier == 'l') {
2448                                        qualifier = 'L';
2449                                        fmt++;
2450                                }
2451                        }
2452                }
2453
2454                if (!*fmt)
2455                        break;
2456
2457                if (*fmt == 'n') {
2458                        /* return number of characters read so far */
2459                        *va_arg(args, int *) = str - buf;
2460                        ++fmt;
2461                        continue;
2462                }
2463
2464                if (!*str)
2465                        break;
2466
2467                base = 10;
2468                is_sign = 0;
2469
2470                switch (*fmt++) {
2471                case 'c':
2472                {
2473                        char *s = (char *)va_arg(args, char*);
2474                        if (field_width == -1)
2475                                field_width = 1;
2476                        do {
2477                                *s++ = *str++;
2478                        } while (--field_width > 0 && *str);
2479                        num++;
2480                }
2481                continue;
2482                case 's':
2483                {
2484                        char *s = (char *)va_arg(args, char *);
2485                        if (field_width == -1)
2486                                field_width = SHRT_MAX;
2487                        /* first, skip leading white space in buffer */
2488                        str = skip_spaces(str);
2489
2490                        /* now copy until next white space */
2491                        while (*str && !isspace(*str) && field_width--)
2492                                *s++ = *str++;
2493                        *s = '\0';
2494                        num++;
2495                }
2496                continue;
2497                /*
2498                 * Warning: This implementation of the '[' conversion specifier
2499                 * deviates from its glibc counterpart in the following ways:
2500                 * (1) It does NOT support ranges i.e. '-' is NOT a special
2501                 *     character
2502                 * (2) It cannot match the closing bracket ']' itself
2503                 * (3) A field width is required
2504                 * (4) '%*[' (discard matching input) is currently not supported
2505                 *
2506                 * Example usage:
2507                 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
2508                 *              buf1, buf2, buf3);
2509                 * if (ret < 3)
2510                 *    // etc..
2511                 */
2512                case '[':
2513                {
2514                        char *s = (char *)va_arg(args, char *);
2515                        DECLARE_BITMAP(set, 256) = {0};
2516                        unsigned int len = 0;
2517                        bool negate = (*fmt == '^');
2518
2519                        /* field width is required */
2520                        if (field_width == -1)
2521                                return num;
2522
2523                        if (negate)
2524                                ++fmt;
2525
2526                        for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
2527                                set_bit((u8)*fmt, set);
2528
2529                        /* no ']' or no character set found */
2530                        if (!*fmt || !len)
2531                                return num;
2532                        ++fmt;
2533
2534                        if (negate) {
2535                                bitmap_complement(set, set, 256);
2536                                /* exclude null '\0' byte */
2537                                clear_bit(0, set);
2538                        }
2539
2540                        /* match must be non-empty */
2541                        if (!test_bit((u8)*str, set))
2542                                return num;
2543
2544                        while (test_bit((u8)*str, set) && field_width--)
2545                                *s++ = *str++;
2546                        *s = '\0';
2547                        ++num;
2548                }
2549                continue;
2550                case 'o':
2551                        base = 8;
2552                        break;
2553                case 'x':
2554                case 'X':
2555                        base = 16;
2556                        break;
2557                case 'i':
2558                        base = 0;
2559                case 'd':
2560                        is_sign = 1;
2561                case 'u':
2562                        break;
2563                case '%':
2564                        /* looking for '%' in str */
2565                        if (*str++ != '%')
2566                                return num;
2567                        continue;
2568                default:
2569                        /* invalid format; stop here */
2570                        return num;
2571                }
2572
2573                /* have some sort of integer conversion.
2574                 * first, skip white space in buffer.
2575                 */
2576                str = skip_spaces(str);
2577
2578                digit = *str;
2579                if (is_sign && digit == '-')
2580                        digit = *(str + 1);
2581
2582                if (!digit
2583                    || (base == 16 && !isxdigit(digit))
2584                    || (base == 10 && !isdigit(digit))
2585                    || (base == 8 && (!isdigit(digit) || digit > '7'))
2586                    || (base == 0 && !isdigit(digit)))
2587                        break;
2588
2589                if (is_sign)
2590                        val.s = qualifier != 'L' ?
2591                                simple_strtol(str, &next, base) :
2592                                simple_strtoll(str, &next, base);
2593                else
2594                        val.u = qualifier != 'L' ?
2595                                simple_strtoul(str, &next, base) :
2596                                simple_strtoull(str, &next, base);
2597
2598                if (field_width > 0 && next - str > field_width) {
2599                        if (base == 0)
2600                                _parse_integer_fixup_radix(str, &base);
2601                        while (next - str > field_width) {
2602                                if (is_sign)
2603                                        val.s = div_s64(val.s, base);
2604                                else
2605                                        val.u = div_u64(val.u, base);
2606                                --next;
2607                        }
2608                }
2609
2610                switch (qualifier) {
2611                case 'H':       /* that's 'hh' in format */
2612                        if (is_sign)
2613                                *va_arg(args, signed char *) = val.s;
2614                        else
2615                                *va_arg(args, unsigned char *) = val.u;
2616                        break;
2617                case 'h':
2618                        if (is_sign)
2619                                *va_arg(args, short *) = val.s;
2620                        else
2621                                *va_arg(args, unsigned short *) = val.u;
2622                        break;
2623                case 'l':
2624                        if (is_sign)
2625                                *va_arg(args, long *) = val.s;
2626                        else
2627                                *va_arg(args, unsigned long *) = val.u;
2628                        break;
2629                case 'L':
2630                        if (is_sign)
2631                                *va_arg(args, long long *) = val.s;
2632                        else
2633                                *va_arg(args, unsigned long long *) = val.u;
2634                        break;
2635                case 'Z':
2636                case 'z':
2637                        *va_arg(args, size_t *) = val.u;
2638                        break;
2639                default:
2640                        if (is_sign)
2641                                *va_arg(args, int *) = val.s;
2642                        else
2643                                *va_arg(args, unsigned int *) = val.u;
2644                        break;
2645                }
2646                num++;
2647
2648                if (!next)
2649                        break;
2650                str = next;
2651        }
2652
2653        return num;
2654}
2655EXPORT_SYMBOL(vsscanf);
2656
2657/**
2658 * sscanf - Unformat a buffer into a list of arguments
2659 * @buf:        input buffer
2660 * @fmt:        formatting of buffer
2661 * @...:        resulting arguments
2662 */
2663int sscanf(const char *buf, const char *fmt, ...)
2664{
2665        va_list args;
2666        int i;
2667
2668        va_start(args, fmt);
2669        i = vsscanf(buf, fmt, args);
2670        va_end(args);
2671
2672        return i;
2673}
2674EXPORT_SYMBOL(sscanf);
2675