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