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