4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
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
20 #include <linux/clk.h>
21 #include <linux/clk-provider.h>
22 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/ctype.h>
26 #include <linux/kernel.h>
27 #include <linux/kallsyms.h>
28 #include <linux/math64.h>
29 #include <linux/uaccess.h>
30 #include <linux/ioport.h>
31 #include <linux/dcache.h>
32 #include <linux/cred.h>
33 #include <linux/uuid.h>
35 #include <net/addrconf.h>
36 #include <linux/siphash.h>
37 #include <linux/compiler.h>
39 #include <linux/blkdev.h>
42 #include "../mm/internal.h" /* For the trace_print_flags arrays */
44 #include <asm/page.h> /* for PAGE_SIZE */
45 #include <asm/byteorder.h> /* cpu_to_le16 */
47 #include <linux/string_helpers.h>
50 static unsigned long long simple_strntoull(const char *startp, size_t max_chars,
51 char **endp, unsigned int base)
54 unsigned long long result = 0ULL;
58 cp = _parse_integer_fixup_radix(startp, &base);
59 prefix_chars = cp - startp;
60 if (prefix_chars < max_chars) {
61 rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
63 cp += (rv & ~KSTRTOX_OVERFLOW);
65 /* Field too short for prefix + digit, skip over without converting */
66 cp = startp + max_chars;
76 * simple_strtoull - convert a string to an unsigned long long
77 * @cp: The start of the string
78 * @endp: A pointer to the end of the parsed string will be placed here
79 * @base: The number base to use
81 * This function is obsolete. Please use kstrtoull instead.
83 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
85 return simple_strntoull(cp, INT_MAX, endp, base);
87 EXPORT_SYMBOL(simple_strtoull);
90 * simple_strtoul - convert a string to an unsigned long
91 * @cp: The start of the string
92 * @endp: A pointer to the end of the parsed string will be placed here
93 * @base: The number base to use
95 * This function is obsolete. Please use kstrtoul instead.
97 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
99 return simple_strtoull(cp, endp, base);
101 EXPORT_SYMBOL(simple_strtoul);
104 * simple_strtol - convert a string to a signed long
105 * @cp: The start of the string
106 * @endp: A pointer to the end of the parsed string will be placed here
107 * @base: The number base to use
109 * This function is obsolete. Please use kstrtol instead.
111 long simple_strtol(const char *cp, char **endp, unsigned int base)
114 return -simple_strtoul(cp + 1, endp, base);
116 return simple_strtoul(cp, endp, base);
118 EXPORT_SYMBOL(simple_strtol);
120 static long long simple_strntoll(const char *cp, size_t max_chars, char **endp,
124 * simple_strntoull() safely handles receiving max_chars==0 in the
125 * case cp[0] == '-' && max_chars == 1.
126 * If max_chars == 0 we can drop through and pass it to simple_strntoull()
127 * and the content of *cp is irrelevant.
129 if (*cp == '-' && max_chars > 0)
130 return -simple_strntoull(cp + 1, max_chars - 1, endp, base);
132 return simple_strntoull(cp, max_chars, endp, base);
136 * simple_strtoll - convert a string to a signed long long
137 * @cp: The start of the string
138 * @endp: A pointer to the end of the parsed string will be placed here
139 * @base: The number base to use
141 * This function is obsolete. Please use kstrtoll instead.
143 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
145 return simple_strntoll(cp, INT_MAX, endp, base);
147 EXPORT_SYMBOL(simple_strtoll);
149 static noinline_for_stack
150 int skip_atoi(const char **s)
155 i = i*10 + *((*s)++) - '0';
156 } while (isdigit(**s));
162 * Decimal conversion is by far the most typical, and is used for
163 * /proc and /sys data. This directly impacts e.g. top performance
164 * with many processes running. We optimize it for speed by emitting
165 * two characters at a time, using a 200 byte lookup table. This
166 * roughly halves the number of multiplications compared to computing
167 * the digits one at a time. Implementation strongly inspired by the
168 * previous version, which in turn used ideas described at
169 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
170 * from the author, Douglas W. Jones).
172 * It turns out there is precisely one 26 bit fixed-point
173 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
174 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
175 * range happens to be somewhat larger (x <= 1073741898), but that's
176 * irrelevant for our purpose.
178 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
179 * need a 32x32->64 bit multiply, so we simply use the same constant.
181 * For dividing a number in the range [100, 10^4-1] by 100, there are
182 * several options. The simplest is (x * 0x147b) >> 19, which is valid
183 * for all x <= 43698.
186 static const u16 decpair[100] = {
187 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
188 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
189 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
190 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
191 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
192 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
193 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
194 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
195 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
196 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
197 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
202 * This will print a single '0' even if r == 0, since we would
203 * immediately jump to out_r where two 0s would be written but only
204 * one of them accounted for in buf. This is needed by ip4_string
205 * below. All other callers pass a non-zero value of r.
207 static noinline_for_stack
208 char *put_dec_trunc8(char *buf, unsigned r)
216 /* 100 <= r < 10^8 */
217 q = (r * (u64)0x28f5c29) >> 32;
218 *((u16 *)buf) = decpair[r - 100*q];
225 /* 100 <= q < 10^6 */
226 r = (q * (u64)0x28f5c29) >> 32;
227 *((u16 *)buf) = decpair[q - 100*r];
234 /* 100 <= r < 10^4 */
235 q = (r * 0x147b) >> 19;
236 *((u16 *)buf) = decpair[r - 100*q];
243 *((u16 *)buf) = decpair[r];
244 buf += r < 10 ? 1 : 2;
248 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
249 static noinline_for_stack
250 char *put_dec_full8(char *buf, unsigned r)
255 q = (r * (u64)0x28f5c29) >> 32;
256 *((u16 *)buf) = decpair[r - 100*q];
260 r = (q * (u64)0x28f5c29) >> 32;
261 *((u16 *)buf) = decpair[q - 100*r];
265 q = (r * 0x147b) >> 19;
266 *((u16 *)buf) = decpair[r - 100*q];
270 *((u16 *)buf) = decpair[q];
275 static noinline_for_stack
276 char *put_dec(char *buf, unsigned long long n)
278 if (n >= 100*1000*1000)
279 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
280 /* 1 <= n <= 1.6e11 */
281 if (n >= 100*1000*1000)
282 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
284 return put_dec_trunc8(buf, n);
287 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
290 put_dec_full4(char *buf, unsigned r)
295 q = (r * 0x147b) >> 19;
296 *((u16 *)buf) = decpair[r - 100*q];
299 *((u16 *)buf) = decpair[q];
303 * Call put_dec_full4 on x % 10000, return x / 10000.
304 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
305 * holds for all x < 1,128,869,999. The largest value this
306 * helper will ever be asked to convert is 1,125,520,955.
307 * (second call in the put_dec code, assuming n is all-ones).
309 static noinline_for_stack
310 unsigned put_dec_helper4(char *buf, unsigned x)
312 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
314 put_dec_full4(buf, x - q * 10000);
318 /* Based on code by Douglas W. Jones found at
319 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
320 * (with permission from the author).
321 * Performs no 64-bit division and hence should be fast on 32-bit machines.
324 char *put_dec(char *buf, unsigned long long n)
326 uint32_t d3, d2, d1, q, h;
328 if (n < 100*1000*1000)
329 return put_dec_trunc8(buf, n);
331 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
334 d3 = (h >> 16); /* implicit "& 0xffff" */
336 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
337 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
338 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
339 q = put_dec_helper4(buf, q);
341 q += 7671 * d3 + 9496 * d2 + 6 * d1;
342 q = put_dec_helper4(buf+4, q);
344 q += 4749 * d3 + 42 * d2;
345 q = put_dec_helper4(buf+8, q);
350 buf = put_dec_trunc8(buf, q);
351 else while (buf[-1] == '0')
360 * Convert passed number to decimal string.
361 * Returns the length of string. On buffer overflow, returns 0.
363 * If speed is not important, use snprintf(). It's easy to read the code.
365 int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
367 /* put_dec requires 2-byte alignment of the buffer. */
368 char tmp[sizeof(num) * 3] __aligned(2);
371 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
376 len = put_dec(tmp, num) - tmp;
379 if (len > size || width > size)
384 for (idx = 0; idx < width; idx++)
390 for (idx = 0; idx < len; ++idx)
391 buf[idx + width] = tmp[len - idx - 1];
396 #define SIGN 1 /* unsigned/signed, must be 1 */
397 #define LEFT 2 /* left justified */
398 #define PLUS 4 /* show plus */
399 #define SPACE 8 /* space if plus */
400 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
401 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
402 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
405 FORMAT_TYPE_NONE, /* Just a string part */
407 FORMAT_TYPE_PRECISION,
411 FORMAT_TYPE_PERCENT_CHAR,
413 FORMAT_TYPE_LONG_LONG,
427 unsigned int type:8; /* format_type enum */
428 signed int field_width:24; /* width of output field */
429 unsigned int flags:8; /* flags to number() */
430 unsigned int base:8; /* number base, 8, 10 or 16 only */
431 signed int precision:16; /* # of digits/chars */
433 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
434 #define PRECISION_MAX ((1 << 15) - 1)
436 static noinline_for_stack
437 char *number(char *buf, char *end, unsigned long long num,
438 struct printf_spec spec)
440 /* put_dec requires 2-byte alignment of the buffer. */
441 char tmp[3 * sizeof(num)] __aligned(2);
444 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
446 bool is_zero = num == 0LL;
447 int field_width = spec.field_width;
448 int precision = spec.precision;
450 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
452 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
453 * produces same digits or (maybe lowercased) letters */
454 locase = (spec.flags & SMALL);
455 if (spec.flags & LEFT)
456 spec.flags &= ~ZEROPAD;
458 if (spec.flags & SIGN) {
459 if ((signed long long)num < 0) {
461 num = -(signed long long)num;
463 } else if (spec.flags & PLUS) {
466 } else if (spec.flags & SPACE) {
478 /* generate full string in tmp[], in reverse order */
481 tmp[i++] = hex_asc_upper[num] | locase;
482 else if (spec.base != 10) { /* 8 or 16 */
483 int mask = spec.base - 1;
489 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
492 } else { /* base 10 */
493 i = put_dec(tmp, num) - tmp;
496 /* printing 100 using %2d gives "100", not "00" */
499 /* leading space padding */
500 field_width -= precision;
501 if (!(spec.flags & (ZEROPAD | LEFT))) {
502 while (--field_width >= 0) {
514 /* "0x" / "0" prefix */
516 if (spec.base == 16 || !is_zero) {
521 if (spec.base == 16) {
523 *buf = ('X' | locase);
527 /* zero or space padding */
528 if (!(spec.flags & LEFT)) {
529 char c = ' ' + (spec.flags & ZEROPAD);
530 BUILD_BUG_ON(' ' + ZEROPAD != '0');
531 while (--field_width >= 0) {
537 /* hmm even more zero padding? */
538 while (i <= --precision) {
543 /* actual digits of result */
549 /* trailing space padding */
550 while (--field_width >= 0) {
559 static noinline_for_stack
560 char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
562 struct printf_spec spec;
564 spec.type = FORMAT_TYPE_PTR;
565 spec.field_width = 2 + 2 * size; /* 0x + hex */
566 spec.flags = SPECIAL | SMALL | ZEROPAD;
570 return number(buf, end, num, spec);
573 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
576 if (buf >= end) /* nowhere to put anything */
579 if (size <= spaces) {
580 memset(buf, ' ', size);
584 if (len > size - spaces)
586 memmove(buf + spaces, buf, len);
588 memset(buf, ' ', spaces);
592 * Handle field width padding for a string.
593 * @buf: current buffer position
594 * @n: length of string
595 * @end: end of output buffer
596 * @spec: for field width and flags
597 * Returns: new buffer position after padding.
599 static noinline_for_stack
600 char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
604 if (likely(n >= spec.field_width))
606 /* we want to pad the sucker */
607 spaces = spec.field_width - n;
608 if (!(spec.flags & LEFT)) {
609 move_right(buf - n, end, n, spaces);
620 static noinline_for_stack
621 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
624 size_t lim = spec.precision;
626 if ((unsigned long)s < PAGE_SIZE)
638 return widen_string(buf, len, end, spec);
641 static noinline_for_stack
642 char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
645 const char *array[4], *s;
646 const struct dentry *p;
651 case '2': case '3': case '4':
652 depth = fmt[1] - '0';
659 for (i = 0; i < depth; i++, d = p) {
660 p = READ_ONCE(d->d_parent);
661 array[i] = READ_ONCE(d->d_name.name);
670 for (n = 0; n != spec.precision; n++, buf++) {
682 return widen_string(buf, n, end, spec);
686 static noinline_for_stack
687 char *bdev_name(char *buf, char *end, struct block_device *bdev,
688 struct printf_spec spec, const char *fmt)
690 struct gendisk *hd = bdev->bd_disk;
692 buf = string(buf, end, hd->disk_name, spec);
693 if (bdev->bd_part->partno) {
694 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
699 buf = number(buf, end, bdev->bd_part->partno, spec);
705 static noinline_for_stack
706 char *symbol_string(char *buf, char *end, void *ptr,
707 struct printf_spec spec, const char *fmt)
710 #ifdef CONFIG_KALLSYMS
711 char sym[KSYM_SYMBOL_LEN];
715 ptr = __builtin_extract_return_addr(ptr);
716 value = (unsigned long)ptr;
718 #ifdef CONFIG_KALLSYMS
720 sprint_backtrace(sym, value);
721 else if (*fmt != 'f' && *fmt != 's')
722 sprint_symbol(sym, value);
724 sprint_symbol_no_offset(sym, value);
726 return string(buf, end, sym, spec);
728 return special_hex_number(buf, end, value, sizeof(void *));
732 static const struct printf_spec default_str_spec = {
737 static const struct printf_spec default_flag_spec = {
740 .flags = SPECIAL | SMALL,
743 static const struct printf_spec default_dec_spec = {
748 static noinline_for_stack
749 char *resource_string(char *buf, char *end, struct resource *res,
750 struct printf_spec spec, const char *fmt)
752 #ifndef IO_RSRC_PRINTK_SIZE
753 #define IO_RSRC_PRINTK_SIZE 6
756 #ifndef MEM_RSRC_PRINTK_SIZE
757 #define MEM_RSRC_PRINTK_SIZE 10
759 static const struct printf_spec io_spec = {
761 .field_width = IO_RSRC_PRINTK_SIZE,
763 .flags = SPECIAL | SMALL | ZEROPAD,
765 static const struct printf_spec mem_spec = {
767 .field_width = MEM_RSRC_PRINTK_SIZE,
769 .flags = SPECIAL | SMALL | ZEROPAD,
771 static const struct printf_spec bus_spec = {
775 .flags = SMALL | ZEROPAD,
777 static const struct printf_spec str_spec = {
783 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
784 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
785 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
786 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
787 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
788 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
789 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
790 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
792 char *p = sym, *pend = sym + sizeof(sym);
793 int decode = (fmt[0] == 'R') ? 1 : 0;
794 const struct printf_spec *specp;
797 if (res->flags & IORESOURCE_IO) {
798 p = string(p, pend, "io ", str_spec);
800 } else if (res->flags & IORESOURCE_MEM) {
801 p = string(p, pend, "mem ", str_spec);
803 } else if (res->flags & IORESOURCE_IRQ) {
804 p = string(p, pend, "irq ", str_spec);
805 specp = &default_dec_spec;
806 } else if (res->flags & IORESOURCE_DMA) {
807 p = string(p, pend, "dma ", str_spec);
808 specp = &default_dec_spec;
809 } else if (res->flags & IORESOURCE_BUS) {
810 p = string(p, pend, "bus ", str_spec);
813 p = string(p, pend, "??? ", str_spec);
817 if (decode && res->flags & IORESOURCE_UNSET) {
818 p = string(p, pend, "size ", str_spec);
819 p = number(p, pend, resource_size(res), *specp);
821 p = number(p, pend, res->start, *specp);
822 if (res->start != res->end) {
824 p = number(p, pend, res->end, *specp);
828 if (res->flags & IORESOURCE_MEM_64)
829 p = string(p, pend, " 64bit", str_spec);
830 if (res->flags & IORESOURCE_PREFETCH)
831 p = string(p, pend, " pref", str_spec);
832 if (res->flags & IORESOURCE_WINDOW)
833 p = string(p, pend, " window", str_spec);
834 if (res->flags & IORESOURCE_DISABLED)
835 p = string(p, pend, " disabled", str_spec);
837 p = string(p, pend, " flags ", str_spec);
838 p = number(p, pend, res->flags, default_flag_spec);
843 return string(buf, end, sym, spec);
846 static noinline_for_stack
847 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
850 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
851 negative value, fallback to the default */
854 if (spec.field_width == 0)
855 /* nothing to print */
858 if (ZERO_OR_NULL_PTR(addr))
860 return string(buf, end, NULL, spec);
877 if (spec.field_width > 0)
878 len = min_t(int, spec.field_width, 64);
880 for (i = 0; i < len; ++i) {
882 *buf = hex_asc_hi(addr[i]);
885 *buf = hex_asc_lo(addr[i]);
888 if (separator && i != len - 1) {
898 static noinline_for_stack
899 char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
900 struct printf_spec spec, const char *fmt)
902 const int CHUNKSZ = 32;
903 int nr_bits = max_t(int, spec.field_width, 0);
907 /* reused to print numbers */
908 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
910 chunksz = nr_bits & (CHUNKSZ - 1);
914 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
915 for (; i >= 0; i -= CHUNKSZ) {
919 chunkmask = ((1ULL << chunksz) - 1);
920 word = i / BITS_PER_LONG;
921 bit = i % BITS_PER_LONG;
922 val = (bitmap[word] >> bit) & chunkmask;
931 spec.field_width = DIV_ROUND_UP(chunksz, 4);
932 buf = number(buf, end, val, spec);
939 static noinline_for_stack
940 char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
941 struct printf_spec spec, const char *fmt)
943 int nr_bits = max_t(int, spec.field_width, 0);
944 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
948 rbot = cur = find_first_bit(bitmap, nr_bits);
949 while (cur < nr_bits) {
951 cur = find_next_bit(bitmap, nr_bits, cur + 1);
952 if (cur < nr_bits && cur <= rtop + 1)
962 buf = number(buf, end, rbot, default_dec_spec);
968 buf = number(buf, end, rtop, default_dec_spec);
976 static noinline_for_stack
977 char *mac_address_string(char *buf, char *end, u8 *addr,
978 struct printf_spec spec, const char *fmt)
980 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
984 bool reversed = false;
1000 for (i = 0; i < 6; i++) {
1002 p = hex_byte_pack(p, addr[5 - i]);
1004 p = hex_byte_pack(p, addr[i]);
1006 if (fmt[0] == 'M' && i != 5)
1011 return string(buf, end, mac_addr, spec);
1014 static noinline_for_stack
1015 char *ip4_string(char *p, const u8 *addr, const char *fmt)
1018 bool leading_zeros = (fmt[0] == 'i');
1043 for (i = 0; i < 4; i++) {
1044 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
1045 int digits = put_dec_trunc8(temp, addr[index]) - temp;
1046 if (leading_zeros) {
1052 /* reverse the digits in the quad */
1054 *p++ = temp[digits];
1064 static noinline_for_stack
1065 char *ip6_compressed_string(char *p, const char *addr)
1068 unsigned char zerolength[8];
1073 bool needcolon = false;
1075 struct in6_addr in6;
1077 memcpy(&in6, addr, sizeof(struct in6_addr));
1079 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1081 memset(zerolength, 0, sizeof(zerolength));
1088 /* find position of longest 0 run */
1089 for (i = 0; i < range; i++) {
1090 for (j = i; j < range; j++) {
1091 if (in6.s6_addr16[j] != 0)
1096 for (i = 0; i < range; i++) {
1097 if (zerolength[i] > longest) {
1098 longest = zerolength[i];
1102 if (longest == 1) /* don't compress a single 0 */
1106 for (i = 0; i < range; i++) {
1107 if (i == colonpos) {
1108 if (needcolon || i == 0)
1119 /* hex u16 without leading 0s */
1120 word = ntohs(in6.s6_addr16[i]);
1125 p = hex_byte_pack(p, hi);
1127 *p++ = hex_asc_lo(hi);
1128 p = hex_byte_pack(p, lo);
1131 p = hex_byte_pack(p, lo);
1133 *p++ = hex_asc_lo(lo);
1140 p = ip4_string(p, &in6.s6_addr[12], "I4");
1147 static noinline_for_stack
1148 char *ip6_string(char *p, const char *addr, const char *fmt)
1152 for (i = 0; i < 8; i++) {
1153 p = hex_byte_pack(p, *addr++);
1154 p = hex_byte_pack(p, *addr++);
1155 if (fmt[0] == 'I' && i != 7)
1163 static noinline_for_stack
1164 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1165 struct printf_spec spec, const char *fmt)
1167 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1169 if (fmt[0] == 'I' && fmt[2] == 'c')
1170 ip6_compressed_string(ip6_addr, addr);
1172 ip6_string(ip6_addr, addr, fmt);
1174 return string(buf, end, ip6_addr, spec);
1177 static noinline_for_stack
1178 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1179 struct printf_spec spec, const char *fmt)
1181 char ip4_addr[sizeof("255.255.255.255")];
1183 ip4_string(ip4_addr, addr, fmt);
1185 return string(buf, end, ip4_addr, spec);
1188 static noinline_for_stack
1189 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1190 struct printf_spec spec, const char *fmt)
1192 bool have_p = false, have_s = false, have_f = false, have_c = false;
1193 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1194 sizeof(":12345") + sizeof("/123456789") +
1195 sizeof("%1234567890")];
1196 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1197 const u8 *addr = (const u8 *) &sa->sin6_addr;
1198 char fmt6[2] = { fmt[0], '6' };
1202 while (isalpha(*++fmt)) {
1219 if (have_p || have_s || have_f) {
1224 if (fmt6[0] == 'I' && have_c)
1225 p = ip6_compressed_string(ip6_addr + off, addr);
1227 p = ip6_string(ip6_addr + off, addr, fmt6);
1229 if (have_p || have_s || have_f)
1234 p = number(p, pend, ntohs(sa->sin6_port), spec);
1238 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1239 IPV6_FLOWINFO_MASK), spec);
1243 p = number(p, pend, sa->sin6_scope_id, spec);
1247 return string(buf, end, ip6_addr, spec);
1250 static noinline_for_stack
1251 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1252 struct printf_spec spec, const char *fmt)
1254 bool have_p = false;
1255 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1256 char *pend = ip4_addr + sizeof(ip4_addr);
1257 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1258 char fmt4[3] = { fmt[0], '4', 0 };
1261 while (isalpha(*++fmt)) {
1275 p = ip4_string(ip4_addr, addr, fmt4);
1278 p = number(p, pend, ntohs(sa->sin_port), spec);
1282 return string(buf, end, ip4_addr, spec);
1285 static noinline_for_stack
1286 char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1291 unsigned int flags = 0;
1294 if (spec.field_width == 0)
1295 return buf; /* nothing to print */
1297 if (ZERO_OR_NULL_PTR(addr))
1298 return string(buf, end, NULL, spec); /* NULL pointer */
1302 switch (fmt[count++]) {
1304 flags |= ESCAPE_ANY;
1307 flags |= ESCAPE_SPECIAL;
1310 flags |= ESCAPE_HEX;
1313 flags |= ESCAPE_NULL;
1316 flags |= ESCAPE_OCTAL;
1322 flags |= ESCAPE_SPACE;
1331 flags = ESCAPE_ANY_NP;
1333 len = spec.field_width < 0 ? 1 : spec.field_width;
1336 * string_escape_mem() writes as many characters as it can to
1337 * the given buffer, and returns the total size of the output
1338 * had the buffer been big enough.
1340 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1345 static noinline_for_stack
1346 char *uuid_string(char *buf, char *end, const u8 *addr,
1347 struct printf_spec spec, const char *fmt)
1349 char uuid[UUID_STRING_LEN + 1];
1352 const u8 *index = uuid_index;
1357 uc = true; /* fall-through */
1366 for (i = 0; i < 16; i++) {
1368 p = hex_byte_pack_upper(p, addr[index[i]]);
1370 p = hex_byte_pack(p, addr[index[i]]);
1383 return string(buf, end, uuid, spec);
1386 static noinline_for_stack
1387 char *pointer_string(char *buf, char *end, const void *ptr,
1388 struct printf_spec spec)
1391 spec.flags |= SMALL;
1392 if (spec.field_width == -1) {
1393 spec.field_width = 2 * sizeof(ptr);
1394 spec.flags |= ZEROPAD;
1397 return number(buf, end, (unsigned long int)ptr, spec);
1400 int kptr_restrict __read_mostly;
1402 static noinline_for_stack
1403 char *restricted_pointer(char *buf, char *end, const void *ptr,
1404 struct printf_spec spec)
1406 switch (kptr_restrict) {
1408 /* Always print %pK values */
1411 const struct cred *cred;
1414 * kptr_restrict==1 cannot be used in IRQ context
1415 * because its test for CAP_SYSLOG would be meaningless.
1417 if (in_irq() || in_serving_softirq() || in_nmi()) {
1418 if (spec.field_width == -1)
1419 spec.field_width = 2 * sizeof(ptr);
1420 return string(buf, end, "pK-error", spec);
1424 * Only print the real pointer value if the current
1425 * process has CAP_SYSLOG and is running with the
1426 * same credentials it started with. This is because
1427 * access to files is checked at open() time, but %pK
1428 * checks permission at read() time. We don't want to
1429 * leak pointer values if a binary opens a file using
1430 * %pK and then elevates privileges before reading it.
1432 cred = current_cred();
1433 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1434 !uid_eq(cred->euid, cred->uid) ||
1435 !gid_eq(cred->egid, cred->gid))
1441 /* Always print 0's for %pK */
1446 return pointer_string(buf, end, ptr, spec);
1449 static noinline_for_stack
1450 char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
1452 unsigned long long num;
1457 num = *(const netdev_features_t *)addr;
1458 size = sizeof(netdev_features_t);
1461 num = (unsigned long)addr;
1462 size = sizeof(unsigned long);
1466 return special_hex_number(buf, end, num, size);
1469 static noinline_for_stack
1470 char *address_val(char *buf, char *end, const void *addr, const char *fmt)
1472 unsigned long long num;
1477 num = *(const dma_addr_t *)addr;
1478 size = sizeof(dma_addr_t);
1482 num = *(const phys_addr_t *)addr;
1483 size = sizeof(phys_addr_t);
1487 return special_hex_number(buf, end, num, size);
1490 static noinline_for_stack
1491 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1494 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1495 return string(buf, end, NULL, spec);
1500 #ifdef CONFIG_COMMON_CLK
1501 return string(buf, end, __clk_get_name(clk), spec);
1503 return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
1509 char *format_flags(char *buf, char *end, unsigned long flags,
1510 const struct trace_print_flags *names)
1514 for ( ; flags && names->name; names++) {
1516 if ((flags & mask) != mask)
1519 buf = string(buf, end, names->name, default_str_spec);
1530 buf = number(buf, end, flags, default_flag_spec);
1535 static noinline_for_stack
1536 char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1538 unsigned long flags;
1539 const struct trace_print_flags *names;
1543 flags = *(unsigned long *)flags_ptr;
1544 /* Remove zone id */
1545 flags &= (1UL << NR_PAGEFLAGS) - 1;
1546 names = pageflag_names;
1549 flags = *(unsigned long *)flags_ptr;
1550 names = vmaflag_names;
1553 flags = *(gfp_t *)flags_ptr;
1554 names = gfpflag_names;
1557 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1561 return format_flags(buf, end, flags, names);
1564 static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1566 for ( ; np && depth; depth--)
1569 return kbasename(np->full_name);
1572 static noinline_for_stack
1573 char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1576 const struct device_node *parent = np->parent;
1578 /* special case for root node */
1580 return string(buf, end, "/", default_str_spec);
1582 for (depth = 0; parent->parent; depth++)
1583 parent = parent->parent;
1585 for ( ; depth >= 0; depth--) {
1586 buf = string(buf, end, "/", default_str_spec);
1587 buf = string(buf, end, device_node_name_for_depth(np, depth),
1593 static noinline_for_stack
1594 char *device_node_string(char *buf, char *end, struct device_node *dn,
1595 struct printf_spec spec, const char *fmt)
1597 char tbuf[sizeof("xxxx") + 1];
1600 char *buf_start = buf;
1601 struct property *prop;
1602 bool has_mult, pass;
1603 static const struct printf_spec num_spec = {
1610 struct printf_spec str_spec = spec;
1611 str_spec.field_width = -1;
1613 if (!IS_ENABLED(CONFIG_OF))
1614 return string(buf, end, "(!OF)", spec);
1616 if ((unsigned long)dn < PAGE_SIZE)
1617 return string(buf, end, "(null)", spec);
1619 /* simple case without anything any more format specifiers */
1621 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1624 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1632 case 'f': /* full_name */
1633 buf = device_node_gen_full_name(dn, buf, end);
1635 case 'n': /* name */
1636 buf = string(buf, end, dn->name, str_spec);
1638 case 'p': /* phandle */
1639 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1641 case 'P': /* path-spec */
1642 p = kbasename(of_node_full_name(dn));
1645 buf = string(buf, end, p, str_spec);
1647 case 'F': /* flags */
1648 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1649 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1650 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1651 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1653 buf = string(buf, end, tbuf, str_spec);
1655 case 'c': /* major compatible string */
1656 ret = of_property_read_string(dn, "compatible", &p);
1658 buf = string(buf, end, p, str_spec);
1660 case 'C': /* full compatible string */
1662 of_property_for_each_string(dn, "compatible", prop, p) {
1664 buf = string(buf, end, ",", str_spec);
1665 buf = string(buf, end, "\"", str_spec);
1666 buf = string(buf, end, p, str_spec);
1667 buf = string(buf, end, "\"", str_spec);
1677 return widen_string(buf, buf - buf_start, end, spec);
1680 /* Make pointers available for printing early in the boot sequence. */
1681 static int debug_boot_weak_hash __ro_after_init;
1683 static int __init debug_boot_weak_hash_enable(char *str)
1685 debug_boot_weak_hash = 1;
1686 pr_info("debug_boot_weak_hash enabled\n");
1689 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
1691 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
1692 static siphash_key_t ptr_key __read_mostly;
1694 static void enable_ptr_key_workfn(struct work_struct *work)
1696 get_random_bytes(&ptr_key, sizeof(ptr_key));
1697 /* Needs to run from preemptible context */
1698 static_branch_disable(¬_filled_random_ptr_key);
1701 static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
1703 static void fill_random_ptr_key(struct random_ready_callback *unused)
1705 /* This may be in an interrupt handler. */
1706 queue_work(system_unbound_wq, &enable_ptr_key_work);
1709 static struct random_ready_callback random_ready = {
1710 .func = fill_random_ptr_key
1713 static int __init initialize_ptr_random(void)
1715 int key_size = sizeof(ptr_key);
1718 /* Use hw RNG if available. */
1719 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
1720 static_branch_disable(¬_filled_random_ptr_key);
1724 ret = add_random_ready_callback(&random_ready);
1727 } else if (ret == -EALREADY) {
1728 /* This is in preemptible context */
1729 enable_ptr_key_workfn(&enable_ptr_key_work);
1735 early_initcall(initialize_ptr_random);
1737 /* Maps a pointer to a 32 bit unique identifier. */
1738 static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1740 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
1741 unsigned long hashval;
1743 /* When debugging early boot use non-cryptographically secure hash. */
1744 if (unlikely(debug_boot_weak_hash)) {
1745 hashval = hash_long((unsigned long)ptr, 32);
1746 return pointer_string(buf, end, (const void *)hashval, spec);
1749 if (static_branch_unlikely(¬_filled_random_ptr_key)) {
1750 spec.field_width = 2 * sizeof(ptr);
1751 /* string length must be less than default_width */
1752 return string(buf, end, str, spec);
1756 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1758 * Mask off the first 32 bits, this makes explicit that we have
1759 * modified the address (and 32 bits is plenty for a unique ID).
1761 hashval = hashval & 0xffffffff;
1763 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1765 return pointer_string(buf, end, (const void *)hashval, spec);
1769 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1770 * by an extra set of alphanumeric characters that are extended format
1773 * Please update scripts/checkpatch.pl when adding/removing conversion
1774 * characters. (Search for "check for vsprintf extension").
1776 * Right now we handle:
1778 * - 'S' For symbolic direct pointers (or function descriptors) with offset
1779 * - 's' For symbolic direct pointers (or function descriptors) without offset
1782 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1783 * - 'B' For backtraced symbolic direct pointers with offset
1784 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1785 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1786 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1787 * width which must be explicitly specified either as part of the
1788 * format string '%32b[l]' or through '%*b[l]', [l] selects
1789 * range-list format instead of hex format
1790 * - 'M' For a 6-byte MAC address, it prints the address in the
1791 * usual colon-separated hex notation
1792 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1793 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1794 * with a dash-separated hex notation
1795 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1796 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1797 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1798 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1800 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1801 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1802 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1803 * IPv6 omits the colons (01020304...0f)
1804 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1806 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1807 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1808 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1809 * - 'I[6S]c' for IPv6 addresses printed as specified by
1810 * http://tools.ietf.org/html/rfc5952
1811 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1812 * of the following flags (see string_escape_mem() for the
1815 * c - ESCAPE_SPECIAL
1821 * By default ESCAPE_ANY_NP is used.
1822 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1823 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1824 * Options for %pU are:
1825 * b big endian lower case hex (default)
1826 * B big endian UPPER case hex
1827 * l little endian lower case hex
1828 * L little endian UPPER case hex
1829 * big endian output byte order is:
1830 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1831 * little endian output byte order is:
1832 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1833 * - 'V' For a struct va_format which contains a format string * and va_list *,
1834 * call vsnprintf(->format, *->va_list).
1835 * Implements a "recursive vsnprintf".
1836 * Do not use this feature without some mechanism to verify the
1837 * correctness of the format string and va_list arguments.
1838 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1839 * - 'NF' For a netdev_features_t
1840 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1841 * a certain separator (' ' by default):
1845 * The maximum supported length is 64 bytes of the input. Consider
1846 * to use print_hex_dump() for the larger input.
1847 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1848 * (default assumed to be phys_addr_t, passed by reference)
1849 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1850 * - 'D[234]' Same as 'd' but for a struct file
1851 * - 'g' For block_device name (gendisk + partition number)
1852 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1853 * (legacy clock framework) of the clock
1854 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1855 * (legacy clock framework) of the clock
1856 * - 'Cr' For a clock, it prints the current rate of the clock
1857 * - 'G' For flags to be printed as a collection of symbolic strings that would
1858 * construct the specific value. Supported flags given by option:
1859 * p page flags (see struct page) given as pointer to unsigned long
1860 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1861 * v vma flags (VM_*) given as pointer to unsigned long
1862 * - 'O' For a kobject based struct. Must be one of the following:
1863 * - 'OF[fnpPcCF]' For a device tree object
1864 * Without any optional arguments prints the full_name
1865 * f device node full_name
1866 * n device node name
1867 * p device node phandle
1868 * P device node path spec (name + @unit)
1869 * F device node flags
1870 * c major compatible string
1871 * C full compatible string
1873 * - 'x' For printing the address. Equivalent to "%lx".
1875 * ** When making changes please also update:
1876 * Documentation/core-api/printk-formats.rst
1878 * Note: The default behaviour (unadorned %p) is to hash the address,
1879 * rendering it useful as a unique identifier.
1881 static noinline_for_stack
1882 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1883 struct printf_spec spec)
1885 const int default_width = 2 * sizeof(void *);
1887 if (!ptr && *fmt != 'K' && *fmt != 'x') {
1889 * Print (null) with the same width as a pointer so it makes
1890 * tabular output look nice.
1892 if (spec.field_width == -1)
1893 spec.field_width = default_width;
1894 return string(buf, end, "(null)", spec);
1902 ptr = dereference_symbol_descriptor(ptr);
1905 return symbol_string(buf, end, ptr, spec, fmt);
1908 return resource_string(buf, end, ptr, spec, fmt);
1910 return hex_string(buf, end, ptr, spec, fmt);
1914 return bitmap_list_string(buf, end, ptr, spec, fmt);
1916 return bitmap_string(buf, end, ptr, spec, fmt);
1918 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1919 case 'm': /* Contiguous: 000102030405 */
1921 /* [mM]R (Reverse order; Bluetooth) */
1922 return mac_address_string(buf, end, ptr, spec, fmt);
1923 case 'I': /* Formatted IP supported
1925 * 6: 0001:0203:...:0708
1926 * 6c: 1::708 or 1::1.2.3.4
1928 case 'i': /* Contiguous:
1929 * 4: 001.002.003.004
1934 return ip6_addr_string(buf, end, ptr, spec, fmt);
1936 return ip4_addr_string(buf, end, ptr, spec, fmt);
1939 struct sockaddr raw;
1940 struct sockaddr_in v4;
1941 struct sockaddr_in6 v6;
1944 switch (sa->raw.sa_family) {
1946 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1948 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1950 return string(buf, end, "(invalid address)", spec);
1955 return escaped_string(buf, end, ptr, spec, fmt);
1957 return uuid_string(buf, end, ptr, spec, fmt);
1962 va_copy(va, *((struct va_format *)ptr)->va);
1963 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1964 ((struct va_format *)ptr)->fmt, va);
1971 return restricted_pointer(buf, end, ptr, spec);
1973 return netdev_bits(buf, end, ptr, fmt);
1975 return address_val(buf, end, ptr, fmt);
1977 return dentry_name(buf, end, ptr, spec, fmt);
1979 return clock(buf, end, ptr, spec, fmt);
1981 return dentry_name(buf, end,
1982 ((const struct file *)ptr)->f_path.dentry,
1986 return bdev_name(buf, end, ptr, spec, fmt);
1990 return flags_string(buf, end, ptr, fmt);
1994 return device_node_string(buf, end, ptr, spec, fmt + 1);
1998 return pointer_string(buf, end, ptr, spec);
2001 /* default is to _not_ leak addresses, hash before printing */
2002 return ptr_to_id(buf, end, ptr, spec);
2006 * Helper function to decode printf style format.
2007 * Each call decode a token from the format and return the
2008 * number of characters read (or likely the delta where it wants
2009 * to go on the next call).
2010 * The decoded token is returned through the parameters
2012 * 'h', 'l', or 'L' for integer fields
2013 * 'z' support added 23/7/1999 S.H.
2014 * 'z' changed to 'Z' --davidm 1/25/99
2015 * 'Z' changed to 'z' --adobriyan 2017-01-25
2016 * 't' added for ptrdiff_t
2018 * @fmt: the format string
2019 * @type of the token returned
2020 * @flags: various flags such as +, -, # tokens..
2021 * @field_width: overwritten width
2022 * @base: base of the number (octal, hex, ...)
2023 * @precision: precision of a number
2024 * @qualifier: qualifier of a number (long, size_t, ...)
2026 static noinline_for_stack
2027 int format_decode(const char *fmt, struct printf_spec *spec)
2029 const char *start = fmt;
2032 /* we finished early by reading the field width */
2033 if (spec->type == FORMAT_TYPE_WIDTH) {
2034 if (spec->field_width < 0) {
2035 spec->field_width = -spec->field_width;
2036 spec->flags |= LEFT;
2038 spec->type = FORMAT_TYPE_NONE;
2042 /* we finished early by reading the precision */
2043 if (spec->type == FORMAT_TYPE_PRECISION) {
2044 if (spec->precision < 0)
2045 spec->precision = 0;
2047 spec->type = FORMAT_TYPE_NONE;
2052 spec->type = FORMAT_TYPE_NONE;
2054 for (; *fmt ; ++fmt) {
2059 /* Return the current non-format string */
2060 if (fmt != start || !*fmt)
2066 while (1) { /* this also skips first '%' */
2072 case '-': spec->flags |= LEFT; break;
2073 case '+': spec->flags |= PLUS; break;
2074 case ' ': spec->flags |= SPACE; break;
2075 case '#': spec->flags |= SPECIAL; break;
2076 case '0': spec->flags |= ZEROPAD; break;
2077 default: found = false;
2084 /* get field width */
2085 spec->field_width = -1;
2088 spec->field_width = skip_atoi(&fmt);
2089 else if (*fmt == '*') {
2090 /* it's the next argument */
2091 spec->type = FORMAT_TYPE_WIDTH;
2092 return ++fmt - start;
2096 /* get the precision */
2097 spec->precision = -1;
2100 if (isdigit(*fmt)) {
2101 spec->precision = skip_atoi(&fmt);
2102 if (spec->precision < 0)
2103 spec->precision = 0;
2104 } else if (*fmt == '*') {
2105 /* it's the next argument */
2106 spec->type = FORMAT_TYPE_PRECISION;
2107 return ++fmt - start;
2112 /* get the conversion qualifier */
2114 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2115 *fmt == 'z' || *fmt == 't') {
2117 if (unlikely(qualifier == *fmt)) {
2118 if (qualifier == 'l') {
2121 } else if (qualifier == 'h') {
2132 spec->type = FORMAT_TYPE_CHAR;
2133 return ++fmt - start;
2136 spec->type = FORMAT_TYPE_STR;
2137 return ++fmt - start;
2140 spec->type = FORMAT_TYPE_PTR;
2141 return ++fmt - start;
2144 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2145 return ++fmt - start;
2147 /* integer number formats - set up the flags and "break" */
2153 spec->flags |= SMALL;
2162 spec->flags |= SIGN;
2168 * Since %n poses a greater security risk than
2169 * utility, treat it as any other invalid or
2170 * unsupported format specifier.
2175 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2176 spec->type = FORMAT_TYPE_INVALID;
2180 if (qualifier == 'L')
2181 spec->type = FORMAT_TYPE_LONG_LONG;
2182 else if (qualifier == 'l') {
2183 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2184 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2185 } else if (qualifier == 'z') {
2186 spec->type = FORMAT_TYPE_SIZE_T;
2187 } else if (qualifier == 't') {
2188 spec->type = FORMAT_TYPE_PTRDIFF;
2189 } else if (qualifier == 'H') {
2190 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2191 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2192 } else if (qualifier == 'h') {
2193 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2194 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2196 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2197 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2200 return ++fmt - start;
2204 set_field_width(struct printf_spec *spec, int width)
2206 spec->field_width = width;
2207 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2208 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2213 set_precision(struct printf_spec *spec, int prec)
2215 spec->precision = prec;
2216 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2217 spec->precision = clamp(prec, 0, PRECISION_MAX);
2222 * vsnprintf - Format a string and place it in a buffer
2223 * @buf: The buffer to place the result into
2224 * @size: The size of the buffer, including the trailing null space
2225 * @fmt: The format string to use
2226 * @args: Arguments for the format string
2228 * This function generally follows C99 vsnprintf, but has some
2229 * extensions and a few limitations:
2231 * - ``%n`` is unsupported
2232 * - ``%p*`` is handled by pointer()
2234 * See pointer() or Documentation/core-api/printk-formats.rst for more
2235 * extensive description.
2237 * **Please update the documentation in both places when making changes**
2239 * The return value is the number of characters which would
2240 * be generated for the given input, excluding the trailing
2241 * '\0', as per ISO C99. If you want to have the exact
2242 * number of characters written into @buf as return value
2243 * (not including the trailing '\0'), use vscnprintf(). If the
2244 * return is greater than or equal to @size, the resulting
2245 * string is truncated.
2247 * If you're not already dealing with a va_list consider using snprintf().
2249 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2251 unsigned long long num;
2253 struct printf_spec spec = {0};
2255 /* Reject out-of-range values early. Large positive sizes are
2256 used for unknown buffer sizes. */
2257 if (WARN_ON_ONCE(size > INT_MAX))
2263 /* Make sure end is always >= buf */
2270 const char *old_fmt = fmt;
2271 int read = format_decode(fmt, &spec);
2275 switch (spec.type) {
2276 case FORMAT_TYPE_NONE: {
2279 if (copy > end - str)
2281 memcpy(str, old_fmt, copy);
2287 case FORMAT_TYPE_WIDTH:
2288 set_field_width(&spec, va_arg(args, int));
2291 case FORMAT_TYPE_PRECISION:
2292 set_precision(&spec, va_arg(args, int));
2295 case FORMAT_TYPE_CHAR: {
2298 if (!(spec.flags & LEFT)) {
2299 while (--spec.field_width > 0) {
2306 c = (unsigned char) va_arg(args, int);
2310 while (--spec.field_width > 0) {
2318 case FORMAT_TYPE_STR:
2319 str = string(str, end, va_arg(args, char *), spec);
2322 case FORMAT_TYPE_PTR:
2323 str = pointer(fmt, str, end, va_arg(args, void *),
2325 while (isalnum(*fmt))
2329 case FORMAT_TYPE_PERCENT_CHAR:
2335 case FORMAT_TYPE_INVALID:
2337 * Presumably the arguments passed gcc's type
2338 * checking, but there is no safe or sane way
2339 * for us to continue parsing the format and
2340 * fetching from the va_list; the remaining
2341 * specifiers and arguments would be out of
2347 switch (spec.type) {
2348 case FORMAT_TYPE_LONG_LONG:
2349 num = va_arg(args, long long);
2351 case FORMAT_TYPE_ULONG:
2352 num = va_arg(args, unsigned long);
2354 case FORMAT_TYPE_LONG:
2355 num = va_arg(args, long);
2357 case FORMAT_TYPE_SIZE_T:
2358 if (spec.flags & SIGN)
2359 num = va_arg(args, ssize_t);
2361 num = va_arg(args, size_t);
2363 case FORMAT_TYPE_PTRDIFF:
2364 num = va_arg(args, ptrdiff_t);
2366 case FORMAT_TYPE_UBYTE:
2367 num = (unsigned char) va_arg(args, int);
2369 case FORMAT_TYPE_BYTE:
2370 num = (signed char) va_arg(args, int);
2372 case FORMAT_TYPE_USHORT:
2373 num = (unsigned short) va_arg(args, int);
2375 case FORMAT_TYPE_SHORT:
2376 num = (short) va_arg(args, int);
2378 case FORMAT_TYPE_INT:
2379 num = (int) va_arg(args, int);
2382 num = va_arg(args, unsigned int);
2385 str = number(str, end, num, spec);
2397 /* the trailing null byte doesn't count towards the total */
2401 EXPORT_SYMBOL(vsnprintf);
2404 * vscnprintf - Format a string and place it in a buffer
2405 * @buf: The buffer to place the result into
2406 * @size: The size of the buffer, including the trailing null space
2407 * @fmt: The format string to use
2408 * @args: Arguments for the format string
2410 * The return value is the number of characters which have been written into
2411 * the @buf not including the trailing '\0'. If @size is == 0 the function
2414 * If you're not already dealing with a va_list consider using scnprintf().
2416 * See the vsnprintf() documentation for format string extensions over C99.
2418 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2422 i = vsnprintf(buf, size, fmt, args);
2424 if (likely(i < size))
2430 EXPORT_SYMBOL(vscnprintf);
2433 * snprintf - Format a string and place it in a buffer
2434 * @buf: The buffer to place the result into
2435 * @size: The size of the buffer, including the trailing null space
2436 * @fmt: The format string to use
2437 * @...: Arguments for the format string
2439 * The return value is the number of characters which would be
2440 * generated for the given input, excluding the trailing null,
2441 * as per ISO C99. If the return is greater than or equal to
2442 * @size, the resulting string is truncated.
2444 * See the vsnprintf() documentation for format string extensions over C99.
2446 int snprintf(char *buf, size_t size, const char *fmt, ...)
2451 va_start(args, fmt);
2452 i = vsnprintf(buf, size, fmt, args);
2457 EXPORT_SYMBOL(snprintf);
2460 * scnprintf - Format a string and place it in a buffer
2461 * @buf: The buffer to place the result into
2462 * @size: The size of the buffer, including the trailing null space
2463 * @fmt: The format string to use
2464 * @...: Arguments for the format string
2466 * The return value is the number of characters written into @buf not including
2467 * the trailing '\0'. If @size is == 0 the function returns 0.
2470 int scnprintf(char *buf, size_t size, const char *fmt, ...)
2475 va_start(args, fmt);
2476 i = vscnprintf(buf, size, fmt, args);
2481 EXPORT_SYMBOL(scnprintf);
2484 * vsprintf - Format a string and place it in a buffer
2485 * @buf: The buffer to place the result into
2486 * @fmt: The format string to use
2487 * @args: Arguments for the format string
2489 * The function returns the number of characters written
2490 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2493 * If you're not already dealing with a va_list consider using sprintf().
2495 * See the vsnprintf() documentation for format string extensions over C99.
2497 int vsprintf(char *buf, const char *fmt, va_list args)
2499 return vsnprintf(buf, INT_MAX, fmt, args);
2501 EXPORT_SYMBOL(vsprintf);
2504 * sprintf - Format a string and place it in a buffer
2505 * @buf: The buffer to place the result into
2506 * @fmt: The format string to use
2507 * @...: Arguments for the format string
2509 * The function returns the number of characters written
2510 * into @buf. Use snprintf() or scnprintf() in order to avoid
2513 * See the vsnprintf() documentation for format string extensions over C99.
2515 int sprintf(char *buf, const char *fmt, ...)
2520 va_start(args, fmt);
2521 i = vsnprintf(buf, INT_MAX, fmt, args);
2526 EXPORT_SYMBOL(sprintf);
2528 #ifdef CONFIG_BINARY_PRINTF
2531 * vbin_printf() - VA arguments to binary data
2532 * bstr_printf() - Binary data to text string
2536 * vbin_printf - Parse a format string and place args' binary value in a buffer
2537 * @bin_buf: The buffer to place args' binary value
2538 * @size: The size of the buffer(by words(32bits), not characters)
2539 * @fmt: The format string to use
2540 * @args: Arguments for the format string
2542 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2545 * The return value is the number of words(32bits) which would be generated for
2549 * If the return value is greater than @size, the resulting bin_buf is NOT
2550 * valid for bstr_printf().
2552 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2554 struct printf_spec spec = {0};
2558 str = (char *)bin_buf;
2559 end = (char *)(bin_buf + size);
2561 #define save_arg(type) \
2563 unsigned long long value; \
2564 if (sizeof(type) == 8) { \
2565 unsigned long long val8; \
2566 str = PTR_ALIGN(str, sizeof(u32)); \
2567 val8 = va_arg(args, unsigned long long); \
2568 if (str + sizeof(type) <= end) { \
2569 *(u32 *)str = *(u32 *)&val8; \
2570 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2574 unsigned int val4; \
2575 str = PTR_ALIGN(str, sizeof(type)); \
2576 val4 = va_arg(args, int); \
2577 if (str + sizeof(type) <= end) \
2578 *(typeof(type) *)str = (type)(long)val4; \
2579 value = (unsigned long long)val4; \
2581 str += sizeof(type); \
2586 int read = format_decode(fmt, &spec);
2590 switch (spec.type) {
2591 case FORMAT_TYPE_NONE:
2592 case FORMAT_TYPE_PERCENT_CHAR:
2594 case FORMAT_TYPE_INVALID:
2597 case FORMAT_TYPE_WIDTH:
2598 case FORMAT_TYPE_PRECISION:
2599 width = (int)save_arg(int);
2600 /* Pointers may require the width */
2602 set_field_width(&spec, width);
2605 case FORMAT_TYPE_CHAR:
2609 case FORMAT_TYPE_STR: {
2610 const char *save_str = va_arg(args, char *);
2613 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2614 || (unsigned long)save_str < PAGE_SIZE)
2615 save_str = "(null)";
2616 len = strlen(save_str) + 1;
2617 if (str + len < end)
2618 memcpy(str, save_str, len);
2623 case FORMAT_TYPE_PTR:
2624 /* Dereferenced pointers must be done now */
2626 /* Dereference of functions is still OK */
2636 if (!isalnum(*fmt)) {
2640 str = pointer(fmt, str, end, va_arg(args, void *),
2645 end[-1] = '\0'; /* Must be nul terminated */
2647 /* skip all alphanumeric pointer suffixes */
2648 while (isalnum(*fmt))
2653 switch (spec.type) {
2655 case FORMAT_TYPE_LONG_LONG:
2656 save_arg(long long);
2658 case FORMAT_TYPE_ULONG:
2659 case FORMAT_TYPE_LONG:
2660 save_arg(unsigned long);
2662 case FORMAT_TYPE_SIZE_T:
2665 case FORMAT_TYPE_PTRDIFF:
2666 save_arg(ptrdiff_t);
2668 case FORMAT_TYPE_UBYTE:
2669 case FORMAT_TYPE_BYTE:
2672 case FORMAT_TYPE_USHORT:
2673 case FORMAT_TYPE_SHORT:
2683 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2686 EXPORT_SYMBOL_GPL(vbin_printf);
2689 * bstr_printf - Format a string from binary arguments and place it in a buffer
2690 * @buf: The buffer to place the result into
2691 * @size: The size of the buffer, including the trailing null space
2692 * @fmt: The format string to use
2693 * @bin_buf: Binary arguments for the format string
2695 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2696 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2697 * a binary buffer that generated by vbin_printf.
2699 * The format follows C99 vsnprintf, but has some extensions:
2700 * see vsnprintf comment for details.
2702 * The return value is the number of characters which would
2703 * be generated for the given input, excluding the trailing
2704 * '\0', as per ISO C99. If you want to have the exact
2705 * number of characters written into @buf as return value
2706 * (not including the trailing '\0'), use vscnprintf(). If the
2707 * return is greater than or equal to @size, the resulting
2708 * string is truncated.
2710 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2712 struct printf_spec spec = {0};
2714 const char *args = (const char *)bin_buf;
2716 if (WARN_ON_ONCE(size > INT_MAX))
2722 #define get_arg(type) \
2724 typeof(type) value; \
2725 if (sizeof(type) == 8) { \
2726 args = PTR_ALIGN(args, sizeof(u32)); \
2727 *(u32 *)&value = *(u32 *)args; \
2728 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2730 args = PTR_ALIGN(args, sizeof(type)); \
2731 value = *(typeof(type) *)args; \
2733 args += sizeof(type); \
2737 /* Make sure end is always >= buf */
2744 const char *old_fmt = fmt;
2745 int read = format_decode(fmt, &spec);
2749 switch (spec.type) {
2750 case FORMAT_TYPE_NONE: {
2753 if (copy > end - str)
2755 memcpy(str, old_fmt, copy);
2761 case FORMAT_TYPE_WIDTH:
2762 set_field_width(&spec, get_arg(int));
2765 case FORMAT_TYPE_PRECISION:
2766 set_precision(&spec, get_arg(int));
2769 case FORMAT_TYPE_CHAR: {
2772 if (!(spec.flags & LEFT)) {
2773 while (--spec.field_width > 0) {
2779 c = (unsigned char) get_arg(char);
2783 while (--spec.field_width > 0) {
2791 case FORMAT_TYPE_STR: {
2792 const char *str_arg = args;
2793 args += strlen(str_arg) + 1;
2794 str = string(str, end, (char *)str_arg, spec);
2798 case FORMAT_TYPE_PTR: {
2799 bool process = false;
2801 /* Non function dereferences were already done */
2812 if (!isalnum(*fmt)) {
2816 /* Pointer dereference was already processed */
2818 len = copy = strlen(args);
2819 if (copy > end - str)
2821 memcpy(str, args, copy);
2827 str = pointer(fmt, str, end, get_arg(void *), spec);
2829 while (isalnum(*fmt))
2834 case FORMAT_TYPE_PERCENT_CHAR:
2840 case FORMAT_TYPE_INVALID:
2844 unsigned long long num;
2846 switch (spec.type) {
2848 case FORMAT_TYPE_LONG_LONG:
2849 num = get_arg(long long);
2851 case FORMAT_TYPE_ULONG:
2852 case FORMAT_TYPE_LONG:
2853 num = get_arg(unsigned long);
2855 case FORMAT_TYPE_SIZE_T:
2856 num = get_arg(size_t);
2858 case FORMAT_TYPE_PTRDIFF:
2859 num = get_arg(ptrdiff_t);
2861 case FORMAT_TYPE_UBYTE:
2862 num = get_arg(unsigned char);
2864 case FORMAT_TYPE_BYTE:
2865 num = get_arg(signed char);
2867 case FORMAT_TYPE_USHORT:
2868 num = get_arg(unsigned short);
2870 case FORMAT_TYPE_SHORT:
2871 num = get_arg(short);
2873 case FORMAT_TYPE_UINT:
2874 num = get_arg(unsigned int);
2880 str = number(str, end, num, spec);
2882 } /* switch(spec.type) */
2895 /* the trailing null byte doesn't count towards the total */
2898 EXPORT_SYMBOL_GPL(bstr_printf);
2901 * bprintf - Parse a format string and place args' binary value in a buffer
2902 * @bin_buf: The buffer to place args' binary value
2903 * @size: The size of the buffer(by words(32bits), not characters)
2904 * @fmt: The format string to use
2905 * @...: Arguments for the format string
2907 * The function returns the number of words(u32) written
2910 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2915 va_start(args, fmt);
2916 ret = vbin_printf(bin_buf, size, fmt, args);
2921 EXPORT_SYMBOL_GPL(bprintf);
2923 #endif /* CONFIG_BINARY_PRINTF */
2926 * vsscanf - Unformat a buffer into a list of arguments
2927 * @buf: input buffer
2928 * @fmt: format of buffer
2931 int vsscanf(const char *buf, const char *fmt, va_list args)
2933 const char *str = buf;
2941 unsigned long long u;
2947 /* skip any white space in format */
2948 /* white space in format matchs any amount of
2949 * white space, including none, in the input.
2951 if (isspace(*fmt)) {
2952 fmt = skip_spaces(++fmt);
2953 str = skip_spaces(str);
2956 /* anything that is not a conversion must match exactly */
2957 if (*fmt != '%' && *fmt) {
2958 if (*fmt++ != *str++)
2967 /* skip this conversion.
2968 * advance both strings to next white space
2973 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2974 /* '%*[' not yet supported, invalid format */
2979 while (!isspace(*str) && *str)
2984 /* get field width */
2986 if (isdigit(*fmt)) {
2987 field_width = skip_atoi(&fmt);
2988 if (field_width <= 0)
2992 /* get conversion qualifier */
2994 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2997 if (unlikely(qualifier == *fmt)) {
2998 if (qualifier == 'h') {
3001 } else if (qualifier == 'l') {
3012 /* return number of characters read so far */
3013 *va_arg(args, int *) = str - buf;
3027 char *s = (char *)va_arg(args, char*);
3028 if (field_width == -1)
3032 } while (--field_width > 0 && *str);
3038 char *s = (char *)va_arg(args, char *);
3039 if (field_width == -1)
3040 field_width = SHRT_MAX;
3041 /* first, skip leading white space in buffer */
3042 str = skip_spaces(str);
3044 /* now copy until next white space */
3045 while (*str && !isspace(*str) && field_width--)
3052 * Warning: This implementation of the '[' conversion specifier
3053 * deviates from its glibc counterpart in the following ways:
3054 * (1) It does NOT support ranges i.e. '-' is NOT a special
3056 * (2) It cannot match the closing bracket ']' itself
3057 * (3) A field width is required
3058 * (4) '%*[' (discard matching input) is currently not supported
3061 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3062 * buf1, buf2, buf3);
3068 char *s = (char *)va_arg(args, char *);
3069 DECLARE_BITMAP(set, 256) = {0};
3070 unsigned int len = 0;
3071 bool negate = (*fmt == '^');
3073 /* field width is required */
3074 if (field_width == -1)
3080 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3081 set_bit((u8)*fmt, set);
3083 /* no ']' or no character set found */
3089 bitmap_complement(set, set, 256);
3090 /* exclude null '\0' byte */
3094 /* match must be non-empty */
3095 if (!test_bit((u8)*str, set))
3098 while (test_bit((u8)*str, set) && field_width--)
3120 /* looking for '%' in str */
3125 /* invalid format; stop here */
3129 /* have some sort of integer conversion.
3130 * first, skip white space in buffer.
3132 str = skip_spaces(str);
3135 if (is_sign && digit == '-')
3139 || (base == 16 && !isxdigit(digit))
3140 || (base == 10 && !isdigit(digit))
3141 || (base == 8 && (!isdigit(digit) || digit > '7'))
3142 || (base == 0 && !isdigit(digit)))
3146 val.s = simple_strntoll(str,
3147 field_width >= 0 ? field_width : INT_MAX,
3150 val.u = simple_strntoull(str,
3151 field_width >= 0 ? field_width : INT_MAX,
3154 switch (qualifier) {
3155 case 'H': /* that's 'hh' in format */
3157 *va_arg(args, signed char *) = val.s;
3159 *va_arg(args, unsigned char *) = val.u;
3163 *va_arg(args, short *) = val.s;
3165 *va_arg(args, unsigned short *) = val.u;
3169 *va_arg(args, long *) = val.s;
3171 *va_arg(args, unsigned long *) = val.u;
3175 *va_arg(args, long long *) = val.s;
3177 *va_arg(args, unsigned long long *) = val.u;
3180 *va_arg(args, size_t *) = val.u;
3184 *va_arg(args, int *) = val.s;
3186 *va_arg(args, unsigned int *) = val.u;
3198 EXPORT_SYMBOL(vsscanf);
3201 * sscanf - Unformat a buffer into a list of arguments
3202 * @buf: input buffer
3203 * @fmt: formatting of buffer
3204 * @...: resulting arguments
3206 int sscanf(const char *buf, const char *fmt, ...)
3211 va_start(args, fmt);
3212 i = vsscanf(buf, fmt, args);
3217 EXPORT_SYMBOL(sscanf);