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 * Do not call any complex external code here. Nested printk()/vsprintf()
632 * might cause infinite loops. Failures might break printk() and would
635 static const char *check_pointer_msg(const void *ptr
)
640 if ((unsigned long)ptr
< PAGE_SIZE
|| IS_ERR_VALUE(ptr
))
646 static int check_pointer(char **buf
, char *end
, const void *ptr
,
647 struct printf_spec spec
)
651 err_msg
= check_pointer_msg(ptr
);
653 *buf
= error_string(*buf
, end
, err_msg
, spec
);
660 static noinline_for_stack
661 char *string(char *buf
, char *end
, const char *s
,
662 struct printf_spec spec
)
664 if (check_pointer(&buf
, end
, s
, spec
))
667 return string_nocheck(buf
, end
, s
, spec
);
670 static char *pointer_string(char *buf
, char *end
,
672 struct printf_spec spec
)
676 if (spec
.field_width
== -1) {
677 spec
.field_width
= 2 * sizeof(ptr
);
678 spec
.flags
|= ZEROPAD
;
681 return number(buf
, end
, (unsigned long int)ptr
, spec
);
684 /* Make pointers available for printing early in the boot sequence. */
685 static int debug_boot_weak_hash __ro_after_init
;
687 static int __init
debug_boot_weak_hash_enable(char *str
)
689 debug_boot_weak_hash
= 1;
690 pr_info("debug_boot_weak_hash enabled\n");
693 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable
);
695 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key
);
696 static siphash_key_t ptr_key __read_mostly
;
698 static void enable_ptr_key_workfn(struct work_struct
*work
)
700 get_random_bytes(&ptr_key
, sizeof(ptr_key
));
701 /* Needs to run from preemptible context */
702 static_branch_disable(¬_filled_random_ptr_key
);
705 static DECLARE_WORK(enable_ptr_key_work
, enable_ptr_key_workfn
);
707 static void fill_random_ptr_key(struct random_ready_callback
*unused
)
709 /* This may be in an interrupt handler. */
710 queue_work(system_unbound_wq
, &enable_ptr_key_work
);
713 static struct random_ready_callback random_ready
= {
714 .func
= fill_random_ptr_key
717 static int __init
initialize_ptr_random(void)
719 int key_size
= sizeof(ptr_key
);
722 /* Use hw RNG if available. */
723 if (get_random_bytes_arch(&ptr_key
, key_size
) == key_size
) {
724 static_branch_disable(¬_filled_random_ptr_key
);
728 ret
= add_random_ready_callback(&random_ready
);
731 } else if (ret
== -EALREADY
) {
732 /* This is in preemptible context */
733 enable_ptr_key_workfn(&enable_ptr_key_work
);
739 early_initcall(initialize_ptr_random
);
741 /* Maps a pointer to a 32 bit unique identifier. */
742 static char *ptr_to_id(char *buf
, char *end
, const void *ptr
,
743 struct printf_spec spec
)
745 const char *str
= sizeof(ptr
) == 8 ? "(____ptrval____)" : "(ptrval)";
746 unsigned long hashval
;
748 /* When debugging early boot use non-cryptographically secure hash. */
749 if (unlikely(debug_boot_weak_hash
)) {
750 hashval
= hash_long((unsigned long)ptr
, 32);
751 return pointer_string(buf
, end
, (const void *)hashval
, spec
);
754 if (static_branch_unlikely(¬_filled_random_ptr_key
)) {
755 spec
.field_width
= 2 * sizeof(ptr
);
756 /* string length must be less than default_width */
757 return error_string(buf
, end
, str
, spec
);
761 hashval
= (unsigned long)siphash_1u64((u64
)ptr
, &ptr_key
);
763 * Mask off the first 32 bits, this makes explicit that we have
764 * modified the address (and 32 bits is plenty for a unique ID).
766 hashval
= hashval
& 0xffffffff;
768 hashval
= (unsigned long)siphash_1u32((u32
)ptr
, &ptr_key
);
770 return pointer_string(buf
, end
, (const void *)hashval
, spec
);
773 int kptr_restrict __read_mostly
;
775 static noinline_for_stack
776 char *restricted_pointer(char *buf
, char *end
, const void *ptr
,
777 struct printf_spec spec
)
779 switch (kptr_restrict
) {
781 /* Handle as %p, hash and do _not_ leak addresses. */
782 return ptr_to_id(buf
, end
, ptr
, spec
);
784 const struct cred
*cred
;
787 * kptr_restrict==1 cannot be used in IRQ context
788 * because its test for CAP_SYSLOG would be meaningless.
790 if (in_irq() || in_serving_softirq() || in_nmi()) {
791 if (spec
.field_width
== -1)
792 spec
.field_width
= 2 * sizeof(ptr
);
793 return error_string(buf
, end
, "pK-error", spec
);
797 * Only print the real pointer value if the current
798 * process has CAP_SYSLOG and is running with the
799 * same credentials it started with. This is because
800 * access to files is checked at open() time, but %pK
801 * checks permission at read() time. We don't want to
802 * leak pointer values if a binary opens a file using
803 * %pK and then elevates privileges before reading it.
805 cred
= current_cred();
806 if (!has_capability_noaudit(current
, CAP_SYSLOG
) ||
807 !uid_eq(cred
->euid
, cred
->uid
) ||
808 !gid_eq(cred
->egid
, cred
->gid
))
814 /* Always print 0's for %pK */
819 return pointer_string(buf
, end
, ptr
, spec
);
822 static noinline_for_stack
823 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
826 const char *array
[4], *s
;
827 const struct dentry
*p
;
832 case '2': case '3': case '4':
833 depth
= fmt
[1] - '0';
840 for (i
= 0; i
< depth
; i
++, d
= p
) {
841 if (check_pointer(&buf
, end
, d
, spec
)) {
846 p
= READ_ONCE(d
->d_parent
);
847 array
[i
] = READ_ONCE(d
->d_name
.name
);
856 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
868 return widen_string(buf
, n
, end
, spec
);
872 static noinline_for_stack
873 char *bdev_name(char *buf
, char *end
, struct block_device
*bdev
,
874 struct printf_spec spec
, const char *fmt
)
878 if (check_pointer(&buf
, end
, bdev
, spec
))
882 buf
= string(buf
, end
, hd
->disk_name
, spec
);
883 if (bdev
->bd_part
->partno
) {
884 if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1])) {
889 buf
= number(buf
, end
, bdev
->bd_part
->partno
, spec
);
895 static noinline_for_stack
896 char *symbol_string(char *buf
, char *end
, void *ptr
,
897 struct printf_spec spec
, const char *fmt
)
900 #ifdef CONFIG_KALLSYMS
901 char sym
[KSYM_SYMBOL_LEN
];
905 ptr
= __builtin_extract_return_addr(ptr
);
906 value
= (unsigned long)ptr
;
908 #ifdef CONFIG_KALLSYMS
910 sprint_backtrace(sym
, value
);
911 else if (*fmt
!= 'f' && *fmt
!= 's')
912 sprint_symbol(sym
, value
);
914 sprint_symbol_no_offset(sym
, value
);
916 return string_nocheck(buf
, end
, sym
, spec
);
918 return special_hex_number(buf
, end
, value
, sizeof(void *));
922 static const struct printf_spec default_str_spec
= {
927 static const struct printf_spec default_flag_spec
= {
930 .flags
= SPECIAL
| SMALL
,
933 static const struct printf_spec default_dec_spec
= {
938 static const struct printf_spec default_dec02_spec
= {
945 static const struct printf_spec default_dec04_spec
= {
952 static noinline_for_stack
953 char *resource_string(char *buf
, char *end
, struct resource
*res
,
954 struct printf_spec spec
, const char *fmt
)
956 #ifndef IO_RSRC_PRINTK_SIZE
957 #define IO_RSRC_PRINTK_SIZE 6
960 #ifndef MEM_RSRC_PRINTK_SIZE
961 #define MEM_RSRC_PRINTK_SIZE 10
963 static const struct printf_spec io_spec
= {
965 .field_width
= IO_RSRC_PRINTK_SIZE
,
967 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
969 static const struct printf_spec mem_spec
= {
971 .field_width
= MEM_RSRC_PRINTK_SIZE
,
973 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
975 static const struct printf_spec bus_spec
= {
979 .flags
= SMALL
| ZEROPAD
,
981 static const struct printf_spec str_spec
= {
987 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
988 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
989 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
990 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
991 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
992 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
993 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
994 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
996 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
997 int decode
= (fmt
[0] == 'R') ? 1 : 0;
998 const struct printf_spec
*specp
;
1000 if (check_pointer(&buf
, end
, res
, spec
))
1004 if (res
->flags
& IORESOURCE_IO
) {
1005 p
= string_nocheck(p
, pend
, "io ", str_spec
);
1007 } else if (res
->flags
& IORESOURCE_MEM
) {
1008 p
= string_nocheck(p
, pend
, "mem ", str_spec
);
1010 } else if (res
->flags
& IORESOURCE_IRQ
) {
1011 p
= string_nocheck(p
, pend
, "irq ", str_spec
);
1012 specp
= &default_dec_spec
;
1013 } else if (res
->flags
& IORESOURCE_DMA
) {
1014 p
= string_nocheck(p
, pend
, "dma ", str_spec
);
1015 specp
= &default_dec_spec
;
1016 } else if (res
->flags
& IORESOURCE_BUS
) {
1017 p
= string_nocheck(p
, pend
, "bus ", str_spec
);
1020 p
= string_nocheck(p
, pend
, "??? ", str_spec
);
1024 if (decode
&& res
->flags
& IORESOURCE_UNSET
) {
1025 p
= string_nocheck(p
, pend
, "size ", str_spec
);
1026 p
= number(p
, pend
, resource_size(res
), *specp
);
1028 p
= number(p
, pend
, res
->start
, *specp
);
1029 if (res
->start
!= res
->end
) {
1031 p
= number(p
, pend
, res
->end
, *specp
);
1035 if (res
->flags
& IORESOURCE_MEM_64
)
1036 p
= string_nocheck(p
, pend
, " 64bit", str_spec
);
1037 if (res
->flags
& IORESOURCE_PREFETCH
)
1038 p
= string_nocheck(p
, pend
, " pref", str_spec
);
1039 if (res
->flags
& IORESOURCE_WINDOW
)
1040 p
= string_nocheck(p
, pend
, " window", str_spec
);
1041 if (res
->flags
& IORESOURCE_DISABLED
)
1042 p
= string_nocheck(p
, pend
, " disabled", str_spec
);
1044 p
= string_nocheck(p
, pend
, " flags ", str_spec
);
1045 p
= number(p
, pend
, res
->flags
, default_flag_spec
);
1050 return string_nocheck(buf
, end
, sym
, spec
);
1053 static noinline_for_stack
1054 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1057 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
1058 negative value, fallback to the default */
1061 if (spec
.field_width
== 0)
1062 /* nothing to print */
1065 if (check_pointer(&buf
, end
, addr
, spec
))
1083 if (spec
.field_width
> 0)
1084 len
= min_t(int, spec
.field_width
, 64);
1086 for (i
= 0; i
< len
; ++i
) {
1088 *buf
= hex_asc_hi(addr
[i
]);
1091 *buf
= hex_asc_lo(addr
[i
]);
1094 if (separator
&& i
!= len
- 1) {
1104 static noinline_for_stack
1105 char *bitmap_string(char *buf
, char *end
, unsigned long *bitmap
,
1106 struct printf_spec spec
, const char *fmt
)
1108 const int CHUNKSZ
= 32;
1109 int nr_bits
= max_t(int, spec
.field_width
, 0);
1113 if (check_pointer(&buf
, end
, bitmap
, spec
))
1116 /* reused to print numbers */
1117 spec
= (struct printf_spec
){ .flags
= SMALL
| ZEROPAD
, .base
= 16 };
1119 chunksz
= nr_bits
& (CHUNKSZ
- 1);
1123 i
= ALIGN(nr_bits
, CHUNKSZ
) - CHUNKSZ
;
1124 for (; i
>= 0; i
-= CHUNKSZ
) {
1128 chunkmask
= ((1ULL << chunksz
) - 1);
1129 word
= i
/ BITS_PER_LONG
;
1130 bit
= i
% BITS_PER_LONG
;
1131 val
= (bitmap
[word
] >> bit
) & chunkmask
;
1140 spec
.field_width
= DIV_ROUND_UP(chunksz
, 4);
1141 buf
= number(buf
, end
, val
, spec
);
1148 static noinline_for_stack
1149 char *bitmap_list_string(char *buf
, char *end
, unsigned long *bitmap
,
1150 struct printf_spec spec
, const char *fmt
)
1152 int nr_bits
= max_t(int, spec
.field_width
, 0);
1153 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1154 int cur
, rbot
, rtop
;
1157 if (check_pointer(&buf
, end
, bitmap
, spec
))
1160 rbot
= cur
= find_first_bit(bitmap
, nr_bits
);
1161 while (cur
< nr_bits
) {
1163 cur
= find_next_bit(bitmap
, nr_bits
, cur
+ 1);
1164 if (cur
< nr_bits
&& cur
<= rtop
+ 1)
1174 buf
= number(buf
, end
, rbot
, default_dec_spec
);
1180 buf
= number(buf
, end
, rtop
, default_dec_spec
);
1188 static noinline_for_stack
1189 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
1190 struct printf_spec spec
, const char *fmt
)
1192 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
1196 bool reversed
= false;
1198 if (check_pointer(&buf
, end
, addr
, spec
))
1215 for (i
= 0; i
< 6; i
++) {
1217 p
= hex_byte_pack(p
, addr
[5 - i
]);
1219 p
= hex_byte_pack(p
, addr
[i
]);
1221 if (fmt
[0] == 'M' && i
!= 5)
1226 return string_nocheck(buf
, end
, mac_addr
, spec
);
1229 static noinline_for_stack
1230 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
1233 bool leading_zeros
= (fmt
[0] == 'i');
1258 for (i
= 0; i
< 4; i
++) {
1259 char temp
[4] __aligned(2); /* hold each IP quad in reverse order */
1260 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
1261 if (leading_zeros
) {
1267 /* reverse the digits in the quad */
1269 *p
++ = temp
[digits
];
1279 static noinline_for_stack
1280 char *ip6_compressed_string(char *p
, const char *addr
)
1283 unsigned char zerolength
[8];
1288 bool needcolon
= false;
1290 struct in6_addr in6
;
1292 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
1294 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
1296 memset(zerolength
, 0, sizeof(zerolength
));
1303 /* find position of longest 0 run */
1304 for (i
= 0; i
< range
; i
++) {
1305 for (j
= i
; j
< range
; j
++) {
1306 if (in6
.s6_addr16
[j
] != 0)
1311 for (i
= 0; i
< range
; i
++) {
1312 if (zerolength
[i
] > longest
) {
1313 longest
= zerolength
[i
];
1317 if (longest
== 1) /* don't compress a single 0 */
1321 for (i
= 0; i
< range
; i
++) {
1322 if (i
== colonpos
) {
1323 if (needcolon
|| i
== 0)
1334 /* hex u16 without leading 0s */
1335 word
= ntohs(in6
.s6_addr16
[i
]);
1340 p
= hex_byte_pack(p
, hi
);
1342 *p
++ = hex_asc_lo(hi
);
1343 p
= hex_byte_pack(p
, lo
);
1346 p
= hex_byte_pack(p
, lo
);
1348 *p
++ = hex_asc_lo(lo
);
1355 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
1362 static noinline_for_stack
1363 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
1367 for (i
= 0; i
< 8; i
++) {
1368 p
= hex_byte_pack(p
, *addr
++);
1369 p
= hex_byte_pack(p
, *addr
++);
1370 if (fmt
[0] == 'I' && i
!= 7)
1378 static noinline_for_stack
1379 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
1380 struct printf_spec spec
, const char *fmt
)
1382 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1384 if (fmt
[0] == 'I' && fmt
[2] == 'c')
1385 ip6_compressed_string(ip6_addr
, addr
);
1387 ip6_string(ip6_addr
, addr
, fmt
);
1389 return string_nocheck(buf
, end
, ip6_addr
, spec
);
1392 static noinline_for_stack
1393 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
1394 struct printf_spec spec
, const char *fmt
)
1396 char ip4_addr
[sizeof("255.255.255.255")];
1398 ip4_string(ip4_addr
, addr
, fmt
);
1400 return string_nocheck(buf
, end
, ip4_addr
, spec
);
1403 static noinline_for_stack
1404 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1405 struct printf_spec spec
, const char *fmt
)
1407 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1408 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1409 sizeof(":12345") + sizeof("/123456789") +
1410 sizeof("%1234567890")];
1411 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1412 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1413 char fmt6
[2] = { fmt
[0], '6' };
1417 while (isalpha(*++fmt
)) {
1434 if (have_p
|| have_s
|| have_f
) {
1439 if (fmt6
[0] == 'I' && have_c
)
1440 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1442 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1444 if (have_p
|| have_s
|| have_f
)
1449 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1453 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1454 IPV6_FLOWINFO_MASK
), spec
);
1458 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1462 return string_nocheck(buf
, end
, ip6_addr
, spec
);
1465 static noinline_for_stack
1466 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1467 struct printf_spec spec
, const char *fmt
)
1469 bool have_p
= false;
1470 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1471 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1472 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1473 char fmt4
[3] = { fmt
[0], '4', 0 };
1476 while (isalpha(*++fmt
)) {
1490 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1493 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1497 return string_nocheck(buf
, end
, ip4_addr
, spec
);
1500 static noinline_for_stack
1501 char *ip_addr_string(char *buf
, char *end
, const void *ptr
,
1502 struct printf_spec spec
, const char *fmt
)
1506 if (check_pointer(&buf
, end
, ptr
, spec
))
1511 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1513 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1516 struct sockaddr raw
;
1517 struct sockaddr_in v4
;
1518 struct sockaddr_in6 v6
;
1521 switch (sa
->raw
.sa_family
) {
1523 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
1525 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
1527 return error_string(buf
, end
, "(einval)", spec
);
1531 err_fmt_msg
= fmt
[0] == 'i' ? "(%pi?)" : "(%pI?)";
1532 return error_string(buf
, end
, err_fmt_msg
, spec
);
1535 static noinline_for_stack
1536 char *escaped_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1541 unsigned int flags
= 0;
1544 if (spec
.field_width
== 0)
1545 return buf
; /* nothing to print */
1547 if (check_pointer(&buf
, end
, addr
, spec
))
1551 switch (fmt
[count
++]) {
1553 flags
|= ESCAPE_ANY
;
1556 flags
|= ESCAPE_SPECIAL
;
1559 flags
|= ESCAPE_HEX
;
1562 flags
|= ESCAPE_NULL
;
1565 flags
|= ESCAPE_OCTAL
;
1571 flags
|= ESCAPE_SPACE
;
1580 flags
= ESCAPE_ANY_NP
;
1582 len
= spec
.field_width
< 0 ? 1 : spec
.field_width
;
1585 * string_escape_mem() writes as many characters as it can to
1586 * the given buffer, and returns the total size of the output
1587 * had the buffer been big enough.
1589 buf
+= string_escape_mem(addr
, len
, buf
, buf
< end
? end
- buf
: 0, flags
, NULL
);
1594 static char *va_format(char *buf
, char *end
, struct va_format
*va_fmt
,
1595 struct printf_spec spec
, const char *fmt
)
1599 if (check_pointer(&buf
, end
, va_fmt
, spec
))
1602 va_copy(va
, *va_fmt
->va
);
1603 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0, va_fmt
->fmt
, va
);
1609 static noinline_for_stack
1610 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1611 struct printf_spec spec
, const char *fmt
)
1613 char uuid
[UUID_STRING_LEN
+ 1];
1616 const u8
*index
= uuid_index
;
1619 if (check_pointer(&buf
, end
, addr
, spec
))
1624 uc
= true; /* fall-through */
1633 for (i
= 0; i
< 16; i
++) {
1635 p
= hex_byte_pack_upper(p
, addr
[index
[i
]]);
1637 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1650 return string_nocheck(buf
, end
, uuid
, spec
);
1653 static noinline_for_stack
1654 char *netdev_bits(char *buf
, char *end
, const void *addr
,
1655 struct printf_spec spec
, const char *fmt
)
1657 unsigned long long num
;
1660 if (check_pointer(&buf
, end
, addr
, spec
))
1665 num
= *(const netdev_features_t
*)addr
;
1666 size
= sizeof(netdev_features_t
);
1669 return error_string(buf
, end
, "(%pN?)", spec
);
1672 return special_hex_number(buf
, end
, num
, size
);
1675 static noinline_for_stack
1676 char *address_val(char *buf
, char *end
, const void *addr
,
1677 struct printf_spec spec
, const char *fmt
)
1679 unsigned long long num
;
1682 if (check_pointer(&buf
, end
, addr
, spec
))
1687 num
= *(const dma_addr_t
*)addr
;
1688 size
= sizeof(dma_addr_t
);
1692 num
= *(const phys_addr_t
*)addr
;
1693 size
= sizeof(phys_addr_t
);
1697 return special_hex_number(buf
, end
, num
, size
);
1700 static noinline_for_stack
1701 char *date_str(char *buf
, char *end
, const struct rtc_time
*tm
, bool r
)
1703 int year
= tm
->tm_year
+ (r
? 0 : 1900);
1704 int mon
= tm
->tm_mon
+ (r
? 0 : 1);
1706 buf
= number(buf
, end
, year
, default_dec04_spec
);
1711 buf
= number(buf
, end
, mon
, default_dec02_spec
);
1716 return number(buf
, end
, tm
->tm_mday
, default_dec02_spec
);
1719 static noinline_for_stack
1720 char *time_str(char *buf
, char *end
, const struct rtc_time
*tm
, bool r
)
1722 buf
= number(buf
, end
, tm
->tm_hour
, default_dec02_spec
);
1727 buf
= number(buf
, end
, tm
->tm_min
, default_dec02_spec
);
1732 return number(buf
, end
, tm
->tm_sec
, default_dec02_spec
);
1735 static noinline_for_stack
1736 char *rtc_str(char *buf
, char *end
, const struct rtc_time
*tm
,
1737 struct printf_spec spec
, const char *fmt
)
1739 bool have_t
= true, have_d
= true;
1743 if (check_pointer(&buf
, end
, tm
, spec
))
1746 switch (fmt
[count
]) {
1757 raw
= fmt
[count
] == 'r';
1760 buf
= date_str(buf
, end
, tm
, raw
);
1761 if (have_d
&& have_t
) {
1762 /* Respect ISO 8601 */
1768 buf
= time_str(buf
, end
, tm
, raw
);
1773 static noinline_for_stack
1774 char *time_and_date(char *buf
, char *end
, void *ptr
, struct printf_spec spec
,
1779 return rtc_str(buf
, end
, (const struct rtc_time
*)ptr
, spec
, fmt
);
1781 return error_string(buf
, end
, "(%ptR?)", spec
);
1785 static noinline_for_stack
1786 char *clock(char *buf
, char *end
, struct clk
*clk
, struct printf_spec spec
,
1789 if (!IS_ENABLED(CONFIG_HAVE_CLK
))
1790 return error_string(buf
, end
, "(%pC?)", spec
);
1792 if (check_pointer(&buf
, end
, clk
, spec
))
1798 #ifdef CONFIG_COMMON_CLK
1799 return string(buf
, end
, __clk_get_name(clk
), spec
);
1801 return error_string(buf
, end
, "(%pC?)", spec
);
1807 char *format_flags(char *buf
, char *end
, unsigned long flags
,
1808 const struct trace_print_flags
*names
)
1812 for ( ; flags
&& names
->name
; names
++) {
1814 if ((flags
& mask
) != mask
)
1817 buf
= string(buf
, end
, names
->name
, default_str_spec
);
1828 buf
= number(buf
, end
, flags
, default_flag_spec
);
1833 static noinline_for_stack
1834 char *flags_string(char *buf
, char *end
, void *flags_ptr
,
1835 struct printf_spec spec
, const char *fmt
)
1837 unsigned long flags
;
1838 const struct trace_print_flags
*names
;
1840 if (check_pointer(&buf
, end
, flags_ptr
, spec
))
1845 flags
= *(unsigned long *)flags_ptr
;
1846 /* Remove zone id */
1847 flags
&= (1UL << NR_PAGEFLAGS
) - 1;
1848 names
= pageflag_names
;
1851 flags
= *(unsigned long *)flags_ptr
;
1852 names
= vmaflag_names
;
1855 flags
= *(gfp_t
*)flags_ptr
;
1856 names
= gfpflag_names
;
1859 return error_string(buf
, end
, "(%pG?)", spec
);
1862 return format_flags(buf
, end
, flags
, names
);
1865 static const char *device_node_name_for_depth(const struct device_node
*np
, int depth
)
1867 for ( ; np
&& depth
; depth
--)
1870 return kbasename(np
->full_name
);
1873 static noinline_for_stack
1874 char *device_node_gen_full_name(const struct device_node
*np
, char *buf
, char *end
)
1877 const struct device_node
*parent
= np
->parent
;
1879 /* special case for root node */
1881 return string_nocheck(buf
, end
, "/", default_str_spec
);
1883 for (depth
= 0; parent
->parent
; depth
++)
1884 parent
= parent
->parent
;
1886 for ( ; depth
>= 0; depth
--) {
1887 buf
= string_nocheck(buf
, end
, "/", default_str_spec
);
1888 buf
= string(buf
, end
, device_node_name_for_depth(np
, depth
),
1894 static noinline_for_stack
1895 char *device_node_string(char *buf
, char *end
, struct device_node
*dn
,
1896 struct printf_spec spec
, const char *fmt
)
1898 char tbuf
[sizeof("xxxx") + 1];
1901 char *buf_start
= buf
;
1902 struct property
*prop
;
1903 bool has_mult
, pass
;
1904 static const struct printf_spec num_spec
= {
1911 struct printf_spec str_spec
= spec
;
1912 str_spec
.field_width
= -1;
1914 if (!IS_ENABLED(CONFIG_OF
))
1915 return error_string(buf
, end
, "(%pOF?)", spec
);
1917 if (check_pointer(&buf
, end
, dn
, spec
))
1920 /* simple case without anything any more format specifiers */
1922 if (fmt
[0] == '\0' || strcspn(fmt
,"fnpPFcC") > 0)
1925 for (pass
= false; strspn(fmt
,"fnpPFcC"); fmt
++, pass
= true) {
1934 case 'f': /* full_name */
1935 buf
= device_node_gen_full_name(dn
, buf
, end
);
1937 case 'n': /* name */
1938 p
= kbasename(of_node_full_name(dn
));
1939 precision
= str_spec
.precision
;
1940 str_spec
.precision
= strchrnul(p
, '@') - p
;
1941 buf
= string(buf
, end
, p
, str_spec
);
1942 str_spec
.precision
= precision
;
1944 case 'p': /* phandle */
1945 buf
= number(buf
, end
, (unsigned int)dn
->phandle
, num_spec
);
1947 case 'P': /* path-spec */
1948 p
= kbasename(of_node_full_name(dn
));
1951 buf
= string(buf
, end
, p
, str_spec
);
1953 case 'F': /* flags */
1954 tbuf
[0] = of_node_check_flag(dn
, OF_DYNAMIC
) ? 'D' : '-';
1955 tbuf
[1] = of_node_check_flag(dn
, OF_DETACHED
) ? 'd' : '-';
1956 tbuf
[2] = of_node_check_flag(dn
, OF_POPULATED
) ? 'P' : '-';
1957 tbuf
[3] = of_node_check_flag(dn
, OF_POPULATED_BUS
) ? 'B' : '-';
1959 buf
= string_nocheck(buf
, end
, tbuf
, str_spec
);
1961 case 'c': /* major compatible string */
1962 ret
= of_property_read_string(dn
, "compatible", &p
);
1964 buf
= string(buf
, end
, p
, str_spec
);
1966 case 'C': /* full compatible string */
1968 of_property_for_each_string(dn
, "compatible", prop
, p
) {
1970 buf
= string_nocheck(buf
, end
, ",", str_spec
);
1971 buf
= string_nocheck(buf
, end
, "\"", str_spec
);
1972 buf
= string(buf
, end
, p
, str_spec
);
1973 buf
= string_nocheck(buf
, end
, "\"", str_spec
);
1983 return widen_string(buf
, buf
- buf_start
, end
, spec
);
1986 static char *kobject_string(char *buf
, char *end
, void *ptr
,
1987 struct printf_spec spec
, const char *fmt
)
1991 return device_node_string(buf
, end
, ptr
, spec
, fmt
+ 1);
1994 return error_string(buf
, end
, "(%pO?)", spec
);
1998 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1999 * by an extra set of alphanumeric characters that are extended format
2002 * Please update scripts/checkpatch.pl when adding/removing conversion
2003 * characters. (Search for "check for vsprintf extension").
2005 * Right now we handle:
2007 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2008 * - 's' For symbolic direct pointers (or function descriptors) without offset
2011 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
2012 * - 'B' For backtraced symbolic direct pointers with offset
2013 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2014 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2015 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2016 * width which must be explicitly specified either as part of the
2017 * format string '%32b[l]' or through '%*b[l]', [l] selects
2018 * range-list format instead of hex format
2019 * - 'M' For a 6-byte MAC address, it prints the address in the
2020 * usual colon-separated hex notation
2021 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2022 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2023 * with a dash-separated hex notation
2024 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
2025 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2026 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2027 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
2029 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2030 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2031 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2032 * IPv6 omits the colons (01020304...0f)
2033 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
2035 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2036 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2037 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2038 * - 'I[6S]c' for IPv6 addresses printed as specified by
2039 * http://tools.ietf.org/html/rfc5952
2040 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2041 * of the following flags (see string_escape_mem() for the
2044 * c - ESCAPE_SPECIAL
2050 * By default ESCAPE_ANY_NP is used.
2051 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2052 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2053 * Options for %pU are:
2054 * b big endian lower case hex (default)
2055 * B big endian UPPER case hex
2056 * l little endian lower case hex
2057 * L little endian UPPER case hex
2058 * big endian output byte order is:
2059 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2060 * little endian output byte order is:
2061 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
2062 * - 'V' For a struct va_format which contains a format string * and va_list *,
2063 * call vsnprintf(->format, *->va_list).
2064 * Implements a "recursive vsnprintf".
2065 * Do not use this feature without some mechanism to verify the
2066 * correctness of the format string and va_list arguments.
2067 * - 'K' For a kernel pointer that should be hidden from unprivileged users
2068 * - 'NF' For a netdev_features_t
2069 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2070 * a certain separator (' ' by default):
2074 * The maximum supported length is 64 bytes of the input. Consider
2075 * to use print_hex_dump() for the larger input.
2076 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2077 * (default assumed to be phys_addr_t, passed by reference)
2078 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2079 * - 'D[234]' Same as 'd' but for a struct file
2080 * - 'g' For block_device name (gendisk + partition number)
2081 * - 't[R][dt][r]' For time and date as represented:
2083 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2084 * (legacy clock framework) of the clock
2085 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2086 * (legacy clock framework) of the clock
2087 * - 'G' For flags to be printed as a collection of symbolic strings that would
2088 * construct the specific value. Supported flags given by option:
2089 * p page flags (see struct page) given as pointer to unsigned long
2090 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2091 * v vma flags (VM_*) given as pointer to unsigned long
2092 * - 'OF[fnpPcCF]' For a device tree object
2093 * Without any optional arguments prints the full_name
2094 * f device node full_name
2095 * n device node name
2096 * p device node phandle
2097 * P device node path spec (name + @unit)
2098 * F device node flags
2099 * c major compatible string
2100 * C full compatible string
2101 * - 'x' For printing the address. Equivalent to "%lx".
2103 * ** When making changes please also update:
2104 * Documentation/core-api/printk-formats.rst
2106 * Note: The default behaviour (unadorned %p) is to hash the address,
2107 * rendering it useful as a unique identifier.
2109 static noinline_for_stack
2110 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
2111 struct printf_spec spec
)
2118 ptr
= dereference_symbol_descriptor(ptr
);
2121 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
2124 return resource_string(buf
, end
, ptr
, spec
, fmt
);
2126 return hex_string(buf
, end
, ptr
, spec
, fmt
);
2130 return bitmap_list_string(buf
, end
, ptr
, spec
, fmt
);
2132 return bitmap_string(buf
, end
, ptr
, spec
, fmt
);
2134 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2135 case 'm': /* Contiguous: 000102030405 */
2137 /* [mM]R (Reverse order; Bluetooth) */
2138 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
2139 case 'I': /* Formatted IP supported
2141 * 6: 0001:0203:...:0708
2142 * 6c: 1::708 or 1::1.2.3.4
2144 case 'i': /* Contiguous:
2145 * 4: 001.002.003.004
2148 return ip_addr_string(buf
, end
, ptr
, spec
, fmt
);
2150 return escaped_string(buf
, end
, ptr
, spec
, fmt
);
2152 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
2154 return va_format(buf
, end
, ptr
, spec
, fmt
);
2156 return restricted_pointer(buf
, end
, ptr
, spec
);
2158 return netdev_bits(buf
, end
, ptr
, spec
, fmt
);
2160 return address_val(buf
, end
, ptr
, spec
, fmt
);
2162 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
2164 return time_and_date(buf
, end
, ptr
, spec
, fmt
);
2166 return clock(buf
, end
, ptr
, spec
, fmt
);
2168 return dentry_name(buf
, end
,
2169 ((const struct file
*)ptr
)->f_path
.dentry
,
2173 return bdev_name(buf
, end
, ptr
, spec
, fmt
);
2177 return flags_string(buf
, end
, ptr
, spec
, fmt
);
2179 return kobject_string(buf
, end
, ptr
, spec
, fmt
);
2181 return pointer_string(buf
, end
, ptr
, spec
);
2184 /* default is to _not_ leak addresses, hash before printing */
2185 return ptr_to_id(buf
, end
, ptr
, spec
);
2189 * Helper function to decode printf style format.
2190 * Each call decode a token from the format and return the
2191 * number of characters read (or likely the delta where it wants
2192 * to go on the next call).
2193 * The decoded token is returned through the parameters
2195 * 'h', 'l', or 'L' for integer fields
2196 * 'z' support added 23/7/1999 S.H.
2197 * 'z' changed to 'Z' --davidm 1/25/99
2198 * 'Z' changed to 'z' --adobriyan 2017-01-25
2199 * 't' added for ptrdiff_t
2201 * @fmt: the format string
2202 * @type of the token returned
2203 * @flags: various flags such as +, -, # tokens..
2204 * @field_width: overwritten width
2205 * @base: base of the number (octal, hex, ...)
2206 * @precision: precision of a number
2207 * @qualifier: qualifier of a number (long, size_t, ...)
2209 static noinline_for_stack
2210 int format_decode(const char *fmt
, struct printf_spec
*spec
)
2212 const char *start
= fmt
;
2215 /* we finished early by reading the field width */
2216 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
2217 if (spec
->field_width
< 0) {
2218 spec
->field_width
= -spec
->field_width
;
2219 spec
->flags
|= LEFT
;
2221 spec
->type
= FORMAT_TYPE_NONE
;
2225 /* we finished early by reading the precision */
2226 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
2227 if (spec
->precision
< 0)
2228 spec
->precision
= 0;
2230 spec
->type
= FORMAT_TYPE_NONE
;
2235 spec
->type
= FORMAT_TYPE_NONE
;
2237 for (; *fmt
; ++fmt
) {
2242 /* Return the current non-format string */
2243 if (fmt
!= start
|| !*fmt
)
2249 while (1) { /* this also skips first '%' */
2255 case '-': spec
->flags
|= LEFT
; break;
2256 case '+': spec
->flags
|= PLUS
; break;
2257 case ' ': spec
->flags
|= SPACE
; break;
2258 case '#': spec
->flags
|= SPECIAL
; break;
2259 case '0': spec
->flags
|= ZEROPAD
; break;
2260 default: found
= false;
2267 /* get field width */
2268 spec
->field_width
= -1;
2271 spec
->field_width
= skip_atoi(&fmt
);
2272 else if (*fmt
== '*') {
2273 /* it's the next argument */
2274 spec
->type
= FORMAT_TYPE_WIDTH
;
2275 return ++fmt
- start
;
2279 /* get the precision */
2280 spec
->precision
= -1;
2283 if (isdigit(*fmt
)) {
2284 spec
->precision
= skip_atoi(&fmt
);
2285 if (spec
->precision
< 0)
2286 spec
->precision
= 0;
2287 } else if (*fmt
== '*') {
2288 /* it's the next argument */
2289 spec
->type
= FORMAT_TYPE_PRECISION
;
2290 return ++fmt
- start
;
2295 /* get the conversion qualifier */
2297 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2298 *fmt
== 'z' || *fmt
== 't') {
2300 if (unlikely(qualifier
== *fmt
)) {
2301 if (qualifier
== 'l') {
2304 } else if (qualifier
== 'h') {
2315 spec
->type
= FORMAT_TYPE_CHAR
;
2316 return ++fmt
- start
;
2319 spec
->type
= FORMAT_TYPE_STR
;
2320 return ++fmt
- start
;
2323 spec
->type
= FORMAT_TYPE_PTR
;
2324 return ++fmt
- start
;
2327 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
2328 return ++fmt
- start
;
2330 /* integer number formats - set up the flags and "break" */
2336 spec
->flags
|= SMALL
;
2345 spec
->flags
|= SIGN
;
2351 * Since %n poses a greater security risk than
2352 * utility, treat it as any other invalid or
2353 * unsupported format specifier.
2358 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt
);
2359 spec
->type
= FORMAT_TYPE_INVALID
;
2363 if (qualifier
== 'L')
2364 spec
->type
= FORMAT_TYPE_LONG_LONG
;
2365 else if (qualifier
== 'l') {
2366 BUILD_BUG_ON(FORMAT_TYPE_ULONG
+ SIGN
!= FORMAT_TYPE_LONG
);
2367 spec
->type
= FORMAT_TYPE_ULONG
+ (spec
->flags
& SIGN
);
2368 } else if (qualifier
== 'z') {
2369 spec
->type
= FORMAT_TYPE_SIZE_T
;
2370 } else if (qualifier
== 't') {
2371 spec
->type
= FORMAT_TYPE_PTRDIFF
;
2372 } else if (qualifier
== 'H') {
2373 BUILD_BUG_ON(FORMAT_TYPE_UBYTE
+ SIGN
!= FORMAT_TYPE_BYTE
);
2374 spec
->type
= FORMAT_TYPE_UBYTE
+ (spec
->flags
& SIGN
);
2375 } else if (qualifier
== 'h') {
2376 BUILD_BUG_ON(FORMAT_TYPE_USHORT
+ SIGN
!= FORMAT_TYPE_SHORT
);
2377 spec
->type
= FORMAT_TYPE_USHORT
+ (spec
->flags
& SIGN
);
2379 BUILD_BUG_ON(FORMAT_TYPE_UINT
+ SIGN
!= FORMAT_TYPE_INT
);
2380 spec
->type
= FORMAT_TYPE_UINT
+ (spec
->flags
& SIGN
);
2383 return ++fmt
- start
;
2387 set_field_width(struct printf_spec
*spec
, int width
)
2389 spec
->field_width
= width
;
2390 if (WARN_ONCE(spec
->field_width
!= width
, "field width %d too large", width
)) {
2391 spec
->field_width
= clamp(width
, -FIELD_WIDTH_MAX
, FIELD_WIDTH_MAX
);
2396 set_precision(struct printf_spec
*spec
, int prec
)
2398 spec
->precision
= prec
;
2399 if (WARN_ONCE(spec
->precision
!= prec
, "precision %d too large", prec
)) {
2400 spec
->precision
= clamp(prec
, 0, PRECISION_MAX
);
2405 * vsnprintf - Format a string and place it in a buffer
2406 * @buf: The buffer to place the result into
2407 * @size: The size of the buffer, including the trailing null space
2408 * @fmt: The format string to use
2409 * @args: Arguments for the format string
2411 * This function generally follows C99 vsnprintf, but has some
2412 * extensions and a few limitations:
2414 * - ``%n`` is unsupported
2415 * - ``%p*`` is handled by pointer()
2417 * See pointer() or Documentation/core-api/printk-formats.rst for more
2418 * extensive description.
2420 * **Please update the documentation in both places when making changes**
2422 * The return value is the number of characters which would
2423 * be generated for the given input, excluding the trailing
2424 * '\0', as per ISO C99. If you want to have the exact
2425 * number of characters written into @buf as return value
2426 * (not including the trailing '\0'), use vscnprintf(). If the
2427 * return is greater than or equal to @size, the resulting
2428 * string is truncated.
2430 * If you're not already dealing with a va_list consider using snprintf().
2432 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2434 unsigned long long num
;
2436 struct printf_spec spec
= {0};
2438 /* Reject out-of-range values early. Large positive sizes are
2439 used for unknown buffer sizes. */
2440 if (WARN_ON_ONCE(size
> INT_MAX
))
2446 /* Make sure end is always >= buf */
2453 const char *old_fmt
= fmt
;
2454 int read
= format_decode(fmt
, &spec
);
2458 switch (spec
.type
) {
2459 case FORMAT_TYPE_NONE
: {
2462 if (copy
> end
- str
)
2464 memcpy(str
, old_fmt
, copy
);
2470 case FORMAT_TYPE_WIDTH
:
2471 set_field_width(&spec
, va_arg(args
, int));
2474 case FORMAT_TYPE_PRECISION
:
2475 set_precision(&spec
, va_arg(args
, int));
2478 case FORMAT_TYPE_CHAR
: {
2481 if (!(spec
.flags
& LEFT
)) {
2482 while (--spec
.field_width
> 0) {
2489 c
= (unsigned char) va_arg(args
, int);
2493 while (--spec
.field_width
> 0) {
2501 case FORMAT_TYPE_STR
:
2502 str
= string(str
, end
, va_arg(args
, char *), spec
);
2505 case FORMAT_TYPE_PTR
:
2506 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2508 while (isalnum(*fmt
))
2512 case FORMAT_TYPE_PERCENT_CHAR
:
2518 case FORMAT_TYPE_INVALID
:
2520 * Presumably the arguments passed gcc's type
2521 * checking, but there is no safe or sane way
2522 * for us to continue parsing the format and
2523 * fetching from the va_list; the remaining
2524 * specifiers and arguments would be out of
2530 switch (spec
.type
) {
2531 case FORMAT_TYPE_LONG_LONG
:
2532 num
= va_arg(args
, long long);
2534 case FORMAT_TYPE_ULONG
:
2535 num
= va_arg(args
, unsigned long);
2537 case FORMAT_TYPE_LONG
:
2538 num
= va_arg(args
, long);
2540 case FORMAT_TYPE_SIZE_T
:
2541 if (spec
.flags
& SIGN
)
2542 num
= va_arg(args
, ssize_t
);
2544 num
= va_arg(args
, size_t);
2546 case FORMAT_TYPE_PTRDIFF
:
2547 num
= va_arg(args
, ptrdiff_t);
2549 case FORMAT_TYPE_UBYTE
:
2550 num
= (unsigned char) va_arg(args
, int);
2552 case FORMAT_TYPE_BYTE
:
2553 num
= (signed char) va_arg(args
, int);
2555 case FORMAT_TYPE_USHORT
:
2556 num
= (unsigned short) va_arg(args
, int);
2558 case FORMAT_TYPE_SHORT
:
2559 num
= (short) va_arg(args
, int);
2561 case FORMAT_TYPE_INT
:
2562 num
= (int) va_arg(args
, int);
2565 num
= va_arg(args
, unsigned int);
2568 str
= number(str
, end
, num
, spec
);
2580 /* the trailing null byte doesn't count towards the total */
2584 EXPORT_SYMBOL(vsnprintf
);
2587 * vscnprintf - Format a string and place it in a buffer
2588 * @buf: The buffer to place the result into
2589 * @size: The size of the buffer, including the trailing null space
2590 * @fmt: The format string to use
2591 * @args: Arguments for the format string
2593 * The return value is the number of characters which have been written into
2594 * the @buf not including the trailing '\0'. If @size is == 0 the function
2597 * If you're not already dealing with a va_list consider using scnprintf().
2599 * See the vsnprintf() documentation for format string extensions over C99.
2601 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2605 i
= vsnprintf(buf
, size
, fmt
, args
);
2607 if (likely(i
< size
))
2613 EXPORT_SYMBOL(vscnprintf
);
2616 * snprintf - Format a string and place it in a buffer
2617 * @buf: The buffer to place the result into
2618 * @size: The size of the buffer, including the trailing null space
2619 * @fmt: The format string to use
2620 * @...: Arguments for the format string
2622 * The return value is the number of characters which would be
2623 * generated for the given input, excluding the trailing null,
2624 * as per ISO C99. If the return is greater than or equal to
2625 * @size, the resulting string is truncated.
2627 * See the vsnprintf() documentation for format string extensions over C99.
2629 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
2634 va_start(args
, fmt
);
2635 i
= vsnprintf(buf
, size
, fmt
, args
);
2640 EXPORT_SYMBOL(snprintf
);
2643 * scnprintf - Format a string and place it in a buffer
2644 * @buf: The buffer to place the result into
2645 * @size: The size of the buffer, including the trailing null space
2646 * @fmt: The format string to use
2647 * @...: Arguments for the format string
2649 * The return value is the number of characters written into @buf not including
2650 * the trailing '\0'. If @size is == 0 the function returns 0.
2653 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
2658 va_start(args
, fmt
);
2659 i
= vscnprintf(buf
, size
, fmt
, args
);
2664 EXPORT_SYMBOL(scnprintf
);
2667 * vsprintf - Format a string and place it in a buffer
2668 * @buf: The buffer to place the result into
2669 * @fmt: The format string to use
2670 * @args: Arguments for the format string
2672 * The function returns the number of characters written
2673 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2676 * If you're not already dealing with a va_list consider using sprintf().
2678 * See the vsnprintf() documentation for format string extensions over C99.
2680 int vsprintf(char *buf
, const char *fmt
, va_list args
)
2682 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
2684 EXPORT_SYMBOL(vsprintf
);
2687 * sprintf - Format a string and place it in a buffer
2688 * @buf: The buffer to place the result into
2689 * @fmt: The format string to use
2690 * @...: Arguments for the format string
2692 * The function returns the number of characters written
2693 * into @buf. Use snprintf() or scnprintf() in order to avoid
2696 * See the vsnprintf() documentation for format string extensions over C99.
2698 int sprintf(char *buf
, const char *fmt
, ...)
2703 va_start(args
, fmt
);
2704 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
2709 EXPORT_SYMBOL(sprintf
);
2711 #ifdef CONFIG_BINARY_PRINTF
2714 * vbin_printf() - VA arguments to binary data
2715 * bstr_printf() - Binary data to text string
2719 * vbin_printf - Parse a format string and place args' binary value in a buffer
2720 * @bin_buf: The buffer to place args' binary value
2721 * @size: The size of the buffer(by words(32bits), not characters)
2722 * @fmt: The format string to use
2723 * @args: Arguments for the format string
2725 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2728 * The return value is the number of words(32bits) which would be generated for
2732 * If the return value is greater than @size, the resulting bin_buf is NOT
2733 * valid for bstr_printf().
2735 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
2737 struct printf_spec spec
= {0};
2741 str
= (char *)bin_buf
;
2742 end
= (char *)(bin_buf
+ size
);
2744 #define save_arg(type) \
2746 unsigned long long value; \
2747 if (sizeof(type) == 8) { \
2748 unsigned long long val8; \
2749 str = PTR_ALIGN(str, sizeof(u32)); \
2750 val8 = va_arg(args, unsigned long long); \
2751 if (str + sizeof(type) <= end) { \
2752 *(u32 *)str = *(u32 *)&val8; \
2753 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2757 unsigned int val4; \
2758 str = PTR_ALIGN(str, sizeof(type)); \
2759 val4 = va_arg(args, int); \
2760 if (str + sizeof(type) <= end) \
2761 *(typeof(type) *)str = (type)(long)val4; \
2762 value = (unsigned long long)val4; \
2764 str += sizeof(type); \
2769 int read
= format_decode(fmt
, &spec
);
2773 switch (spec
.type
) {
2774 case FORMAT_TYPE_NONE
:
2775 case FORMAT_TYPE_PERCENT_CHAR
:
2777 case FORMAT_TYPE_INVALID
:
2780 case FORMAT_TYPE_WIDTH
:
2781 case FORMAT_TYPE_PRECISION
:
2782 width
= (int)save_arg(int);
2783 /* Pointers may require the width */
2785 set_field_width(&spec
, width
);
2788 case FORMAT_TYPE_CHAR
:
2792 case FORMAT_TYPE_STR
: {
2793 const char *save_str
= va_arg(args
, char *);
2794 const char *err_msg
;
2797 err_msg
= check_pointer_msg(save_str
);
2801 len
= strlen(save_str
) + 1;
2802 if (str
+ len
< end
)
2803 memcpy(str
, save_str
, len
);
2808 case FORMAT_TYPE_PTR
:
2809 /* Dereferenced pointers must be done now */
2811 /* Dereference of functions is still OK */
2821 if (!isalnum(*fmt
)) {
2825 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2830 end
[-1] = '\0'; /* Must be nul terminated */
2832 /* skip all alphanumeric pointer suffixes */
2833 while (isalnum(*fmt
))
2838 switch (spec
.type
) {
2840 case FORMAT_TYPE_LONG_LONG
:
2841 save_arg(long long);
2843 case FORMAT_TYPE_ULONG
:
2844 case FORMAT_TYPE_LONG
:
2845 save_arg(unsigned long);
2847 case FORMAT_TYPE_SIZE_T
:
2850 case FORMAT_TYPE_PTRDIFF
:
2851 save_arg(ptrdiff_t);
2853 case FORMAT_TYPE_UBYTE
:
2854 case FORMAT_TYPE_BYTE
:
2857 case FORMAT_TYPE_USHORT
:
2858 case FORMAT_TYPE_SHORT
:
2868 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2871 EXPORT_SYMBOL_GPL(vbin_printf
);
2874 * bstr_printf - Format a string from binary arguments and place it in a buffer
2875 * @buf: The buffer to place the result into
2876 * @size: The size of the buffer, including the trailing null space
2877 * @fmt: The format string to use
2878 * @bin_buf: Binary arguments for the format string
2880 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2881 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2882 * a binary buffer that generated by vbin_printf.
2884 * The format follows C99 vsnprintf, but has some extensions:
2885 * see vsnprintf comment for details.
2887 * The return value is the number of characters which would
2888 * be generated for the given input, excluding the trailing
2889 * '\0', as per ISO C99. If you want to have the exact
2890 * number of characters written into @buf as return value
2891 * (not including the trailing '\0'), use vscnprintf(). If the
2892 * return is greater than or equal to @size, the resulting
2893 * string is truncated.
2895 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2897 struct printf_spec spec
= {0};
2899 const char *args
= (const char *)bin_buf
;
2901 if (WARN_ON_ONCE(size
> INT_MAX
))
2907 #define get_arg(type) \
2909 typeof(type) value; \
2910 if (sizeof(type) == 8) { \
2911 args = PTR_ALIGN(args, sizeof(u32)); \
2912 *(u32 *)&value = *(u32 *)args; \
2913 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2915 args = PTR_ALIGN(args, sizeof(type)); \
2916 value = *(typeof(type) *)args; \
2918 args += sizeof(type); \
2922 /* Make sure end is always >= buf */
2929 const char *old_fmt
= fmt
;
2930 int read
= format_decode(fmt
, &spec
);
2934 switch (spec
.type
) {
2935 case FORMAT_TYPE_NONE
: {
2938 if (copy
> end
- str
)
2940 memcpy(str
, old_fmt
, copy
);
2946 case FORMAT_TYPE_WIDTH
:
2947 set_field_width(&spec
, get_arg(int));
2950 case FORMAT_TYPE_PRECISION
:
2951 set_precision(&spec
, get_arg(int));
2954 case FORMAT_TYPE_CHAR
: {
2957 if (!(spec
.flags
& LEFT
)) {
2958 while (--spec
.field_width
> 0) {
2964 c
= (unsigned char) get_arg(char);
2968 while (--spec
.field_width
> 0) {
2976 case FORMAT_TYPE_STR
: {
2977 const char *str_arg
= args
;
2978 args
+= strlen(str_arg
) + 1;
2979 str
= string(str
, end
, (char *)str_arg
, spec
);
2983 case FORMAT_TYPE_PTR
: {
2984 bool process
= false;
2986 /* Non function dereferences were already done */
2997 if (!isalnum(*fmt
)) {
3001 /* Pointer dereference was already processed */
3003 len
= copy
= strlen(args
);
3004 if (copy
> end
- str
)
3006 memcpy(str
, args
, copy
);
3012 str
= pointer(fmt
, str
, end
, get_arg(void *), spec
);
3014 while (isalnum(*fmt
))
3019 case FORMAT_TYPE_PERCENT_CHAR
:
3025 case FORMAT_TYPE_INVALID
:
3029 unsigned long long num
;
3031 switch (spec
.type
) {
3033 case FORMAT_TYPE_LONG_LONG
:
3034 num
= get_arg(long long);
3036 case FORMAT_TYPE_ULONG
:
3037 case FORMAT_TYPE_LONG
:
3038 num
= get_arg(unsigned long);
3040 case FORMAT_TYPE_SIZE_T
:
3041 num
= get_arg(size_t);
3043 case FORMAT_TYPE_PTRDIFF
:
3044 num
= get_arg(ptrdiff_t);
3046 case FORMAT_TYPE_UBYTE
:
3047 num
= get_arg(unsigned char);
3049 case FORMAT_TYPE_BYTE
:
3050 num
= get_arg(signed char);
3052 case FORMAT_TYPE_USHORT
:
3053 num
= get_arg(unsigned short);
3055 case FORMAT_TYPE_SHORT
:
3056 num
= get_arg(short);
3058 case FORMAT_TYPE_UINT
:
3059 num
= get_arg(unsigned int);
3065 str
= number(str
, end
, num
, spec
);
3067 } /* switch(spec.type) */
3080 /* the trailing null byte doesn't count towards the total */
3083 EXPORT_SYMBOL_GPL(bstr_printf
);
3086 * bprintf - Parse a format string and place args' binary value in a buffer
3087 * @bin_buf: The buffer to place args' binary value
3088 * @size: The size of the buffer(by words(32bits), not characters)
3089 * @fmt: The format string to use
3090 * @...: Arguments for the format string
3092 * The function returns the number of words(u32) written
3095 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
3100 va_start(args
, fmt
);
3101 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
3106 EXPORT_SYMBOL_GPL(bprintf
);
3108 #endif /* CONFIG_BINARY_PRINTF */
3111 * vsscanf - Unformat a buffer into a list of arguments
3112 * @buf: input buffer
3113 * @fmt: format of buffer
3116 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
3118 const char *str
= buf
;
3126 unsigned long long u
;
3132 /* skip any white space in format */
3133 /* white space in format matchs any amount of
3134 * white space, including none, in the input.
3136 if (isspace(*fmt
)) {
3137 fmt
= skip_spaces(++fmt
);
3138 str
= skip_spaces(str
);
3141 /* anything that is not a conversion must match exactly */
3142 if (*fmt
!= '%' && *fmt
) {
3143 if (*fmt
++ != *str
++)
3152 /* skip this conversion.
3153 * advance both strings to next white space
3158 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
) {
3159 /* '%*[' not yet supported, invalid format */
3164 while (!isspace(*str
) && *str
)
3169 /* get field width */
3171 if (isdigit(*fmt
)) {
3172 field_width
= skip_atoi(&fmt
);
3173 if (field_width
<= 0)
3177 /* get conversion qualifier */
3179 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
3182 if (unlikely(qualifier
== *fmt
)) {
3183 if (qualifier
== 'h') {
3186 } else if (qualifier
== 'l') {
3197 /* return number of characters read so far */
3198 *va_arg(args
, int *) = str
- buf
;
3212 char *s
= (char *)va_arg(args
, char*);
3213 if (field_width
== -1)
3217 } while (--field_width
> 0 && *str
);
3223 char *s
= (char *)va_arg(args
, char *);
3224 if (field_width
== -1)
3225 field_width
= SHRT_MAX
;
3226 /* first, skip leading white space in buffer */
3227 str
= skip_spaces(str
);
3229 /* now copy until next white space */
3230 while (*str
&& !isspace(*str
) && field_width
--)
3237 * Warning: This implementation of the '[' conversion specifier
3238 * deviates from its glibc counterpart in the following ways:
3239 * (1) It does NOT support ranges i.e. '-' is NOT a special
3241 * (2) It cannot match the closing bracket ']' itself
3242 * (3) A field width is required
3243 * (4) '%*[' (discard matching input) is currently not supported
3246 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3247 * buf1, buf2, buf3);
3253 char *s
= (char *)va_arg(args
, char *);
3254 DECLARE_BITMAP(set
, 256) = {0};
3255 unsigned int len
= 0;
3256 bool negate
= (*fmt
== '^');
3258 /* field width is required */
3259 if (field_width
== -1)
3265 for ( ; *fmt
&& *fmt
!= ']'; ++fmt
, ++len
)
3266 set_bit((u8
)*fmt
, set
);
3268 /* no ']' or no character set found */
3274 bitmap_complement(set
, set
, 256);
3275 /* exclude null '\0' byte */
3279 /* match must be non-empty */
3280 if (!test_bit((u8
)*str
, set
))
3283 while (test_bit((u8
)*str
, set
) && field_width
--)
3305 /* looking for '%' in str */
3310 /* invalid format; stop here */
3314 /* have some sort of integer conversion.
3315 * first, skip white space in buffer.
3317 str
= skip_spaces(str
);
3320 if (is_sign
&& digit
== '-')
3324 || (base
== 16 && !isxdigit(digit
))
3325 || (base
== 10 && !isdigit(digit
))
3326 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
3327 || (base
== 0 && !isdigit(digit
)))
3331 val
.s
= qualifier
!= 'L' ?
3332 simple_strtol(str
, &next
, base
) :
3333 simple_strtoll(str
, &next
, base
);
3335 val
.u
= qualifier
!= 'L' ?
3336 simple_strtoul(str
, &next
, base
) :
3337 simple_strtoull(str
, &next
, base
);
3339 if (field_width
> 0 && next
- str
> field_width
) {
3341 _parse_integer_fixup_radix(str
, &base
);
3342 while (next
- str
> field_width
) {
3344 val
.s
= div_s64(val
.s
, base
);
3346 val
.u
= div_u64(val
.u
, base
);
3351 switch (qualifier
) {
3352 case 'H': /* that's 'hh' in format */
3354 *va_arg(args
, signed char *) = val
.s
;
3356 *va_arg(args
, unsigned char *) = val
.u
;
3360 *va_arg(args
, short *) = val
.s
;
3362 *va_arg(args
, unsigned short *) = val
.u
;
3366 *va_arg(args
, long *) = val
.s
;
3368 *va_arg(args
, unsigned long *) = val
.u
;
3372 *va_arg(args
, long long *) = val
.s
;
3374 *va_arg(args
, unsigned long long *) = val
.u
;
3377 *va_arg(args
, size_t *) = val
.u
;
3381 *va_arg(args
, int *) = val
.s
;
3383 *va_arg(args
, unsigned int *) = val
.u
;
3395 EXPORT_SYMBOL(vsscanf
);
3398 * sscanf - Unformat a buffer into a list of arguments
3399 * @buf: input buffer
3400 * @fmt: formatting of buffer
3401 * @...: resulting arguments
3403 int sscanf(const char *buf
, const char *fmt
, ...)
3408 va_start(args
, fmt
);
3409 i
= vsscanf(buf
, fmt
, args
);
3414 EXPORT_SYMBOL(sscanf
);