4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
20 #include <linux/clk.h>
21 #include <linux/clk-provider.h>
22 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/ctype.h>
26 #include <linux/kernel.h>
27 #include <linux/kallsyms.h>
28 #include <linux/math64.h>
29 #include <linux/uaccess.h>
30 #include <linux/ioport.h>
31 #include <linux/dcache.h>
32 #include <linux/cred.h>
33 #include <linux/rtc.h>
34 #include <linux/uuid.h>
36 #include <net/addrconf.h>
37 #include <linux/siphash.h>
38 #include <linux/compiler.h>
40 #include <linux/blkdev.h>
43 #include "../mm/internal.h" /* For the trace_print_flags arrays */
45 #include <asm/page.h> /* for PAGE_SIZE */
46 #include <asm/byteorder.h> /* cpu_to_le16 */
48 #include <linux/string_helpers.h>
52 * simple_strtoull - convert a string to an unsigned long long
53 * @cp: The start of the string
54 * @endp: A pointer to the end of the parsed string will be placed here
55 * @base: The number base to use
57 * This function is obsolete. Please use kstrtoull instead.
59 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
61 unsigned long long result
;
64 cp
= _parse_integer_fixup_radix(cp
, &base
);
65 rv
= _parse_integer(cp
, base
, &result
);
67 cp
+= (rv
& ~KSTRTOX_OVERFLOW
);
74 EXPORT_SYMBOL(simple_strtoull
);
77 * simple_strtoul - convert a string to an unsigned long
78 * @cp: The start of the string
79 * @endp: A pointer to the end of the parsed string will be placed here
80 * @base: The number base to use
82 * This function is obsolete. Please use kstrtoul instead.
84 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
86 return simple_strtoull(cp
, endp
, base
);
88 EXPORT_SYMBOL(simple_strtoul
);
91 * simple_strtol - convert a string to a signed long
92 * @cp: The start of the string
93 * @endp: A pointer to the end of the parsed string will be placed here
94 * @base: The number base to use
96 * This function is obsolete. Please use kstrtol instead.
98 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
101 return -simple_strtoul(cp
+ 1, endp
, base
);
103 return simple_strtoul(cp
, endp
, base
);
105 EXPORT_SYMBOL(simple_strtol
);
108 * simple_strtoll - convert a string to a signed long long
109 * @cp: The start of the string
110 * @endp: A pointer to the end of the parsed string will be placed here
111 * @base: The number base to use
113 * This function is obsolete. Please use kstrtoll instead.
115 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
118 return -simple_strtoull(cp
+ 1, endp
, base
);
120 return simple_strtoull(cp
, endp
, base
);
122 EXPORT_SYMBOL(simple_strtoll
);
124 static noinline_for_stack
125 int skip_atoi(const char **s
)
130 i
= i
*10 + *((*s
)++) - '0';
131 } while (isdigit(**s
));
137 * Decimal conversion is by far the most typical, and is used for
138 * /proc and /sys data. This directly impacts e.g. top performance
139 * with many processes running. We optimize it for speed by emitting
140 * two characters at a time, using a 200 byte lookup table. This
141 * roughly halves the number of multiplications compared to computing
142 * the digits one at a time. Implementation strongly inspired by the
143 * previous version, which in turn used ideas described at
144 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
145 * from the author, Douglas W. Jones).
147 * It turns out there is precisely one 26 bit fixed-point
148 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
149 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
150 * range happens to be somewhat larger (x <= 1073741898), but that's
151 * irrelevant for our purpose.
153 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
154 * need a 32x32->64 bit multiply, so we simply use the same constant.
156 * For dividing a number in the range [100, 10^4-1] by 100, there are
157 * several options. The simplest is (x * 0x147b) >> 19, which is valid
158 * for all x <= 43698.
161 static const u16 decpair
[100] = {
162 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
163 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
164 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
165 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
166 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
167 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
168 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
169 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
170 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
171 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
172 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
177 * This will print a single '0' even if r == 0, since we would
178 * immediately jump to out_r where two 0s would be written but only
179 * one of them accounted for in buf. This is needed by ip4_string
180 * below. All other callers pass a non-zero value of r.
182 static noinline_for_stack
183 char *put_dec_trunc8(char *buf
, unsigned r
)
191 /* 100 <= r < 10^8 */
192 q
= (r
* (u64
)0x28f5c29) >> 32;
193 *((u16
*)buf
) = decpair
[r
- 100*q
];
200 /* 100 <= q < 10^6 */
201 r
= (q
* (u64
)0x28f5c29) >> 32;
202 *((u16
*)buf
) = decpair
[q
- 100*r
];
209 /* 100 <= r < 10^4 */
210 q
= (r
* 0x147b) >> 19;
211 *((u16
*)buf
) = decpair
[r
- 100*q
];
218 *((u16
*)buf
) = decpair
[r
];
219 buf
+= r
< 10 ? 1 : 2;
223 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
224 static noinline_for_stack
225 char *put_dec_full8(char *buf
, unsigned r
)
230 q
= (r
* (u64
)0x28f5c29) >> 32;
231 *((u16
*)buf
) = decpair
[r
- 100*q
];
235 r
= (q
* (u64
)0x28f5c29) >> 32;
236 *((u16
*)buf
) = decpair
[q
- 100*r
];
240 q
= (r
* 0x147b) >> 19;
241 *((u16
*)buf
) = decpair
[r
- 100*q
];
245 *((u16
*)buf
) = decpair
[q
];
250 static noinline_for_stack
251 char *put_dec(char *buf
, unsigned long long n
)
253 if (n
>= 100*1000*1000)
254 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
255 /* 1 <= n <= 1.6e11 */
256 if (n
>= 100*1000*1000)
257 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
259 return put_dec_trunc8(buf
, n
);
262 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
265 put_dec_full4(char *buf
, unsigned r
)
270 q
= (r
* 0x147b) >> 19;
271 *((u16
*)buf
) = decpair
[r
- 100*q
];
274 *((u16
*)buf
) = decpair
[q
];
278 * Call put_dec_full4 on x % 10000, return x / 10000.
279 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
280 * holds for all x < 1,128,869,999. The largest value this
281 * helper will ever be asked to convert is 1,125,520,955.
282 * (second call in the put_dec code, assuming n is all-ones).
284 static noinline_for_stack
285 unsigned put_dec_helper4(char *buf
, unsigned x
)
287 uint32_t q
= (x
* (uint64_t)0x346DC5D7) >> 43;
289 put_dec_full4(buf
, x
- q
* 10000);
293 /* Based on code by Douglas W. Jones found at
294 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
295 * (with permission from the author).
296 * Performs no 64-bit division and hence should be fast on 32-bit machines.
299 char *put_dec(char *buf
, unsigned long long n
)
301 uint32_t d3
, d2
, d1
, q
, h
;
303 if (n
< 100*1000*1000)
304 return put_dec_trunc8(buf
, n
);
306 d1
= ((uint32_t)n
>> 16); /* implicit "& 0xffff" */
309 d3
= (h
>> 16); /* implicit "& 0xffff" */
311 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
312 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
313 q
= 656 * d3
+ 7296 * d2
+ 5536 * d1
+ ((uint32_t)n
& 0xffff);
314 q
= put_dec_helper4(buf
, q
);
316 q
+= 7671 * d3
+ 9496 * d2
+ 6 * d1
;
317 q
= put_dec_helper4(buf
+4, q
);
319 q
+= 4749 * d3
+ 42 * d2
;
320 q
= put_dec_helper4(buf
+8, q
);
325 buf
= put_dec_trunc8(buf
, q
);
326 else while (buf
[-1] == '0')
335 * Convert passed number to decimal string.
336 * Returns the length of string. On buffer overflow, returns 0.
338 * If speed is not important, use snprintf(). It's easy to read the code.
340 int num_to_str(char *buf
, int size
, unsigned long long num
, unsigned int width
)
342 /* put_dec requires 2-byte alignment of the buffer. */
343 char tmp
[sizeof(num
) * 3] __aligned(2);
346 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
351 len
= put_dec(tmp
, num
) - tmp
;
354 if (len
> size
|| width
> size
)
359 for (idx
= 0; idx
< width
; idx
++)
365 for (idx
= 0; idx
< len
; ++idx
)
366 buf
[idx
+ width
] = tmp
[len
- idx
- 1];
371 #define SIGN 1 /* unsigned/signed, must be 1 */
372 #define LEFT 2 /* left justified */
373 #define PLUS 4 /* show plus */
374 #define SPACE 8 /* space if plus */
375 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
376 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
377 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
380 FORMAT_TYPE_NONE
, /* Just a string part */
382 FORMAT_TYPE_PRECISION
,
386 FORMAT_TYPE_PERCENT_CHAR
,
388 FORMAT_TYPE_LONG_LONG
,
402 unsigned int type
:8; /* format_type enum */
403 signed int field_width
:24; /* width of output field */
404 unsigned int flags
:8; /* flags to number() */
405 unsigned int base
:8; /* number base, 8, 10 or 16 only */
406 signed int precision
:16; /* # of digits/chars */
408 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
409 #define PRECISION_MAX ((1 << 15) - 1)
411 static noinline_for_stack
412 char *number(char *buf
, char *end
, unsigned long long num
,
413 struct printf_spec spec
)
415 /* put_dec requires 2-byte alignment of the buffer. */
416 char tmp
[3 * sizeof(num
)] __aligned(2);
419 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
421 bool is_zero
= num
== 0LL;
422 int field_width
= spec
.field_width
;
423 int precision
= spec
.precision
;
425 BUILD_BUG_ON(sizeof(struct printf_spec
) != 8);
427 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
428 * produces same digits or (maybe lowercased) letters */
429 locase
= (spec
.flags
& SMALL
);
430 if (spec
.flags
& LEFT
)
431 spec
.flags
&= ~ZEROPAD
;
433 if (spec
.flags
& SIGN
) {
434 if ((signed long long)num
< 0) {
436 num
= -(signed long long)num
;
438 } else if (spec
.flags
& PLUS
) {
441 } else if (spec
.flags
& SPACE
) {
453 /* generate full string in tmp[], in reverse order */
456 tmp
[i
++] = hex_asc_upper
[num
] | locase
;
457 else if (spec
.base
!= 10) { /* 8 or 16 */
458 int mask
= spec
.base
- 1;
464 tmp
[i
++] = (hex_asc_upper
[((unsigned char)num
) & mask
] | locase
);
467 } else { /* base 10 */
468 i
= put_dec(tmp
, num
) - tmp
;
471 /* printing 100 using %2d gives "100", not "00" */
474 /* leading space padding */
475 field_width
-= precision
;
476 if (!(spec
.flags
& (ZEROPAD
| LEFT
))) {
477 while (--field_width
>= 0) {
489 /* "0x" / "0" prefix */
491 if (spec
.base
== 16 || !is_zero
) {
496 if (spec
.base
== 16) {
498 *buf
= ('X' | locase
);
502 /* zero or space padding */
503 if (!(spec
.flags
& LEFT
)) {
504 char c
= ' ' + (spec
.flags
& ZEROPAD
);
505 BUILD_BUG_ON(' ' + ZEROPAD
!= '0');
506 while (--field_width
>= 0) {
512 /* hmm even more zero padding? */
513 while (i
<= --precision
) {
518 /* actual digits of result */
524 /* trailing space padding */
525 while (--field_width
>= 0) {
534 static noinline_for_stack
535 char *special_hex_number(char *buf
, char *end
, unsigned long long num
, int size
)
537 struct printf_spec spec
;
539 spec
.type
= FORMAT_TYPE_PTR
;
540 spec
.field_width
= 2 + 2 * size
; /* 0x + hex */
541 spec
.flags
= SPECIAL
| SMALL
| ZEROPAD
;
545 return number(buf
, end
, num
, spec
);
548 static void move_right(char *buf
, char *end
, unsigned len
, unsigned spaces
)
551 if (buf
>= end
) /* nowhere to put anything */
554 if (size
<= spaces
) {
555 memset(buf
, ' ', size
);
559 if (len
> size
- spaces
)
561 memmove(buf
+ spaces
, buf
, len
);
563 memset(buf
, ' ', spaces
);
567 * Handle field width padding for a string.
568 * @buf: current buffer position
569 * @n: length of string
570 * @end: end of output buffer
571 * @spec: for field width and flags
572 * Returns: new buffer position after padding.
574 static noinline_for_stack
575 char *widen_string(char *buf
, int n
, char *end
, struct printf_spec spec
)
579 if (likely(n
>= spec
.field_width
))
581 /* we want to pad the sucker */
582 spaces
= spec
.field_width
- n
;
583 if (!(spec
.flags
& LEFT
)) {
584 move_right(buf
- n
, end
, n
, spaces
);
595 static noinline_for_stack
596 char *string(char *buf
, char *end
, const char *s
, struct printf_spec spec
)
599 size_t lim
= spec
.precision
;
601 if ((unsigned long)s
< PAGE_SIZE
)
613 return widen_string(buf
, len
, end
, spec
);
616 static noinline_for_stack
617 char *pointer_string(char *buf
, char *end
, const void *ptr
,
618 struct printf_spec spec
)
622 if (spec
.field_width
== -1) {
623 spec
.field_width
= 2 * sizeof(ptr
);
624 spec
.flags
|= ZEROPAD
;
627 return number(buf
, end
, (unsigned long int)ptr
, spec
);
630 /* Make pointers available for printing early in the boot sequence. */
631 static int debug_boot_weak_hash __ro_after_init
;
633 static int __init
debug_boot_weak_hash_enable(char *str
)
635 debug_boot_weak_hash
= 1;
636 pr_info("debug_boot_weak_hash enabled\n");
639 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable
);
641 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key
);
642 static siphash_key_t ptr_key __read_mostly
;
644 static void enable_ptr_key_workfn(struct work_struct
*work
)
646 get_random_bytes(&ptr_key
, sizeof(ptr_key
));
647 /* Needs to run from preemptible context */
648 static_branch_disable(¬_filled_random_ptr_key
);
651 static DECLARE_WORK(enable_ptr_key_work
, enable_ptr_key_workfn
);
653 static void fill_random_ptr_key(struct random_ready_callback
*unused
)
655 /* This may be in an interrupt handler. */
656 queue_work(system_unbound_wq
, &enable_ptr_key_work
);
659 static struct random_ready_callback random_ready
= {
660 .func
= fill_random_ptr_key
663 static int __init
initialize_ptr_random(void)
665 int key_size
= sizeof(ptr_key
);
668 /* Use hw RNG if available. */
669 if (get_random_bytes_arch(&ptr_key
, key_size
) == key_size
) {
670 static_branch_disable(¬_filled_random_ptr_key
);
674 ret
= add_random_ready_callback(&random_ready
);
677 } else if (ret
== -EALREADY
) {
678 /* This is in preemptible context */
679 enable_ptr_key_workfn(&enable_ptr_key_work
);
685 early_initcall(initialize_ptr_random
);
687 /* Maps a pointer to a 32 bit unique identifier. */
688 static char *ptr_to_id(char *buf
, char *end
, const void *ptr
,
689 struct printf_spec spec
)
691 const char *str
= sizeof(ptr
) == 8 ? "(____ptrval____)" : "(ptrval)";
692 unsigned long hashval
;
694 /* When debugging early boot use non-cryptographically secure hash. */
695 if (unlikely(debug_boot_weak_hash
)) {
696 hashval
= hash_long((unsigned long)ptr
, 32);
697 return pointer_string(buf
, end
, (const void *)hashval
, spec
);
700 if (static_branch_unlikely(¬_filled_random_ptr_key
)) {
701 spec
.field_width
= 2 * sizeof(ptr
);
702 /* string length must be less than default_width */
703 return string(buf
, end
, str
, spec
);
707 hashval
= (unsigned long)siphash_1u64((u64
)ptr
, &ptr_key
);
709 * Mask off the first 32 bits, this makes explicit that we have
710 * modified the address (and 32 bits is plenty for a unique ID).
712 hashval
= hashval
& 0xffffffff;
714 hashval
= (unsigned long)siphash_1u32((u32
)ptr
, &ptr_key
);
716 return pointer_string(buf
, end
, (const void *)hashval
, spec
);
719 static noinline_for_stack
720 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
723 const char *array
[4], *s
;
724 const struct dentry
*p
;
729 case '2': case '3': case '4':
730 depth
= fmt
[1] - '0';
737 for (i
= 0; i
< depth
; i
++, d
= p
) {
738 p
= READ_ONCE(d
->d_parent
);
739 array
[i
] = READ_ONCE(d
->d_name
.name
);
748 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
760 return widen_string(buf
, n
, end
, spec
);
764 static noinline_for_stack
765 char *bdev_name(char *buf
, char *end
, struct block_device
*bdev
,
766 struct printf_spec spec
, const char *fmt
)
768 struct gendisk
*hd
= bdev
->bd_disk
;
770 buf
= string(buf
, end
, hd
->disk_name
, spec
);
771 if (bdev
->bd_part
->partno
) {
772 if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1])) {
777 buf
= number(buf
, end
, bdev
->bd_part
->partno
, spec
);
783 static noinline_for_stack
784 char *symbol_string(char *buf
, char *end
, void *ptr
,
785 struct printf_spec spec
, const char *fmt
)
788 #ifdef CONFIG_KALLSYMS
789 char sym
[KSYM_SYMBOL_LEN
];
793 ptr
= __builtin_extract_return_addr(ptr
);
794 value
= (unsigned long)ptr
;
796 #ifdef CONFIG_KALLSYMS
798 sprint_backtrace(sym
, value
);
799 else if (*fmt
!= 'f' && *fmt
!= 's')
800 sprint_symbol(sym
, value
);
802 sprint_symbol_no_offset(sym
, value
);
804 return string(buf
, end
, sym
, spec
);
806 return special_hex_number(buf
, end
, value
, sizeof(void *));
810 static const struct printf_spec default_str_spec
= {
815 static const struct printf_spec default_flag_spec
= {
818 .flags
= SPECIAL
| SMALL
,
821 static const struct printf_spec default_dec_spec
= {
826 static const struct printf_spec default_dec02_spec
= {
833 static const struct printf_spec default_dec04_spec
= {
840 static noinline_for_stack
841 char *resource_string(char *buf
, char *end
, struct resource
*res
,
842 struct printf_spec spec
, const char *fmt
)
844 #ifndef IO_RSRC_PRINTK_SIZE
845 #define IO_RSRC_PRINTK_SIZE 6
848 #ifndef MEM_RSRC_PRINTK_SIZE
849 #define MEM_RSRC_PRINTK_SIZE 10
851 static const struct printf_spec io_spec
= {
853 .field_width
= IO_RSRC_PRINTK_SIZE
,
855 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
857 static const struct printf_spec mem_spec
= {
859 .field_width
= MEM_RSRC_PRINTK_SIZE
,
861 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
863 static const struct printf_spec bus_spec
= {
867 .flags
= SMALL
| ZEROPAD
,
869 static const struct printf_spec str_spec
= {
875 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
876 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
877 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
878 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
879 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
880 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
881 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
882 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
884 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
885 int decode
= (fmt
[0] == 'R') ? 1 : 0;
886 const struct printf_spec
*specp
;
889 if (res
->flags
& IORESOURCE_IO
) {
890 p
= string(p
, pend
, "io ", str_spec
);
892 } else if (res
->flags
& IORESOURCE_MEM
) {
893 p
= string(p
, pend
, "mem ", str_spec
);
895 } else if (res
->flags
& IORESOURCE_IRQ
) {
896 p
= string(p
, pend
, "irq ", str_spec
);
897 specp
= &default_dec_spec
;
898 } else if (res
->flags
& IORESOURCE_DMA
) {
899 p
= string(p
, pend
, "dma ", str_spec
);
900 specp
= &default_dec_spec
;
901 } else if (res
->flags
& IORESOURCE_BUS
) {
902 p
= string(p
, pend
, "bus ", str_spec
);
905 p
= string(p
, pend
, "??? ", str_spec
);
909 if (decode
&& res
->flags
& IORESOURCE_UNSET
) {
910 p
= string(p
, pend
, "size ", str_spec
);
911 p
= number(p
, pend
, resource_size(res
), *specp
);
913 p
= number(p
, pend
, res
->start
, *specp
);
914 if (res
->start
!= res
->end
) {
916 p
= number(p
, pend
, res
->end
, *specp
);
920 if (res
->flags
& IORESOURCE_MEM_64
)
921 p
= string(p
, pend
, " 64bit", str_spec
);
922 if (res
->flags
& IORESOURCE_PREFETCH
)
923 p
= string(p
, pend
, " pref", str_spec
);
924 if (res
->flags
& IORESOURCE_WINDOW
)
925 p
= string(p
, pend
, " window", str_spec
);
926 if (res
->flags
& IORESOURCE_DISABLED
)
927 p
= string(p
, pend
, " disabled", str_spec
);
929 p
= string(p
, pend
, " flags ", str_spec
);
930 p
= number(p
, pend
, res
->flags
, default_flag_spec
);
935 return string(buf
, end
, sym
, spec
);
938 static noinline_for_stack
939 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
942 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
943 negative value, fallback to the default */
946 if (spec
.field_width
== 0)
947 /* nothing to print */
950 if (ZERO_OR_NULL_PTR(addr
))
952 return string(buf
, end
, NULL
, spec
);
969 if (spec
.field_width
> 0)
970 len
= min_t(int, spec
.field_width
, 64);
972 for (i
= 0; i
< len
; ++i
) {
974 *buf
= hex_asc_hi(addr
[i
]);
977 *buf
= hex_asc_lo(addr
[i
]);
980 if (separator
&& i
!= len
- 1) {
990 static noinline_for_stack
991 char *bitmap_string(char *buf
, char *end
, unsigned long *bitmap
,
992 struct printf_spec spec
, const char *fmt
)
994 const int CHUNKSZ
= 32;
995 int nr_bits
= max_t(int, spec
.field_width
, 0);
999 /* reused to print numbers */
1000 spec
= (struct printf_spec
){ .flags
= SMALL
| ZEROPAD
, .base
= 16 };
1002 chunksz
= nr_bits
& (CHUNKSZ
- 1);
1006 i
= ALIGN(nr_bits
, CHUNKSZ
) - CHUNKSZ
;
1007 for (; i
>= 0; i
-= CHUNKSZ
) {
1011 chunkmask
= ((1ULL << chunksz
) - 1);
1012 word
= i
/ BITS_PER_LONG
;
1013 bit
= i
% BITS_PER_LONG
;
1014 val
= (bitmap
[word
] >> bit
) & chunkmask
;
1023 spec
.field_width
= DIV_ROUND_UP(chunksz
, 4);
1024 buf
= number(buf
, end
, val
, spec
);
1031 static noinline_for_stack
1032 char *bitmap_list_string(char *buf
, char *end
, unsigned long *bitmap
,
1033 struct printf_spec spec
, const char *fmt
)
1035 int nr_bits
= max_t(int, spec
.field_width
, 0);
1036 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1037 int cur
, rbot
, rtop
;
1040 rbot
= cur
= find_first_bit(bitmap
, nr_bits
);
1041 while (cur
< nr_bits
) {
1043 cur
= find_next_bit(bitmap
, nr_bits
, cur
+ 1);
1044 if (cur
< nr_bits
&& cur
<= rtop
+ 1)
1054 buf
= number(buf
, end
, rbot
, default_dec_spec
);
1060 buf
= number(buf
, end
, rtop
, default_dec_spec
);
1068 static noinline_for_stack
1069 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
1070 struct printf_spec spec
, const char *fmt
)
1072 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
1076 bool reversed
= false;
1092 for (i
= 0; i
< 6; i
++) {
1094 p
= hex_byte_pack(p
, addr
[5 - i
]);
1096 p
= hex_byte_pack(p
, addr
[i
]);
1098 if (fmt
[0] == 'M' && i
!= 5)
1103 return string(buf
, end
, mac_addr
, spec
);
1106 static noinline_for_stack
1107 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
1110 bool leading_zeros
= (fmt
[0] == 'i');
1135 for (i
= 0; i
< 4; i
++) {
1136 char temp
[4] __aligned(2); /* hold each IP quad in reverse order */
1137 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
1138 if (leading_zeros
) {
1144 /* reverse the digits in the quad */
1146 *p
++ = temp
[digits
];
1156 static noinline_for_stack
1157 char *ip6_compressed_string(char *p
, const char *addr
)
1160 unsigned char zerolength
[8];
1165 bool needcolon
= false;
1167 struct in6_addr in6
;
1169 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
1171 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
1173 memset(zerolength
, 0, sizeof(zerolength
));
1180 /* find position of longest 0 run */
1181 for (i
= 0; i
< range
; i
++) {
1182 for (j
= i
; j
< range
; j
++) {
1183 if (in6
.s6_addr16
[j
] != 0)
1188 for (i
= 0; i
< range
; i
++) {
1189 if (zerolength
[i
] > longest
) {
1190 longest
= zerolength
[i
];
1194 if (longest
== 1) /* don't compress a single 0 */
1198 for (i
= 0; i
< range
; i
++) {
1199 if (i
== colonpos
) {
1200 if (needcolon
|| i
== 0)
1211 /* hex u16 without leading 0s */
1212 word
= ntohs(in6
.s6_addr16
[i
]);
1217 p
= hex_byte_pack(p
, hi
);
1219 *p
++ = hex_asc_lo(hi
);
1220 p
= hex_byte_pack(p
, lo
);
1223 p
= hex_byte_pack(p
, lo
);
1225 *p
++ = hex_asc_lo(lo
);
1232 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
1239 static noinline_for_stack
1240 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
1244 for (i
= 0; i
< 8; i
++) {
1245 p
= hex_byte_pack(p
, *addr
++);
1246 p
= hex_byte_pack(p
, *addr
++);
1247 if (fmt
[0] == 'I' && i
!= 7)
1255 static noinline_for_stack
1256 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
1257 struct printf_spec spec
, const char *fmt
)
1259 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1261 if (fmt
[0] == 'I' && fmt
[2] == 'c')
1262 ip6_compressed_string(ip6_addr
, addr
);
1264 ip6_string(ip6_addr
, addr
, fmt
);
1266 return string(buf
, end
, ip6_addr
, spec
);
1269 static noinline_for_stack
1270 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
1271 struct printf_spec spec
, const char *fmt
)
1273 char ip4_addr
[sizeof("255.255.255.255")];
1275 ip4_string(ip4_addr
, addr
, fmt
);
1277 return string(buf
, end
, ip4_addr
, spec
);
1280 static noinline_for_stack
1281 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1282 struct printf_spec spec
, const char *fmt
)
1284 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1285 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1286 sizeof(":12345") + sizeof("/123456789") +
1287 sizeof("%1234567890")];
1288 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1289 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1290 char fmt6
[2] = { fmt
[0], '6' };
1294 while (isalpha(*++fmt
)) {
1311 if (have_p
|| have_s
|| have_f
) {
1316 if (fmt6
[0] == 'I' && have_c
)
1317 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1319 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1321 if (have_p
|| have_s
|| have_f
)
1326 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1330 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1331 IPV6_FLOWINFO_MASK
), spec
);
1335 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1339 return string(buf
, end
, ip6_addr
, spec
);
1342 static noinline_for_stack
1343 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1344 struct printf_spec spec
, const char *fmt
)
1346 bool have_p
= false;
1347 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1348 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1349 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1350 char fmt4
[3] = { fmt
[0], '4', 0 };
1353 while (isalpha(*++fmt
)) {
1367 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1370 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1374 return string(buf
, end
, ip4_addr
, spec
);
1377 static noinline_for_stack
1378 char *escaped_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1383 unsigned int flags
= 0;
1386 if (spec
.field_width
== 0)
1387 return buf
; /* nothing to print */
1389 if (ZERO_OR_NULL_PTR(addr
))
1390 return string(buf
, end
, NULL
, spec
); /* NULL pointer */
1394 switch (fmt
[count
++]) {
1396 flags
|= ESCAPE_ANY
;
1399 flags
|= ESCAPE_SPECIAL
;
1402 flags
|= ESCAPE_HEX
;
1405 flags
|= ESCAPE_NULL
;
1408 flags
|= ESCAPE_OCTAL
;
1414 flags
|= ESCAPE_SPACE
;
1423 flags
= ESCAPE_ANY_NP
;
1425 len
= spec
.field_width
< 0 ? 1 : spec
.field_width
;
1428 * string_escape_mem() writes as many characters as it can to
1429 * the given buffer, and returns the total size of the output
1430 * had the buffer been big enough.
1432 buf
+= string_escape_mem(addr
, len
, buf
, buf
< end
? end
- buf
: 0, flags
, NULL
);
1437 static noinline_for_stack
1438 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1439 struct printf_spec spec
, const char *fmt
)
1441 char uuid
[UUID_STRING_LEN
+ 1];
1444 const u8
*index
= uuid_index
;
1449 uc
= true; /* fall-through */
1458 for (i
= 0; i
< 16; i
++) {
1460 p
= hex_byte_pack_upper(p
, addr
[index
[i
]]);
1462 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1475 return string(buf
, end
, uuid
, spec
);
1478 int kptr_restrict __read_mostly
;
1480 static noinline_for_stack
1481 char *restricted_pointer(char *buf
, char *end
, const void *ptr
,
1482 struct printf_spec spec
)
1484 switch (kptr_restrict
) {
1486 /* Always print %pK values */
1489 const struct cred
*cred
;
1492 * kptr_restrict==1 cannot be used in IRQ context
1493 * because its test for CAP_SYSLOG would be meaningless.
1495 if (in_irq() || in_serving_softirq() || in_nmi()) {
1496 if (spec
.field_width
== -1)
1497 spec
.field_width
= 2 * sizeof(ptr
);
1498 return string(buf
, end
, "pK-error", spec
);
1502 * Only print the real pointer value if the current
1503 * process has CAP_SYSLOG and is running with the
1504 * same credentials it started with. This is because
1505 * access to files is checked at open() time, but %pK
1506 * checks permission at read() time. We don't want to
1507 * leak pointer values if a binary opens a file using
1508 * %pK and then elevates privileges before reading it.
1510 cred
= current_cred();
1511 if (!has_capability_noaudit(current
, CAP_SYSLOG
) ||
1512 !uid_eq(cred
->euid
, cred
->uid
) ||
1513 !gid_eq(cred
->egid
, cred
->gid
))
1519 /* Always print 0's for %pK */
1524 return pointer_string(buf
, end
, ptr
, spec
);
1527 static noinline_for_stack
1528 char *netdev_bits(char *buf
, char *end
, const void *addr
,
1529 struct printf_spec spec
, const char *fmt
)
1531 unsigned long long num
;
1536 num
= *(const netdev_features_t
*)addr
;
1537 size
= sizeof(netdev_features_t
);
1540 return ptr_to_id(buf
, end
, addr
, spec
);
1543 return special_hex_number(buf
, end
, num
, size
);
1546 static noinline_for_stack
1547 char *address_val(char *buf
, char *end
, const void *addr
, const char *fmt
)
1549 unsigned long long num
;
1554 num
= *(const dma_addr_t
*)addr
;
1555 size
= sizeof(dma_addr_t
);
1559 num
= *(const phys_addr_t
*)addr
;
1560 size
= sizeof(phys_addr_t
);
1564 return special_hex_number(buf
, end
, num
, size
);
1567 static noinline_for_stack
1568 char *date_str(char *buf
, char *end
, const struct rtc_time
*tm
, bool r
)
1570 int year
= tm
->tm_year
+ (r
? 0 : 1900);
1571 int mon
= tm
->tm_mon
+ (r
? 0 : 1);
1573 buf
= number(buf
, end
, year
, default_dec04_spec
);
1578 buf
= number(buf
, end
, mon
, default_dec02_spec
);
1583 return number(buf
, end
, tm
->tm_mday
, default_dec02_spec
);
1586 static noinline_for_stack
1587 char *time_str(char *buf
, char *end
, const struct rtc_time
*tm
, bool r
)
1589 buf
= number(buf
, end
, tm
->tm_hour
, default_dec02_spec
);
1594 buf
= number(buf
, end
, tm
->tm_min
, default_dec02_spec
);
1599 return number(buf
, end
, tm
->tm_sec
, default_dec02_spec
);
1602 static noinline_for_stack
1603 char *rtc_str(char *buf
, char *end
, const struct rtc_time
*tm
, const char *fmt
)
1605 bool have_t
= true, have_d
= true;
1609 switch (fmt
[count
]) {
1620 raw
= fmt
[count
] == 'r';
1623 buf
= date_str(buf
, end
, tm
, raw
);
1624 if (have_d
&& have_t
) {
1625 /* Respect ISO 8601 */
1631 buf
= time_str(buf
, end
, tm
, raw
);
1636 static noinline_for_stack
1637 char *time_and_date(char *buf
, char *end
, void *ptr
, struct printf_spec spec
,
1642 return rtc_str(buf
, end
, (const struct rtc_time
*)ptr
, fmt
);
1644 return ptr_to_id(buf
, end
, ptr
, spec
);
1648 static noinline_for_stack
1649 char *clock(char *buf
, char *end
, struct clk
*clk
, struct printf_spec spec
,
1652 if (!IS_ENABLED(CONFIG_HAVE_CLK
) || !clk
)
1653 return string(buf
, end
, NULL
, spec
);
1658 #ifdef CONFIG_COMMON_CLK
1659 return string(buf
, end
, __clk_get_name(clk
), spec
);
1661 return ptr_to_id(buf
, end
, clk
, spec
);
1667 char *format_flags(char *buf
, char *end
, unsigned long flags
,
1668 const struct trace_print_flags
*names
)
1672 for ( ; flags
&& names
->name
; names
++) {
1674 if ((flags
& mask
) != mask
)
1677 buf
= string(buf
, end
, names
->name
, default_str_spec
);
1688 buf
= number(buf
, end
, flags
, default_flag_spec
);
1693 static noinline_for_stack
1694 char *flags_string(char *buf
, char *end
, void *flags_ptr
, const char *fmt
)
1696 unsigned long flags
;
1697 const struct trace_print_flags
*names
;
1701 flags
= *(unsigned long *)flags_ptr
;
1702 /* Remove zone id */
1703 flags
&= (1UL << NR_PAGEFLAGS
) - 1;
1704 names
= pageflag_names
;
1707 flags
= *(unsigned long *)flags_ptr
;
1708 names
= vmaflag_names
;
1711 flags
= *(gfp_t
*)flags_ptr
;
1712 names
= gfpflag_names
;
1715 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt
[1]);
1719 return format_flags(buf
, end
, flags
, names
);
1722 static const char *device_node_name_for_depth(const struct device_node
*np
, int depth
)
1724 for ( ; np
&& depth
; depth
--)
1727 return kbasename(np
->full_name
);
1730 static noinline_for_stack
1731 char *device_node_gen_full_name(const struct device_node
*np
, char *buf
, char *end
)
1734 const struct device_node
*parent
= np
->parent
;
1736 /* special case for root node */
1738 return string(buf
, end
, "/", default_str_spec
);
1740 for (depth
= 0; parent
->parent
; depth
++)
1741 parent
= parent
->parent
;
1743 for ( ; depth
>= 0; depth
--) {
1744 buf
= string(buf
, end
, "/", default_str_spec
);
1745 buf
= string(buf
, end
, device_node_name_for_depth(np
, depth
),
1751 static noinline_for_stack
1752 char *device_node_string(char *buf
, char *end
, struct device_node
*dn
,
1753 struct printf_spec spec
, const char *fmt
)
1755 char tbuf
[sizeof("xxxx") + 1];
1758 char *buf_start
= buf
;
1759 struct property
*prop
;
1760 bool has_mult
, pass
;
1761 static const struct printf_spec num_spec
= {
1768 struct printf_spec str_spec
= spec
;
1769 str_spec
.field_width
= -1;
1771 if (!IS_ENABLED(CONFIG_OF
))
1772 return string(buf
, end
, "(!OF)", spec
);
1774 if ((unsigned long)dn
< PAGE_SIZE
)
1775 return string(buf
, end
, "(null)", spec
);
1777 /* simple case without anything any more format specifiers */
1779 if (fmt
[0] == '\0' || strcspn(fmt
,"fnpPFcC") > 0)
1782 for (pass
= false; strspn(fmt
,"fnpPFcC"); fmt
++, pass
= true) {
1791 case 'f': /* full_name */
1792 buf
= device_node_gen_full_name(dn
, buf
, end
);
1794 case 'n': /* name */
1795 p
= kbasename(of_node_full_name(dn
));
1796 precision
= str_spec
.precision
;
1797 str_spec
.precision
= strchrnul(p
, '@') - p
;
1798 buf
= string(buf
, end
, p
, str_spec
);
1799 str_spec
.precision
= precision
;
1801 case 'p': /* phandle */
1802 buf
= number(buf
, end
, (unsigned int)dn
->phandle
, num_spec
);
1804 case 'P': /* path-spec */
1805 p
= kbasename(of_node_full_name(dn
));
1808 buf
= string(buf
, end
, p
, str_spec
);
1810 case 'F': /* flags */
1811 tbuf
[0] = of_node_check_flag(dn
, OF_DYNAMIC
) ? 'D' : '-';
1812 tbuf
[1] = of_node_check_flag(dn
, OF_DETACHED
) ? 'd' : '-';
1813 tbuf
[2] = of_node_check_flag(dn
, OF_POPULATED
) ? 'P' : '-';
1814 tbuf
[3] = of_node_check_flag(dn
, OF_POPULATED_BUS
) ? 'B' : '-';
1816 buf
= string(buf
, end
, tbuf
, str_spec
);
1818 case 'c': /* major compatible string */
1819 ret
= of_property_read_string(dn
, "compatible", &p
);
1821 buf
= string(buf
, end
, p
, str_spec
);
1823 case 'C': /* full compatible string */
1825 of_property_for_each_string(dn
, "compatible", prop
, p
) {
1827 buf
= string(buf
, end
, ",", str_spec
);
1828 buf
= string(buf
, end
, "\"", str_spec
);
1829 buf
= string(buf
, end
, p
, str_spec
);
1830 buf
= string(buf
, end
, "\"", str_spec
);
1840 return widen_string(buf
, buf
- buf_start
, end
, spec
);
1844 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1845 * by an extra set of alphanumeric characters that are extended format
1848 * Please update scripts/checkpatch.pl when adding/removing conversion
1849 * characters. (Search for "check for vsprintf extension").
1851 * Right now we handle:
1853 * - 'S' For symbolic direct pointers (or function descriptors) with offset
1854 * - 's' For symbolic direct pointers (or function descriptors) without offset
1857 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1858 * - 'B' For backtraced symbolic direct pointers with offset
1859 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1860 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1861 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1862 * width which must be explicitly specified either as part of the
1863 * format string '%32b[l]' or through '%*b[l]', [l] selects
1864 * range-list format instead of hex format
1865 * - 'M' For a 6-byte MAC address, it prints the address in the
1866 * usual colon-separated hex notation
1867 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1868 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1869 * with a dash-separated hex notation
1870 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1871 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1872 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1873 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1875 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1876 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1877 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1878 * IPv6 omits the colons (01020304...0f)
1879 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1881 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1882 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1883 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1884 * - 'I[6S]c' for IPv6 addresses printed as specified by
1885 * http://tools.ietf.org/html/rfc5952
1886 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1887 * of the following flags (see string_escape_mem() for the
1890 * c - ESCAPE_SPECIAL
1896 * By default ESCAPE_ANY_NP is used.
1897 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1898 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1899 * Options for %pU are:
1900 * b big endian lower case hex (default)
1901 * B big endian UPPER case hex
1902 * l little endian lower case hex
1903 * L little endian UPPER case hex
1904 * big endian output byte order is:
1905 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1906 * little endian output byte order is:
1907 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1908 * - 'V' For a struct va_format which contains a format string * and va_list *,
1909 * call vsnprintf(->format, *->va_list).
1910 * Implements a "recursive vsnprintf".
1911 * Do not use this feature without some mechanism to verify the
1912 * correctness of the format string and va_list arguments.
1913 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1914 * - 'NF' For a netdev_features_t
1915 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1916 * a certain separator (' ' by default):
1920 * The maximum supported length is 64 bytes of the input. Consider
1921 * to use print_hex_dump() for the larger input.
1922 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1923 * (default assumed to be phys_addr_t, passed by reference)
1924 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1925 * - 'D[234]' Same as 'd' but for a struct file
1926 * - 'g' For block_device name (gendisk + partition number)
1927 * - 't[R][dt][r]' For time and date as represented:
1929 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1930 * (legacy clock framework) of the clock
1931 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1932 * (legacy clock framework) of the clock
1933 * - 'Cr' For a clock, it prints the current rate of the clock
1934 * - 'G' For flags to be printed as a collection of symbolic strings that would
1935 * construct the specific value. Supported flags given by option:
1936 * p page flags (see struct page) given as pointer to unsigned long
1937 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1938 * v vma flags (VM_*) given as pointer to unsigned long
1939 * - 'OF[fnpPcCF]' For a device tree object
1940 * Without any optional arguments prints the full_name
1941 * f device node full_name
1942 * n device node name
1943 * p device node phandle
1944 * P device node path spec (name + @unit)
1945 * F device node flags
1946 * c major compatible string
1947 * C full compatible string
1948 * - 'x' For printing the address. Equivalent to "%lx".
1950 * ** When making changes please also update:
1951 * Documentation/core-api/printk-formats.rst
1953 * Note: The default behaviour (unadorned %p) is to hash the address,
1954 * rendering it useful as a unique identifier.
1956 static noinline_for_stack
1957 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
1958 struct printf_spec spec
)
1960 const int default_width
= 2 * sizeof(void *);
1962 if (!ptr
&& *fmt
!= 'K' && *fmt
!= 'x') {
1964 * Print (null) with the same width as a pointer so it makes
1965 * tabular output look nice.
1967 if (spec
.field_width
== -1)
1968 spec
.field_width
= default_width
;
1969 return string(buf
, end
, "(null)", spec
);
1977 ptr
= dereference_symbol_descriptor(ptr
);
1980 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
1983 return resource_string(buf
, end
, ptr
, spec
, fmt
);
1985 return hex_string(buf
, end
, ptr
, spec
, fmt
);
1989 return bitmap_list_string(buf
, end
, ptr
, spec
, fmt
);
1991 return bitmap_string(buf
, end
, ptr
, spec
, fmt
);
1993 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1994 case 'm': /* Contiguous: 000102030405 */
1996 /* [mM]R (Reverse order; Bluetooth) */
1997 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
1998 case 'I': /* Formatted IP supported
2000 * 6: 0001:0203:...:0708
2001 * 6c: 1::708 or 1::1.2.3.4
2003 case 'i': /* Contiguous:
2004 * 4: 001.002.003.004
2009 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
2011 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
2014 struct sockaddr raw
;
2015 struct sockaddr_in v4
;
2016 struct sockaddr_in6 v6
;
2019 switch (sa
->raw
.sa_family
) {
2021 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
2023 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
2025 return string(buf
, end
, "(invalid address)", spec
);
2030 return escaped_string(buf
, end
, ptr
, spec
, fmt
);
2032 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
2037 va_copy(va
, *((struct va_format
*)ptr
)->va
);
2038 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0,
2039 ((struct va_format
*)ptr
)->fmt
, va
);
2046 return restricted_pointer(buf
, end
, ptr
, spec
);
2048 return netdev_bits(buf
, end
, ptr
, spec
, fmt
);
2050 return address_val(buf
, end
, ptr
, fmt
);
2052 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
2054 return time_and_date(buf
, end
, ptr
, spec
, fmt
);
2056 return clock(buf
, end
, ptr
, spec
, fmt
);
2058 return dentry_name(buf
, end
,
2059 ((const struct file
*)ptr
)->f_path
.dentry
,
2063 return bdev_name(buf
, end
, ptr
, spec
, fmt
);
2067 return flags_string(buf
, end
, ptr
, fmt
);
2071 return device_node_string(buf
, end
, ptr
, spec
, fmt
+ 1);
2075 return pointer_string(buf
, end
, ptr
, spec
);
2078 /* default is to _not_ leak addresses, hash before printing */
2079 return ptr_to_id(buf
, end
, ptr
, spec
);
2083 * Helper function to decode printf style format.
2084 * Each call decode a token from the format and return the
2085 * number of characters read (or likely the delta where it wants
2086 * to go on the next call).
2087 * The decoded token is returned through the parameters
2089 * 'h', 'l', or 'L' for integer fields
2090 * 'z' support added 23/7/1999 S.H.
2091 * 'z' changed to 'Z' --davidm 1/25/99
2092 * 'Z' changed to 'z' --adobriyan 2017-01-25
2093 * 't' added for ptrdiff_t
2095 * @fmt: the format string
2096 * @type of the token returned
2097 * @flags: various flags such as +, -, # tokens..
2098 * @field_width: overwritten width
2099 * @base: base of the number (octal, hex, ...)
2100 * @precision: precision of a number
2101 * @qualifier: qualifier of a number (long, size_t, ...)
2103 static noinline_for_stack
2104 int format_decode(const char *fmt
, struct printf_spec
*spec
)
2106 const char *start
= fmt
;
2109 /* we finished early by reading the field width */
2110 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
2111 if (spec
->field_width
< 0) {
2112 spec
->field_width
= -spec
->field_width
;
2113 spec
->flags
|= LEFT
;
2115 spec
->type
= FORMAT_TYPE_NONE
;
2119 /* we finished early by reading the precision */
2120 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
2121 if (spec
->precision
< 0)
2122 spec
->precision
= 0;
2124 spec
->type
= FORMAT_TYPE_NONE
;
2129 spec
->type
= FORMAT_TYPE_NONE
;
2131 for (; *fmt
; ++fmt
) {
2136 /* Return the current non-format string */
2137 if (fmt
!= start
|| !*fmt
)
2143 while (1) { /* this also skips first '%' */
2149 case '-': spec
->flags
|= LEFT
; break;
2150 case '+': spec
->flags
|= PLUS
; break;
2151 case ' ': spec
->flags
|= SPACE
; break;
2152 case '#': spec
->flags
|= SPECIAL
; break;
2153 case '0': spec
->flags
|= ZEROPAD
; break;
2154 default: found
= false;
2161 /* get field width */
2162 spec
->field_width
= -1;
2165 spec
->field_width
= skip_atoi(&fmt
);
2166 else if (*fmt
== '*') {
2167 /* it's the next argument */
2168 spec
->type
= FORMAT_TYPE_WIDTH
;
2169 return ++fmt
- start
;
2173 /* get the precision */
2174 spec
->precision
= -1;
2177 if (isdigit(*fmt
)) {
2178 spec
->precision
= skip_atoi(&fmt
);
2179 if (spec
->precision
< 0)
2180 spec
->precision
= 0;
2181 } else if (*fmt
== '*') {
2182 /* it's the next argument */
2183 spec
->type
= FORMAT_TYPE_PRECISION
;
2184 return ++fmt
- start
;
2189 /* get the conversion qualifier */
2191 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2192 *fmt
== 'z' || *fmt
== 't') {
2194 if (unlikely(qualifier
== *fmt
)) {
2195 if (qualifier
== 'l') {
2198 } else if (qualifier
== 'h') {
2209 spec
->type
= FORMAT_TYPE_CHAR
;
2210 return ++fmt
- start
;
2213 spec
->type
= FORMAT_TYPE_STR
;
2214 return ++fmt
- start
;
2217 spec
->type
= FORMAT_TYPE_PTR
;
2218 return ++fmt
- start
;
2221 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
2222 return ++fmt
- start
;
2224 /* integer number formats - set up the flags and "break" */
2230 spec
->flags
|= SMALL
;
2239 spec
->flags
|= SIGN
;
2245 * Since %n poses a greater security risk than
2246 * utility, treat it as any other invalid or
2247 * unsupported format specifier.
2252 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt
);
2253 spec
->type
= FORMAT_TYPE_INVALID
;
2257 if (qualifier
== 'L')
2258 spec
->type
= FORMAT_TYPE_LONG_LONG
;
2259 else if (qualifier
== 'l') {
2260 BUILD_BUG_ON(FORMAT_TYPE_ULONG
+ SIGN
!= FORMAT_TYPE_LONG
);
2261 spec
->type
= FORMAT_TYPE_ULONG
+ (spec
->flags
& SIGN
);
2262 } else if (qualifier
== 'z') {
2263 spec
->type
= FORMAT_TYPE_SIZE_T
;
2264 } else if (qualifier
== 't') {
2265 spec
->type
= FORMAT_TYPE_PTRDIFF
;
2266 } else if (qualifier
== 'H') {
2267 BUILD_BUG_ON(FORMAT_TYPE_UBYTE
+ SIGN
!= FORMAT_TYPE_BYTE
);
2268 spec
->type
= FORMAT_TYPE_UBYTE
+ (spec
->flags
& SIGN
);
2269 } else if (qualifier
== 'h') {
2270 BUILD_BUG_ON(FORMAT_TYPE_USHORT
+ SIGN
!= FORMAT_TYPE_SHORT
);
2271 spec
->type
= FORMAT_TYPE_USHORT
+ (spec
->flags
& SIGN
);
2273 BUILD_BUG_ON(FORMAT_TYPE_UINT
+ SIGN
!= FORMAT_TYPE_INT
);
2274 spec
->type
= FORMAT_TYPE_UINT
+ (spec
->flags
& SIGN
);
2277 return ++fmt
- start
;
2281 set_field_width(struct printf_spec
*spec
, int width
)
2283 spec
->field_width
= width
;
2284 if (WARN_ONCE(spec
->field_width
!= width
, "field width %d too large", width
)) {
2285 spec
->field_width
= clamp(width
, -FIELD_WIDTH_MAX
, FIELD_WIDTH_MAX
);
2290 set_precision(struct printf_spec
*spec
, int prec
)
2292 spec
->precision
= prec
;
2293 if (WARN_ONCE(spec
->precision
!= prec
, "precision %d too large", prec
)) {
2294 spec
->precision
= clamp(prec
, 0, PRECISION_MAX
);
2299 * vsnprintf - Format a string and place it in a buffer
2300 * @buf: The buffer to place the result into
2301 * @size: The size of the buffer, including the trailing null space
2302 * @fmt: The format string to use
2303 * @args: Arguments for the format string
2305 * This function generally follows C99 vsnprintf, but has some
2306 * extensions and a few limitations:
2308 * - ``%n`` is unsupported
2309 * - ``%p*`` is handled by pointer()
2311 * See pointer() or Documentation/core-api/printk-formats.rst for more
2312 * extensive description.
2314 * **Please update the documentation in both places when making changes**
2316 * The return value is the number of characters which would
2317 * be generated for the given input, excluding the trailing
2318 * '\0', as per ISO C99. If you want to have the exact
2319 * number of characters written into @buf as return value
2320 * (not including the trailing '\0'), use vscnprintf(). If the
2321 * return is greater than or equal to @size, the resulting
2322 * string is truncated.
2324 * If you're not already dealing with a va_list consider using snprintf().
2326 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2328 unsigned long long num
;
2330 struct printf_spec spec
= {0};
2332 /* Reject out-of-range values early. Large positive sizes are
2333 used for unknown buffer sizes. */
2334 if (WARN_ON_ONCE(size
> INT_MAX
))
2340 /* Make sure end is always >= buf */
2347 const char *old_fmt
= fmt
;
2348 int read
= format_decode(fmt
, &spec
);
2352 switch (spec
.type
) {
2353 case FORMAT_TYPE_NONE
: {
2356 if (copy
> end
- str
)
2358 memcpy(str
, old_fmt
, copy
);
2364 case FORMAT_TYPE_WIDTH
:
2365 set_field_width(&spec
, va_arg(args
, int));
2368 case FORMAT_TYPE_PRECISION
:
2369 set_precision(&spec
, va_arg(args
, int));
2372 case FORMAT_TYPE_CHAR
: {
2375 if (!(spec
.flags
& LEFT
)) {
2376 while (--spec
.field_width
> 0) {
2383 c
= (unsigned char) va_arg(args
, int);
2387 while (--spec
.field_width
> 0) {
2395 case FORMAT_TYPE_STR
:
2396 str
= string(str
, end
, va_arg(args
, char *), spec
);
2399 case FORMAT_TYPE_PTR
:
2400 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2402 while (isalnum(*fmt
))
2406 case FORMAT_TYPE_PERCENT_CHAR
:
2412 case FORMAT_TYPE_INVALID
:
2414 * Presumably the arguments passed gcc's type
2415 * checking, but there is no safe or sane way
2416 * for us to continue parsing the format and
2417 * fetching from the va_list; the remaining
2418 * specifiers and arguments would be out of
2424 switch (spec
.type
) {
2425 case FORMAT_TYPE_LONG_LONG
:
2426 num
= va_arg(args
, long long);
2428 case FORMAT_TYPE_ULONG
:
2429 num
= va_arg(args
, unsigned long);
2431 case FORMAT_TYPE_LONG
:
2432 num
= va_arg(args
, long);
2434 case FORMAT_TYPE_SIZE_T
:
2435 if (spec
.flags
& SIGN
)
2436 num
= va_arg(args
, ssize_t
);
2438 num
= va_arg(args
, size_t);
2440 case FORMAT_TYPE_PTRDIFF
:
2441 num
= va_arg(args
, ptrdiff_t);
2443 case FORMAT_TYPE_UBYTE
:
2444 num
= (unsigned char) va_arg(args
, int);
2446 case FORMAT_TYPE_BYTE
:
2447 num
= (signed char) va_arg(args
, int);
2449 case FORMAT_TYPE_USHORT
:
2450 num
= (unsigned short) va_arg(args
, int);
2452 case FORMAT_TYPE_SHORT
:
2453 num
= (short) va_arg(args
, int);
2455 case FORMAT_TYPE_INT
:
2456 num
= (int) va_arg(args
, int);
2459 num
= va_arg(args
, unsigned int);
2462 str
= number(str
, end
, num
, spec
);
2474 /* the trailing null byte doesn't count towards the total */
2478 EXPORT_SYMBOL(vsnprintf
);
2481 * vscnprintf - Format a string and place it in a buffer
2482 * @buf: The buffer to place the result into
2483 * @size: The size of the buffer, including the trailing null space
2484 * @fmt: The format string to use
2485 * @args: Arguments for the format string
2487 * The return value is the number of characters which have been written into
2488 * the @buf not including the trailing '\0'. If @size is == 0 the function
2491 * If you're not already dealing with a va_list consider using scnprintf().
2493 * See the vsnprintf() documentation for format string extensions over C99.
2495 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2499 i
= vsnprintf(buf
, size
, fmt
, args
);
2501 if (likely(i
< size
))
2507 EXPORT_SYMBOL(vscnprintf
);
2510 * snprintf - Format a string and place it in a buffer
2511 * @buf: The buffer to place the result into
2512 * @size: The size of the buffer, including the trailing null space
2513 * @fmt: The format string to use
2514 * @...: Arguments for the format string
2516 * The return value is the number of characters which would be
2517 * generated for the given input, excluding the trailing null,
2518 * as per ISO C99. If the return is greater than or equal to
2519 * @size, the resulting string is truncated.
2521 * See the vsnprintf() documentation for format string extensions over C99.
2523 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
2528 va_start(args
, fmt
);
2529 i
= vsnprintf(buf
, size
, fmt
, args
);
2534 EXPORT_SYMBOL(snprintf
);
2537 * scnprintf - Format a string and place it in a buffer
2538 * @buf: The buffer to place the result into
2539 * @size: The size of the buffer, including the trailing null space
2540 * @fmt: The format string to use
2541 * @...: Arguments for the format string
2543 * The return value is the number of characters written into @buf not including
2544 * the trailing '\0'. If @size is == 0 the function returns 0.
2547 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
2552 va_start(args
, fmt
);
2553 i
= vscnprintf(buf
, size
, fmt
, args
);
2558 EXPORT_SYMBOL(scnprintf
);
2561 * vsprintf - Format a string and place it in a buffer
2562 * @buf: The buffer to place the result into
2563 * @fmt: The format string to use
2564 * @args: Arguments for the format string
2566 * The function returns the number of characters written
2567 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2570 * If you're not already dealing with a va_list consider using sprintf().
2572 * See the vsnprintf() documentation for format string extensions over C99.
2574 int vsprintf(char *buf
, const char *fmt
, va_list args
)
2576 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
2578 EXPORT_SYMBOL(vsprintf
);
2581 * sprintf - Format a string and place it in a buffer
2582 * @buf: The buffer to place the result into
2583 * @fmt: The format string to use
2584 * @...: Arguments for the format string
2586 * The function returns the number of characters written
2587 * into @buf. Use snprintf() or scnprintf() in order to avoid
2590 * See the vsnprintf() documentation for format string extensions over C99.
2592 int sprintf(char *buf
, const char *fmt
, ...)
2597 va_start(args
, fmt
);
2598 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
2603 EXPORT_SYMBOL(sprintf
);
2605 #ifdef CONFIG_BINARY_PRINTF
2608 * vbin_printf() - VA arguments to binary data
2609 * bstr_printf() - Binary data to text string
2613 * vbin_printf - Parse a format string and place args' binary value in a buffer
2614 * @bin_buf: The buffer to place args' binary value
2615 * @size: The size of the buffer(by words(32bits), not characters)
2616 * @fmt: The format string to use
2617 * @args: Arguments for the format string
2619 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2622 * The return value is the number of words(32bits) which would be generated for
2626 * If the return value is greater than @size, the resulting bin_buf is NOT
2627 * valid for bstr_printf().
2629 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
2631 struct printf_spec spec
= {0};
2635 str
= (char *)bin_buf
;
2636 end
= (char *)(bin_buf
+ size
);
2638 #define save_arg(type) \
2640 unsigned long long value; \
2641 if (sizeof(type) == 8) { \
2642 unsigned long long val8; \
2643 str = PTR_ALIGN(str, sizeof(u32)); \
2644 val8 = va_arg(args, unsigned long long); \
2645 if (str + sizeof(type) <= end) { \
2646 *(u32 *)str = *(u32 *)&val8; \
2647 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2651 unsigned int val4; \
2652 str = PTR_ALIGN(str, sizeof(type)); \
2653 val4 = va_arg(args, int); \
2654 if (str + sizeof(type) <= end) \
2655 *(typeof(type) *)str = (type)(long)val4; \
2656 value = (unsigned long long)val4; \
2658 str += sizeof(type); \
2663 int read
= format_decode(fmt
, &spec
);
2667 switch (spec
.type
) {
2668 case FORMAT_TYPE_NONE
:
2669 case FORMAT_TYPE_PERCENT_CHAR
:
2671 case FORMAT_TYPE_INVALID
:
2674 case FORMAT_TYPE_WIDTH
:
2675 case FORMAT_TYPE_PRECISION
:
2676 width
= (int)save_arg(int);
2677 /* Pointers may require the width */
2679 set_field_width(&spec
, width
);
2682 case FORMAT_TYPE_CHAR
:
2686 case FORMAT_TYPE_STR
: {
2687 const char *save_str
= va_arg(args
, char *);
2690 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
2691 || (unsigned long)save_str
< PAGE_SIZE
)
2692 save_str
= "(null)";
2693 len
= strlen(save_str
) + 1;
2694 if (str
+ len
< end
)
2695 memcpy(str
, save_str
, len
);
2700 case FORMAT_TYPE_PTR
:
2701 /* Dereferenced pointers must be done now */
2703 /* Dereference of functions is still OK */
2713 if (!isalnum(*fmt
)) {
2717 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2722 end
[-1] = '\0'; /* Must be nul terminated */
2724 /* skip all alphanumeric pointer suffixes */
2725 while (isalnum(*fmt
))
2730 switch (spec
.type
) {
2732 case FORMAT_TYPE_LONG_LONG
:
2733 save_arg(long long);
2735 case FORMAT_TYPE_ULONG
:
2736 case FORMAT_TYPE_LONG
:
2737 save_arg(unsigned long);
2739 case FORMAT_TYPE_SIZE_T
:
2742 case FORMAT_TYPE_PTRDIFF
:
2743 save_arg(ptrdiff_t);
2745 case FORMAT_TYPE_UBYTE
:
2746 case FORMAT_TYPE_BYTE
:
2749 case FORMAT_TYPE_USHORT
:
2750 case FORMAT_TYPE_SHORT
:
2760 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2763 EXPORT_SYMBOL_GPL(vbin_printf
);
2766 * bstr_printf - Format a string from binary arguments and place it in a buffer
2767 * @buf: The buffer to place the result into
2768 * @size: The size of the buffer, including the trailing null space
2769 * @fmt: The format string to use
2770 * @bin_buf: Binary arguments for the format string
2772 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2773 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2774 * a binary buffer that generated by vbin_printf.
2776 * The format follows C99 vsnprintf, but has some extensions:
2777 * see vsnprintf comment for details.
2779 * The return value is the number of characters which would
2780 * be generated for the given input, excluding the trailing
2781 * '\0', as per ISO C99. If you want to have the exact
2782 * number of characters written into @buf as return value
2783 * (not including the trailing '\0'), use vscnprintf(). If the
2784 * return is greater than or equal to @size, the resulting
2785 * string is truncated.
2787 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2789 struct printf_spec spec
= {0};
2791 const char *args
= (const char *)bin_buf
;
2793 if (WARN_ON_ONCE(size
> INT_MAX
))
2799 #define get_arg(type) \
2801 typeof(type) value; \
2802 if (sizeof(type) == 8) { \
2803 args = PTR_ALIGN(args, sizeof(u32)); \
2804 *(u32 *)&value = *(u32 *)args; \
2805 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2807 args = PTR_ALIGN(args, sizeof(type)); \
2808 value = *(typeof(type) *)args; \
2810 args += sizeof(type); \
2814 /* Make sure end is always >= buf */
2821 const char *old_fmt
= fmt
;
2822 int read
= format_decode(fmt
, &spec
);
2826 switch (spec
.type
) {
2827 case FORMAT_TYPE_NONE
: {
2830 if (copy
> end
- str
)
2832 memcpy(str
, old_fmt
, copy
);
2838 case FORMAT_TYPE_WIDTH
:
2839 set_field_width(&spec
, get_arg(int));
2842 case FORMAT_TYPE_PRECISION
:
2843 set_precision(&spec
, get_arg(int));
2846 case FORMAT_TYPE_CHAR
: {
2849 if (!(spec
.flags
& LEFT
)) {
2850 while (--spec
.field_width
> 0) {
2856 c
= (unsigned char) get_arg(char);
2860 while (--spec
.field_width
> 0) {
2868 case FORMAT_TYPE_STR
: {
2869 const char *str_arg
= args
;
2870 args
+= strlen(str_arg
) + 1;
2871 str
= string(str
, end
, (char *)str_arg
, spec
);
2875 case FORMAT_TYPE_PTR
: {
2876 bool process
= false;
2878 /* Non function dereferences were already done */
2889 if (!isalnum(*fmt
)) {
2893 /* Pointer dereference was already processed */
2895 len
= copy
= strlen(args
);
2896 if (copy
> end
- str
)
2898 memcpy(str
, args
, copy
);
2904 str
= pointer(fmt
, str
, end
, get_arg(void *), spec
);
2906 while (isalnum(*fmt
))
2911 case FORMAT_TYPE_PERCENT_CHAR
:
2917 case FORMAT_TYPE_INVALID
:
2921 unsigned long long num
;
2923 switch (spec
.type
) {
2925 case FORMAT_TYPE_LONG_LONG
:
2926 num
= get_arg(long long);
2928 case FORMAT_TYPE_ULONG
:
2929 case FORMAT_TYPE_LONG
:
2930 num
= get_arg(unsigned long);
2932 case FORMAT_TYPE_SIZE_T
:
2933 num
= get_arg(size_t);
2935 case FORMAT_TYPE_PTRDIFF
:
2936 num
= get_arg(ptrdiff_t);
2938 case FORMAT_TYPE_UBYTE
:
2939 num
= get_arg(unsigned char);
2941 case FORMAT_TYPE_BYTE
:
2942 num
= get_arg(signed char);
2944 case FORMAT_TYPE_USHORT
:
2945 num
= get_arg(unsigned short);
2947 case FORMAT_TYPE_SHORT
:
2948 num
= get_arg(short);
2950 case FORMAT_TYPE_UINT
:
2951 num
= get_arg(unsigned int);
2957 str
= number(str
, end
, num
, spec
);
2959 } /* switch(spec.type) */
2972 /* the trailing null byte doesn't count towards the total */
2975 EXPORT_SYMBOL_GPL(bstr_printf
);
2978 * bprintf - Parse a format string and place args' binary value in a buffer
2979 * @bin_buf: The buffer to place args' binary value
2980 * @size: The size of the buffer(by words(32bits), not characters)
2981 * @fmt: The format string to use
2982 * @...: Arguments for the format string
2984 * The function returns the number of words(u32) written
2987 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
2992 va_start(args
, fmt
);
2993 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
2998 EXPORT_SYMBOL_GPL(bprintf
);
3000 #endif /* CONFIG_BINARY_PRINTF */
3003 * vsscanf - Unformat a buffer into a list of arguments
3004 * @buf: input buffer
3005 * @fmt: format of buffer
3008 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
3010 const char *str
= buf
;
3018 unsigned long long u
;
3024 /* skip any white space in format */
3025 /* white space in format matchs any amount of
3026 * white space, including none, in the input.
3028 if (isspace(*fmt
)) {
3029 fmt
= skip_spaces(++fmt
);
3030 str
= skip_spaces(str
);
3033 /* anything that is not a conversion must match exactly */
3034 if (*fmt
!= '%' && *fmt
) {
3035 if (*fmt
++ != *str
++)
3044 /* skip this conversion.
3045 * advance both strings to next white space
3050 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
) {
3051 /* '%*[' not yet supported, invalid format */
3056 while (!isspace(*str
) && *str
)
3061 /* get field width */
3063 if (isdigit(*fmt
)) {
3064 field_width
= skip_atoi(&fmt
);
3065 if (field_width
<= 0)
3069 /* get conversion qualifier */
3071 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
3074 if (unlikely(qualifier
== *fmt
)) {
3075 if (qualifier
== 'h') {
3078 } else if (qualifier
== 'l') {
3089 /* return number of characters read so far */
3090 *va_arg(args
, int *) = str
- buf
;
3104 char *s
= (char *)va_arg(args
, char*);
3105 if (field_width
== -1)
3109 } while (--field_width
> 0 && *str
);
3115 char *s
= (char *)va_arg(args
, char *);
3116 if (field_width
== -1)
3117 field_width
= SHRT_MAX
;
3118 /* first, skip leading white space in buffer */
3119 str
= skip_spaces(str
);
3121 /* now copy until next white space */
3122 while (*str
&& !isspace(*str
) && field_width
--)
3129 * Warning: This implementation of the '[' conversion specifier
3130 * deviates from its glibc counterpart in the following ways:
3131 * (1) It does NOT support ranges i.e. '-' is NOT a special
3133 * (2) It cannot match the closing bracket ']' itself
3134 * (3) A field width is required
3135 * (4) '%*[' (discard matching input) is currently not supported
3138 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3139 * buf1, buf2, buf3);
3145 char *s
= (char *)va_arg(args
, char *);
3146 DECLARE_BITMAP(set
, 256) = {0};
3147 unsigned int len
= 0;
3148 bool negate
= (*fmt
== '^');
3150 /* field width is required */
3151 if (field_width
== -1)
3157 for ( ; *fmt
&& *fmt
!= ']'; ++fmt
, ++len
)
3158 set_bit((u8
)*fmt
, set
);
3160 /* no ']' or no character set found */
3166 bitmap_complement(set
, set
, 256);
3167 /* exclude null '\0' byte */
3171 /* match must be non-empty */
3172 if (!test_bit((u8
)*str
, set
))
3175 while (test_bit((u8
)*str
, set
) && field_width
--)
3197 /* looking for '%' in str */
3202 /* invalid format; stop here */
3206 /* have some sort of integer conversion.
3207 * first, skip white space in buffer.
3209 str
= skip_spaces(str
);
3212 if (is_sign
&& digit
== '-')
3216 || (base
== 16 && !isxdigit(digit
))
3217 || (base
== 10 && !isdigit(digit
))
3218 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
3219 || (base
== 0 && !isdigit(digit
)))
3223 val
.s
= qualifier
!= 'L' ?
3224 simple_strtol(str
, &next
, base
) :
3225 simple_strtoll(str
, &next
, base
);
3227 val
.u
= qualifier
!= 'L' ?
3228 simple_strtoul(str
, &next
, base
) :
3229 simple_strtoull(str
, &next
, base
);
3231 if (field_width
> 0 && next
- str
> field_width
) {
3233 _parse_integer_fixup_radix(str
, &base
);
3234 while (next
- str
> field_width
) {
3236 val
.s
= div_s64(val
.s
, base
);
3238 val
.u
= div_u64(val
.u
, base
);
3243 switch (qualifier
) {
3244 case 'H': /* that's 'hh' in format */
3246 *va_arg(args
, signed char *) = val
.s
;
3248 *va_arg(args
, unsigned char *) = val
.u
;
3252 *va_arg(args
, short *) = val
.s
;
3254 *va_arg(args
, unsigned short *) = val
.u
;
3258 *va_arg(args
, long *) = val
.s
;
3260 *va_arg(args
, unsigned long *) = val
.u
;
3264 *va_arg(args
, long long *) = val
.s
;
3266 *va_arg(args
, unsigned long long *) = val
.u
;
3269 *va_arg(args
, size_t *) = val
.u
;
3273 *va_arg(args
, int *) = val
.s
;
3275 *va_arg(args
, unsigned int *) = val
.u
;
3287 EXPORT_SYMBOL(vsscanf
);
3290 * sscanf - Unformat a buffer into a list of arguments
3291 * @buf: input buffer
3292 * @fmt: formatting of buffer
3293 * @...: resulting arguments
3295 int sscanf(const char *buf
, const char *fmt
, ...)
3300 va_start(args
, fmt
);
3301 i
= vsscanf(buf
, fmt
, args
);
3306 EXPORT_SYMBOL(sscanf
);