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