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