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/build_bug.h>
21 #include <linux/clk.h>
22 #include <linux/clk-provider.h>
23 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
24 #include <linux/types.h>
25 #include <linux/string.h>
26 #include <linux/ctype.h>
27 #include <linux/kernel.h>
28 #include <linux/kallsyms.h>
29 #include <linux/math64.h>
30 #include <linux/uaccess.h>
31 #include <linux/ioport.h>
32 #include <linux/dcache.h>
33 #include <linux/cred.h>
34 #include <linux/rtc.h>
35 #include <linux/uuid.h>
37 #include <net/addrconf.h>
38 #include <linux/siphash.h>
39 #include <linux/compiler.h>
41 #include <linux/blkdev.h>
44 #include "../mm/internal.h" /* For the trace_print_flags arrays */
46 #include <asm/page.h> /* for PAGE_SIZE */
47 #include <asm/byteorder.h> /* cpu_to_le16 */
49 #include <linux/string_helpers.h>
53 * simple_strtoull - convert a string to an unsigned long long
54 * @cp: The start of the string
55 * @endp: A pointer to the end of the parsed string will be placed here
56 * @base: The number base to use
58 * This function is obsolete. Please use kstrtoull instead.
60 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
62 unsigned long long result;
65 cp = _parse_integer_fixup_radix(cp, &base);
66 rv = _parse_integer(cp, base, &result);
68 cp += (rv & ~KSTRTOX_OVERFLOW);
75 EXPORT_SYMBOL(simple_strtoull);
78 * simple_strtoul - convert a string to an unsigned long
79 * @cp: The start of the string
80 * @endp: A pointer to the end of the parsed string will be placed here
81 * @base: The number base to use
83 * This function is obsolete. Please use kstrtoul instead.
85 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
87 return simple_strtoull(cp, endp, base);
89 EXPORT_SYMBOL(simple_strtoul);
92 * simple_strtol - convert a string to a signed long
93 * @cp: The start of the string
94 * @endp: A pointer to the end of the parsed string will be placed here
95 * @base: The number base to use
97 * This function is obsolete. Please use kstrtol instead.
99 long simple_strtol(const char *cp, char **endp, unsigned int base)
102 return -simple_strtoul(cp + 1, endp, base);
104 return simple_strtoul(cp, endp, base);
106 EXPORT_SYMBOL(simple_strtol);
109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use
114 * This function is obsolete. Please use kstrtoll instead.
116 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
119 return -simple_strtoull(cp + 1, endp, base);
121 return simple_strtoull(cp, endp, base);
123 EXPORT_SYMBOL(simple_strtoll);
125 static noinline_for_stack
126 int skip_atoi(const char **s)
131 i = i*10 + *((*s)++) - '0';
132 } while (isdigit(**s));
138 * Decimal conversion is by far the most typical, and is used for
139 * /proc and /sys data. This directly impacts e.g. top performance
140 * with many processes running. We optimize it for speed by emitting
141 * two characters at a time, using a 200 byte lookup table. This
142 * roughly halves the number of multiplications compared to computing
143 * the digits one at a time. Implementation strongly inspired by the
144 * previous version, which in turn used ideas described at
145 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
146 * from the author, Douglas W. Jones).
148 * It turns out there is precisely one 26 bit fixed-point
149 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
150 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
151 * range happens to be somewhat larger (x <= 1073741898), but that's
152 * irrelevant for our purpose.
154 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
155 * need a 32x32->64 bit multiply, so we simply use the same constant.
157 * For dividing a number in the range [100, 10^4-1] by 100, there are
158 * several options. The simplest is (x * 0x147b) >> 19, which is valid
159 * for all x <= 43698.
162 static const u16 decpair[100] = {
163 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
164 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
165 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
166 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
167 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
168 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
169 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
170 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
171 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
172 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
173 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
178 * This will print a single '0' even if r == 0, since we would
179 * immediately jump to out_r where two 0s would be written but only
180 * one of them accounted for in buf. This is needed by ip4_string
181 * below. All other callers pass a non-zero value of r.
183 static noinline_for_stack
184 char *put_dec_trunc8(char *buf, unsigned r)
192 /* 100 <= r < 10^8 */
193 q = (r * (u64)0x28f5c29) >> 32;
194 *((u16 *)buf) = decpair[r - 100*q];
201 /* 100 <= q < 10^6 */
202 r = (q * (u64)0x28f5c29) >> 32;
203 *((u16 *)buf) = decpair[q - 100*r];
210 /* 100 <= r < 10^4 */
211 q = (r * 0x147b) >> 19;
212 *((u16 *)buf) = decpair[r - 100*q];
219 *((u16 *)buf) = decpair[r];
220 buf += r < 10 ? 1 : 2;
224 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
225 static noinline_for_stack
226 char *put_dec_full8(char *buf, unsigned r)
231 q = (r * (u64)0x28f5c29) >> 32;
232 *((u16 *)buf) = decpair[r - 100*q];
236 r = (q * (u64)0x28f5c29) >> 32;
237 *((u16 *)buf) = decpair[q - 100*r];
241 q = (r * 0x147b) >> 19;
242 *((u16 *)buf) = decpair[r - 100*q];
246 *((u16 *)buf) = decpair[q];
251 static noinline_for_stack
252 char *put_dec(char *buf, unsigned long long n)
254 if (n >= 100*1000*1000)
255 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
256 /* 1 <= n <= 1.6e11 */
257 if (n >= 100*1000*1000)
258 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
260 return put_dec_trunc8(buf, n);
263 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
266 put_dec_full4(char *buf, unsigned r)
271 q = (r * 0x147b) >> 19;
272 *((u16 *)buf) = decpair[r - 100*q];
275 *((u16 *)buf) = decpair[q];
279 * Call put_dec_full4 on x % 10000, return x / 10000.
280 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
281 * holds for all x < 1,128,869,999. The largest value this
282 * helper will ever be asked to convert is 1,125,520,955.
283 * (second call in the put_dec code, assuming n is all-ones).
285 static noinline_for_stack
286 unsigned put_dec_helper4(char *buf, unsigned x)
288 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
290 put_dec_full4(buf, x - q * 10000);
294 /* Based on code by Douglas W. Jones found at
295 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
296 * (with permission from the author).
297 * Performs no 64-bit division and hence should be fast on 32-bit machines.
300 char *put_dec(char *buf, unsigned long long n)
302 uint32_t d3, d2, d1, q, h;
304 if (n < 100*1000*1000)
305 return put_dec_trunc8(buf, n);
307 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
310 d3 = (h >> 16); /* implicit "& 0xffff" */
312 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
313 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
314 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
315 q = put_dec_helper4(buf, q);
317 q += 7671 * d3 + 9496 * d2 + 6 * d1;
318 q = put_dec_helper4(buf+4, q);
320 q += 4749 * d3 + 42 * d2;
321 q = put_dec_helper4(buf+8, q);
326 buf = put_dec_trunc8(buf, q);
327 else while (buf[-1] == '0')
336 * Convert passed number to decimal string.
337 * Returns the length of string. On buffer overflow, returns 0.
339 * If speed is not important, use snprintf(). It's easy to read the code.
341 int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
343 /* put_dec requires 2-byte alignment of the buffer. */
344 char tmp[sizeof(num) * 3] __aligned(2);
347 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
352 len = put_dec(tmp, num) - tmp;
355 if (len > size || width > size)
360 for (idx = 0; idx < width; idx++)
366 for (idx = 0; idx < len; ++idx)
367 buf[idx + width] = tmp[len - idx - 1];
372 #define SIGN 1 /* unsigned/signed, must be 1 */
373 #define LEFT 2 /* left justified */
374 #define PLUS 4 /* show plus */
375 #define SPACE 8 /* space if plus */
376 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
377 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
378 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
381 FORMAT_TYPE_NONE, /* Just a string part */
383 FORMAT_TYPE_PRECISION,
387 FORMAT_TYPE_PERCENT_CHAR,
389 FORMAT_TYPE_LONG_LONG,
403 unsigned int type:8; /* format_type enum */
404 signed int field_width:24; /* width of output field */
405 unsigned int flags:8; /* flags to number() */
406 unsigned int base:8; /* number base, 8, 10 or 16 only */
407 signed int precision:16; /* # of digits/chars */
409 static_assert(sizeof(struct printf_spec) == 8);
411 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
412 #define PRECISION_MAX ((1 << 15) - 1)
414 static noinline_for_stack
415 char *number(char *buf, char *end, unsigned long long num,
416 struct printf_spec spec)
418 /* put_dec requires 2-byte alignment of the buffer. */
419 char tmp[3 * sizeof(num)] __aligned(2);
422 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
424 bool is_zero = num == 0LL;
425 int field_width = spec.field_width;
426 int precision = spec.precision;
428 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
429 * produces same digits or (maybe lowercased) letters */
430 locase = (spec.flags & SMALL);
431 if (spec.flags & LEFT)
432 spec.flags &= ~ZEROPAD;
434 if (spec.flags & SIGN) {
435 if ((signed long long)num < 0) {
437 num = -(signed long long)num;
439 } else if (spec.flags & PLUS) {
442 } else if (spec.flags & SPACE) {
454 /* generate full string in tmp[], in reverse order */
457 tmp[i++] = hex_asc_upper[num] | locase;
458 else if (spec.base != 10) { /* 8 or 16 */
459 int mask = spec.base - 1;
465 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
468 } else { /* base 10 */
469 i = put_dec(tmp, num) - tmp;
472 /* printing 100 using %2d gives "100", not "00" */
475 /* leading space padding */
476 field_width -= precision;
477 if (!(spec.flags & (ZEROPAD | LEFT))) {
478 while (--field_width >= 0) {
490 /* "0x" / "0" prefix */
492 if (spec.base == 16 || !is_zero) {
497 if (spec.base == 16) {
499 *buf = ('X' | locase);
503 /* zero or space padding */
504 if (!(spec.flags & LEFT)) {
505 char c = ' ' + (spec.flags & ZEROPAD);
506 BUILD_BUG_ON(' ' + ZEROPAD != '0');
507 while (--field_width >= 0) {
513 /* hmm even more zero padding? */
514 while (i <= --precision) {
519 /* actual digits of result */
525 /* trailing space padding */
526 while (--field_width >= 0) {
535 static noinline_for_stack
536 char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
538 struct printf_spec spec;
540 spec.type = FORMAT_TYPE_PTR;
541 spec.field_width = 2 + 2 * size; /* 0x + hex */
542 spec.flags = SPECIAL | SMALL | ZEROPAD;
546 return number(buf, end, num, spec);
549 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
552 if (buf >= end) /* nowhere to put anything */
555 if (size <= spaces) {
556 memset(buf, ' ', size);
560 if (len > size - spaces)
562 memmove(buf + spaces, buf, len);
564 memset(buf, ' ', spaces);
568 * Handle field width padding for a string.
569 * @buf: current buffer position
570 * @n: length of string
571 * @end: end of output buffer
572 * @spec: for field width and flags
573 * Returns: new buffer position after padding.
575 static noinline_for_stack
576 char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
580 if (likely(n >= spec.field_width))
582 /* we want to pad the sucker */
583 spaces = spec.field_width - n;
584 if (!(spec.flags & LEFT)) {
585 move_right(buf - n, end, n, spaces);
596 /* Handle string from a well known address. */
597 static char *string_nocheck(char *buf, char *end, const char *s,
598 struct printf_spec spec)
601 size_t lim = spec.precision;
612 return widen_string(buf, len, end, spec);
615 /* Be careful: error messages must fit into the given buffer. */
616 static char *error_string(char *buf, char *end, const char *s,
617 struct printf_spec spec)
620 * Hard limit to avoid a completely insane messages. It actually
621 * works pretty well because most error messages are in
622 * the many pointer format modifiers.
624 if (spec.precision == -1)
625 spec.precision = 2 * sizeof(void *);
627 return string_nocheck(buf, end, s, spec);
631 * This is not a fool-proof test. 99% of the time that this will fault is
632 * due to a bad pointer, not one that crosses into bad memory. Just test
633 * the address to make sure it doesn't fault due to a poorly added printk
636 static const char *check_pointer_msg(const void *ptr)
643 if (probe_kernel_address(ptr, byte))
649 static int check_pointer(char **buf, char *end, const void *ptr,
650 struct printf_spec spec)
654 err_msg = check_pointer_msg(ptr);
656 *buf = error_string(*buf, end, err_msg, spec);
663 static noinline_for_stack
664 char *string(char *buf, char *end, const char *s,
665 struct printf_spec spec)
667 if (check_pointer(&buf, end, s, spec))
670 return string_nocheck(buf, end, s, spec);
673 static char *pointer_string(char *buf, char *end,
675 struct printf_spec spec)
679 if (spec.field_width == -1) {
680 spec.field_width = 2 * sizeof(ptr);
681 spec.flags |= ZEROPAD;
684 return number(buf, end, (unsigned long int)ptr, spec);
687 /* Make pointers available for printing early in the boot sequence. */
688 static int debug_boot_weak_hash __ro_after_init;
690 static int __init debug_boot_weak_hash_enable(char *str)
692 debug_boot_weak_hash = 1;
693 pr_info("debug_boot_weak_hash enabled\n");
696 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
698 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
699 static siphash_key_t ptr_key __read_mostly;
701 static void enable_ptr_key_workfn(struct work_struct *work)
703 get_random_bytes(&ptr_key, sizeof(ptr_key));
704 /* Needs to run from preemptible context */
705 static_branch_disable(¬_filled_random_ptr_key);
708 static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
710 static void fill_random_ptr_key(struct random_ready_callback *unused)
712 /* This may be in an interrupt handler. */
713 queue_work(system_unbound_wq, &enable_ptr_key_work);
716 static struct random_ready_callback random_ready = {
717 .func = fill_random_ptr_key
720 static int __init initialize_ptr_random(void)
722 int key_size = sizeof(ptr_key);
725 /* Use hw RNG if available. */
726 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
727 static_branch_disable(¬_filled_random_ptr_key);
731 ret = add_random_ready_callback(&random_ready);
734 } else if (ret == -EALREADY) {
735 /* This is in preemptible context */
736 enable_ptr_key_workfn(&enable_ptr_key_work);
742 early_initcall(initialize_ptr_random);
744 /* Maps a pointer to a 32 bit unique identifier. */
745 static char *ptr_to_id(char *buf, char *end, const void *ptr,
746 struct printf_spec spec)
748 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
749 unsigned long hashval;
751 /* When debugging early boot use non-cryptographically secure hash. */
752 if (unlikely(debug_boot_weak_hash)) {
753 hashval = hash_long((unsigned long)ptr, 32);
754 return pointer_string(buf, end, (const void *)hashval, spec);
757 if (static_branch_unlikely(¬_filled_random_ptr_key)) {
758 spec.field_width = 2 * sizeof(ptr);
759 /* string length must be less than default_width */
760 return error_string(buf, end, str, spec);
764 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
766 * Mask off the first 32 bits, this makes explicit that we have
767 * modified the address (and 32 bits is plenty for a unique ID).
769 hashval = hashval & 0xffffffff;
771 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
773 return pointer_string(buf, end, (const void *)hashval, spec);
776 int kptr_restrict __read_mostly;
778 static noinline_for_stack
779 char *restricted_pointer(char *buf, char *end, const void *ptr,
780 struct printf_spec spec)
782 switch (kptr_restrict) {
784 /* Handle as %p, hash and do _not_ leak addresses. */
785 return ptr_to_id(buf, end, ptr, spec);
787 const struct cred *cred;
790 * kptr_restrict==1 cannot be used in IRQ context
791 * because its test for CAP_SYSLOG would be meaningless.
793 if (in_irq() || in_serving_softirq() || in_nmi()) {
794 if (spec.field_width == -1)
795 spec.field_width = 2 * sizeof(ptr);
796 return error_string(buf, end, "pK-error", spec);
800 * Only print the real pointer value if the current
801 * process has CAP_SYSLOG and is running with the
802 * same credentials it started with. This is because
803 * access to files is checked at open() time, but %pK
804 * checks permission at read() time. We don't want to
805 * leak pointer values if a binary opens a file using
806 * %pK and then elevates privileges before reading it.
808 cred = current_cred();
809 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
810 !uid_eq(cred->euid, cred->uid) ||
811 !gid_eq(cred->egid, cred->gid))
817 /* Always print 0's for %pK */
822 return pointer_string(buf, end, ptr, spec);
825 static noinline_for_stack
826 char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
829 const char *array[4], *s;
830 const struct dentry *p;
835 case '2': case '3': case '4':
836 depth = fmt[1] - '0';
843 for (i = 0; i < depth; i++, d = p) {
844 if (check_pointer(&buf, end, d, spec)) {
849 p = READ_ONCE(d->d_parent);
850 array[i] = READ_ONCE(d->d_name.name);
859 for (n = 0; n != spec.precision; n++, buf++) {
871 return widen_string(buf, n, end, spec);
875 static noinline_for_stack
876 char *bdev_name(char *buf, char *end, struct block_device *bdev,
877 struct printf_spec spec, const char *fmt)
881 if (check_pointer(&buf, end, bdev, spec))
885 buf = string(buf, end, hd->disk_name, spec);
886 if (bdev->bd_part->partno) {
887 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
892 buf = number(buf, end, bdev->bd_part->partno, spec);
898 static noinline_for_stack
899 char *symbol_string(char *buf, char *end, void *ptr,
900 struct printf_spec spec, const char *fmt)
903 #ifdef CONFIG_KALLSYMS
904 char sym[KSYM_SYMBOL_LEN];
908 ptr = __builtin_extract_return_addr(ptr);
909 value = (unsigned long)ptr;
911 #ifdef CONFIG_KALLSYMS
913 sprint_backtrace(sym, value);
914 else if (*fmt != 'f' && *fmt != 's')
915 sprint_symbol(sym, value);
917 sprint_symbol_no_offset(sym, value);
919 return string_nocheck(buf, end, sym, spec);
921 return special_hex_number(buf, end, value, sizeof(void *));
925 static const struct printf_spec default_str_spec = {
930 static const struct printf_spec default_flag_spec = {
933 .flags = SPECIAL | SMALL,
936 static const struct printf_spec default_dec_spec = {
941 static const struct printf_spec default_dec02_spec = {
948 static const struct printf_spec default_dec04_spec = {
955 static noinline_for_stack
956 char *resource_string(char *buf, char *end, struct resource *res,
957 struct printf_spec spec, const char *fmt)
959 #ifndef IO_RSRC_PRINTK_SIZE
960 #define IO_RSRC_PRINTK_SIZE 6
963 #ifndef MEM_RSRC_PRINTK_SIZE
964 #define MEM_RSRC_PRINTK_SIZE 10
966 static const struct printf_spec io_spec = {
968 .field_width = IO_RSRC_PRINTK_SIZE,
970 .flags = SPECIAL | SMALL | ZEROPAD,
972 static const struct printf_spec mem_spec = {
974 .field_width = MEM_RSRC_PRINTK_SIZE,
976 .flags = SPECIAL | SMALL | ZEROPAD,
978 static const struct printf_spec bus_spec = {
982 .flags = SMALL | ZEROPAD,
984 static const struct printf_spec str_spec = {
990 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
991 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
992 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
993 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
994 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
995 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
996 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
997 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
999 char *p = sym, *pend = sym + sizeof(sym);
1000 int decode = (fmt[0] == 'R') ? 1 : 0;
1001 const struct printf_spec *specp;
1003 if (check_pointer(&buf, end, res, spec))
1007 if (res->flags & IORESOURCE_IO) {
1008 p = string_nocheck(p, pend, "io ", str_spec);
1010 } else if (res->flags & IORESOURCE_MEM) {
1011 p = string_nocheck(p, pend, "mem ", str_spec);
1013 } else if (res->flags & IORESOURCE_IRQ) {
1014 p = string_nocheck(p, pend, "irq ", str_spec);
1015 specp = &default_dec_spec;
1016 } else if (res->flags & IORESOURCE_DMA) {
1017 p = string_nocheck(p, pend, "dma ", str_spec);
1018 specp = &default_dec_spec;
1019 } else if (res->flags & IORESOURCE_BUS) {
1020 p = string_nocheck(p, pend, "bus ", str_spec);
1023 p = string_nocheck(p, pend, "??? ", str_spec);
1027 if (decode && res->flags & IORESOURCE_UNSET) {
1028 p = string_nocheck(p, pend, "size ", str_spec);
1029 p = number(p, pend, resource_size(res), *specp);
1031 p = number(p, pend, res->start, *specp);
1032 if (res->start != res->end) {
1034 p = number(p, pend, res->end, *specp);
1038 if (res->flags & IORESOURCE_MEM_64)
1039 p = string_nocheck(p, pend, " 64bit", str_spec);
1040 if (res->flags & IORESOURCE_PREFETCH)
1041 p = string_nocheck(p, pend, " pref", str_spec);
1042 if (res->flags & IORESOURCE_WINDOW)
1043 p = string_nocheck(p, pend, " window", str_spec);
1044 if (res->flags & IORESOURCE_DISABLED)
1045 p = string_nocheck(p, pend, " disabled", str_spec);
1047 p = string_nocheck(p, pend, " flags ", str_spec);
1048 p = number(p, pend, res->flags, default_flag_spec);
1053 return string_nocheck(buf, end, sym, spec);
1056 static noinline_for_stack
1057 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1060 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
1061 negative value, fallback to the default */
1064 if (spec.field_width == 0)
1065 /* nothing to print */
1068 if (check_pointer(&buf, end, addr, spec))
1086 if (spec.field_width > 0)
1087 len = min_t(int, spec.field_width, 64);
1089 for (i = 0; i < len; ++i) {
1091 *buf = hex_asc_hi(addr[i]);
1094 *buf = hex_asc_lo(addr[i]);
1097 if (separator && i != len - 1) {
1107 static noinline_for_stack
1108 char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1109 struct printf_spec spec, const char *fmt)
1111 const int CHUNKSZ = 32;
1112 int nr_bits = max_t(int, spec.field_width, 0);
1116 if (check_pointer(&buf, end, bitmap, spec))
1119 /* reused to print numbers */
1120 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1122 chunksz = nr_bits & (CHUNKSZ - 1);
1126 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1127 for (; i >= 0; i -= CHUNKSZ) {
1131 chunkmask = ((1ULL << chunksz) - 1);
1132 word = i / BITS_PER_LONG;
1133 bit = i % BITS_PER_LONG;
1134 val = (bitmap[word] >> bit) & chunkmask;
1143 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1144 buf = number(buf, end, val, spec);
1151 static noinline_for_stack
1152 char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1153 struct printf_spec spec, const char *fmt)
1155 int nr_bits = max_t(int, spec.field_width, 0);
1156 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1157 int cur, rbot, rtop;
1160 if (check_pointer(&buf, end, bitmap, spec))
1163 rbot = cur = find_first_bit(bitmap, nr_bits);
1164 while (cur < nr_bits) {
1166 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1167 if (cur < nr_bits && cur <= rtop + 1)
1177 buf = number(buf, end, rbot, default_dec_spec);
1183 buf = number(buf, end, rtop, default_dec_spec);
1191 static noinline_for_stack
1192 char *mac_address_string(char *buf, char *end, u8 *addr,
1193 struct printf_spec spec, const char *fmt)
1195 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1199 bool reversed = false;
1201 if (check_pointer(&buf, end, addr, spec))
1218 for (i = 0; i < 6; i++) {
1220 p = hex_byte_pack(p, addr[5 - i]);
1222 p = hex_byte_pack(p, addr[i]);
1224 if (fmt[0] == 'M' && i != 5)
1229 return string_nocheck(buf, end, mac_addr, spec);
1232 static noinline_for_stack
1233 char *ip4_string(char *p, const u8 *addr, const char *fmt)
1236 bool leading_zeros = (fmt[0] == 'i');
1261 for (i = 0; i < 4; i++) {
1262 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
1263 int digits = put_dec_trunc8(temp, addr[index]) - temp;
1264 if (leading_zeros) {
1270 /* reverse the digits in the quad */
1272 *p++ = temp[digits];
1282 static noinline_for_stack
1283 char *ip6_compressed_string(char *p, const char *addr)
1286 unsigned char zerolength[8];
1291 bool needcolon = false;
1293 struct in6_addr in6;
1295 memcpy(&in6, addr, sizeof(struct in6_addr));
1297 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1299 memset(zerolength, 0, sizeof(zerolength));
1306 /* find position of longest 0 run */
1307 for (i = 0; i < range; i++) {
1308 for (j = i; j < range; j++) {
1309 if (in6.s6_addr16[j] != 0)
1314 for (i = 0; i < range; i++) {
1315 if (zerolength[i] > longest) {
1316 longest = zerolength[i];
1320 if (longest == 1) /* don't compress a single 0 */
1324 for (i = 0; i < range; i++) {
1325 if (i == colonpos) {
1326 if (needcolon || i == 0)
1337 /* hex u16 without leading 0s */
1338 word = ntohs(in6.s6_addr16[i]);
1343 p = hex_byte_pack(p, hi);
1345 *p++ = hex_asc_lo(hi);
1346 p = hex_byte_pack(p, lo);
1349 p = hex_byte_pack(p, lo);
1351 *p++ = hex_asc_lo(lo);
1358 p = ip4_string(p, &in6.s6_addr[12], "I4");
1365 static noinline_for_stack
1366 char *ip6_string(char *p, const char *addr, const char *fmt)
1370 for (i = 0; i < 8; i++) {
1371 p = hex_byte_pack(p, *addr++);
1372 p = hex_byte_pack(p, *addr++);
1373 if (fmt[0] == 'I' && i != 7)
1381 static noinline_for_stack
1382 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1383 struct printf_spec spec, const char *fmt)
1385 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1387 if (fmt[0] == 'I' && fmt[2] == 'c')
1388 ip6_compressed_string(ip6_addr, addr);
1390 ip6_string(ip6_addr, addr, fmt);
1392 return string_nocheck(buf, end, ip6_addr, spec);
1395 static noinline_for_stack
1396 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1397 struct printf_spec spec, const char *fmt)
1399 char ip4_addr[sizeof("255.255.255.255")];
1401 ip4_string(ip4_addr, addr, fmt);
1403 return string_nocheck(buf, end, ip4_addr, spec);
1406 static noinline_for_stack
1407 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1408 struct printf_spec spec, const char *fmt)
1410 bool have_p = false, have_s = false, have_f = false, have_c = false;
1411 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1412 sizeof(":12345") + sizeof("/123456789") +
1413 sizeof("%1234567890")];
1414 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1415 const u8 *addr = (const u8 *) &sa->sin6_addr;
1416 char fmt6[2] = { fmt[0], '6' };
1420 while (isalpha(*++fmt)) {
1437 if (have_p || have_s || have_f) {
1442 if (fmt6[0] == 'I' && have_c)
1443 p = ip6_compressed_string(ip6_addr + off, addr);
1445 p = ip6_string(ip6_addr + off, addr, fmt6);
1447 if (have_p || have_s || have_f)
1452 p = number(p, pend, ntohs(sa->sin6_port), spec);
1456 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1457 IPV6_FLOWINFO_MASK), spec);
1461 p = number(p, pend, sa->sin6_scope_id, spec);
1465 return string_nocheck(buf, end, ip6_addr, spec);
1468 static noinline_for_stack
1469 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1470 struct printf_spec spec, const char *fmt)
1472 bool have_p = false;
1473 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1474 char *pend = ip4_addr + sizeof(ip4_addr);
1475 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1476 char fmt4[3] = { fmt[0], '4', 0 };
1479 while (isalpha(*++fmt)) {
1493 p = ip4_string(ip4_addr, addr, fmt4);
1496 p = number(p, pend, ntohs(sa->sin_port), spec);
1500 return string_nocheck(buf, end, ip4_addr, spec);
1503 static noinline_for_stack
1504 char *ip_addr_string(char *buf, char *end, const void *ptr,
1505 struct printf_spec spec, const char *fmt)
1509 if (check_pointer(&buf, end, ptr, spec))
1514 return ip6_addr_string(buf, end, ptr, spec, fmt);
1516 return ip4_addr_string(buf, end, ptr, spec, fmt);
1519 struct sockaddr raw;
1520 struct sockaddr_in v4;
1521 struct sockaddr_in6 v6;
1524 switch (sa->raw.sa_family) {
1526 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1528 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1530 return error_string(buf, end, "(einval)", spec);
1534 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1535 return error_string(buf, end, err_fmt_msg, spec);
1538 static noinline_for_stack
1539 char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1544 unsigned int flags = 0;
1547 if (spec.field_width == 0)
1548 return buf; /* nothing to print */
1550 if (check_pointer(&buf, end, addr, spec))
1554 switch (fmt[count++]) {
1556 flags |= ESCAPE_ANY;
1559 flags |= ESCAPE_SPECIAL;
1562 flags |= ESCAPE_HEX;
1565 flags |= ESCAPE_NULL;
1568 flags |= ESCAPE_OCTAL;
1574 flags |= ESCAPE_SPACE;
1583 flags = ESCAPE_ANY_NP;
1585 len = spec.field_width < 0 ? 1 : spec.field_width;
1588 * string_escape_mem() writes as many characters as it can to
1589 * the given buffer, and returns the total size of the output
1590 * had the buffer been big enough.
1592 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1597 static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1598 struct printf_spec spec, const char *fmt)
1602 if (check_pointer(&buf, end, va_fmt, spec))
1605 va_copy(va, *va_fmt->va);
1606 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1612 static noinline_for_stack
1613 char *uuid_string(char *buf, char *end, const u8 *addr,
1614 struct printf_spec spec, const char *fmt)
1616 char uuid[UUID_STRING_LEN + 1];
1619 const u8 *index = uuid_index;
1622 if (check_pointer(&buf, end, addr, spec))
1627 uc = true; /* fall-through */
1636 for (i = 0; i < 16; i++) {
1638 p = hex_byte_pack_upper(p, addr[index[i]]);
1640 p = hex_byte_pack(p, addr[index[i]]);
1653 return string_nocheck(buf, end, uuid, spec);
1656 static noinline_for_stack
1657 char *netdev_bits(char *buf, char *end, const void *addr,
1658 struct printf_spec spec, const char *fmt)
1660 unsigned long long num;
1663 if (check_pointer(&buf, end, addr, spec))
1668 num = *(const netdev_features_t *)addr;
1669 size = sizeof(netdev_features_t);
1672 return error_string(buf, end, "(%pN?)", spec);
1675 return special_hex_number(buf, end, num, size);
1678 static noinline_for_stack
1679 char *address_val(char *buf, char *end, const void *addr,
1680 struct printf_spec spec, const char *fmt)
1682 unsigned long long num;
1685 if (check_pointer(&buf, end, addr, spec))
1690 num = *(const dma_addr_t *)addr;
1691 size = sizeof(dma_addr_t);
1695 num = *(const phys_addr_t *)addr;
1696 size = sizeof(phys_addr_t);
1700 return special_hex_number(buf, end, num, size);
1703 static noinline_for_stack
1704 char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1706 int year = tm->tm_year + (r ? 0 : 1900);
1707 int mon = tm->tm_mon + (r ? 0 : 1);
1709 buf = number(buf, end, year, default_dec04_spec);
1714 buf = number(buf, end, mon, default_dec02_spec);
1719 return number(buf, end, tm->tm_mday, default_dec02_spec);
1722 static noinline_for_stack
1723 char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1725 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1730 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1735 return number(buf, end, tm->tm_sec, default_dec02_spec);
1738 static noinline_for_stack
1739 char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1740 struct printf_spec spec, const char *fmt)
1742 bool have_t = true, have_d = true;
1746 if (check_pointer(&buf, end, tm, spec))
1749 switch (fmt[count]) {
1760 raw = fmt[count] == 'r';
1763 buf = date_str(buf, end, tm, raw);
1764 if (have_d && have_t) {
1765 /* Respect ISO 8601 */
1771 buf = time_str(buf, end, tm, raw);
1776 static noinline_for_stack
1777 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1782 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
1784 return error_string(buf, end, "(%ptR?)", spec);
1788 static noinline_for_stack
1789 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1792 if (!IS_ENABLED(CONFIG_HAVE_CLK))
1793 return error_string(buf, end, "(%pC?)", spec);
1795 if (check_pointer(&buf, end, clk, spec))
1801 #ifdef CONFIG_COMMON_CLK
1802 return string(buf, end, __clk_get_name(clk), spec);
1804 return error_string(buf, end, "(%pC?)", spec);
1810 char *format_flags(char *buf, char *end, unsigned long flags,
1811 const struct trace_print_flags *names)
1815 for ( ; flags && names->name; names++) {
1817 if ((flags & mask) != mask)
1820 buf = string(buf, end, names->name, default_str_spec);
1831 buf = number(buf, end, flags, default_flag_spec);
1836 static noinline_for_stack
1837 char *flags_string(char *buf, char *end, void *flags_ptr,
1838 struct printf_spec spec, const char *fmt)
1840 unsigned long flags;
1841 const struct trace_print_flags *names;
1843 if (check_pointer(&buf, end, flags_ptr, spec))
1848 flags = *(unsigned long *)flags_ptr;
1849 /* Remove zone id */
1850 flags &= (1UL << NR_PAGEFLAGS) - 1;
1851 names = pageflag_names;
1854 flags = *(unsigned long *)flags_ptr;
1855 names = vmaflag_names;
1858 flags = *(gfp_t *)flags_ptr;
1859 names = gfpflag_names;
1862 return error_string(buf, end, "(%pG?)", spec);
1865 return format_flags(buf, end, flags, names);
1868 static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1870 for ( ; np && depth; depth--)
1873 return kbasename(np->full_name);
1876 static noinline_for_stack
1877 char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1880 const struct device_node *parent = np->parent;
1882 /* special case for root node */
1884 return string_nocheck(buf, end, "/", default_str_spec);
1886 for (depth = 0; parent->parent; depth++)
1887 parent = parent->parent;
1889 for ( ; depth >= 0; depth--) {
1890 buf = string_nocheck(buf, end, "/", default_str_spec);
1891 buf = string(buf, end, device_node_name_for_depth(np, depth),
1897 static noinline_for_stack
1898 char *device_node_string(char *buf, char *end, struct device_node *dn,
1899 struct printf_spec spec, const char *fmt)
1901 char tbuf[sizeof("xxxx") + 1];
1904 char *buf_start = buf;
1905 struct property *prop;
1906 bool has_mult, pass;
1907 static const struct printf_spec num_spec = {
1914 struct printf_spec str_spec = spec;
1915 str_spec.field_width = -1;
1917 if (!IS_ENABLED(CONFIG_OF))
1918 return error_string(buf, end, "(%pOF?)", spec);
1920 if (check_pointer(&buf, end, dn, spec))
1923 /* simple case without anything any more format specifiers */
1925 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1928 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1937 case 'f': /* full_name */
1938 buf = device_node_gen_full_name(dn, buf, end);
1940 case 'n': /* name */
1941 p = kbasename(of_node_full_name(dn));
1942 precision = str_spec.precision;
1943 str_spec.precision = strchrnul(p, '@') - p;
1944 buf = string(buf, end, p, str_spec);
1945 str_spec.precision = precision;
1947 case 'p': /* phandle */
1948 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1950 case 'P': /* path-spec */
1951 p = kbasename(of_node_full_name(dn));
1954 buf = string(buf, end, p, str_spec);
1956 case 'F': /* flags */
1957 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1958 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1959 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1960 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1962 buf = string_nocheck(buf, end, tbuf, str_spec);
1964 case 'c': /* major compatible string */
1965 ret = of_property_read_string(dn, "compatible", &p);
1967 buf = string(buf, end, p, str_spec);
1969 case 'C': /* full compatible string */
1971 of_property_for_each_string(dn, "compatible", prop, p) {
1973 buf = string_nocheck(buf, end, ",", str_spec);
1974 buf = string_nocheck(buf, end, "\"", str_spec);
1975 buf = string(buf, end, p, str_spec);
1976 buf = string_nocheck(buf, end, "\"", str_spec);
1986 return widen_string(buf, buf - buf_start, end, spec);
1989 static char *kobject_string(char *buf, char *end, void *ptr,
1990 struct printf_spec spec, const char *fmt)
1994 return device_node_string(buf, end, ptr, spec, fmt + 1);
1997 return error_string(buf, end, "(%pO?)", spec);
2001 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2002 * by an extra set of alphanumeric characters that are extended format
2005 * Please update scripts/checkpatch.pl when adding/removing conversion
2006 * characters. (Search for "check for vsprintf extension").
2008 * Right now we handle:
2010 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2011 * - 's' For symbolic direct pointers (or function descriptors) without offset
2014 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
2015 * - 'B' For backtraced symbolic direct pointers with offset
2016 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2017 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2018 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2019 * width which must be explicitly specified either as part of the
2020 * format string '%32b[l]' or through '%*b[l]', [l] selects
2021 * range-list format instead of hex format
2022 * - 'M' For a 6-byte MAC address, it prints the address in the
2023 * usual colon-separated hex notation
2024 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2025 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2026 * with a dash-separated hex notation
2027 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
2028 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2029 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2030 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
2032 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2033 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2034 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2035 * IPv6 omits the colons (01020304...0f)
2036 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
2038 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2039 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2040 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2041 * - 'I[6S]c' for IPv6 addresses printed as specified by
2042 * http://tools.ietf.org/html/rfc5952
2043 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2044 * of the following flags (see string_escape_mem() for the
2047 * c - ESCAPE_SPECIAL
2053 * By default ESCAPE_ANY_NP is used.
2054 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2055 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2056 * Options for %pU are:
2057 * b big endian lower case hex (default)
2058 * B big endian UPPER case hex
2059 * l little endian lower case hex
2060 * L little endian UPPER case hex
2061 * big endian output byte order is:
2062 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2063 * little endian output byte order is:
2064 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
2065 * - 'V' For a struct va_format which contains a format string * and va_list *,
2066 * call vsnprintf(->format, *->va_list).
2067 * Implements a "recursive vsnprintf".
2068 * Do not use this feature without some mechanism to verify the
2069 * correctness of the format string and va_list arguments.
2070 * - 'K' For a kernel pointer that should be hidden from unprivileged users
2071 * - 'NF' For a netdev_features_t
2072 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2073 * a certain separator (' ' by default):
2077 * The maximum supported length is 64 bytes of the input. Consider
2078 * to use print_hex_dump() for the larger input.
2079 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2080 * (default assumed to be phys_addr_t, passed by reference)
2081 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2082 * - 'D[234]' Same as 'd' but for a struct file
2083 * - 'g' For block_device name (gendisk + partition number)
2084 * - 't[R][dt][r]' For time and date as represented:
2086 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2087 * (legacy clock framework) of the clock
2088 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2089 * (legacy clock framework) of the clock
2090 * - 'G' For flags to be printed as a collection of symbolic strings that would
2091 * construct the specific value. Supported flags given by option:
2092 * p page flags (see struct page) given as pointer to unsigned long
2093 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2094 * v vma flags (VM_*) given as pointer to unsigned long
2095 * - 'OF[fnpPcCF]' For a device tree object
2096 * Without any optional arguments prints the full_name
2097 * f device node full_name
2098 * n device node name
2099 * p device node phandle
2100 * P device node path spec (name + @unit)
2101 * F device node flags
2102 * c major compatible string
2103 * C full compatible string
2104 * - 'x' For printing the address. Equivalent to "%lx".
2106 * ** When making changes please also update:
2107 * Documentation/core-api/printk-formats.rst
2109 * Note: The default behaviour (unadorned %p) is to hash the address,
2110 * rendering it useful as a unique identifier.
2112 static noinline_for_stack
2113 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2114 struct printf_spec spec)
2121 ptr = dereference_symbol_descriptor(ptr);
2124 return symbol_string(buf, end, ptr, spec, fmt);
2127 return resource_string(buf, end, ptr, spec, fmt);
2129 return hex_string(buf, end, ptr, spec, fmt);
2133 return bitmap_list_string(buf, end, ptr, spec, fmt);
2135 return bitmap_string(buf, end, ptr, spec, fmt);
2137 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2138 case 'm': /* Contiguous: 000102030405 */
2140 /* [mM]R (Reverse order; Bluetooth) */
2141 return mac_address_string(buf, end, ptr, spec, fmt);
2142 case 'I': /* Formatted IP supported
2144 * 6: 0001:0203:...:0708
2145 * 6c: 1::708 or 1::1.2.3.4
2147 case 'i': /* Contiguous:
2148 * 4: 001.002.003.004
2151 return ip_addr_string(buf, end, ptr, spec, fmt);
2153 return escaped_string(buf, end, ptr, spec, fmt);
2155 return uuid_string(buf, end, ptr, spec, fmt);
2157 return va_format(buf, end, ptr, spec, fmt);
2159 return restricted_pointer(buf, end, ptr, spec);
2161 return netdev_bits(buf, end, ptr, spec, fmt);
2163 return address_val(buf, end, ptr, spec, fmt);
2165 return dentry_name(buf, end, ptr, spec, fmt);
2167 return time_and_date(buf, end, ptr, spec, fmt);
2169 return clock(buf, end, ptr, spec, fmt);
2171 return dentry_name(buf, end,
2172 ((const struct file *)ptr)->f_path.dentry,
2176 return bdev_name(buf, end, ptr, spec, fmt);
2180 return flags_string(buf, end, ptr, spec, fmt);
2182 return kobject_string(buf, end, ptr, spec, fmt);
2184 return pointer_string(buf, end, ptr, spec);
2187 /* default is to _not_ leak addresses, hash before printing */
2188 return ptr_to_id(buf, end, ptr, spec);
2192 * Helper function to decode printf style format.
2193 * Each call decode a token from the format and return the
2194 * number of characters read (or likely the delta where it wants
2195 * to go on the next call).
2196 * The decoded token is returned through the parameters
2198 * 'h', 'l', or 'L' for integer fields
2199 * 'z' support added 23/7/1999 S.H.
2200 * 'z' changed to 'Z' --davidm 1/25/99
2201 * 'Z' changed to 'z' --adobriyan 2017-01-25
2202 * 't' added for ptrdiff_t
2204 * @fmt: the format string
2205 * @type of the token returned
2206 * @flags: various flags such as +, -, # tokens..
2207 * @field_width: overwritten width
2208 * @base: base of the number (octal, hex, ...)
2209 * @precision: precision of a number
2210 * @qualifier: qualifier of a number (long, size_t, ...)
2212 static noinline_for_stack
2213 int format_decode(const char *fmt, struct printf_spec *spec)
2215 const char *start = fmt;
2218 /* we finished early by reading the field width */
2219 if (spec->type == FORMAT_TYPE_WIDTH) {
2220 if (spec->field_width < 0) {
2221 spec->field_width = -spec->field_width;
2222 spec->flags |= LEFT;
2224 spec->type = FORMAT_TYPE_NONE;
2228 /* we finished early by reading the precision */
2229 if (spec->type == FORMAT_TYPE_PRECISION) {
2230 if (spec->precision < 0)
2231 spec->precision = 0;
2233 spec->type = FORMAT_TYPE_NONE;
2238 spec->type = FORMAT_TYPE_NONE;
2240 for (; *fmt ; ++fmt) {
2245 /* Return the current non-format string */
2246 if (fmt != start || !*fmt)
2252 while (1) { /* this also skips first '%' */
2258 case '-': spec->flags |= LEFT; break;
2259 case '+': spec->flags |= PLUS; break;
2260 case ' ': spec->flags |= SPACE; break;
2261 case '#': spec->flags |= SPECIAL; break;
2262 case '0': spec->flags |= ZEROPAD; break;
2263 default: found = false;
2270 /* get field width */
2271 spec->field_width = -1;
2274 spec->field_width = skip_atoi(&fmt);
2275 else if (*fmt == '*') {
2276 /* it's the next argument */
2277 spec->type = FORMAT_TYPE_WIDTH;
2278 return ++fmt - start;
2282 /* get the precision */
2283 spec->precision = -1;
2286 if (isdigit(*fmt)) {
2287 spec->precision = skip_atoi(&fmt);
2288 if (spec->precision < 0)
2289 spec->precision = 0;
2290 } else if (*fmt == '*') {
2291 /* it's the next argument */
2292 spec->type = FORMAT_TYPE_PRECISION;
2293 return ++fmt - start;
2298 /* get the conversion qualifier */
2300 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2301 *fmt == 'z' || *fmt == 't') {
2303 if (unlikely(qualifier == *fmt)) {
2304 if (qualifier == 'l') {
2307 } else if (qualifier == 'h') {
2318 spec->type = FORMAT_TYPE_CHAR;
2319 return ++fmt - start;
2322 spec->type = FORMAT_TYPE_STR;
2323 return ++fmt - start;
2326 spec->type = FORMAT_TYPE_PTR;
2327 return ++fmt - start;
2330 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2331 return ++fmt - start;
2333 /* integer number formats - set up the flags and "break" */
2339 spec->flags |= SMALL;
2348 spec->flags |= SIGN;
2354 * Since %n poses a greater security risk than
2355 * utility, treat it as any other invalid or
2356 * unsupported format specifier.
2361 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2362 spec->type = FORMAT_TYPE_INVALID;
2366 if (qualifier == 'L')
2367 spec->type = FORMAT_TYPE_LONG_LONG;
2368 else if (qualifier == 'l') {
2369 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2370 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2371 } else if (qualifier == 'z') {
2372 spec->type = FORMAT_TYPE_SIZE_T;
2373 } else if (qualifier == 't') {
2374 spec->type = FORMAT_TYPE_PTRDIFF;
2375 } else if (qualifier == 'H') {
2376 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2377 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2378 } else if (qualifier == 'h') {
2379 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2380 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2382 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2383 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2386 return ++fmt - start;
2390 set_field_width(struct printf_spec *spec, int width)
2392 spec->field_width = width;
2393 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2394 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2399 set_precision(struct printf_spec *spec, int prec)
2401 spec->precision = prec;
2402 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2403 spec->precision = clamp(prec, 0, PRECISION_MAX);
2408 * vsnprintf - Format a string and place it in a buffer
2409 * @buf: The buffer to place the result into
2410 * @size: The size of the buffer, including the trailing null space
2411 * @fmt: The format string to use
2412 * @args: Arguments for the format string
2414 * This function generally follows C99 vsnprintf, but has some
2415 * extensions and a few limitations:
2417 * - ``%n`` is unsupported
2418 * - ``%p*`` is handled by pointer()
2420 * See pointer() or Documentation/core-api/printk-formats.rst for more
2421 * extensive description.
2423 * **Please update the documentation in both places when making changes**
2425 * The return value is the number of characters which would
2426 * be generated for the given input, excluding the trailing
2427 * '\0', as per ISO C99. If you want to have the exact
2428 * number of characters written into @buf as return value
2429 * (not including the trailing '\0'), use vscnprintf(). If the
2430 * return is greater than or equal to @size, the resulting
2431 * string is truncated.
2433 * If you're not already dealing with a va_list consider using snprintf().
2435 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2437 unsigned long long num;
2439 struct printf_spec spec = {0};
2441 /* Reject out-of-range values early. Large positive sizes are
2442 used for unknown buffer sizes. */
2443 if (WARN_ON_ONCE(size > INT_MAX))
2449 /* Make sure end is always >= buf */
2456 const char *old_fmt = fmt;
2457 int read = format_decode(fmt, &spec);
2461 switch (spec.type) {
2462 case FORMAT_TYPE_NONE: {
2465 if (copy > end - str)
2467 memcpy(str, old_fmt, copy);
2473 case FORMAT_TYPE_WIDTH:
2474 set_field_width(&spec, va_arg(args, int));
2477 case FORMAT_TYPE_PRECISION:
2478 set_precision(&spec, va_arg(args, int));
2481 case FORMAT_TYPE_CHAR: {
2484 if (!(spec.flags & LEFT)) {
2485 while (--spec.field_width > 0) {
2492 c = (unsigned char) va_arg(args, int);
2496 while (--spec.field_width > 0) {
2504 case FORMAT_TYPE_STR:
2505 str = string(str, end, va_arg(args, char *), spec);
2508 case FORMAT_TYPE_PTR:
2509 str = pointer(fmt, str, end, va_arg(args, void *),
2511 while (isalnum(*fmt))
2515 case FORMAT_TYPE_PERCENT_CHAR:
2521 case FORMAT_TYPE_INVALID:
2523 * Presumably the arguments passed gcc's type
2524 * checking, but there is no safe or sane way
2525 * for us to continue parsing the format and
2526 * fetching from the va_list; the remaining
2527 * specifiers and arguments would be out of
2533 switch (spec.type) {
2534 case FORMAT_TYPE_LONG_LONG:
2535 num = va_arg(args, long long);
2537 case FORMAT_TYPE_ULONG:
2538 num = va_arg(args, unsigned long);
2540 case FORMAT_TYPE_LONG:
2541 num = va_arg(args, long);
2543 case FORMAT_TYPE_SIZE_T:
2544 if (spec.flags & SIGN)
2545 num = va_arg(args, ssize_t);
2547 num = va_arg(args, size_t);
2549 case FORMAT_TYPE_PTRDIFF:
2550 num = va_arg(args, ptrdiff_t);
2552 case FORMAT_TYPE_UBYTE:
2553 num = (unsigned char) va_arg(args, int);
2555 case FORMAT_TYPE_BYTE:
2556 num = (signed char) va_arg(args, int);
2558 case FORMAT_TYPE_USHORT:
2559 num = (unsigned short) va_arg(args, int);
2561 case FORMAT_TYPE_SHORT:
2562 num = (short) va_arg(args, int);
2564 case FORMAT_TYPE_INT:
2565 num = (int) va_arg(args, int);
2568 num = va_arg(args, unsigned int);
2571 str = number(str, end, num, spec);
2583 /* the trailing null byte doesn't count towards the total */
2587 EXPORT_SYMBOL(vsnprintf);
2590 * vscnprintf - Format a string and place it in a buffer
2591 * @buf: The buffer to place the result into
2592 * @size: The size of the buffer, including the trailing null space
2593 * @fmt: The format string to use
2594 * @args: Arguments for the format string
2596 * The return value is the number of characters which have been written into
2597 * the @buf not including the trailing '\0'. If @size is == 0 the function
2600 * If you're not already dealing with a va_list consider using scnprintf().
2602 * See the vsnprintf() documentation for format string extensions over C99.
2604 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2608 i = vsnprintf(buf, size, fmt, args);
2610 if (likely(i < size))
2616 EXPORT_SYMBOL(vscnprintf);
2619 * snprintf - Format a string and place it in a buffer
2620 * @buf: The buffer to place the result into
2621 * @size: The size of the buffer, including the trailing null space
2622 * @fmt: The format string to use
2623 * @...: Arguments for the format string
2625 * The return value is the number of characters which would be
2626 * generated for the given input, excluding the trailing null,
2627 * as per ISO C99. If the return is greater than or equal to
2628 * @size, the resulting string is truncated.
2630 * See the vsnprintf() documentation for format string extensions over C99.
2632 int snprintf(char *buf, size_t size, const char *fmt, ...)
2637 va_start(args, fmt);
2638 i = vsnprintf(buf, size, fmt, args);
2643 EXPORT_SYMBOL(snprintf);
2646 * scnprintf - Format a string and place it in a buffer
2647 * @buf: The buffer to place the result into
2648 * @size: The size of the buffer, including the trailing null space
2649 * @fmt: The format string to use
2650 * @...: Arguments for the format string
2652 * The return value is the number of characters written into @buf not including
2653 * the trailing '\0'. If @size is == 0 the function returns 0.
2656 int scnprintf(char *buf, size_t size, const char *fmt, ...)
2661 va_start(args, fmt);
2662 i = vscnprintf(buf, size, fmt, args);
2667 EXPORT_SYMBOL(scnprintf);
2670 * vsprintf - Format a string and place it in a buffer
2671 * @buf: The buffer to place the result into
2672 * @fmt: The format string to use
2673 * @args: Arguments for the format string
2675 * The function returns the number of characters written
2676 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2679 * If you're not already dealing with a va_list consider using sprintf().
2681 * See the vsnprintf() documentation for format string extensions over C99.
2683 int vsprintf(char *buf, const char *fmt, va_list args)
2685 return vsnprintf(buf, INT_MAX, fmt, args);
2687 EXPORT_SYMBOL(vsprintf);
2690 * sprintf - Format a string and place it in a buffer
2691 * @buf: The buffer to place the result into
2692 * @fmt: The format string to use
2693 * @...: Arguments for the format string
2695 * The function returns the number of characters written
2696 * into @buf. Use snprintf() or scnprintf() in order to avoid
2699 * See the vsnprintf() documentation for format string extensions over C99.
2701 int sprintf(char *buf, const char *fmt, ...)
2706 va_start(args, fmt);
2707 i = vsnprintf(buf, INT_MAX, fmt, args);
2712 EXPORT_SYMBOL(sprintf);
2714 #ifdef CONFIG_BINARY_PRINTF
2717 * vbin_printf() - VA arguments to binary data
2718 * bstr_printf() - Binary data to text string
2722 * vbin_printf - Parse a format string and place args' binary value in a buffer
2723 * @bin_buf: The buffer to place args' binary value
2724 * @size: The size of the buffer(by words(32bits), not characters)
2725 * @fmt: The format string to use
2726 * @args: Arguments for the format string
2728 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2731 * The return value is the number of words(32bits) which would be generated for
2735 * If the return value is greater than @size, the resulting bin_buf is NOT
2736 * valid for bstr_printf().
2738 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2740 struct printf_spec spec = {0};
2744 str = (char *)bin_buf;
2745 end = (char *)(bin_buf + size);
2747 #define save_arg(type) \
2749 unsigned long long value; \
2750 if (sizeof(type) == 8) { \
2751 unsigned long long val8; \
2752 str = PTR_ALIGN(str, sizeof(u32)); \
2753 val8 = va_arg(args, unsigned long long); \
2754 if (str + sizeof(type) <= end) { \
2755 *(u32 *)str = *(u32 *)&val8; \
2756 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2760 unsigned int val4; \
2761 str = PTR_ALIGN(str, sizeof(type)); \
2762 val4 = va_arg(args, int); \
2763 if (str + sizeof(type) <= end) \
2764 *(typeof(type) *)str = (type)(long)val4; \
2765 value = (unsigned long long)val4; \
2767 str += sizeof(type); \
2772 int read = format_decode(fmt, &spec);
2776 switch (spec.type) {
2777 case FORMAT_TYPE_NONE:
2778 case FORMAT_TYPE_PERCENT_CHAR:
2780 case FORMAT_TYPE_INVALID:
2783 case FORMAT_TYPE_WIDTH:
2784 case FORMAT_TYPE_PRECISION:
2785 width = (int)save_arg(int);
2786 /* Pointers may require the width */
2788 set_field_width(&spec, width);
2791 case FORMAT_TYPE_CHAR:
2795 case FORMAT_TYPE_STR: {
2796 const char *save_str = va_arg(args, char *);
2797 const char *err_msg;
2800 err_msg = check_pointer_msg(save_str);
2804 len = strlen(save_str) + 1;
2805 if (str + len < end)
2806 memcpy(str, save_str, len);
2811 case FORMAT_TYPE_PTR:
2812 /* Dereferenced pointers must be done now */
2814 /* Dereference of functions is still OK */
2824 if (!isalnum(*fmt)) {
2828 str = pointer(fmt, str, end, va_arg(args, void *),
2833 end[-1] = '\0'; /* Must be nul terminated */
2835 /* skip all alphanumeric pointer suffixes */
2836 while (isalnum(*fmt))
2841 switch (spec.type) {
2843 case FORMAT_TYPE_LONG_LONG:
2844 save_arg(long long);
2846 case FORMAT_TYPE_ULONG:
2847 case FORMAT_TYPE_LONG:
2848 save_arg(unsigned long);
2850 case FORMAT_TYPE_SIZE_T:
2853 case FORMAT_TYPE_PTRDIFF:
2854 save_arg(ptrdiff_t);
2856 case FORMAT_TYPE_UBYTE:
2857 case FORMAT_TYPE_BYTE:
2860 case FORMAT_TYPE_USHORT:
2861 case FORMAT_TYPE_SHORT:
2871 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2874 EXPORT_SYMBOL_GPL(vbin_printf);
2877 * bstr_printf - Format a string from binary arguments and place it in a buffer
2878 * @buf: The buffer to place the result into
2879 * @size: The size of the buffer, including the trailing null space
2880 * @fmt: The format string to use
2881 * @bin_buf: Binary arguments for the format string
2883 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2884 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2885 * a binary buffer that generated by vbin_printf.
2887 * The format follows C99 vsnprintf, but has some extensions:
2888 * see vsnprintf comment for details.
2890 * The return value is the number of characters which would
2891 * be generated for the given input, excluding the trailing
2892 * '\0', as per ISO C99. If you want to have the exact
2893 * number of characters written into @buf as return value
2894 * (not including the trailing '\0'), use vscnprintf(). If the
2895 * return is greater than or equal to @size, the resulting
2896 * string is truncated.
2898 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2900 struct printf_spec spec = {0};
2902 const char *args = (const char *)bin_buf;
2904 if (WARN_ON_ONCE(size > INT_MAX))
2910 #define get_arg(type) \
2912 typeof(type) value; \
2913 if (sizeof(type) == 8) { \
2914 args = PTR_ALIGN(args, sizeof(u32)); \
2915 *(u32 *)&value = *(u32 *)args; \
2916 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2918 args = PTR_ALIGN(args, sizeof(type)); \
2919 value = *(typeof(type) *)args; \
2921 args += sizeof(type); \
2925 /* Make sure end is always >= buf */
2932 const char *old_fmt = fmt;
2933 int read = format_decode(fmt, &spec);
2937 switch (spec.type) {
2938 case FORMAT_TYPE_NONE: {
2941 if (copy > end - str)
2943 memcpy(str, old_fmt, copy);
2949 case FORMAT_TYPE_WIDTH:
2950 set_field_width(&spec, get_arg(int));
2953 case FORMAT_TYPE_PRECISION:
2954 set_precision(&spec, get_arg(int));
2957 case FORMAT_TYPE_CHAR: {
2960 if (!(spec.flags & LEFT)) {
2961 while (--spec.field_width > 0) {
2967 c = (unsigned char) get_arg(char);
2971 while (--spec.field_width > 0) {
2979 case FORMAT_TYPE_STR: {
2980 const char *str_arg = args;
2981 args += strlen(str_arg) + 1;
2982 str = string(str, end, (char *)str_arg, spec);
2986 case FORMAT_TYPE_PTR: {
2987 bool process = false;
2989 /* Non function dereferences were already done */
3000 if (!isalnum(*fmt)) {
3004 /* Pointer dereference was already processed */
3006 len = copy = strlen(args);
3007 if (copy > end - str)
3009 memcpy(str, args, copy);
3015 str = pointer(fmt, str, end, get_arg(void *), spec);
3017 while (isalnum(*fmt))
3022 case FORMAT_TYPE_PERCENT_CHAR:
3028 case FORMAT_TYPE_INVALID:
3032 unsigned long long num;
3034 switch (spec.type) {
3036 case FORMAT_TYPE_LONG_LONG:
3037 num = get_arg(long long);
3039 case FORMAT_TYPE_ULONG:
3040 case FORMAT_TYPE_LONG:
3041 num = get_arg(unsigned long);
3043 case FORMAT_TYPE_SIZE_T:
3044 num = get_arg(size_t);
3046 case FORMAT_TYPE_PTRDIFF:
3047 num = get_arg(ptrdiff_t);
3049 case FORMAT_TYPE_UBYTE:
3050 num = get_arg(unsigned char);
3052 case FORMAT_TYPE_BYTE:
3053 num = get_arg(signed char);
3055 case FORMAT_TYPE_USHORT:
3056 num = get_arg(unsigned short);
3058 case FORMAT_TYPE_SHORT:
3059 num = get_arg(short);
3061 case FORMAT_TYPE_UINT:
3062 num = get_arg(unsigned int);
3068 str = number(str, end, num, spec);
3070 } /* switch(spec.type) */
3083 /* the trailing null byte doesn't count towards the total */
3086 EXPORT_SYMBOL_GPL(bstr_printf);
3089 * bprintf - Parse a format string and place args' binary value in a buffer
3090 * @bin_buf: The buffer to place args' binary value
3091 * @size: The size of the buffer(by words(32bits), not characters)
3092 * @fmt: The format string to use
3093 * @...: Arguments for the format string
3095 * The function returns the number of words(u32) written
3098 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3103 va_start(args, fmt);
3104 ret = vbin_printf(bin_buf, size, fmt, args);
3109 EXPORT_SYMBOL_GPL(bprintf);
3111 #endif /* CONFIG_BINARY_PRINTF */
3114 * vsscanf - Unformat a buffer into a list of arguments
3115 * @buf: input buffer
3116 * @fmt: format of buffer
3119 int vsscanf(const char *buf, const char *fmt, va_list args)
3121 const char *str = buf;
3129 unsigned long long u;
3135 /* skip any white space in format */
3136 /* white space in format matchs any amount of
3137 * white space, including none, in the input.
3139 if (isspace(*fmt)) {
3140 fmt = skip_spaces(++fmt);
3141 str = skip_spaces(str);
3144 /* anything that is not a conversion must match exactly */
3145 if (*fmt != '%' && *fmt) {
3146 if (*fmt++ != *str++)
3155 /* skip this conversion.
3156 * advance both strings to next white space
3161 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3162 /* '%*[' not yet supported, invalid format */
3167 while (!isspace(*str) && *str)
3172 /* get field width */
3174 if (isdigit(*fmt)) {
3175 field_width = skip_atoi(&fmt);
3176 if (field_width <= 0)
3180 /* get conversion qualifier */
3182 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
3185 if (unlikely(qualifier == *fmt)) {
3186 if (qualifier == 'h') {
3189 } else if (qualifier == 'l') {
3200 /* return number of characters read so far */
3201 *va_arg(args, int *) = str - buf;
3215 char *s = (char *)va_arg(args, char*);
3216 if (field_width == -1)
3220 } while (--field_width > 0 && *str);
3226 char *s = (char *)va_arg(args, char *);
3227 if (field_width == -1)
3228 field_width = SHRT_MAX;
3229 /* first, skip leading white space in buffer */
3230 str = skip_spaces(str);
3232 /* now copy until next white space */
3233 while (*str && !isspace(*str) && field_width--)
3240 * Warning: This implementation of the '[' conversion specifier
3241 * deviates from its glibc counterpart in the following ways:
3242 * (1) It does NOT support ranges i.e. '-' is NOT a special
3244 * (2) It cannot match the closing bracket ']' itself
3245 * (3) A field width is required
3246 * (4) '%*[' (discard matching input) is currently not supported
3249 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3250 * buf1, buf2, buf3);
3256 char *s = (char *)va_arg(args, char *);
3257 DECLARE_BITMAP(set, 256) = {0};
3258 unsigned int len = 0;
3259 bool negate = (*fmt == '^');
3261 /* field width is required */
3262 if (field_width == -1)
3268 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3269 set_bit((u8)*fmt, set);
3271 /* no ']' or no character set found */
3277 bitmap_complement(set, set, 256);
3278 /* exclude null '\0' byte */
3282 /* match must be non-empty */
3283 if (!test_bit((u8)*str, set))
3286 while (test_bit((u8)*str, set) && field_width--)
3308 /* looking for '%' in str */
3313 /* invalid format; stop here */
3317 /* have some sort of integer conversion.
3318 * first, skip white space in buffer.
3320 str = skip_spaces(str);
3323 if (is_sign && digit == '-')
3327 || (base == 16 && !isxdigit(digit))
3328 || (base == 10 && !isdigit(digit))
3329 || (base == 8 && (!isdigit(digit) || digit > '7'))
3330 || (base == 0 && !isdigit(digit)))
3334 val.s = qualifier != 'L' ?
3335 simple_strtol(str, &next, base) :
3336 simple_strtoll(str, &next, base);
3338 val.u = qualifier != 'L' ?
3339 simple_strtoul(str, &next, base) :
3340 simple_strtoull(str, &next, base);
3342 if (field_width > 0 && next - str > field_width) {
3344 _parse_integer_fixup_radix(str, &base);
3345 while (next - str > field_width) {
3347 val.s = div_s64(val.s, base);
3349 val.u = div_u64(val.u, base);
3354 switch (qualifier) {
3355 case 'H': /* that's 'hh' in format */
3357 *va_arg(args, signed char *) = val.s;
3359 *va_arg(args, unsigned char *) = val.u;
3363 *va_arg(args, short *) = val.s;
3365 *va_arg(args, unsigned short *) = val.u;
3369 *va_arg(args, long *) = val.s;
3371 *va_arg(args, unsigned long *) = val.u;
3375 *va_arg(args, long long *) = val.s;
3377 *va_arg(args, unsigned long long *) = val.u;
3380 *va_arg(args, size_t *) = val.u;
3384 *va_arg(args, int *) = val.s;
3386 *va_arg(args, unsigned int *) = val.u;
3398 EXPORT_SYMBOL(vsscanf);
3401 * sscanf - Unformat a buffer into a list of arguments
3402 * @buf: input buffer
3403 * @fmt: formatting of buffer
3404 * @...: resulting arguments
3406 int sscanf(const char *buf, const char *fmt, ...)
3411 va_start(args, fmt);
3412 i = vsscanf(buf, fmt, args);
3417 EXPORT_SYMBOL(sscanf);