1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
10 * Wirzenius wrote this portably, Torvalds fucked it up :-)
14 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
15 * - changed to provide snprintf and vsnprintf functions
16 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
17 * - scnprintf and vscnprintf
21 #include <linux/build_bug.h>
22 #include <linux/clk.h>
23 #include <linux/clk-provider.h>
24 #include <linux/errname.h>
25 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/ctype.h>
29 #include <linux/kernel.h>
30 #include <linux/kallsyms.h>
31 #include <linux/math64.h>
32 #include <linux/uaccess.h>
33 #include <linux/ioport.h>
34 #include <linux/dcache.h>
35 #include <linux/cred.h>
36 #include <linux/rtc.h>
37 #include <linux/uuid.h>
39 #include <net/addrconf.h>
40 #include <linux/siphash.h>
41 #include <linux/compiler.h>
42 #include <linux/property.h>
44 #include <linux/blkdev.h>
47 #include "../mm/internal.h" /* For the trace_print_flags arrays */
49 #include <asm/page.h> /* for PAGE_SIZE */
50 #include <asm/byteorder.h> /* cpu_to_le16 */
52 #include <linux/string_helpers.h>
56 * simple_strtoull - convert a string to an unsigned long long
57 * @cp: The start of the string
58 * @endp: A pointer to the end of the parsed string will be placed here
59 * @base: The number base to use
61 * This function is obsolete. Please use kstrtoull instead.
63 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
65 unsigned long long result
;
68 cp
= _parse_integer_fixup_radix(cp
, &base
);
69 rv
= _parse_integer(cp
, base
, &result
);
71 cp
+= (rv
& ~KSTRTOX_OVERFLOW
);
78 EXPORT_SYMBOL(simple_strtoull
);
81 * simple_strtoul - convert a string to an unsigned long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
86 * This function is obsolete. Please use kstrtoul instead.
88 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
90 return simple_strtoull(cp
, endp
, base
);
92 EXPORT_SYMBOL(simple_strtoul
);
95 * simple_strtol - convert a string to a signed long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
100 * This function is obsolete. Please use kstrtol instead.
102 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
105 return -simple_strtoul(cp
+ 1, endp
, base
);
107 return simple_strtoul(cp
, endp
, base
);
109 EXPORT_SYMBOL(simple_strtol
);
112 * simple_strtoll - convert a string to a signed long long
113 * @cp: The start of the string
114 * @endp: A pointer to the end of the parsed string will be placed here
115 * @base: The number base to use
117 * This function is obsolete. Please use kstrtoll instead.
119 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
122 return -simple_strtoull(cp
+ 1, endp
, base
);
124 return simple_strtoull(cp
, endp
, base
);
126 EXPORT_SYMBOL(simple_strtoll
);
128 static noinline_for_stack
129 int skip_atoi(const char **s
)
134 i
= i
*10 + *((*s
)++) - '0';
135 } while (isdigit(**s
));
141 * Decimal conversion is by far the most typical, and is used for
142 * /proc and /sys data. This directly impacts e.g. top performance
143 * with many processes running. We optimize it for speed by emitting
144 * two characters at a time, using a 200 byte lookup table. This
145 * roughly halves the number of multiplications compared to computing
146 * the digits one at a time. Implementation strongly inspired by the
147 * previous version, which in turn used ideas described at
148 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
149 * from the author, Douglas W. Jones).
151 * It turns out there is precisely one 26 bit fixed-point
152 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
153 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
154 * range happens to be somewhat larger (x <= 1073741898), but that's
155 * irrelevant for our purpose.
157 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
158 * need a 32x32->64 bit multiply, so we simply use the same constant.
160 * For dividing a number in the range [100, 10^4-1] by 100, there are
161 * several options. The simplest is (x * 0x147b) >> 19, which is valid
162 * for all x <= 43698.
165 static const u16 decpair
[100] = {
166 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
167 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
168 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
169 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
170 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
171 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
172 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
173 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
174 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
175 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
176 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
181 * This will print a single '0' even if r == 0, since we would
182 * immediately jump to out_r where two 0s would be written but only
183 * one of them accounted for in buf. This is needed by ip4_string
184 * below. All other callers pass a non-zero value of r.
186 static noinline_for_stack
187 char *put_dec_trunc8(char *buf
, unsigned r
)
195 /* 100 <= r < 10^8 */
196 q
= (r
* (u64
)0x28f5c29) >> 32;
197 *((u16
*)buf
) = decpair
[r
- 100*q
];
204 /* 100 <= q < 10^6 */
205 r
= (q
* (u64
)0x28f5c29) >> 32;
206 *((u16
*)buf
) = decpair
[q
- 100*r
];
213 /* 100 <= r < 10^4 */
214 q
= (r
* 0x147b) >> 19;
215 *((u16
*)buf
) = decpair
[r
- 100*q
];
222 *((u16
*)buf
) = decpair
[r
];
223 buf
+= r
< 10 ? 1 : 2;
227 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
228 static noinline_for_stack
229 char *put_dec_full8(char *buf
, unsigned r
)
234 q
= (r
* (u64
)0x28f5c29) >> 32;
235 *((u16
*)buf
) = decpair
[r
- 100*q
];
239 r
= (q
* (u64
)0x28f5c29) >> 32;
240 *((u16
*)buf
) = decpair
[q
- 100*r
];
244 q
= (r
* 0x147b) >> 19;
245 *((u16
*)buf
) = decpair
[r
- 100*q
];
249 *((u16
*)buf
) = decpair
[q
];
254 static noinline_for_stack
255 char *put_dec(char *buf
, unsigned long long n
)
257 if (n
>= 100*1000*1000)
258 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
259 /* 1 <= n <= 1.6e11 */
260 if (n
>= 100*1000*1000)
261 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
263 return put_dec_trunc8(buf
, n
);
266 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
269 put_dec_full4(char *buf
, unsigned r
)
274 q
= (r
* 0x147b) >> 19;
275 *((u16
*)buf
) = decpair
[r
- 100*q
];
278 *((u16
*)buf
) = decpair
[q
];
282 * Call put_dec_full4 on x % 10000, return x / 10000.
283 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
284 * holds for all x < 1,128,869,999. The largest value this
285 * helper will ever be asked to convert is 1,125,520,955.
286 * (second call in the put_dec code, assuming n is all-ones).
288 static noinline_for_stack
289 unsigned put_dec_helper4(char *buf
, unsigned x
)
291 uint32_t q
= (x
* (uint64_t)0x346DC5D7) >> 43;
293 put_dec_full4(buf
, x
- q
* 10000);
297 /* Based on code by Douglas W. Jones found at
298 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
299 * (with permission from the author).
300 * Performs no 64-bit division and hence should be fast on 32-bit machines.
303 char *put_dec(char *buf
, unsigned long long n
)
305 uint32_t d3
, d2
, d1
, q
, h
;
307 if (n
< 100*1000*1000)
308 return put_dec_trunc8(buf
, n
);
310 d1
= ((uint32_t)n
>> 16); /* implicit "& 0xffff" */
313 d3
= (h
>> 16); /* implicit "& 0xffff" */
315 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
316 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
317 q
= 656 * d3
+ 7296 * d2
+ 5536 * d1
+ ((uint32_t)n
& 0xffff);
318 q
= put_dec_helper4(buf
, q
);
320 q
+= 7671 * d3
+ 9496 * d2
+ 6 * d1
;
321 q
= put_dec_helper4(buf
+4, q
);
323 q
+= 4749 * d3
+ 42 * d2
;
324 q
= put_dec_helper4(buf
+8, q
);
329 buf
= put_dec_trunc8(buf
, q
);
330 else while (buf
[-1] == '0')
339 * Convert passed number to decimal string.
340 * Returns the length of string. On buffer overflow, returns 0.
342 * If speed is not important, use snprintf(). It's easy to read the code.
344 int num_to_str(char *buf
, int size
, unsigned long long num
, unsigned int width
)
346 /* put_dec requires 2-byte alignment of the buffer. */
347 char tmp
[sizeof(num
) * 3] __aligned(2);
350 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
355 len
= put_dec(tmp
, num
) - tmp
;
358 if (len
> size
|| width
> size
)
363 for (idx
= 0; idx
< width
; idx
++)
369 for (idx
= 0; idx
< len
; ++idx
)
370 buf
[idx
+ width
] = tmp
[len
- idx
- 1];
375 #define SIGN 1 /* unsigned/signed, must be 1 */
376 #define LEFT 2 /* left justified */
377 #define PLUS 4 /* show plus */
378 #define SPACE 8 /* space if plus */
379 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
380 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
381 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
384 FORMAT_TYPE_NONE
, /* Just a string part */
386 FORMAT_TYPE_PRECISION
,
390 FORMAT_TYPE_PERCENT_CHAR
,
392 FORMAT_TYPE_LONG_LONG
,
406 unsigned int type
:8; /* format_type enum */
407 signed int field_width
:24; /* width of output field */
408 unsigned int flags
:8; /* flags to number() */
409 unsigned int base
:8; /* number base, 8, 10 or 16 only */
410 signed int precision
:16; /* # of digits/chars */
412 static_assert(sizeof(struct printf_spec
) == 8);
414 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
415 #define PRECISION_MAX ((1 << 15) - 1)
417 static noinline_for_stack
418 char *number(char *buf
, char *end
, unsigned long long num
,
419 struct printf_spec spec
)
421 /* put_dec requires 2-byte alignment of the buffer. */
422 char tmp
[3 * sizeof(num
)] __aligned(2);
425 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
427 bool is_zero
= num
== 0LL;
428 int field_width
= spec
.field_width
;
429 int precision
= spec
.precision
;
431 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
432 * produces same digits or (maybe lowercased) letters */
433 locase
= (spec
.flags
& SMALL
);
434 if (spec
.flags
& LEFT
)
435 spec
.flags
&= ~ZEROPAD
;
437 if (spec
.flags
& SIGN
) {
438 if ((signed long long)num
< 0) {
440 num
= -(signed long long)num
;
442 } else if (spec
.flags
& PLUS
) {
445 } else if (spec
.flags
& SPACE
) {
457 /* generate full string in tmp[], in reverse order */
460 tmp
[i
++] = hex_asc_upper
[num
] | locase
;
461 else if (spec
.base
!= 10) { /* 8 or 16 */
462 int mask
= spec
.base
- 1;
468 tmp
[i
++] = (hex_asc_upper
[((unsigned char)num
) & mask
] | locase
);
471 } else { /* base 10 */
472 i
= put_dec(tmp
, num
) - tmp
;
475 /* printing 100 using %2d gives "100", not "00" */
478 /* leading space padding */
479 field_width
-= precision
;
480 if (!(spec
.flags
& (ZEROPAD
| LEFT
))) {
481 while (--field_width
>= 0) {
493 /* "0x" / "0" prefix */
495 if (spec
.base
== 16 || !is_zero
) {
500 if (spec
.base
== 16) {
502 *buf
= ('X' | locase
);
506 /* zero or space padding */
507 if (!(spec
.flags
& LEFT
)) {
508 char c
= ' ' + (spec
.flags
& ZEROPAD
);
509 BUILD_BUG_ON(' ' + ZEROPAD
!= '0');
510 while (--field_width
>= 0) {
516 /* hmm even more zero padding? */
517 while (i
<= --precision
) {
522 /* actual digits of result */
528 /* trailing space padding */
529 while (--field_width
>= 0) {
538 static noinline_for_stack
539 char *special_hex_number(char *buf
, char *end
, unsigned long long num
, int size
)
541 struct printf_spec spec
;
543 spec
.type
= FORMAT_TYPE_PTR
;
544 spec
.field_width
= 2 + 2 * size
; /* 0x + hex */
545 spec
.flags
= SPECIAL
| SMALL
| ZEROPAD
;
549 return number(buf
, end
, num
, spec
);
552 static void move_right(char *buf
, char *end
, unsigned len
, unsigned spaces
)
555 if (buf
>= end
) /* nowhere to put anything */
558 if (size
<= spaces
) {
559 memset(buf
, ' ', size
);
563 if (len
> size
- spaces
)
565 memmove(buf
+ spaces
, buf
, len
);
567 memset(buf
, ' ', spaces
);
571 * Handle field width padding for a string.
572 * @buf: current buffer position
573 * @n: length of string
574 * @end: end of output buffer
575 * @spec: for field width and flags
576 * Returns: new buffer position after padding.
578 static noinline_for_stack
579 char *widen_string(char *buf
, int n
, char *end
, struct printf_spec spec
)
583 if (likely(n
>= spec
.field_width
))
585 /* we want to pad the sucker */
586 spaces
= spec
.field_width
- n
;
587 if (!(spec
.flags
& LEFT
)) {
588 move_right(buf
- n
, end
, n
, spaces
);
599 /* Handle string from a well known address. */
600 static char *string_nocheck(char *buf
, char *end
, const char *s
,
601 struct printf_spec spec
)
604 int lim
= spec
.precision
;
615 return widen_string(buf
, len
, end
, spec
);
618 static char *err_ptr(char *buf
, char *end
, void *ptr
,
619 struct printf_spec spec
)
621 int err
= PTR_ERR(ptr
);
622 const char *sym
= errname(err
);
625 return string_nocheck(buf
, end
, sym
, spec
);
628 * Somebody passed ERR_PTR(-1234) or some other non-existing
629 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
630 * printing it as its decimal representation.
634 return number(buf
, end
, err
, spec
);
637 /* Be careful: error messages must fit into the given buffer. */
638 static char *error_string(char *buf
, char *end
, const char *s
,
639 struct printf_spec spec
)
642 * Hard limit to avoid a completely insane messages. It actually
643 * works pretty well because most error messages are in
644 * the many pointer format modifiers.
646 if (spec
.precision
== -1)
647 spec
.precision
= 2 * sizeof(void *);
649 return string_nocheck(buf
, end
, s
, spec
);
653 * Do not call any complex external code here. Nested printk()/vsprintf()
654 * might cause infinite loops. Failures might break printk() and would
657 static const char *check_pointer_msg(const void *ptr
)
662 if ((unsigned long)ptr
< PAGE_SIZE
|| IS_ERR_VALUE(ptr
))
668 static int check_pointer(char **buf
, char *end
, const void *ptr
,
669 struct printf_spec spec
)
673 err_msg
= check_pointer_msg(ptr
);
675 *buf
= error_string(*buf
, end
, err_msg
, spec
);
682 static noinline_for_stack
683 char *string(char *buf
, char *end
, const char *s
,
684 struct printf_spec spec
)
686 if (check_pointer(&buf
, end
, s
, spec
))
689 return string_nocheck(buf
, end
, s
, spec
);
692 static char *pointer_string(char *buf
, char *end
,
694 struct printf_spec spec
)
698 if (spec
.field_width
== -1) {
699 spec
.field_width
= 2 * sizeof(ptr
);
700 spec
.flags
|= ZEROPAD
;
703 return number(buf
, end
, (unsigned long int)ptr
, spec
);
706 /* Make pointers available for printing early in the boot sequence. */
707 static int debug_boot_weak_hash __ro_after_init
;
709 static int __init
debug_boot_weak_hash_enable(char *str
)
711 debug_boot_weak_hash
= 1;
712 pr_info("debug_boot_weak_hash enabled\n");
715 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable
);
717 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key
);
718 static siphash_key_t ptr_key __read_mostly
;
720 static void enable_ptr_key_workfn(struct work_struct
*work
)
722 get_random_bytes(&ptr_key
, sizeof(ptr_key
));
723 /* Needs to run from preemptible context */
724 static_branch_disable(¬_filled_random_ptr_key
);
727 static DECLARE_WORK(enable_ptr_key_work
, enable_ptr_key_workfn
);
729 static void fill_random_ptr_key(struct random_ready_callback
*unused
)
731 /* This may be in an interrupt handler. */
732 queue_work(system_unbound_wq
, &enable_ptr_key_work
);
735 static struct random_ready_callback random_ready
= {
736 .func
= fill_random_ptr_key
739 static int __init
initialize_ptr_random(void)
741 int key_size
= sizeof(ptr_key
);
744 /* Use hw RNG if available. */
745 if (get_random_bytes_arch(&ptr_key
, key_size
) == key_size
) {
746 static_branch_disable(¬_filled_random_ptr_key
);
750 ret
= add_random_ready_callback(&random_ready
);
753 } else if (ret
== -EALREADY
) {
754 /* This is in preemptible context */
755 enable_ptr_key_workfn(&enable_ptr_key_work
);
761 early_initcall(initialize_ptr_random
);
763 /* Maps a pointer to a 32 bit unique identifier. */
764 static inline int __ptr_to_hashval(const void *ptr
, unsigned long *hashval_out
)
766 unsigned long hashval
;
768 if (static_branch_unlikely(¬_filled_random_ptr_key
))
772 hashval
= (unsigned long)siphash_1u64((u64
)ptr
, &ptr_key
);
774 * Mask off the first 32 bits, this makes explicit that we have
775 * modified the address (and 32 bits is plenty for a unique ID).
777 hashval
= hashval
& 0xffffffff;
779 hashval
= (unsigned long)siphash_1u32((u32
)ptr
, &ptr_key
);
781 *hashval_out
= hashval
;
785 int ptr_to_hashval(const void *ptr
, unsigned long *hashval_out
)
787 return __ptr_to_hashval(ptr
, hashval_out
);
790 static char *ptr_to_id(char *buf
, char *end
, const void *ptr
,
791 struct printf_spec spec
)
793 const char *str
= sizeof(ptr
) == 8 ? "(____ptrval____)" : "(ptrval)";
794 unsigned long hashval
;
798 * Print the real pointer value for NULL and error pointers,
799 * as they are not actual addresses.
801 if (IS_ERR_OR_NULL(ptr
))
802 return pointer_string(buf
, end
, ptr
, spec
);
804 /* When debugging early boot use non-cryptographically secure hash. */
805 if (unlikely(debug_boot_weak_hash
)) {
806 hashval
= hash_long((unsigned long)ptr
, 32);
807 return pointer_string(buf
, end
, (const void *)hashval
, spec
);
810 ret
= __ptr_to_hashval(ptr
, &hashval
);
812 spec
.field_width
= 2 * sizeof(ptr
);
813 /* string length must be less than default_width */
814 return error_string(buf
, end
, str
, spec
);
817 return pointer_string(buf
, end
, (const void *)hashval
, spec
);
820 int kptr_restrict __read_mostly
;
822 static noinline_for_stack
823 char *restricted_pointer(char *buf
, char *end
, const void *ptr
,
824 struct printf_spec spec
)
826 switch (kptr_restrict
) {
828 /* Handle as %p, hash and do _not_ leak addresses. */
829 return ptr_to_id(buf
, end
, ptr
, spec
);
831 const struct cred
*cred
;
834 * kptr_restrict==1 cannot be used in IRQ context
835 * because its test for CAP_SYSLOG would be meaningless.
837 if (in_irq() || in_serving_softirq() || in_nmi()) {
838 if (spec
.field_width
== -1)
839 spec
.field_width
= 2 * sizeof(ptr
);
840 return error_string(buf
, end
, "pK-error", spec
);
844 * Only print the real pointer value if the current
845 * process has CAP_SYSLOG and is running with the
846 * same credentials it started with. This is because
847 * access to files is checked at open() time, but %pK
848 * checks permission at read() time. We don't want to
849 * leak pointer values if a binary opens a file using
850 * %pK and then elevates privileges before reading it.
852 cred
= current_cred();
853 if (!has_capability_noaudit(current
, CAP_SYSLOG
) ||
854 !uid_eq(cred
->euid
, cred
->uid
) ||
855 !gid_eq(cred
->egid
, cred
->gid
))
861 /* Always print 0's for %pK */
866 return pointer_string(buf
, end
, ptr
, spec
);
869 static noinline_for_stack
870 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
873 const char *array
[4], *s
;
874 const struct dentry
*p
;
879 case '2': case '3': case '4':
880 depth
= fmt
[1] - '0';
887 for (i
= 0; i
< depth
; i
++, d
= p
) {
888 if (check_pointer(&buf
, end
, d
, spec
)) {
893 p
= READ_ONCE(d
->d_parent
);
894 array
[i
] = READ_ONCE(d
->d_name
.name
);
903 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
915 return widen_string(buf
, n
, end
, spec
);
918 static noinline_for_stack
919 char *file_dentry_name(char *buf
, char *end
, const struct file
*f
,
920 struct printf_spec spec
, const char *fmt
)
922 if (check_pointer(&buf
, end
, f
, spec
))
925 return dentry_name(buf
, end
, f
->f_path
.dentry
, spec
, fmt
);
928 static noinline_for_stack
929 char *bdev_name(char *buf
, char *end
, struct block_device
*bdev
,
930 struct printf_spec spec
, const char *fmt
)
934 if (check_pointer(&buf
, end
, bdev
, spec
))
938 buf
= string(buf
, end
, hd
->disk_name
, spec
);
939 if (bdev
->bd_part
->partno
) {
940 if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1])) {
945 buf
= number(buf
, end
, bdev
->bd_part
->partno
, spec
);
951 static noinline_for_stack
952 char *symbol_string(char *buf
, char *end
, void *ptr
,
953 struct printf_spec spec
, const char *fmt
)
956 #ifdef CONFIG_KALLSYMS
957 char sym
[KSYM_SYMBOL_LEN
];
961 ptr
= __builtin_extract_return_addr(ptr
);
962 value
= (unsigned long)ptr
;
964 #ifdef CONFIG_KALLSYMS
966 sprint_backtrace(sym
, value
);
967 else if (*fmt
!= 's')
968 sprint_symbol(sym
, value
);
970 sprint_symbol_no_offset(sym
, value
);
972 return string_nocheck(buf
, end
, sym
, spec
);
974 return special_hex_number(buf
, end
, value
, sizeof(void *));
978 static const struct printf_spec default_str_spec
= {
983 static const struct printf_spec default_flag_spec
= {
986 .flags
= SPECIAL
| SMALL
,
989 static const struct printf_spec default_dec_spec
= {
994 static const struct printf_spec default_dec02_spec
= {
1001 static const struct printf_spec default_dec04_spec
= {
1008 static noinline_for_stack
1009 char *resource_string(char *buf
, char *end
, struct resource
*res
,
1010 struct printf_spec spec
, const char *fmt
)
1012 #ifndef IO_RSRC_PRINTK_SIZE
1013 #define IO_RSRC_PRINTK_SIZE 6
1016 #ifndef MEM_RSRC_PRINTK_SIZE
1017 #define MEM_RSRC_PRINTK_SIZE 10
1019 static const struct printf_spec io_spec
= {
1021 .field_width
= IO_RSRC_PRINTK_SIZE
,
1023 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
1025 static const struct printf_spec mem_spec
= {
1027 .field_width
= MEM_RSRC_PRINTK_SIZE
,
1029 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
1031 static const struct printf_spec bus_spec
= {
1035 .flags
= SMALL
| ZEROPAD
,
1037 static const struct printf_spec str_spec
= {
1043 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1044 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1045 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1046 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
1047 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
1048 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1049 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
1050 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
1052 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
1053 int decode
= (fmt
[0] == 'R') ? 1 : 0;
1054 const struct printf_spec
*specp
;
1056 if (check_pointer(&buf
, end
, res
, spec
))
1060 if (res
->flags
& IORESOURCE_IO
) {
1061 p
= string_nocheck(p
, pend
, "io ", str_spec
);
1063 } else if (res
->flags
& IORESOURCE_MEM
) {
1064 p
= string_nocheck(p
, pend
, "mem ", str_spec
);
1066 } else if (res
->flags
& IORESOURCE_IRQ
) {
1067 p
= string_nocheck(p
, pend
, "irq ", str_spec
);
1068 specp
= &default_dec_spec
;
1069 } else if (res
->flags
& IORESOURCE_DMA
) {
1070 p
= string_nocheck(p
, pend
, "dma ", str_spec
);
1071 specp
= &default_dec_spec
;
1072 } else if (res
->flags
& IORESOURCE_BUS
) {
1073 p
= string_nocheck(p
, pend
, "bus ", str_spec
);
1076 p
= string_nocheck(p
, pend
, "??? ", str_spec
);
1080 if (decode
&& res
->flags
& IORESOURCE_UNSET
) {
1081 p
= string_nocheck(p
, pend
, "size ", str_spec
);
1082 p
= number(p
, pend
, resource_size(res
), *specp
);
1084 p
= number(p
, pend
, res
->start
, *specp
);
1085 if (res
->start
!= res
->end
) {
1087 p
= number(p
, pend
, res
->end
, *specp
);
1091 if (res
->flags
& IORESOURCE_MEM_64
)
1092 p
= string_nocheck(p
, pend
, " 64bit", str_spec
);
1093 if (res
->flags
& IORESOURCE_PREFETCH
)
1094 p
= string_nocheck(p
, pend
, " pref", str_spec
);
1095 if (res
->flags
& IORESOURCE_WINDOW
)
1096 p
= string_nocheck(p
, pend
, " window", str_spec
);
1097 if (res
->flags
& IORESOURCE_DISABLED
)
1098 p
= string_nocheck(p
, pend
, " disabled", str_spec
);
1100 p
= string_nocheck(p
, pend
, " flags ", str_spec
);
1101 p
= number(p
, pend
, res
->flags
, default_flag_spec
);
1106 return string_nocheck(buf
, end
, sym
, spec
);
1109 static noinline_for_stack
1110 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1113 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
1114 negative value, fallback to the default */
1117 if (spec
.field_width
== 0)
1118 /* nothing to print */
1121 if (check_pointer(&buf
, end
, addr
, spec
))
1139 if (spec
.field_width
> 0)
1140 len
= min_t(int, spec
.field_width
, 64);
1142 for (i
= 0; i
< len
; ++i
) {
1144 *buf
= hex_asc_hi(addr
[i
]);
1147 *buf
= hex_asc_lo(addr
[i
]);
1150 if (separator
&& i
!= len
- 1) {
1160 static noinline_for_stack
1161 char *bitmap_string(char *buf
, char *end
, unsigned long *bitmap
,
1162 struct printf_spec spec
, const char *fmt
)
1164 const int CHUNKSZ
= 32;
1165 int nr_bits
= max_t(int, spec
.field_width
, 0);
1169 if (check_pointer(&buf
, end
, bitmap
, spec
))
1172 /* reused to print numbers */
1173 spec
= (struct printf_spec
){ .flags
= SMALL
| ZEROPAD
, .base
= 16 };
1175 chunksz
= nr_bits
& (CHUNKSZ
- 1);
1179 i
= ALIGN(nr_bits
, CHUNKSZ
) - CHUNKSZ
;
1180 for (; i
>= 0; i
-= CHUNKSZ
) {
1184 chunkmask
= ((1ULL << chunksz
) - 1);
1185 word
= i
/ BITS_PER_LONG
;
1186 bit
= i
% BITS_PER_LONG
;
1187 val
= (bitmap
[word
] >> bit
) & chunkmask
;
1196 spec
.field_width
= DIV_ROUND_UP(chunksz
, 4);
1197 buf
= number(buf
, end
, val
, spec
);
1204 static noinline_for_stack
1205 char *bitmap_list_string(char *buf
, char *end
, unsigned long *bitmap
,
1206 struct printf_spec spec
, const char *fmt
)
1208 int nr_bits
= max_t(int, spec
.field_width
, 0);
1209 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1210 int cur
, rbot
, rtop
;
1213 if (check_pointer(&buf
, end
, bitmap
, spec
))
1216 rbot
= cur
= find_first_bit(bitmap
, nr_bits
);
1217 while (cur
< nr_bits
) {
1219 cur
= find_next_bit(bitmap
, nr_bits
, cur
+ 1);
1220 if (cur
< nr_bits
&& cur
<= rtop
+ 1)
1230 buf
= number(buf
, end
, rbot
, default_dec_spec
);
1236 buf
= number(buf
, end
, rtop
, default_dec_spec
);
1244 static noinline_for_stack
1245 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
1246 struct printf_spec spec
, const char *fmt
)
1248 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
1252 bool reversed
= false;
1254 if (check_pointer(&buf
, end
, addr
, spec
))
1271 for (i
= 0; i
< 6; i
++) {
1273 p
= hex_byte_pack(p
, addr
[5 - i
]);
1275 p
= hex_byte_pack(p
, addr
[i
]);
1277 if (fmt
[0] == 'M' && i
!= 5)
1282 return string_nocheck(buf
, end
, mac_addr
, spec
);
1285 static noinline_for_stack
1286 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
1289 bool leading_zeros
= (fmt
[0] == 'i');
1314 for (i
= 0; i
< 4; i
++) {
1315 char temp
[4] __aligned(2); /* hold each IP quad in reverse order */
1316 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
1317 if (leading_zeros
) {
1323 /* reverse the digits in the quad */
1325 *p
++ = temp
[digits
];
1335 static noinline_for_stack
1336 char *ip6_compressed_string(char *p
, const char *addr
)
1339 unsigned char zerolength
[8];
1344 bool needcolon
= false;
1346 struct in6_addr in6
;
1348 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
1350 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
1352 memset(zerolength
, 0, sizeof(zerolength
));
1359 /* find position of longest 0 run */
1360 for (i
= 0; i
< range
; i
++) {
1361 for (j
= i
; j
< range
; j
++) {
1362 if (in6
.s6_addr16
[j
] != 0)
1367 for (i
= 0; i
< range
; i
++) {
1368 if (zerolength
[i
] > longest
) {
1369 longest
= zerolength
[i
];
1373 if (longest
== 1) /* don't compress a single 0 */
1377 for (i
= 0; i
< range
; i
++) {
1378 if (i
== colonpos
) {
1379 if (needcolon
|| i
== 0)
1390 /* hex u16 without leading 0s */
1391 word
= ntohs(in6
.s6_addr16
[i
]);
1396 p
= hex_byte_pack(p
, hi
);
1398 *p
++ = hex_asc_lo(hi
);
1399 p
= hex_byte_pack(p
, lo
);
1402 p
= hex_byte_pack(p
, lo
);
1404 *p
++ = hex_asc_lo(lo
);
1411 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
1418 static noinline_for_stack
1419 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
1423 for (i
= 0; i
< 8; i
++) {
1424 p
= hex_byte_pack(p
, *addr
++);
1425 p
= hex_byte_pack(p
, *addr
++);
1426 if (fmt
[0] == 'I' && i
!= 7)
1434 static noinline_for_stack
1435 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
1436 struct printf_spec spec
, const char *fmt
)
1438 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1440 if (fmt
[0] == 'I' && fmt
[2] == 'c')
1441 ip6_compressed_string(ip6_addr
, addr
);
1443 ip6_string(ip6_addr
, addr
, fmt
);
1445 return string_nocheck(buf
, end
, ip6_addr
, spec
);
1448 static noinline_for_stack
1449 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
1450 struct printf_spec spec
, const char *fmt
)
1452 char ip4_addr
[sizeof("255.255.255.255")];
1454 ip4_string(ip4_addr
, addr
, fmt
);
1456 return string_nocheck(buf
, end
, ip4_addr
, spec
);
1459 static noinline_for_stack
1460 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1461 struct printf_spec spec
, const char *fmt
)
1463 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1464 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1465 sizeof(":12345") + sizeof("/123456789") +
1466 sizeof("%1234567890")];
1467 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1468 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1469 char fmt6
[2] = { fmt
[0], '6' };
1473 while (isalpha(*++fmt
)) {
1490 if (have_p
|| have_s
|| have_f
) {
1495 if (fmt6
[0] == 'I' && have_c
)
1496 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1498 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1500 if (have_p
|| have_s
|| have_f
)
1505 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1509 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1510 IPV6_FLOWINFO_MASK
), spec
);
1514 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1518 return string_nocheck(buf
, end
, ip6_addr
, spec
);
1521 static noinline_for_stack
1522 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1523 struct printf_spec spec
, const char *fmt
)
1525 bool have_p
= false;
1526 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1527 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1528 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1529 char fmt4
[3] = { fmt
[0], '4', 0 };
1532 while (isalpha(*++fmt
)) {
1546 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1549 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1553 return string_nocheck(buf
, end
, ip4_addr
, spec
);
1556 static noinline_for_stack
1557 char *ip_addr_string(char *buf
, char *end
, const void *ptr
,
1558 struct printf_spec spec
, const char *fmt
)
1562 if (check_pointer(&buf
, end
, ptr
, spec
))
1567 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1569 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1572 struct sockaddr raw
;
1573 struct sockaddr_in v4
;
1574 struct sockaddr_in6 v6
;
1577 switch (sa
->raw
.sa_family
) {
1579 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
1581 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
1583 return error_string(buf
, end
, "(einval)", spec
);
1587 err_fmt_msg
= fmt
[0] == 'i' ? "(%pi?)" : "(%pI?)";
1588 return error_string(buf
, end
, err_fmt_msg
, spec
);
1591 static noinline_for_stack
1592 char *escaped_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1597 unsigned int flags
= 0;
1600 if (spec
.field_width
== 0)
1601 return buf
; /* nothing to print */
1603 if (check_pointer(&buf
, end
, addr
, spec
))
1607 switch (fmt
[count
++]) {
1609 flags
|= ESCAPE_ANY
;
1612 flags
|= ESCAPE_SPECIAL
;
1615 flags
|= ESCAPE_HEX
;
1618 flags
|= ESCAPE_NULL
;
1621 flags
|= ESCAPE_OCTAL
;
1627 flags
|= ESCAPE_SPACE
;
1636 flags
= ESCAPE_ANY_NP
;
1638 len
= spec
.field_width
< 0 ? 1 : spec
.field_width
;
1641 * string_escape_mem() writes as many characters as it can to
1642 * the given buffer, and returns the total size of the output
1643 * had the buffer been big enough.
1645 buf
+= string_escape_mem(addr
, len
, buf
, buf
< end
? end
- buf
: 0, flags
, NULL
);
1650 static char *va_format(char *buf
, char *end
, struct va_format
*va_fmt
,
1651 struct printf_spec spec
, const char *fmt
)
1655 if (check_pointer(&buf
, end
, va_fmt
, spec
))
1658 va_copy(va
, *va_fmt
->va
);
1659 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0, va_fmt
->fmt
, va
);
1665 static noinline_for_stack
1666 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1667 struct printf_spec spec
, const char *fmt
)
1669 char uuid
[UUID_STRING_LEN
+ 1];
1672 const u8
*index
= uuid_index
;
1675 if (check_pointer(&buf
, end
, addr
, spec
))
1680 uc
= true; /* fall-through */
1689 for (i
= 0; i
< 16; i
++) {
1691 p
= hex_byte_pack_upper(p
, addr
[index
[i
]]);
1693 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1706 return string_nocheck(buf
, end
, uuid
, spec
);
1709 static noinline_for_stack
1710 char *netdev_bits(char *buf
, char *end
, const void *addr
,
1711 struct printf_spec spec
, const char *fmt
)
1713 unsigned long long num
;
1716 if (check_pointer(&buf
, end
, addr
, spec
))
1721 num
= *(const netdev_features_t
*)addr
;
1722 size
= sizeof(netdev_features_t
);
1725 return error_string(buf
, end
, "(%pN?)", spec
);
1728 return special_hex_number(buf
, end
, num
, size
);
1731 static noinline_for_stack
1732 char *address_val(char *buf
, char *end
, const void *addr
,
1733 struct printf_spec spec
, const char *fmt
)
1735 unsigned long long num
;
1738 if (check_pointer(&buf
, end
, addr
, spec
))
1743 num
= *(const dma_addr_t
*)addr
;
1744 size
= sizeof(dma_addr_t
);
1748 num
= *(const phys_addr_t
*)addr
;
1749 size
= sizeof(phys_addr_t
);
1753 return special_hex_number(buf
, end
, num
, size
);
1756 static noinline_for_stack
1757 char *date_str(char *buf
, char *end
, const struct rtc_time
*tm
, bool r
)
1759 int year
= tm
->tm_year
+ (r
? 0 : 1900);
1760 int mon
= tm
->tm_mon
+ (r
? 0 : 1);
1762 buf
= number(buf
, end
, year
, default_dec04_spec
);
1767 buf
= number(buf
, end
, mon
, default_dec02_spec
);
1772 return number(buf
, end
, tm
->tm_mday
, default_dec02_spec
);
1775 static noinline_for_stack
1776 char *time_str(char *buf
, char *end
, const struct rtc_time
*tm
, bool r
)
1778 buf
= number(buf
, end
, tm
->tm_hour
, default_dec02_spec
);
1783 buf
= number(buf
, end
, tm
->tm_min
, default_dec02_spec
);
1788 return number(buf
, end
, tm
->tm_sec
, default_dec02_spec
);
1791 static noinline_for_stack
1792 char *rtc_str(char *buf
, char *end
, const struct rtc_time
*tm
,
1793 struct printf_spec spec
, const char *fmt
)
1795 bool have_t
= true, have_d
= true;
1799 if (check_pointer(&buf
, end
, tm
, spec
))
1802 switch (fmt
[count
]) {
1813 raw
= fmt
[count
] == 'r';
1816 buf
= date_str(buf
, end
, tm
, raw
);
1817 if (have_d
&& have_t
) {
1818 /* Respect ISO 8601 */
1824 buf
= time_str(buf
, end
, tm
, raw
);
1829 static noinline_for_stack
1830 char *time_and_date(char *buf
, char *end
, void *ptr
, struct printf_spec spec
,
1835 return rtc_str(buf
, end
, (const struct rtc_time
*)ptr
, spec
, fmt
);
1837 return error_string(buf
, end
, "(%ptR?)", spec
);
1841 static noinline_for_stack
1842 char *clock(char *buf
, char *end
, struct clk
*clk
, struct printf_spec spec
,
1845 if (!IS_ENABLED(CONFIG_HAVE_CLK
))
1846 return error_string(buf
, end
, "(%pC?)", spec
);
1848 if (check_pointer(&buf
, end
, clk
, spec
))
1854 #ifdef CONFIG_COMMON_CLK
1855 return string(buf
, end
, __clk_get_name(clk
), spec
);
1857 return ptr_to_id(buf
, end
, clk
, spec
);
1863 char *format_flags(char *buf
, char *end
, unsigned long flags
,
1864 const struct trace_print_flags
*names
)
1868 for ( ; flags
&& names
->name
; names
++) {
1870 if ((flags
& mask
) != mask
)
1873 buf
= string(buf
, end
, names
->name
, default_str_spec
);
1884 buf
= number(buf
, end
, flags
, default_flag_spec
);
1889 static noinline_for_stack
1890 char *flags_string(char *buf
, char *end
, void *flags_ptr
,
1891 struct printf_spec spec
, const char *fmt
)
1893 unsigned long flags
;
1894 const struct trace_print_flags
*names
;
1896 if (check_pointer(&buf
, end
, flags_ptr
, spec
))
1901 flags
= *(unsigned long *)flags_ptr
;
1902 /* Remove zone id */
1903 flags
&= (1UL << NR_PAGEFLAGS
) - 1;
1904 names
= pageflag_names
;
1907 flags
= *(unsigned long *)flags_ptr
;
1908 names
= vmaflag_names
;
1911 flags
= *(gfp_t
*)flags_ptr
;
1912 names
= gfpflag_names
;
1915 return error_string(buf
, end
, "(%pG?)", spec
);
1918 return format_flags(buf
, end
, flags
, names
);
1921 static noinline_for_stack
1922 char *fwnode_full_name_string(struct fwnode_handle
*fwnode
, char *buf
,
1927 /* Loop starting from the root node to the current node. */
1928 for (depth
= fwnode_count_parents(fwnode
); depth
>= 0; depth
--) {
1929 struct fwnode_handle
*__fwnode
=
1930 fwnode_get_nth_parent(fwnode
, depth
);
1932 buf
= string(buf
, end
, fwnode_get_name_prefix(__fwnode
),
1934 buf
= string(buf
, end
, fwnode_get_name(__fwnode
),
1937 fwnode_handle_put(__fwnode
);
1943 static noinline_for_stack
1944 char *device_node_string(char *buf
, char *end
, struct device_node
*dn
,
1945 struct printf_spec spec
, const char *fmt
)
1947 char tbuf
[sizeof("xxxx") + 1];
1950 char *buf_start
= buf
;
1951 struct property
*prop
;
1952 bool has_mult
, pass
;
1953 static const struct printf_spec num_spec
= {
1960 struct printf_spec str_spec
= spec
;
1961 str_spec
.field_width
= -1;
1964 return error_string(buf
, end
, "(%pO?)", spec
);
1966 if (!IS_ENABLED(CONFIG_OF
))
1967 return error_string(buf
, end
, "(%pOF?)", spec
);
1969 if (check_pointer(&buf
, end
, dn
, spec
))
1972 /* simple case without anything any more format specifiers */
1974 if (fmt
[0] == '\0' || strcspn(fmt
,"fnpPFcC") > 0)
1977 for (pass
= false; strspn(fmt
,"fnpPFcC"); fmt
++, pass
= true) {
1986 case 'f': /* full_name */
1987 buf
= fwnode_full_name_string(of_fwnode_handle(dn
), buf
,
1990 case 'n': /* name */
1991 p
= fwnode_get_name(of_fwnode_handle(dn
));
1992 precision
= str_spec
.precision
;
1993 str_spec
.precision
= strchrnul(p
, '@') - p
;
1994 buf
= string(buf
, end
, p
, str_spec
);
1995 str_spec
.precision
= precision
;
1997 case 'p': /* phandle */
1998 buf
= number(buf
, end
, (unsigned int)dn
->phandle
, num_spec
);
2000 case 'P': /* path-spec */
2001 p
= fwnode_get_name(of_fwnode_handle(dn
));
2004 buf
= string(buf
, end
, p
, str_spec
);
2006 case 'F': /* flags */
2007 tbuf
[0] = of_node_check_flag(dn
, OF_DYNAMIC
) ? 'D' : '-';
2008 tbuf
[1] = of_node_check_flag(dn
, OF_DETACHED
) ? 'd' : '-';
2009 tbuf
[2] = of_node_check_flag(dn
, OF_POPULATED
) ? 'P' : '-';
2010 tbuf
[3] = of_node_check_flag(dn
, OF_POPULATED_BUS
) ? 'B' : '-';
2012 buf
= string_nocheck(buf
, end
, tbuf
, str_spec
);
2014 case 'c': /* major compatible string */
2015 ret
= of_property_read_string(dn
, "compatible", &p
);
2017 buf
= string(buf
, end
, p
, str_spec
);
2019 case 'C': /* full compatible string */
2021 of_property_for_each_string(dn
, "compatible", prop
, p
) {
2023 buf
= string_nocheck(buf
, end
, ",", str_spec
);
2024 buf
= string_nocheck(buf
, end
, "\"", str_spec
);
2025 buf
= string(buf
, end
, p
, str_spec
);
2026 buf
= string_nocheck(buf
, end
, "\"", str_spec
);
2036 return widen_string(buf
, buf
- buf_start
, end
, spec
);
2039 static noinline_for_stack
2040 char *fwnode_string(char *buf
, char *end
, struct fwnode_handle
*fwnode
,
2041 struct printf_spec spec
, const char *fmt
)
2043 struct printf_spec str_spec
= spec
;
2044 char *buf_start
= buf
;
2046 str_spec
.field_width
= -1;
2049 return error_string(buf
, end
, "(%pf?)", spec
);
2051 if (check_pointer(&buf
, end
, fwnode
, spec
))
2057 case 'P': /* name */
2058 buf
= string(buf
, end
, fwnode_get_name(fwnode
), str_spec
);
2060 case 'f': /* full_name */
2062 buf
= fwnode_full_name_string(fwnode
, buf
, end
);
2066 return widen_string(buf
, buf
- buf_start
, end
, spec
);
2070 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2071 * by an extra set of alphanumeric characters that are extended format
2074 * Please update scripts/checkpatch.pl when adding/removing conversion
2075 * characters. (Search for "check for vsprintf extension").
2077 * Right now we handle:
2079 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2080 * - 's' For symbolic direct pointers (or function descriptors) without offset
2081 * - '[Ss]R' as above with __builtin_extract_return_addr() translation
2082 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2083 * %ps and %pS. Be careful when re-using these specifiers.
2084 * - 'B' For backtraced symbolic direct pointers with offset
2085 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2086 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2087 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2088 * width which must be explicitly specified either as part of the
2089 * format string '%32b[l]' or through '%*b[l]', [l] selects
2090 * range-list format instead of hex format
2091 * - 'M' For a 6-byte MAC address, it prints the address in the
2092 * usual colon-separated hex notation
2093 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2094 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2095 * with a dash-separated hex notation
2096 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
2097 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2098 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2099 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
2101 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2102 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2103 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2104 * IPv6 omits the colons (01020304...0f)
2105 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
2107 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2108 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2109 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2110 * - 'I[6S]c' for IPv6 addresses printed as specified by
2111 * http://tools.ietf.org/html/rfc5952
2112 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2113 * of the following flags (see string_escape_mem() for the
2116 * c - ESCAPE_SPECIAL
2122 * By default ESCAPE_ANY_NP is used.
2123 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2124 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2125 * Options for %pU are:
2126 * b big endian lower case hex (default)
2127 * B big endian UPPER case hex
2128 * l little endian lower case hex
2129 * L little endian UPPER case hex
2130 * big endian output byte order is:
2131 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2132 * little endian output byte order is:
2133 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
2134 * - 'V' For a struct va_format which contains a format string * and va_list *,
2135 * call vsnprintf(->format, *->va_list).
2136 * Implements a "recursive vsnprintf".
2137 * Do not use this feature without some mechanism to verify the
2138 * correctness of the format string and va_list arguments.
2139 * - 'K' For a kernel pointer that should be hidden from unprivileged users
2140 * - 'NF' For a netdev_features_t
2141 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2142 * a certain separator (' ' by default):
2146 * The maximum supported length is 64 bytes of the input. Consider
2147 * to use print_hex_dump() for the larger input.
2148 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2149 * (default assumed to be phys_addr_t, passed by reference)
2150 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2151 * - 'D[234]' Same as 'd' but for a struct file
2152 * - 'g' For block_device name (gendisk + partition number)
2153 * - 't[R][dt][r]' For time and date as represented:
2155 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2156 * (legacy clock framework) of the clock
2157 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2158 * (legacy clock framework) of the clock
2159 * - 'G' For flags to be printed as a collection of symbolic strings that would
2160 * construct the specific value. Supported flags given by option:
2161 * p page flags (see struct page) given as pointer to unsigned long
2162 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2163 * v vma flags (VM_*) given as pointer to unsigned long
2164 * - 'OF[fnpPcCF]' For a device tree object
2165 * Without any optional arguments prints the full_name
2166 * f device node full_name
2167 * n device node name
2168 * p device node phandle
2169 * P device node path spec (name + @unit)
2170 * F device node flags
2171 * c major compatible string
2172 * C full compatible string
2173 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2174 * Without an option prints the full name of the node
2176 * P node name, including a possible unit address
2177 * - 'x' For printing the address. Equivalent to "%lx".
2178 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2179 * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2180 * or user (u) memory to probe, and:
2181 * s a string, equivalent to "%s" on direct vsnprintf() use
2183 * ** When making changes please also update:
2184 * Documentation/core-api/printk-formats.rst
2186 * Note: The default behaviour (unadorned %p) is to hash the address,
2187 * rendering it useful as a unique identifier.
2189 static noinline_for_stack
2190 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
2191 struct printf_spec spec
)
2196 ptr
= dereference_symbol_descriptor(ptr
);
2199 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
2202 return resource_string(buf
, end
, ptr
, spec
, fmt
);
2204 return hex_string(buf
, end
, ptr
, spec
, fmt
);
2208 return bitmap_list_string(buf
, end
, ptr
, spec
, fmt
);
2210 return bitmap_string(buf
, end
, ptr
, spec
, fmt
);
2212 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2213 case 'm': /* Contiguous: 000102030405 */
2215 /* [mM]R (Reverse order; Bluetooth) */
2216 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
2217 case 'I': /* Formatted IP supported
2219 * 6: 0001:0203:...:0708
2220 * 6c: 1::708 or 1::1.2.3.4
2222 case 'i': /* Contiguous:
2223 * 4: 001.002.003.004
2226 return ip_addr_string(buf
, end
, ptr
, spec
, fmt
);
2228 return escaped_string(buf
, end
, ptr
, spec
, fmt
);
2230 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
2232 return va_format(buf
, end
, ptr
, spec
, fmt
);
2234 return restricted_pointer(buf
, end
, ptr
, spec
);
2236 return netdev_bits(buf
, end
, ptr
, spec
, fmt
);
2238 return address_val(buf
, end
, ptr
, spec
, fmt
);
2240 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
2242 return time_and_date(buf
, end
, ptr
, spec
, fmt
);
2244 return clock(buf
, end
, ptr
, spec
, fmt
);
2246 return file_dentry_name(buf
, end
, ptr
, spec
, fmt
);
2249 return bdev_name(buf
, end
, ptr
, spec
, fmt
);
2253 return flags_string(buf
, end
, ptr
, spec
, fmt
);
2255 return device_node_string(buf
, end
, ptr
, spec
, fmt
+ 1);
2257 return fwnode_string(buf
, end
, ptr
, spec
, fmt
+ 1);
2259 return pointer_string(buf
, end
, ptr
, spec
);
2261 /* %pe with a non-ERR_PTR gets treated as plain %p */
2264 return err_ptr(buf
, end
, ptr
, spec
);
2269 return string(buf
, end
, ptr
, spec
);
2271 return error_string(buf
, end
, "(einval)", spec
);
2275 /* default is to _not_ leak addresses, hash before printing */
2276 return ptr_to_id(buf
, end
, ptr
, spec
);
2280 * Helper function to decode printf style format.
2281 * Each call decode a token from the format and return the
2282 * number of characters read (or likely the delta where it wants
2283 * to go on the next call).
2284 * The decoded token is returned through the parameters
2286 * 'h', 'l', or 'L' for integer fields
2287 * 'z' support added 23/7/1999 S.H.
2288 * 'z' changed to 'Z' --davidm 1/25/99
2289 * 'Z' changed to 'z' --adobriyan 2017-01-25
2290 * 't' added for ptrdiff_t
2292 * @fmt: the format string
2293 * @type of the token returned
2294 * @flags: various flags such as +, -, # tokens..
2295 * @field_width: overwritten width
2296 * @base: base of the number (octal, hex, ...)
2297 * @precision: precision of a number
2298 * @qualifier: qualifier of a number (long, size_t, ...)
2300 static noinline_for_stack
2301 int format_decode(const char *fmt
, struct printf_spec
*spec
)
2303 const char *start
= fmt
;
2306 /* we finished early by reading the field width */
2307 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
2308 if (spec
->field_width
< 0) {
2309 spec
->field_width
= -spec
->field_width
;
2310 spec
->flags
|= LEFT
;
2312 spec
->type
= FORMAT_TYPE_NONE
;
2316 /* we finished early by reading the precision */
2317 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
2318 if (spec
->precision
< 0)
2319 spec
->precision
= 0;
2321 spec
->type
= FORMAT_TYPE_NONE
;
2326 spec
->type
= FORMAT_TYPE_NONE
;
2328 for (; *fmt
; ++fmt
) {
2333 /* Return the current non-format string */
2334 if (fmt
!= start
|| !*fmt
)
2340 while (1) { /* this also skips first '%' */
2346 case '-': spec
->flags
|= LEFT
; break;
2347 case '+': spec
->flags
|= PLUS
; break;
2348 case ' ': spec
->flags
|= SPACE
; break;
2349 case '#': spec
->flags
|= SPECIAL
; break;
2350 case '0': spec
->flags
|= ZEROPAD
; break;
2351 default: found
= false;
2358 /* get field width */
2359 spec
->field_width
= -1;
2362 spec
->field_width
= skip_atoi(&fmt
);
2363 else if (*fmt
== '*') {
2364 /* it's the next argument */
2365 spec
->type
= FORMAT_TYPE_WIDTH
;
2366 return ++fmt
- start
;
2370 /* get the precision */
2371 spec
->precision
= -1;
2374 if (isdigit(*fmt
)) {
2375 spec
->precision
= skip_atoi(&fmt
);
2376 if (spec
->precision
< 0)
2377 spec
->precision
= 0;
2378 } else if (*fmt
== '*') {
2379 /* it's the next argument */
2380 spec
->type
= FORMAT_TYPE_PRECISION
;
2381 return ++fmt
- start
;
2386 /* get the conversion qualifier */
2388 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2389 *fmt
== 'z' || *fmt
== 't') {
2391 if (unlikely(qualifier
== *fmt
)) {
2392 if (qualifier
== 'l') {
2395 } else if (qualifier
== 'h') {
2406 spec
->type
= FORMAT_TYPE_CHAR
;
2407 return ++fmt
- start
;
2410 spec
->type
= FORMAT_TYPE_STR
;
2411 return ++fmt
- start
;
2414 spec
->type
= FORMAT_TYPE_PTR
;
2415 return ++fmt
- start
;
2418 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
2419 return ++fmt
- start
;
2421 /* integer number formats - set up the flags and "break" */
2427 spec
->flags
|= SMALL
;
2436 spec
->flags
|= SIGN
;
2442 * Since %n poses a greater security risk than
2443 * utility, treat it as any other invalid or
2444 * unsupported format specifier.
2449 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt
);
2450 spec
->type
= FORMAT_TYPE_INVALID
;
2454 if (qualifier
== 'L')
2455 spec
->type
= FORMAT_TYPE_LONG_LONG
;
2456 else if (qualifier
== 'l') {
2457 BUILD_BUG_ON(FORMAT_TYPE_ULONG
+ SIGN
!= FORMAT_TYPE_LONG
);
2458 spec
->type
= FORMAT_TYPE_ULONG
+ (spec
->flags
& SIGN
);
2459 } else if (qualifier
== 'z') {
2460 spec
->type
= FORMAT_TYPE_SIZE_T
;
2461 } else if (qualifier
== 't') {
2462 spec
->type
= FORMAT_TYPE_PTRDIFF
;
2463 } else if (qualifier
== 'H') {
2464 BUILD_BUG_ON(FORMAT_TYPE_UBYTE
+ SIGN
!= FORMAT_TYPE_BYTE
);
2465 spec
->type
= FORMAT_TYPE_UBYTE
+ (spec
->flags
& SIGN
);
2466 } else if (qualifier
== 'h') {
2467 BUILD_BUG_ON(FORMAT_TYPE_USHORT
+ SIGN
!= FORMAT_TYPE_SHORT
);
2468 spec
->type
= FORMAT_TYPE_USHORT
+ (spec
->flags
& SIGN
);
2470 BUILD_BUG_ON(FORMAT_TYPE_UINT
+ SIGN
!= FORMAT_TYPE_INT
);
2471 spec
->type
= FORMAT_TYPE_UINT
+ (spec
->flags
& SIGN
);
2474 return ++fmt
- start
;
2478 set_field_width(struct printf_spec
*spec
, int width
)
2480 spec
->field_width
= width
;
2481 if (WARN_ONCE(spec
->field_width
!= width
, "field width %d too large", width
)) {
2482 spec
->field_width
= clamp(width
, -FIELD_WIDTH_MAX
, FIELD_WIDTH_MAX
);
2487 set_precision(struct printf_spec
*spec
, int prec
)
2489 spec
->precision
= prec
;
2490 if (WARN_ONCE(spec
->precision
!= prec
, "precision %d too large", prec
)) {
2491 spec
->precision
= clamp(prec
, 0, PRECISION_MAX
);
2496 * vsnprintf - Format a string and place it in a buffer
2497 * @buf: The buffer to place the result into
2498 * @size: The size of the buffer, including the trailing null space
2499 * @fmt: The format string to use
2500 * @args: Arguments for the format string
2502 * This function generally follows C99 vsnprintf, but has some
2503 * extensions and a few limitations:
2505 * - ``%n`` is unsupported
2506 * - ``%p*`` is handled by pointer()
2508 * See pointer() or Documentation/core-api/printk-formats.rst for more
2509 * extensive description.
2511 * **Please update the documentation in both places when making changes**
2513 * The return value is the number of characters which would
2514 * be generated for the given input, excluding the trailing
2515 * '\0', as per ISO C99. If you want to have the exact
2516 * number of characters written into @buf as return value
2517 * (not including the trailing '\0'), use vscnprintf(). If the
2518 * return is greater than or equal to @size, the resulting
2519 * string is truncated.
2521 * If you're not already dealing with a va_list consider using snprintf().
2523 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2525 unsigned long long num
;
2527 struct printf_spec spec
= {0};
2529 /* Reject out-of-range values early. Large positive sizes are
2530 used for unknown buffer sizes. */
2531 if (WARN_ON_ONCE(size
> INT_MAX
))
2537 /* Make sure end is always >= buf */
2544 const char *old_fmt
= fmt
;
2545 int read
= format_decode(fmt
, &spec
);
2549 switch (spec
.type
) {
2550 case FORMAT_TYPE_NONE
: {
2553 if (copy
> end
- str
)
2555 memcpy(str
, old_fmt
, copy
);
2561 case FORMAT_TYPE_WIDTH
:
2562 set_field_width(&spec
, va_arg(args
, int));
2565 case FORMAT_TYPE_PRECISION
:
2566 set_precision(&spec
, va_arg(args
, int));
2569 case FORMAT_TYPE_CHAR
: {
2572 if (!(spec
.flags
& LEFT
)) {
2573 while (--spec
.field_width
> 0) {
2580 c
= (unsigned char) va_arg(args
, int);
2584 while (--spec
.field_width
> 0) {
2592 case FORMAT_TYPE_STR
:
2593 str
= string(str
, end
, va_arg(args
, char *), spec
);
2596 case FORMAT_TYPE_PTR
:
2597 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2599 while (isalnum(*fmt
))
2603 case FORMAT_TYPE_PERCENT_CHAR
:
2609 case FORMAT_TYPE_INVALID
:
2611 * Presumably the arguments passed gcc's type
2612 * checking, but there is no safe or sane way
2613 * for us to continue parsing the format and
2614 * fetching from the va_list; the remaining
2615 * specifiers and arguments would be out of
2621 switch (spec
.type
) {
2622 case FORMAT_TYPE_LONG_LONG
:
2623 num
= va_arg(args
, long long);
2625 case FORMAT_TYPE_ULONG
:
2626 num
= va_arg(args
, unsigned long);
2628 case FORMAT_TYPE_LONG
:
2629 num
= va_arg(args
, long);
2631 case FORMAT_TYPE_SIZE_T
:
2632 if (spec
.flags
& SIGN
)
2633 num
= va_arg(args
, ssize_t
);
2635 num
= va_arg(args
, size_t);
2637 case FORMAT_TYPE_PTRDIFF
:
2638 num
= va_arg(args
, ptrdiff_t);
2640 case FORMAT_TYPE_UBYTE
:
2641 num
= (unsigned char) va_arg(args
, int);
2643 case FORMAT_TYPE_BYTE
:
2644 num
= (signed char) va_arg(args
, int);
2646 case FORMAT_TYPE_USHORT
:
2647 num
= (unsigned short) va_arg(args
, int);
2649 case FORMAT_TYPE_SHORT
:
2650 num
= (short) va_arg(args
, int);
2652 case FORMAT_TYPE_INT
:
2653 num
= (int) va_arg(args
, int);
2656 num
= va_arg(args
, unsigned int);
2659 str
= number(str
, end
, num
, spec
);
2671 /* the trailing null byte doesn't count towards the total */
2675 EXPORT_SYMBOL(vsnprintf
);
2678 * vscnprintf - Format a string and place it in a buffer
2679 * @buf: The buffer to place the result into
2680 * @size: The size of the buffer, including the trailing null space
2681 * @fmt: The format string to use
2682 * @args: Arguments for the format string
2684 * The return value is the number of characters which have been written into
2685 * the @buf not including the trailing '\0'. If @size is == 0 the function
2688 * If you're not already dealing with a va_list consider using scnprintf().
2690 * See the vsnprintf() documentation for format string extensions over C99.
2692 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2696 i
= vsnprintf(buf
, size
, fmt
, args
);
2698 if (likely(i
< size
))
2704 EXPORT_SYMBOL(vscnprintf
);
2707 * snprintf - Format a string and place it in a buffer
2708 * @buf: The buffer to place the result into
2709 * @size: The size of the buffer, including the trailing null space
2710 * @fmt: The format string to use
2711 * @...: Arguments for the format string
2713 * The return value is the number of characters which would be
2714 * generated for the given input, excluding the trailing null,
2715 * as per ISO C99. If the return is greater than or equal to
2716 * @size, the resulting string is truncated.
2718 * See the vsnprintf() documentation for format string extensions over C99.
2720 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
2725 va_start(args
, fmt
);
2726 i
= vsnprintf(buf
, size
, fmt
, args
);
2731 EXPORT_SYMBOL(snprintf
);
2734 * scnprintf - Format a string and place it in a buffer
2735 * @buf: The buffer to place the result into
2736 * @size: The size of the buffer, including the trailing null space
2737 * @fmt: The format string to use
2738 * @...: Arguments for the format string
2740 * The return value is the number of characters written into @buf not including
2741 * the trailing '\0'. If @size is == 0 the function returns 0.
2744 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
2749 va_start(args
, fmt
);
2750 i
= vscnprintf(buf
, size
, fmt
, args
);
2755 EXPORT_SYMBOL(scnprintf
);
2758 * vsprintf - Format a string and place it in a buffer
2759 * @buf: The buffer to place the result into
2760 * @fmt: The format string to use
2761 * @args: Arguments for the format string
2763 * The function returns the number of characters written
2764 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2767 * If you're not already dealing with a va_list consider using sprintf().
2769 * See the vsnprintf() documentation for format string extensions over C99.
2771 int vsprintf(char *buf
, const char *fmt
, va_list args
)
2773 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
2775 EXPORT_SYMBOL(vsprintf
);
2778 * sprintf - Format a string and place it in a buffer
2779 * @buf: The buffer to place the result into
2780 * @fmt: The format string to use
2781 * @...: Arguments for the format string
2783 * The function returns the number of characters written
2784 * into @buf. Use snprintf() or scnprintf() in order to avoid
2787 * See the vsnprintf() documentation for format string extensions over C99.
2789 int sprintf(char *buf
, const char *fmt
, ...)
2794 va_start(args
, fmt
);
2795 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
2800 EXPORT_SYMBOL(sprintf
);
2802 #ifdef CONFIG_BINARY_PRINTF
2805 * vbin_printf() - VA arguments to binary data
2806 * bstr_printf() - Binary data to text string
2810 * vbin_printf - Parse a format string and place args' binary value in a buffer
2811 * @bin_buf: The buffer to place args' binary value
2812 * @size: The size of the buffer(by words(32bits), not characters)
2813 * @fmt: The format string to use
2814 * @args: Arguments for the format string
2816 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2819 * The return value is the number of words(32bits) which would be generated for
2823 * If the return value is greater than @size, the resulting bin_buf is NOT
2824 * valid for bstr_printf().
2826 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
2828 struct printf_spec spec
= {0};
2832 str
= (char *)bin_buf
;
2833 end
= (char *)(bin_buf
+ size
);
2835 #define save_arg(type) \
2837 unsigned long long value; \
2838 if (sizeof(type) == 8) { \
2839 unsigned long long val8; \
2840 str = PTR_ALIGN(str, sizeof(u32)); \
2841 val8 = va_arg(args, unsigned long long); \
2842 if (str + sizeof(type) <= end) { \
2843 *(u32 *)str = *(u32 *)&val8; \
2844 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2848 unsigned int val4; \
2849 str = PTR_ALIGN(str, sizeof(type)); \
2850 val4 = va_arg(args, int); \
2851 if (str + sizeof(type) <= end) \
2852 *(typeof(type) *)str = (type)(long)val4; \
2853 value = (unsigned long long)val4; \
2855 str += sizeof(type); \
2860 int read
= format_decode(fmt
, &spec
);
2864 switch (spec
.type
) {
2865 case FORMAT_TYPE_NONE
:
2866 case FORMAT_TYPE_PERCENT_CHAR
:
2868 case FORMAT_TYPE_INVALID
:
2871 case FORMAT_TYPE_WIDTH
:
2872 case FORMAT_TYPE_PRECISION
:
2873 width
= (int)save_arg(int);
2874 /* Pointers may require the width */
2876 set_field_width(&spec
, width
);
2879 case FORMAT_TYPE_CHAR
:
2883 case FORMAT_TYPE_STR
: {
2884 const char *save_str
= va_arg(args
, char *);
2885 const char *err_msg
;
2888 err_msg
= check_pointer_msg(save_str
);
2892 len
= strlen(save_str
) + 1;
2893 if (str
+ len
< end
)
2894 memcpy(str
, save_str
, len
);
2899 case FORMAT_TYPE_PTR
:
2900 /* Dereferenced pointers must be done now */
2902 /* Dereference of functions is still OK */
2911 if (!isalnum(*fmt
)) {
2915 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2920 end
[-1] = '\0'; /* Must be nul terminated */
2922 /* skip all alphanumeric pointer suffixes */
2923 while (isalnum(*fmt
))
2928 switch (spec
.type
) {
2930 case FORMAT_TYPE_LONG_LONG
:
2931 save_arg(long long);
2933 case FORMAT_TYPE_ULONG
:
2934 case FORMAT_TYPE_LONG
:
2935 save_arg(unsigned long);
2937 case FORMAT_TYPE_SIZE_T
:
2940 case FORMAT_TYPE_PTRDIFF
:
2941 save_arg(ptrdiff_t);
2943 case FORMAT_TYPE_UBYTE
:
2944 case FORMAT_TYPE_BYTE
:
2947 case FORMAT_TYPE_USHORT
:
2948 case FORMAT_TYPE_SHORT
:
2958 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2961 EXPORT_SYMBOL_GPL(vbin_printf
);
2964 * bstr_printf - Format a string from binary arguments and place it in a buffer
2965 * @buf: The buffer to place the result into
2966 * @size: The size of the buffer, including the trailing null space
2967 * @fmt: The format string to use
2968 * @bin_buf: Binary arguments for the format string
2970 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2971 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2972 * a binary buffer that generated by vbin_printf.
2974 * The format follows C99 vsnprintf, but has some extensions:
2975 * see vsnprintf comment for details.
2977 * The return value is the number of characters which would
2978 * be generated for the given input, excluding the trailing
2979 * '\0', as per ISO C99. If you want to have the exact
2980 * number of characters written into @buf as return value
2981 * (not including the trailing '\0'), use vscnprintf(). If the
2982 * return is greater than or equal to @size, the resulting
2983 * string is truncated.
2985 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2987 struct printf_spec spec
= {0};
2989 const char *args
= (const char *)bin_buf
;
2991 if (WARN_ON_ONCE(size
> INT_MAX
))
2997 #define get_arg(type) \
2999 typeof(type) value; \
3000 if (sizeof(type) == 8) { \
3001 args = PTR_ALIGN(args, sizeof(u32)); \
3002 *(u32 *)&value = *(u32 *)args; \
3003 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
3005 args = PTR_ALIGN(args, sizeof(type)); \
3006 value = *(typeof(type) *)args; \
3008 args += sizeof(type); \
3012 /* Make sure end is always >= buf */
3019 const char *old_fmt
= fmt
;
3020 int read
= format_decode(fmt
, &spec
);
3024 switch (spec
.type
) {
3025 case FORMAT_TYPE_NONE
: {
3028 if (copy
> end
- str
)
3030 memcpy(str
, old_fmt
, copy
);
3036 case FORMAT_TYPE_WIDTH
:
3037 set_field_width(&spec
, get_arg(int));
3040 case FORMAT_TYPE_PRECISION
:
3041 set_precision(&spec
, get_arg(int));
3044 case FORMAT_TYPE_CHAR
: {
3047 if (!(spec
.flags
& LEFT
)) {
3048 while (--spec
.field_width
> 0) {
3054 c
= (unsigned char) get_arg(char);
3058 while (--spec
.field_width
> 0) {
3066 case FORMAT_TYPE_STR
: {
3067 const char *str_arg
= args
;
3068 args
+= strlen(str_arg
) + 1;
3069 str
= string(str
, end
, (char *)str_arg
, spec
);
3073 case FORMAT_TYPE_PTR
: {
3074 bool process
= false;
3076 /* Non function dereferences were already done */
3088 if (!isalnum(*fmt
)) {
3092 /* Pointer dereference was already processed */
3094 len
= copy
= strlen(args
);
3095 if (copy
> end
- str
)
3097 memcpy(str
, args
, copy
);
3103 str
= pointer(fmt
, str
, end
, get_arg(void *), spec
);
3105 while (isalnum(*fmt
))
3110 case FORMAT_TYPE_PERCENT_CHAR
:
3116 case FORMAT_TYPE_INVALID
:
3120 unsigned long long num
;
3122 switch (spec
.type
) {
3124 case FORMAT_TYPE_LONG_LONG
:
3125 num
= get_arg(long long);
3127 case FORMAT_TYPE_ULONG
:
3128 case FORMAT_TYPE_LONG
:
3129 num
= get_arg(unsigned long);
3131 case FORMAT_TYPE_SIZE_T
:
3132 num
= get_arg(size_t);
3134 case FORMAT_TYPE_PTRDIFF
:
3135 num
= get_arg(ptrdiff_t);
3137 case FORMAT_TYPE_UBYTE
:
3138 num
= get_arg(unsigned char);
3140 case FORMAT_TYPE_BYTE
:
3141 num
= get_arg(signed char);
3143 case FORMAT_TYPE_USHORT
:
3144 num
= get_arg(unsigned short);
3146 case FORMAT_TYPE_SHORT
:
3147 num
= get_arg(short);
3149 case FORMAT_TYPE_UINT
:
3150 num
= get_arg(unsigned int);
3156 str
= number(str
, end
, num
, spec
);
3158 } /* switch(spec.type) */
3171 /* the trailing null byte doesn't count towards the total */
3174 EXPORT_SYMBOL_GPL(bstr_printf
);
3177 * bprintf - Parse a format string and place args' binary value in a buffer
3178 * @bin_buf: The buffer to place args' binary value
3179 * @size: The size of the buffer(by words(32bits), not characters)
3180 * @fmt: The format string to use
3181 * @...: Arguments for the format string
3183 * The function returns the number of words(u32) written
3186 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
3191 va_start(args
, fmt
);
3192 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
3197 EXPORT_SYMBOL_GPL(bprintf
);
3199 #endif /* CONFIG_BINARY_PRINTF */
3202 * vsscanf - Unformat a buffer into a list of arguments
3203 * @buf: input buffer
3204 * @fmt: format of buffer
3207 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
3209 const char *str
= buf
;
3217 unsigned long long u
;
3223 /* skip any white space in format */
3224 /* white space in format matchs any amount of
3225 * white space, including none, in the input.
3227 if (isspace(*fmt
)) {
3228 fmt
= skip_spaces(++fmt
);
3229 str
= skip_spaces(str
);
3232 /* anything that is not a conversion must match exactly */
3233 if (*fmt
!= '%' && *fmt
) {
3234 if (*fmt
++ != *str
++)
3243 /* skip this conversion.
3244 * advance both strings to next white space
3249 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
) {
3250 /* '%*[' not yet supported, invalid format */
3255 while (!isspace(*str
) && *str
)
3260 /* get field width */
3262 if (isdigit(*fmt
)) {
3263 field_width
= skip_atoi(&fmt
);
3264 if (field_width
<= 0)
3268 /* get conversion qualifier */
3270 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
3273 if (unlikely(qualifier
== *fmt
)) {
3274 if (qualifier
== 'h') {
3277 } else if (qualifier
== 'l') {
3288 /* return number of characters read so far */
3289 *va_arg(args
, int *) = str
- buf
;
3303 char *s
= (char *)va_arg(args
, char*);
3304 if (field_width
== -1)
3308 } while (--field_width
> 0 && *str
);
3314 char *s
= (char *)va_arg(args
, char *);
3315 if (field_width
== -1)
3316 field_width
= SHRT_MAX
;
3317 /* first, skip leading white space in buffer */
3318 str
= skip_spaces(str
);
3320 /* now copy until next white space */
3321 while (*str
&& !isspace(*str
) && field_width
--)
3328 * Warning: This implementation of the '[' conversion specifier
3329 * deviates from its glibc counterpart in the following ways:
3330 * (1) It does NOT support ranges i.e. '-' is NOT a special
3332 * (2) It cannot match the closing bracket ']' itself
3333 * (3) A field width is required
3334 * (4) '%*[' (discard matching input) is currently not supported
3337 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3338 * buf1, buf2, buf3);
3344 char *s
= (char *)va_arg(args
, char *);
3345 DECLARE_BITMAP(set
, 256) = {0};
3346 unsigned int len
= 0;
3347 bool negate
= (*fmt
== '^');
3349 /* field width is required */
3350 if (field_width
== -1)
3356 for ( ; *fmt
&& *fmt
!= ']'; ++fmt
, ++len
)
3357 set_bit((u8
)*fmt
, set
);
3359 /* no ']' or no character set found */
3365 bitmap_complement(set
, set
, 256);
3366 /* exclude null '\0' byte */
3370 /* match must be non-empty */
3371 if (!test_bit((u8
)*str
, set
))
3374 while (test_bit((u8
)*str
, set
) && field_width
--)
3396 /* looking for '%' in str */
3401 /* invalid format; stop here */
3405 /* have some sort of integer conversion.
3406 * first, skip white space in buffer.
3408 str
= skip_spaces(str
);
3411 if (is_sign
&& digit
== '-')
3415 || (base
== 16 && !isxdigit(digit
))
3416 || (base
== 10 && !isdigit(digit
))
3417 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
3418 || (base
== 0 && !isdigit(digit
)))
3422 val
.s
= qualifier
!= 'L' ?
3423 simple_strtol(str
, &next
, base
) :
3424 simple_strtoll(str
, &next
, base
);
3426 val
.u
= qualifier
!= 'L' ?
3427 simple_strtoul(str
, &next
, base
) :
3428 simple_strtoull(str
, &next
, base
);
3430 if (field_width
> 0 && next
- str
> field_width
) {
3432 _parse_integer_fixup_radix(str
, &base
);
3433 while (next
- str
> field_width
) {
3435 val
.s
= div_s64(val
.s
, base
);
3437 val
.u
= div_u64(val
.u
, base
);
3442 switch (qualifier
) {
3443 case 'H': /* that's 'hh' in format */
3445 *va_arg(args
, signed char *) = val
.s
;
3447 *va_arg(args
, unsigned char *) = val
.u
;
3451 *va_arg(args
, short *) = val
.s
;
3453 *va_arg(args
, unsigned short *) = val
.u
;
3457 *va_arg(args
, long *) = val
.s
;
3459 *va_arg(args
, unsigned long *) = val
.u
;
3463 *va_arg(args
, long long *) = val
.s
;
3465 *va_arg(args
, unsigned long long *) = val
.u
;
3468 *va_arg(args
, size_t *) = val
.u
;
3472 *va_arg(args
, int *) = val
.s
;
3474 *va_arg(args
, unsigned int *) = val
.u
;
3486 EXPORT_SYMBOL(vsscanf
);
3489 * sscanf - Unformat a buffer into a list of arguments
3490 * @buf: input buffer
3491 * @fmt: formatting of buffer
3492 * @...: resulting arguments
3494 int sscanf(const char *buf
, const char *fmt
, ...)
3499 va_start(args
, fmt
);
3500 i
= vsscanf(buf
, fmt
, args
);
3505 EXPORT_SYMBOL(sscanf
);