1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
10 * Wirzenius wrote this portably, Torvalds fucked it up :-)
14 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
15 * - changed to provide snprintf and vsnprintf functions
16 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
17 * - scnprintf and vscnprintf
21 #include <linux/build_bug.h>
22 #include <linux/clk.h>
23 #include <linux/clk-provider.h>
24 #include <linux/errname.h>
25 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
26 #include <linux/types.h>
27 #include <linux/string.h>
28 #include <linux/ctype.h>
29 #include <linux/kernel.h>
30 #include <linux/kallsyms.h>
31 #include <linux/math64.h>
32 #include <linux/uaccess.h>
33 #include <linux/ioport.h>
34 #include <linux/dcache.h>
35 #include <linux/cred.h>
36 #include <linux/rtc.h>
37 #include <linux/uuid.h>
39 #include <net/addrconf.h>
40 #include <linux/siphash.h>
41 #include <linux/compiler.h>
43 #include <linux/blkdev.h>
46 #include "../mm/internal.h" /* For the trace_print_flags arrays */
48 #include <asm/page.h> /* for PAGE_SIZE */
49 #include <asm/byteorder.h> /* cpu_to_le16 */
51 #include <linux/string_helpers.h>
55 * simple_strtoull - convert a string to an unsigned long long
56 * @cp: The start of the string
57 * @endp: A pointer to the end of the parsed string will be placed here
58 * @base: The number base to use
60 * This function is obsolete. Please use kstrtoull instead.
62 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
64 unsigned long long result;
67 cp = _parse_integer_fixup_radix(cp, &base);
68 rv = _parse_integer(cp, base, &result);
70 cp += (rv & ~KSTRTOX_OVERFLOW);
77 EXPORT_SYMBOL(simple_strtoull);
80 * simple_strtoul - convert a string to an unsigned long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
85 * This function is obsolete. Please use kstrtoul instead.
87 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
89 return simple_strtoull(cp, endp, base);
91 EXPORT_SYMBOL(simple_strtoul);
94 * simple_strtol - convert a string to a signed long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
99 * This function is obsolete. Please use kstrtol instead.
101 long simple_strtol(const char *cp, char **endp, unsigned int base)
104 return -simple_strtoul(cp + 1, endp, base);
106 return simple_strtoul(cp, endp, base);
108 EXPORT_SYMBOL(simple_strtol);
111 * simple_strtoll - convert a string to a signed long long
112 * @cp: The start of the string
113 * @endp: A pointer to the end of the parsed string will be placed here
114 * @base: The number base to use
116 * This function is obsolete. Please use kstrtoll instead.
118 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
121 return -simple_strtoull(cp + 1, endp, base);
123 return simple_strtoull(cp, endp, base);
125 EXPORT_SYMBOL(simple_strtoll);
127 static noinline_for_stack
128 int skip_atoi(const char **s)
133 i = i*10 + *((*s)++) - '0';
134 } while (isdigit(**s));
140 * Decimal conversion is by far the most typical, and is used for
141 * /proc and /sys data. This directly impacts e.g. top performance
142 * with many processes running. We optimize it for speed by emitting
143 * two characters at a time, using a 200 byte lookup table. This
144 * roughly halves the number of multiplications compared to computing
145 * the digits one at a time. Implementation strongly inspired by the
146 * previous version, which in turn used ideas described at
147 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
148 * from the author, Douglas W. Jones).
150 * It turns out there is precisely one 26 bit fixed-point
151 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
152 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
153 * range happens to be somewhat larger (x <= 1073741898), but that's
154 * irrelevant for our purpose.
156 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
157 * need a 32x32->64 bit multiply, so we simply use the same constant.
159 * For dividing a number in the range [100, 10^4-1] by 100, there are
160 * several options. The simplest is (x * 0x147b) >> 19, which is valid
161 * for all x <= 43698.
164 static const u16 decpair[100] = {
165 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
166 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
167 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
168 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
169 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
170 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
171 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
172 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
173 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
174 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
175 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
180 * This will print a single '0' even if r == 0, since we would
181 * immediately jump to out_r where two 0s would be written but only
182 * one of them accounted for in buf. This is needed by ip4_string
183 * below. All other callers pass a non-zero value of r.
185 static noinline_for_stack
186 char *put_dec_trunc8(char *buf, unsigned r)
194 /* 100 <= r < 10^8 */
195 q = (r * (u64)0x28f5c29) >> 32;
196 *((u16 *)buf) = decpair[r - 100*q];
203 /* 100 <= q < 10^6 */
204 r = (q * (u64)0x28f5c29) >> 32;
205 *((u16 *)buf) = decpair[q - 100*r];
212 /* 100 <= r < 10^4 */
213 q = (r * 0x147b) >> 19;
214 *((u16 *)buf) = decpair[r - 100*q];
221 *((u16 *)buf) = decpair[r];
222 buf += r < 10 ? 1 : 2;
226 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
227 static noinline_for_stack
228 char *put_dec_full8(char *buf, unsigned r)
233 q = (r * (u64)0x28f5c29) >> 32;
234 *((u16 *)buf) = decpair[r - 100*q];
238 r = (q * (u64)0x28f5c29) >> 32;
239 *((u16 *)buf) = decpair[q - 100*r];
243 q = (r * 0x147b) >> 19;
244 *((u16 *)buf) = decpair[r - 100*q];
248 *((u16 *)buf) = decpair[q];
253 static noinline_for_stack
254 char *put_dec(char *buf, unsigned long long n)
256 if (n >= 100*1000*1000)
257 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
258 /* 1 <= n <= 1.6e11 */
259 if (n >= 100*1000*1000)
260 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
262 return put_dec_trunc8(buf, n);
265 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
268 put_dec_full4(char *buf, unsigned r)
273 q = (r * 0x147b) >> 19;
274 *((u16 *)buf) = decpair[r - 100*q];
277 *((u16 *)buf) = decpair[q];
281 * Call put_dec_full4 on x % 10000, return x / 10000.
282 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
283 * holds for all x < 1,128,869,999. The largest value this
284 * helper will ever be asked to convert is 1,125,520,955.
285 * (second call in the put_dec code, assuming n is all-ones).
287 static noinline_for_stack
288 unsigned put_dec_helper4(char *buf, unsigned x)
290 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
292 put_dec_full4(buf, x - q * 10000);
296 /* Based on code by Douglas W. Jones found at
297 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
298 * (with permission from the author).
299 * Performs no 64-bit division and hence should be fast on 32-bit machines.
302 char *put_dec(char *buf, unsigned long long n)
304 uint32_t d3, d2, d1, q, h;
306 if (n < 100*1000*1000)
307 return put_dec_trunc8(buf, n);
309 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
312 d3 = (h >> 16); /* implicit "& 0xffff" */
314 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
315 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
316 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
317 q = put_dec_helper4(buf, q);
319 q += 7671 * d3 + 9496 * d2 + 6 * d1;
320 q = put_dec_helper4(buf+4, q);
322 q += 4749 * d3 + 42 * d2;
323 q = put_dec_helper4(buf+8, q);
328 buf = put_dec_trunc8(buf, q);
329 else while (buf[-1] == '0')
338 * Convert passed number to decimal string.
339 * Returns the length of string. On buffer overflow, returns 0.
341 * If speed is not important, use snprintf(). It's easy to read the code.
343 int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
345 /* put_dec requires 2-byte alignment of the buffer. */
346 char tmp[sizeof(num) * 3] __aligned(2);
349 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
354 len = put_dec(tmp, num) - tmp;
357 if (len > size || width > size)
362 for (idx = 0; idx < width; idx++)
368 for (idx = 0; idx < len; ++idx)
369 buf[idx + width] = tmp[len - idx - 1];
374 #define SIGN 1 /* unsigned/signed, must be 1 */
375 #define LEFT 2 /* left justified */
376 #define PLUS 4 /* show plus */
377 #define SPACE 8 /* space if plus */
378 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
379 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
380 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
383 FORMAT_TYPE_NONE, /* Just a string part */
385 FORMAT_TYPE_PRECISION,
389 FORMAT_TYPE_PERCENT_CHAR,
391 FORMAT_TYPE_LONG_LONG,
405 unsigned int type:8; /* format_type enum */
406 signed int field_width:24; /* width of output field */
407 unsigned int flags:8; /* flags to number() */
408 unsigned int base:8; /* number base, 8, 10 or 16 only */
409 signed int precision:16; /* # of digits/chars */
411 static_assert(sizeof(struct printf_spec) == 8);
413 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
414 #define PRECISION_MAX ((1 << 15) - 1)
416 static noinline_for_stack
417 char *number(char *buf, char *end, unsigned long long num,
418 struct printf_spec spec)
420 /* put_dec requires 2-byte alignment of the buffer. */
421 char tmp[3 * sizeof(num)] __aligned(2);
424 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
426 bool is_zero = num == 0LL;
427 int field_width = spec.field_width;
428 int precision = spec.precision;
430 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
431 * produces same digits or (maybe lowercased) letters */
432 locase = (spec.flags & SMALL);
433 if (spec.flags & LEFT)
434 spec.flags &= ~ZEROPAD;
436 if (spec.flags & SIGN) {
437 if ((signed long long)num < 0) {
439 num = -(signed long long)num;
441 } else if (spec.flags & PLUS) {
444 } else if (spec.flags & SPACE) {
456 /* generate full string in tmp[], in reverse order */
459 tmp[i++] = hex_asc_upper[num] | locase;
460 else if (spec.base != 10) { /* 8 or 16 */
461 int mask = spec.base - 1;
467 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
470 } else { /* base 10 */
471 i = put_dec(tmp, num) - tmp;
474 /* printing 100 using %2d gives "100", not "00" */
477 /* leading space padding */
478 field_width -= precision;
479 if (!(spec.flags & (ZEROPAD | LEFT))) {
480 while (--field_width >= 0) {
492 /* "0x" / "0" prefix */
494 if (spec.base == 16 || !is_zero) {
499 if (spec.base == 16) {
501 *buf = ('X' | locase);
505 /* zero or space padding */
506 if (!(spec.flags & LEFT)) {
507 char c = ' ' + (spec.flags & ZEROPAD);
508 BUILD_BUG_ON(' ' + ZEROPAD != '0');
509 while (--field_width >= 0) {
515 /* hmm even more zero padding? */
516 while (i <= --precision) {
521 /* actual digits of result */
527 /* trailing space padding */
528 while (--field_width >= 0) {
537 static noinline_for_stack
538 char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
540 struct printf_spec spec;
542 spec.type = FORMAT_TYPE_PTR;
543 spec.field_width = 2 + 2 * size; /* 0x + hex */
544 spec.flags = SPECIAL | SMALL | ZEROPAD;
548 return number(buf, end, num, spec);
551 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
554 if (buf >= end) /* nowhere to put anything */
557 if (size <= spaces) {
558 memset(buf, ' ', size);
562 if (len > size - spaces)
564 memmove(buf + spaces, buf, len);
566 memset(buf, ' ', spaces);
570 * Handle field width padding for a string.
571 * @buf: current buffer position
572 * @n: length of string
573 * @end: end of output buffer
574 * @spec: for field width and flags
575 * Returns: new buffer position after padding.
577 static noinline_for_stack
578 char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
582 if (likely(n >= spec.field_width))
584 /* we want to pad the sucker */
585 spaces = spec.field_width - n;
586 if (!(spec.flags & LEFT)) {
587 move_right(buf - n, end, n, spaces);
598 /* Handle string from a well known address. */
599 static char *string_nocheck(char *buf, char *end, const char *s,
600 struct printf_spec spec)
603 int lim = spec.precision;
614 return widen_string(buf, len, end, spec);
617 static char *err_ptr(char *buf, char *end, void *ptr,
618 struct printf_spec spec)
620 int err = PTR_ERR(ptr);
621 const char *sym = errname(err);
624 return string_nocheck(buf, end, sym, spec);
627 * Somebody passed ERR_PTR(-1234) or some other non-existing
628 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
629 * printing it as its decimal representation.
633 return number(buf, end, err, spec);
636 /* Be careful: error messages must fit into the given buffer. */
637 static char *error_string(char *buf, char *end, const char *s,
638 struct printf_spec spec)
641 * Hard limit to avoid a completely insane messages. It actually
642 * works pretty well because most error messages are in
643 * the many pointer format modifiers.
645 if (spec.precision == -1)
646 spec.precision = 2 * sizeof(void *);
648 return string_nocheck(buf, end, s, spec);
652 * Do not call any complex external code here. Nested printk()/vsprintf()
653 * might cause infinite loops. Failures might break printk() and would
656 static const char *check_pointer_msg(const void *ptr)
661 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
667 static int check_pointer(char **buf, char *end, const void *ptr,
668 struct printf_spec spec)
672 err_msg = check_pointer_msg(ptr);
674 *buf = error_string(*buf, end, err_msg, spec);
681 static noinline_for_stack
682 char *string(char *buf, char *end, const char *s,
683 struct printf_spec spec)
685 if (check_pointer(&buf, end, s, spec))
688 return string_nocheck(buf, end, s, spec);
691 static char *pointer_string(char *buf, char *end,
693 struct printf_spec spec)
697 if (spec.field_width == -1) {
698 spec.field_width = 2 * sizeof(ptr);
699 spec.flags |= ZEROPAD;
702 return number(buf, end, (unsigned long int)ptr, spec);
705 /* Make pointers available for printing early in the boot sequence. */
706 static int debug_boot_weak_hash __ro_after_init;
708 static int __init debug_boot_weak_hash_enable(char *str)
710 debug_boot_weak_hash = 1;
711 pr_info("debug_boot_weak_hash enabled\n");
714 early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
716 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
717 static siphash_key_t ptr_key __read_mostly;
719 static void enable_ptr_key_workfn(struct work_struct *work)
721 get_random_bytes(&ptr_key, sizeof(ptr_key));
722 /* Needs to run from preemptible context */
723 static_branch_disable(¬_filled_random_ptr_key);
726 static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
728 static void fill_random_ptr_key(struct random_ready_callback *unused)
730 /* This may be in an interrupt handler. */
731 queue_work(system_unbound_wq, &enable_ptr_key_work);
734 static struct random_ready_callback random_ready = {
735 .func = fill_random_ptr_key
738 static int __init initialize_ptr_random(void)
740 int key_size = sizeof(ptr_key);
743 /* Use hw RNG if available. */
744 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
745 static_branch_disable(¬_filled_random_ptr_key);
749 ret = add_random_ready_callback(&random_ready);
752 } else if (ret == -EALREADY) {
753 /* This is in preemptible context */
754 enable_ptr_key_workfn(&enable_ptr_key_work);
760 early_initcall(initialize_ptr_random);
762 /* Maps a pointer to a 32 bit unique identifier. */
763 static char *ptr_to_id(char *buf, char *end, const void *ptr,
764 struct printf_spec spec)
766 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
767 unsigned long hashval;
769 /* When debugging early boot use non-cryptographically secure hash. */
770 if (unlikely(debug_boot_weak_hash)) {
771 hashval = hash_long((unsigned long)ptr, 32);
772 return pointer_string(buf, end, (const void *)hashval, spec);
775 if (static_branch_unlikely(¬_filled_random_ptr_key)) {
776 spec.field_width = 2 * sizeof(ptr);
777 /* string length must be less than default_width */
778 return error_string(buf, end, str, spec);
782 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
784 * Mask off the first 32 bits, this makes explicit that we have
785 * modified the address (and 32 bits is plenty for a unique ID).
787 hashval = hashval & 0xffffffff;
789 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
791 return pointer_string(buf, end, (const void *)hashval, spec);
794 int kptr_restrict __read_mostly;
796 static noinline_for_stack
797 char *restricted_pointer(char *buf, char *end, const void *ptr,
798 struct printf_spec spec)
800 switch (kptr_restrict) {
802 /* Handle as %p, hash and do _not_ leak addresses. */
803 return ptr_to_id(buf, end, ptr, spec);
805 const struct cred *cred;
808 * kptr_restrict==1 cannot be used in IRQ context
809 * because its test for CAP_SYSLOG would be meaningless.
811 if (in_irq() || in_serving_softirq() || in_nmi()) {
812 if (spec.field_width == -1)
813 spec.field_width = 2 * sizeof(ptr);
814 return error_string(buf, end, "pK-error", spec);
818 * Only print the real pointer value if the current
819 * process has CAP_SYSLOG and is running with the
820 * same credentials it started with. This is because
821 * access to files is checked at open() time, but %pK
822 * checks permission at read() time. We don't want to
823 * leak pointer values if a binary opens a file using
824 * %pK and then elevates privileges before reading it.
826 cred = current_cred();
827 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
828 !uid_eq(cred->euid, cred->uid) ||
829 !gid_eq(cred->egid, cred->gid))
835 /* Always print 0's for %pK */
840 return pointer_string(buf, end, ptr, spec);
843 static noinline_for_stack
844 char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
847 const char *array[4], *s;
848 const struct dentry *p;
853 case '2': case '3': case '4':
854 depth = fmt[1] - '0';
861 for (i = 0; i < depth; i++, d = p) {
862 if (check_pointer(&buf, end, d, spec)) {
867 p = READ_ONCE(d->d_parent);
868 array[i] = READ_ONCE(d->d_name.name);
877 for (n = 0; n != spec.precision; n++, buf++) {
889 return widen_string(buf, n, end, spec);
892 static noinline_for_stack
893 char *file_dentry_name(char *buf, char *end, const struct file *f,
894 struct printf_spec spec, const char *fmt)
896 if (check_pointer(&buf, end, f, spec))
899 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
902 static noinline_for_stack
903 char *bdev_name(char *buf, char *end, struct block_device *bdev,
904 struct printf_spec spec, const char *fmt)
908 if (check_pointer(&buf, end, bdev, spec))
912 buf = string(buf, end, hd->disk_name, spec);
913 if (bdev->bd_part->partno) {
914 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
919 buf = number(buf, end, bdev->bd_part->partno, spec);
925 static noinline_for_stack
926 char *symbol_string(char *buf, char *end, void *ptr,
927 struct printf_spec spec, const char *fmt)
930 #ifdef CONFIG_KALLSYMS
931 char sym[KSYM_SYMBOL_LEN];
935 ptr = __builtin_extract_return_addr(ptr);
936 value = (unsigned long)ptr;
938 #ifdef CONFIG_KALLSYMS
940 sprint_backtrace(sym, value);
941 else if (*fmt != 'f' && *fmt != 's')
942 sprint_symbol(sym, value);
944 sprint_symbol_no_offset(sym, value);
946 return string_nocheck(buf, end, sym, spec);
948 return special_hex_number(buf, end, value, sizeof(void *));
952 static const struct printf_spec default_str_spec = {
957 static const struct printf_spec default_flag_spec = {
960 .flags = SPECIAL | SMALL,
963 static const struct printf_spec default_dec_spec = {
968 static const struct printf_spec default_dec02_spec = {
975 static const struct printf_spec default_dec04_spec = {
982 static noinline_for_stack
983 char *resource_string(char *buf, char *end, struct resource *res,
984 struct printf_spec spec, const char *fmt)
986 #ifndef IO_RSRC_PRINTK_SIZE
987 #define IO_RSRC_PRINTK_SIZE 6
990 #ifndef MEM_RSRC_PRINTK_SIZE
991 #define MEM_RSRC_PRINTK_SIZE 10
993 static const struct printf_spec io_spec = {
995 .field_width = IO_RSRC_PRINTK_SIZE,
997 .flags = SPECIAL | SMALL | ZEROPAD,
999 static const struct printf_spec mem_spec = {
1001 .field_width = MEM_RSRC_PRINTK_SIZE,
1003 .flags = SPECIAL | SMALL | ZEROPAD,
1005 static const struct printf_spec bus_spec = {
1009 .flags = SMALL | ZEROPAD,
1011 static const struct printf_spec str_spec = {
1017 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1018 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1019 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1020 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
1021 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
1022 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1023 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1024 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1026 char *p = sym, *pend = sym + sizeof(sym);
1027 int decode = (fmt[0] == 'R') ? 1 : 0;
1028 const struct printf_spec *specp;
1030 if (check_pointer(&buf, end, res, spec))
1034 if (res->flags & IORESOURCE_IO) {
1035 p = string_nocheck(p, pend, "io ", str_spec);
1037 } else if (res->flags & IORESOURCE_MEM) {
1038 p = string_nocheck(p, pend, "mem ", str_spec);
1040 } else if (res->flags & IORESOURCE_IRQ) {
1041 p = string_nocheck(p, pend, "irq ", str_spec);
1042 specp = &default_dec_spec;
1043 } else if (res->flags & IORESOURCE_DMA) {
1044 p = string_nocheck(p, pend, "dma ", str_spec);
1045 specp = &default_dec_spec;
1046 } else if (res->flags & IORESOURCE_BUS) {
1047 p = string_nocheck(p, pend, "bus ", str_spec);
1050 p = string_nocheck(p, pend, "??? ", str_spec);
1054 if (decode && res->flags & IORESOURCE_UNSET) {
1055 p = string_nocheck(p, pend, "size ", str_spec);
1056 p = number(p, pend, resource_size(res), *specp);
1058 p = number(p, pend, res->start, *specp);
1059 if (res->start != res->end) {
1061 p = number(p, pend, res->end, *specp);
1065 if (res->flags & IORESOURCE_MEM_64)
1066 p = string_nocheck(p, pend, " 64bit", str_spec);
1067 if (res->flags & IORESOURCE_PREFETCH)
1068 p = string_nocheck(p, pend, " pref", str_spec);
1069 if (res->flags & IORESOURCE_WINDOW)
1070 p = string_nocheck(p, pend, " window", str_spec);
1071 if (res->flags & IORESOURCE_DISABLED)
1072 p = string_nocheck(p, pend, " disabled", str_spec);
1074 p = string_nocheck(p, pend, " flags ", str_spec);
1075 p = number(p, pend, res->flags, default_flag_spec);
1080 return string_nocheck(buf, end, sym, spec);
1083 static noinline_for_stack
1084 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1087 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
1088 negative value, fallback to the default */
1091 if (spec.field_width == 0)
1092 /* nothing to print */
1095 if (check_pointer(&buf, end, addr, spec))
1113 if (spec.field_width > 0)
1114 len = min_t(int, spec.field_width, 64);
1116 for (i = 0; i < len; ++i) {
1118 *buf = hex_asc_hi(addr[i]);
1121 *buf = hex_asc_lo(addr[i]);
1124 if (separator && i != len - 1) {
1134 static noinline_for_stack
1135 char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1136 struct printf_spec spec, const char *fmt)
1138 const int CHUNKSZ = 32;
1139 int nr_bits = max_t(int, spec.field_width, 0);
1143 if (check_pointer(&buf, end, bitmap, spec))
1146 /* reused to print numbers */
1147 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1149 chunksz = nr_bits & (CHUNKSZ - 1);
1153 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1154 for (; i >= 0; i -= CHUNKSZ) {
1158 chunkmask = ((1ULL << chunksz) - 1);
1159 word = i / BITS_PER_LONG;
1160 bit = i % BITS_PER_LONG;
1161 val = (bitmap[word] >> bit) & chunkmask;
1170 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1171 buf = number(buf, end, val, spec);
1178 static noinline_for_stack
1179 char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1180 struct printf_spec spec, const char *fmt)
1182 int nr_bits = max_t(int, spec.field_width, 0);
1183 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1184 int cur, rbot, rtop;
1187 if (check_pointer(&buf, end, bitmap, spec))
1190 rbot = cur = find_first_bit(bitmap, nr_bits);
1191 while (cur < nr_bits) {
1193 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1194 if (cur < nr_bits && cur <= rtop + 1)
1204 buf = number(buf, end, rbot, default_dec_spec);
1210 buf = number(buf, end, rtop, default_dec_spec);
1218 static noinline_for_stack
1219 char *mac_address_string(char *buf, char *end, u8 *addr,
1220 struct printf_spec spec, const char *fmt)
1222 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1226 bool reversed = false;
1228 if (check_pointer(&buf, end, addr, spec))
1245 for (i = 0; i < 6; i++) {
1247 p = hex_byte_pack(p, addr[5 - i]);
1249 p = hex_byte_pack(p, addr[i]);
1251 if (fmt[0] == 'M' && i != 5)
1256 return string_nocheck(buf, end, mac_addr, spec);
1259 static noinline_for_stack
1260 char *ip4_string(char *p, const u8 *addr, const char *fmt)
1263 bool leading_zeros = (fmt[0] == 'i');
1288 for (i = 0; i < 4; i++) {
1289 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
1290 int digits = put_dec_trunc8(temp, addr[index]) - temp;
1291 if (leading_zeros) {
1297 /* reverse the digits in the quad */
1299 *p++ = temp[digits];
1309 static noinline_for_stack
1310 char *ip6_compressed_string(char *p, const char *addr)
1313 unsigned char zerolength[8];
1318 bool needcolon = false;
1320 struct in6_addr in6;
1322 memcpy(&in6, addr, sizeof(struct in6_addr));
1324 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1326 memset(zerolength, 0, sizeof(zerolength));
1333 /* find position of longest 0 run */
1334 for (i = 0; i < range; i++) {
1335 for (j = i; j < range; j++) {
1336 if (in6.s6_addr16[j] != 0)
1341 for (i = 0; i < range; i++) {
1342 if (zerolength[i] > longest) {
1343 longest = zerolength[i];
1347 if (longest == 1) /* don't compress a single 0 */
1351 for (i = 0; i < range; i++) {
1352 if (i == colonpos) {
1353 if (needcolon || i == 0)
1364 /* hex u16 without leading 0s */
1365 word = ntohs(in6.s6_addr16[i]);
1370 p = hex_byte_pack(p, hi);
1372 *p++ = hex_asc_lo(hi);
1373 p = hex_byte_pack(p, lo);
1376 p = hex_byte_pack(p, lo);
1378 *p++ = hex_asc_lo(lo);
1385 p = ip4_string(p, &in6.s6_addr[12], "I4");
1392 static noinline_for_stack
1393 char *ip6_string(char *p, const char *addr, const char *fmt)
1397 for (i = 0; i < 8; i++) {
1398 p = hex_byte_pack(p, *addr++);
1399 p = hex_byte_pack(p, *addr++);
1400 if (fmt[0] == 'I' && i != 7)
1408 static noinline_for_stack
1409 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1410 struct printf_spec spec, const char *fmt)
1412 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1414 if (fmt[0] == 'I' && fmt[2] == 'c')
1415 ip6_compressed_string(ip6_addr, addr);
1417 ip6_string(ip6_addr, addr, fmt);
1419 return string_nocheck(buf, end, ip6_addr, spec);
1422 static noinline_for_stack
1423 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1424 struct printf_spec spec, const char *fmt)
1426 char ip4_addr[sizeof("255.255.255.255")];
1428 ip4_string(ip4_addr, addr, fmt);
1430 return string_nocheck(buf, end, ip4_addr, spec);
1433 static noinline_for_stack
1434 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1435 struct printf_spec spec, const char *fmt)
1437 bool have_p = false, have_s = false, have_f = false, have_c = false;
1438 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1439 sizeof(":12345") + sizeof("/123456789") +
1440 sizeof("%1234567890")];
1441 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1442 const u8 *addr = (const u8 *) &sa->sin6_addr;
1443 char fmt6[2] = { fmt[0], '6' };
1447 while (isalpha(*++fmt)) {
1464 if (have_p || have_s || have_f) {
1469 if (fmt6[0] == 'I' && have_c)
1470 p = ip6_compressed_string(ip6_addr + off, addr);
1472 p = ip6_string(ip6_addr + off, addr, fmt6);
1474 if (have_p || have_s || have_f)
1479 p = number(p, pend, ntohs(sa->sin6_port), spec);
1483 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1484 IPV6_FLOWINFO_MASK), spec);
1488 p = number(p, pend, sa->sin6_scope_id, spec);
1492 return string_nocheck(buf, end, ip6_addr, spec);
1495 static noinline_for_stack
1496 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1497 struct printf_spec spec, const char *fmt)
1499 bool have_p = false;
1500 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1501 char *pend = ip4_addr + sizeof(ip4_addr);
1502 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1503 char fmt4[3] = { fmt[0], '4', 0 };
1506 while (isalpha(*++fmt)) {
1520 p = ip4_string(ip4_addr, addr, fmt4);
1523 p = number(p, pend, ntohs(sa->sin_port), spec);
1527 return string_nocheck(buf, end, ip4_addr, spec);
1530 static noinline_for_stack
1531 char *ip_addr_string(char *buf, char *end, const void *ptr,
1532 struct printf_spec spec, const char *fmt)
1536 if (check_pointer(&buf, end, ptr, spec))
1541 return ip6_addr_string(buf, end, ptr, spec, fmt);
1543 return ip4_addr_string(buf, end, ptr, spec, fmt);
1546 struct sockaddr raw;
1547 struct sockaddr_in v4;
1548 struct sockaddr_in6 v6;
1551 switch (sa->raw.sa_family) {
1553 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1555 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1557 return error_string(buf, end, "(einval)", spec);
1561 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1562 return error_string(buf, end, err_fmt_msg, spec);
1565 static noinline_for_stack
1566 char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1571 unsigned int flags = 0;
1574 if (spec.field_width == 0)
1575 return buf; /* nothing to print */
1577 if (check_pointer(&buf, end, addr, spec))
1581 switch (fmt[count++]) {
1583 flags |= ESCAPE_ANY;
1586 flags |= ESCAPE_SPECIAL;
1589 flags |= ESCAPE_HEX;
1592 flags |= ESCAPE_NULL;
1595 flags |= ESCAPE_OCTAL;
1601 flags |= ESCAPE_SPACE;
1610 flags = ESCAPE_ANY_NP;
1612 len = spec.field_width < 0 ? 1 : spec.field_width;
1615 * string_escape_mem() writes as many characters as it can to
1616 * the given buffer, and returns the total size of the output
1617 * had the buffer been big enough.
1619 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1624 static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1625 struct printf_spec spec, const char *fmt)
1629 if (check_pointer(&buf, end, va_fmt, spec))
1632 va_copy(va, *va_fmt->va);
1633 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1639 static noinline_for_stack
1640 char *uuid_string(char *buf, char *end, const u8 *addr,
1641 struct printf_spec spec, const char *fmt)
1643 char uuid[UUID_STRING_LEN + 1];
1646 const u8 *index = uuid_index;
1649 if (check_pointer(&buf, end, addr, spec))
1654 uc = true; /* fall-through */
1663 for (i = 0; i < 16; i++) {
1665 p = hex_byte_pack_upper(p, addr[index[i]]);
1667 p = hex_byte_pack(p, addr[index[i]]);
1680 return string_nocheck(buf, end, uuid, spec);
1683 static noinline_for_stack
1684 char *netdev_bits(char *buf, char *end, const void *addr,
1685 struct printf_spec spec, const char *fmt)
1687 unsigned long long num;
1690 if (check_pointer(&buf, end, addr, spec))
1695 num = *(const netdev_features_t *)addr;
1696 size = sizeof(netdev_features_t);
1699 return error_string(buf, end, "(%pN?)", spec);
1702 return special_hex_number(buf, end, num, size);
1705 static noinline_for_stack
1706 char *address_val(char *buf, char *end, const void *addr,
1707 struct printf_spec spec, const char *fmt)
1709 unsigned long long num;
1712 if (check_pointer(&buf, end, addr, spec))
1717 num = *(const dma_addr_t *)addr;
1718 size = sizeof(dma_addr_t);
1722 num = *(const phys_addr_t *)addr;
1723 size = sizeof(phys_addr_t);
1727 return special_hex_number(buf, end, num, size);
1730 static noinline_for_stack
1731 char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1733 int year = tm->tm_year + (r ? 0 : 1900);
1734 int mon = tm->tm_mon + (r ? 0 : 1);
1736 buf = number(buf, end, year, default_dec04_spec);
1741 buf = number(buf, end, mon, default_dec02_spec);
1746 return number(buf, end, tm->tm_mday, default_dec02_spec);
1749 static noinline_for_stack
1750 char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1752 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1757 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1762 return number(buf, end, tm->tm_sec, default_dec02_spec);
1765 static noinline_for_stack
1766 char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1767 struct printf_spec spec, const char *fmt)
1769 bool have_t = true, have_d = true;
1773 if (check_pointer(&buf, end, tm, spec))
1776 switch (fmt[count]) {
1787 raw = fmt[count] == 'r';
1790 buf = date_str(buf, end, tm, raw);
1791 if (have_d && have_t) {
1792 /* Respect ISO 8601 */
1798 buf = time_str(buf, end, tm, raw);
1803 static noinline_for_stack
1804 char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1809 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
1811 return error_string(buf, end, "(%ptR?)", spec);
1815 static noinline_for_stack
1816 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1819 if (!IS_ENABLED(CONFIG_HAVE_CLK))
1820 return error_string(buf, end, "(%pC?)", spec);
1822 if (check_pointer(&buf, end, clk, spec))
1828 #ifdef CONFIG_COMMON_CLK
1829 return string(buf, end, __clk_get_name(clk), spec);
1831 return ptr_to_id(buf, end, clk, spec);
1837 char *format_flags(char *buf, char *end, unsigned long flags,
1838 const struct trace_print_flags *names)
1842 for ( ; flags && names->name; names++) {
1844 if ((flags & mask) != mask)
1847 buf = string(buf, end, names->name, default_str_spec);
1858 buf = number(buf, end, flags, default_flag_spec);
1863 static noinline_for_stack
1864 char *flags_string(char *buf, char *end, void *flags_ptr,
1865 struct printf_spec spec, const char *fmt)
1867 unsigned long flags;
1868 const struct trace_print_flags *names;
1870 if (check_pointer(&buf, end, flags_ptr, spec))
1875 flags = *(unsigned long *)flags_ptr;
1876 /* Remove zone id */
1877 flags &= (1UL << NR_PAGEFLAGS) - 1;
1878 names = pageflag_names;
1881 flags = *(unsigned long *)flags_ptr;
1882 names = vmaflag_names;
1885 flags = *(gfp_t *)flags_ptr;
1886 names = gfpflag_names;
1889 return error_string(buf, end, "(%pG?)", spec);
1892 return format_flags(buf, end, flags, names);
1895 static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1897 for ( ; np && depth; depth--)
1900 return kbasename(np->full_name);
1903 static noinline_for_stack
1904 char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1907 const struct device_node *parent = np->parent;
1909 /* special case for root node */
1911 return string_nocheck(buf, end, "/", default_str_spec);
1913 for (depth = 0; parent->parent; depth++)
1914 parent = parent->parent;
1916 for ( ; depth >= 0; depth--) {
1917 buf = string_nocheck(buf, end, "/", default_str_spec);
1918 buf = string(buf, end, device_node_name_for_depth(np, depth),
1924 static noinline_for_stack
1925 char *device_node_string(char *buf, char *end, struct device_node *dn,
1926 struct printf_spec spec, const char *fmt)
1928 char tbuf[sizeof("xxxx") + 1];
1931 char *buf_start = buf;
1932 struct property *prop;
1933 bool has_mult, pass;
1934 static const struct printf_spec num_spec = {
1941 struct printf_spec str_spec = spec;
1942 str_spec.field_width = -1;
1944 if (!IS_ENABLED(CONFIG_OF))
1945 return error_string(buf, end, "(%pOF?)", spec);
1947 if (check_pointer(&buf, end, dn, spec))
1950 /* simple case without anything any more format specifiers */
1952 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1955 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1964 case 'f': /* full_name */
1965 buf = device_node_gen_full_name(dn, buf, end);
1967 case 'n': /* name */
1968 p = kbasename(of_node_full_name(dn));
1969 precision = str_spec.precision;
1970 str_spec.precision = strchrnul(p, '@') - p;
1971 buf = string(buf, end, p, str_spec);
1972 str_spec.precision = precision;
1974 case 'p': /* phandle */
1975 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1977 case 'P': /* path-spec */
1978 p = kbasename(of_node_full_name(dn));
1981 buf = string(buf, end, p, str_spec);
1983 case 'F': /* flags */
1984 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1985 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1986 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1987 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1989 buf = string_nocheck(buf, end, tbuf, str_spec);
1991 case 'c': /* major compatible string */
1992 ret = of_property_read_string(dn, "compatible", &p);
1994 buf = string(buf, end, p, str_spec);
1996 case 'C': /* full compatible string */
1998 of_property_for_each_string(dn, "compatible", prop, p) {
2000 buf = string_nocheck(buf, end, ",", str_spec);
2001 buf = string_nocheck(buf, end, "\"", str_spec);
2002 buf = string(buf, end, p, str_spec);
2003 buf = string_nocheck(buf, end, "\"", str_spec);
2013 return widen_string(buf, buf - buf_start, end, spec);
2016 static char *kobject_string(char *buf, char *end, void *ptr,
2017 struct printf_spec spec, const char *fmt)
2021 return device_node_string(buf, end, ptr, spec, fmt + 1);
2024 return error_string(buf, end, "(%pO?)", spec);
2028 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2029 * by an extra set of alphanumeric characters that are extended format
2032 * Please update scripts/checkpatch.pl when adding/removing conversion
2033 * characters. (Search for "check for vsprintf extension").
2035 * Right now we handle:
2037 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2038 * - 's' For symbolic direct pointers (or function descriptors) without offset
2041 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
2042 * - 'B' For backtraced symbolic direct pointers with offset
2043 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2044 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2045 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2046 * width which must be explicitly specified either as part of the
2047 * format string '%32b[l]' or through '%*b[l]', [l] selects
2048 * range-list format instead of hex format
2049 * - 'M' For a 6-byte MAC address, it prints the address in the
2050 * usual colon-separated hex notation
2051 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2052 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2053 * with a dash-separated hex notation
2054 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
2055 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2056 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2057 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
2059 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2060 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2061 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2062 * IPv6 omits the colons (01020304...0f)
2063 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
2065 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2066 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2067 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2068 * - 'I[6S]c' for IPv6 addresses printed as specified by
2069 * http://tools.ietf.org/html/rfc5952
2070 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2071 * of the following flags (see string_escape_mem() for the
2074 * c - ESCAPE_SPECIAL
2080 * By default ESCAPE_ANY_NP is used.
2081 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2082 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2083 * Options for %pU are:
2084 * b big endian lower case hex (default)
2085 * B big endian UPPER case hex
2086 * l little endian lower case hex
2087 * L little endian UPPER case hex
2088 * big endian output byte order is:
2089 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2090 * little endian output byte order is:
2091 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
2092 * - 'V' For a struct va_format which contains a format string * and va_list *,
2093 * call vsnprintf(->format, *->va_list).
2094 * Implements a "recursive vsnprintf".
2095 * Do not use this feature without some mechanism to verify the
2096 * correctness of the format string and va_list arguments.
2097 * - 'K' For a kernel pointer that should be hidden from unprivileged users
2098 * - 'NF' For a netdev_features_t
2099 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2100 * a certain separator (' ' by default):
2104 * The maximum supported length is 64 bytes of the input. Consider
2105 * to use print_hex_dump() for the larger input.
2106 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2107 * (default assumed to be phys_addr_t, passed by reference)
2108 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2109 * - 'D[234]' Same as 'd' but for a struct file
2110 * - 'g' For block_device name (gendisk + partition number)
2111 * - 't[R][dt][r]' For time and date as represented:
2113 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2114 * (legacy clock framework) of the clock
2115 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2116 * (legacy clock framework) of the clock
2117 * - 'G' For flags to be printed as a collection of symbolic strings that would
2118 * construct the specific value. Supported flags given by option:
2119 * p page flags (see struct page) given as pointer to unsigned long
2120 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2121 * v vma flags (VM_*) given as pointer to unsigned long
2122 * - 'OF[fnpPcCF]' For a device tree object
2123 * Without any optional arguments prints the full_name
2124 * f device node full_name
2125 * n device node name
2126 * p device node phandle
2127 * P device node path spec (name + @unit)
2128 * F device node flags
2129 * c major compatible string
2130 * C full compatible string
2131 * - 'x' For printing the address. Equivalent to "%lx".
2133 * ** When making changes please also update:
2134 * Documentation/core-api/printk-formats.rst
2136 * Note: The default behaviour (unadorned %p) is to hash the address,
2137 * rendering it useful as a unique identifier.
2139 static noinline_for_stack
2140 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2141 struct printf_spec spec)
2148 ptr = dereference_symbol_descriptor(ptr);
2151 return symbol_string(buf, end, ptr, spec, fmt);
2154 return resource_string(buf, end, ptr, spec, fmt);
2156 return hex_string(buf, end, ptr, spec, fmt);
2160 return bitmap_list_string(buf, end, ptr, spec, fmt);
2162 return bitmap_string(buf, end, ptr, spec, fmt);
2164 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2165 case 'm': /* Contiguous: 000102030405 */
2167 /* [mM]R (Reverse order; Bluetooth) */
2168 return mac_address_string(buf, end, ptr, spec, fmt);
2169 case 'I': /* Formatted IP supported
2171 * 6: 0001:0203:...:0708
2172 * 6c: 1::708 or 1::1.2.3.4
2174 case 'i': /* Contiguous:
2175 * 4: 001.002.003.004
2178 return ip_addr_string(buf, end, ptr, spec, fmt);
2180 return escaped_string(buf, end, ptr, spec, fmt);
2182 return uuid_string(buf, end, ptr, spec, fmt);
2184 return va_format(buf, end, ptr, spec, fmt);
2186 return restricted_pointer(buf, end, ptr, spec);
2188 return netdev_bits(buf, end, ptr, spec, fmt);
2190 return address_val(buf, end, ptr, spec, fmt);
2192 return dentry_name(buf, end, ptr, spec, fmt);
2194 return time_and_date(buf, end, ptr, spec, fmt);
2196 return clock(buf, end, ptr, spec, fmt);
2198 return file_dentry_name(buf, end, ptr, spec, fmt);
2201 return bdev_name(buf, end, ptr, spec, fmt);
2205 return flags_string(buf, end, ptr, spec, fmt);
2207 return kobject_string(buf, end, ptr, spec, fmt);
2209 return pointer_string(buf, end, ptr, spec);
2211 /* %pe with a non-ERR_PTR gets treated as plain %p */
2214 return err_ptr(buf, end, ptr, spec);
2217 /* default is to _not_ leak addresses, hash before printing */
2218 return ptr_to_id(buf, end, ptr, spec);
2222 * Helper function to decode printf style format.
2223 * Each call decode a token from the format and return the
2224 * number of characters read (or likely the delta where it wants
2225 * to go on the next call).
2226 * The decoded token is returned through the parameters
2228 * 'h', 'l', or 'L' for integer fields
2229 * 'z' support added 23/7/1999 S.H.
2230 * 'z' changed to 'Z' --davidm 1/25/99
2231 * 'Z' changed to 'z' --adobriyan 2017-01-25
2232 * 't' added for ptrdiff_t
2234 * @fmt: the format string
2235 * @type of the token returned
2236 * @flags: various flags such as +, -, # tokens..
2237 * @field_width: overwritten width
2238 * @base: base of the number (octal, hex, ...)
2239 * @precision: precision of a number
2240 * @qualifier: qualifier of a number (long, size_t, ...)
2242 static noinline_for_stack
2243 int format_decode(const char *fmt, struct printf_spec *spec)
2245 const char *start = fmt;
2248 /* we finished early by reading the field width */
2249 if (spec->type == FORMAT_TYPE_WIDTH) {
2250 if (spec->field_width < 0) {
2251 spec->field_width = -spec->field_width;
2252 spec->flags |= LEFT;
2254 spec->type = FORMAT_TYPE_NONE;
2258 /* we finished early by reading the precision */
2259 if (spec->type == FORMAT_TYPE_PRECISION) {
2260 if (spec->precision < 0)
2261 spec->precision = 0;
2263 spec->type = FORMAT_TYPE_NONE;
2268 spec->type = FORMAT_TYPE_NONE;
2270 for (; *fmt ; ++fmt) {
2275 /* Return the current non-format string */
2276 if (fmt != start || !*fmt)
2282 while (1) { /* this also skips first '%' */
2288 case '-': spec->flags |= LEFT; break;
2289 case '+': spec->flags |= PLUS; break;
2290 case ' ': spec->flags |= SPACE; break;
2291 case '#': spec->flags |= SPECIAL; break;
2292 case '0': spec->flags |= ZEROPAD; break;
2293 default: found = false;
2300 /* get field width */
2301 spec->field_width = -1;
2304 spec->field_width = skip_atoi(&fmt);
2305 else if (*fmt == '*') {
2306 /* it's the next argument */
2307 spec->type = FORMAT_TYPE_WIDTH;
2308 return ++fmt - start;
2312 /* get the precision */
2313 spec->precision = -1;
2316 if (isdigit(*fmt)) {
2317 spec->precision = skip_atoi(&fmt);
2318 if (spec->precision < 0)
2319 spec->precision = 0;
2320 } else if (*fmt == '*') {
2321 /* it's the next argument */
2322 spec->type = FORMAT_TYPE_PRECISION;
2323 return ++fmt - start;
2328 /* get the conversion qualifier */
2330 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2331 *fmt == 'z' || *fmt == 't') {
2333 if (unlikely(qualifier == *fmt)) {
2334 if (qualifier == 'l') {
2337 } else if (qualifier == 'h') {
2348 spec->type = FORMAT_TYPE_CHAR;
2349 return ++fmt - start;
2352 spec->type = FORMAT_TYPE_STR;
2353 return ++fmt - start;
2356 spec->type = FORMAT_TYPE_PTR;
2357 return ++fmt - start;
2360 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2361 return ++fmt - start;
2363 /* integer number formats - set up the flags and "break" */
2369 spec->flags |= SMALL;
2378 spec->flags |= SIGN;
2384 * Since %n poses a greater security risk than
2385 * utility, treat it as any other invalid or
2386 * unsupported format specifier.
2391 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2392 spec->type = FORMAT_TYPE_INVALID;
2396 if (qualifier == 'L')
2397 spec->type = FORMAT_TYPE_LONG_LONG;
2398 else if (qualifier == 'l') {
2399 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2400 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2401 } else if (qualifier == 'z') {
2402 spec->type = FORMAT_TYPE_SIZE_T;
2403 } else if (qualifier == 't') {
2404 spec->type = FORMAT_TYPE_PTRDIFF;
2405 } else if (qualifier == 'H') {
2406 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2407 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2408 } else if (qualifier == 'h') {
2409 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2410 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2412 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2413 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2416 return ++fmt - start;
2420 set_field_width(struct printf_spec *spec, int width)
2422 spec->field_width = width;
2423 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2424 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2429 set_precision(struct printf_spec *spec, int prec)
2431 spec->precision = prec;
2432 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2433 spec->precision = clamp(prec, 0, PRECISION_MAX);
2438 * vsnprintf - Format a string and place it in a buffer
2439 * @buf: The buffer to place the result into
2440 * @size: The size of the buffer, including the trailing null space
2441 * @fmt: The format string to use
2442 * @args: Arguments for the format string
2444 * This function generally follows C99 vsnprintf, but has some
2445 * extensions and a few limitations:
2447 * - ``%n`` is unsupported
2448 * - ``%p*`` is handled by pointer()
2450 * See pointer() or Documentation/core-api/printk-formats.rst for more
2451 * extensive description.
2453 * **Please update the documentation in both places when making changes**
2455 * The return value is the number of characters which would
2456 * be generated for the given input, excluding the trailing
2457 * '\0', as per ISO C99. If you want to have the exact
2458 * number of characters written into @buf as return value
2459 * (not including the trailing '\0'), use vscnprintf(). If the
2460 * return is greater than or equal to @size, the resulting
2461 * string is truncated.
2463 * If you're not already dealing with a va_list consider using snprintf().
2465 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2467 unsigned long long num;
2469 struct printf_spec spec = {0};
2471 /* Reject out-of-range values early. Large positive sizes are
2472 used for unknown buffer sizes. */
2473 if (WARN_ON_ONCE(size > INT_MAX))
2479 /* Make sure end is always >= buf */
2486 const char *old_fmt = fmt;
2487 int read = format_decode(fmt, &spec);
2491 switch (spec.type) {
2492 case FORMAT_TYPE_NONE: {
2495 if (copy > end - str)
2497 memcpy(str, old_fmt, copy);
2503 case FORMAT_TYPE_WIDTH:
2504 set_field_width(&spec, va_arg(args, int));
2507 case FORMAT_TYPE_PRECISION:
2508 set_precision(&spec, va_arg(args, int));
2511 case FORMAT_TYPE_CHAR: {
2514 if (!(spec.flags & LEFT)) {
2515 while (--spec.field_width > 0) {
2522 c = (unsigned char) va_arg(args, int);
2526 while (--spec.field_width > 0) {
2534 case FORMAT_TYPE_STR:
2535 str = string(str, end, va_arg(args, char *), spec);
2538 case FORMAT_TYPE_PTR:
2539 str = pointer(fmt, str, end, va_arg(args, void *),
2541 while (isalnum(*fmt))
2545 case FORMAT_TYPE_PERCENT_CHAR:
2551 case FORMAT_TYPE_INVALID:
2553 * Presumably the arguments passed gcc's type
2554 * checking, but there is no safe or sane way
2555 * for us to continue parsing the format and
2556 * fetching from the va_list; the remaining
2557 * specifiers and arguments would be out of
2563 switch (spec.type) {
2564 case FORMAT_TYPE_LONG_LONG:
2565 num = va_arg(args, long long);
2567 case FORMAT_TYPE_ULONG:
2568 num = va_arg(args, unsigned long);
2570 case FORMAT_TYPE_LONG:
2571 num = va_arg(args, long);
2573 case FORMAT_TYPE_SIZE_T:
2574 if (spec.flags & SIGN)
2575 num = va_arg(args, ssize_t);
2577 num = va_arg(args, size_t);
2579 case FORMAT_TYPE_PTRDIFF:
2580 num = va_arg(args, ptrdiff_t);
2582 case FORMAT_TYPE_UBYTE:
2583 num = (unsigned char) va_arg(args, int);
2585 case FORMAT_TYPE_BYTE:
2586 num = (signed char) va_arg(args, int);
2588 case FORMAT_TYPE_USHORT:
2589 num = (unsigned short) va_arg(args, int);
2591 case FORMAT_TYPE_SHORT:
2592 num = (short) va_arg(args, int);
2594 case FORMAT_TYPE_INT:
2595 num = (int) va_arg(args, int);
2598 num = va_arg(args, unsigned int);
2601 str = number(str, end, num, spec);
2613 /* the trailing null byte doesn't count towards the total */
2617 EXPORT_SYMBOL(vsnprintf);
2620 * vscnprintf - Format a string and place it in a buffer
2621 * @buf: The buffer to place the result into
2622 * @size: The size of the buffer, including the trailing null space
2623 * @fmt: The format string to use
2624 * @args: Arguments for the format string
2626 * The return value is the number of characters which have been written into
2627 * the @buf not including the trailing '\0'. If @size is == 0 the function
2630 * If you're not already dealing with a va_list consider using scnprintf().
2632 * See the vsnprintf() documentation for format string extensions over C99.
2634 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2638 i = vsnprintf(buf, size, fmt, args);
2640 if (likely(i < size))
2646 EXPORT_SYMBOL(vscnprintf);
2649 * snprintf - Format a string and place it in a buffer
2650 * @buf: The buffer to place the result into
2651 * @size: The size of the buffer, including the trailing null space
2652 * @fmt: The format string to use
2653 * @...: Arguments for the format string
2655 * The return value is the number of characters which would be
2656 * generated for the given input, excluding the trailing null,
2657 * as per ISO C99. If the return is greater than or equal to
2658 * @size, the resulting string is truncated.
2660 * See the vsnprintf() documentation for format string extensions over C99.
2662 int snprintf(char *buf, size_t size, const char *fmt, ...)
2667 va_start(args, fmt);
2668 i = vsnprintf(buf, size, fmt, args);
2673 EXPORT_SYMBOL(snprintf);
2676 * scnprintf - Format a string and place it in a buffer
2677 * @buf: The buffer to place the result into
2678 * @size: The size of the buffer, including the trailing null space
2679 * @fmt: The format string to use
2680 * @...: Arguments for the format string
2682 * The return value is the number of characters written into @buf not including
2683 * the trailing '\0'. If @size is == 0 the function returns 0.
2686 int scnprintf(char *buf, size_t size, const char *fmt, ...)
2691 va_start(args, fmt);
2692 i = vscnprintf(buf, size, fmt, args);
2697 EXPORT_SYMBOL(scnprintf);
2700 * vsprintf - Format a string and place it in a buffer
2701 * @buf: The buffer to place the result into
2702 * @fmt: The format string to use
2703 * @args: Arguments for the format string
2705 * The function returns the number of characters written
2706 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2709 * If you're not already dealing with a va_list consider using sprintf().
2711 * See the vsnprintf() documentation for format string extensions over C99.
2713 int vsprintf(char *buf, const char *fmt, va_list args)
2715 return vsnprintf(buf, INT_MAX, fmt, args);
2717 EXPORT_SYMBOL(vsprintf);
2720 * sprintf - Format a string and place it in a buffer
2721 * @buf: The buffer to place the result into
2722 * @fmt: The format string to use
2723 * @...: Arguments for the format string
2725 * The function returns the number of characters written
2726 * into @buf. Use snprintf() or scnprintf() in order to avoid
2729 * See the vsnprintf() documentation for format string extensions over C99.
2731 int sprintf(char *buf, const char *fmt, ...)
2736 va_start(args, fmt);
2737 i = vsnprintf(buf, INT_MAX, fmt, args);
2742 EXPORT_SYMBOL(sprintf);
2744 #ifdef CONFIG_BINARY_PRINTF
2747 * vbin_printf() - VA arguments to binary data
2748 * bstr_printf() - Binary data to text string
2752 * vbin_printf - Parse a format string and place args' binary value in a buffer
2753 * @bin_buf: The buffer to place args' binary value
2754 * @size: The size of the buffer(by words(32bits), not characters)
2755 * @fmt: The format string to use
2756 * @args: Arguments for the format string
2758 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2761 * The return value is the number of words(32bits) which would be generated for
2765 * If the return value is greater than @size, the resulting bin_buf is NOT
2766 * valid for bstr_printf().
2768 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2770 struct printf_spec spec = {0};
2774 str = (char *)bin_buf;
2775 end = (char *)(bin_buf + size);
2777 #define save_arg(type) \
2779 unsigned long long value; \
2780 if (sizeof(type) == 8) { \
2781 unsigned long long val8; \
2782 str = PTR_ALIGN(str, sizeof(u32)); \
2783 val8 = va_arg(args, unsigned long long); \
2784 if (str + sizeof(type) <= end) { \
2785 *(u32 *)str = *(u32 *)&val8; \
2786 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2790 unsigned int val4; \
2791 str = PTR_ALIGN(str, sizeof(type)); \
2792 val4 = va_arg(args, int); \
2793 if (str + sizeof(type) <= end) \
2794 *(typeof(type) *)str = (type)(long)val4; \
2795 value = (unsigned long long)val4; \
2797 str += sizeof(type); \
2802 int read = format_decode(fmt, &spec);
2806 switch (spec.type) {
2807 case FORMAT_TYPE_NONE:
2808 case FORMAT_TYPE_PERCENT_CHAR:
2810 case FORMAT_TYPE_INVALID:
2813 case FORMAT_TYPE_WIDTH:
2814 case FORMAT_TYPE_PRECISION:
2815 width = (int)save_arg(int);
2816 /* Pointers may require the width */
2818 set_field_width(&spec, width);
2821 case FORMAT_TYPE_CHAR:
2825 case FORMAT_TYPE_STR: {
2826 const char *save_str = va_arg(args, char *);
2827 const char *err_msg;
2830 err_msg = check_pointer_msg(save_str);
2834 len = strlen(save_str) + 1;
2835 if (str + len < end)
2836 memcpy(str, save_str, len);
2841 case FORMAT_TYPE_PTR:
2842 /* Dereferenced pointers must be done now */
2844 /* Dereference of functions is still OK */
2855 if (!isalnum(*fmt)) {
2859 str = pointer(fmt, str, end, va_arg(args, void *),
2864 end[-1] = '\0'; /* Must be nul terminated */
2866 /* skip all alphanumeric pointer suffixes */
2867 while (isalnum(*fmt))
2872 switch (spec.type) {
2874 case FORMAT_TYPE_LONG_LONG:
2875 save_arg(long long);
2877 case FORMAT_TYPE_ULONG:
2878 case FORMAT_TYPE_LONG:
2879 save_arg(unsigned long);
2881 case FORMAT_TYPE_SIZE_T:
2884 case FORMAT_TYPE_PTRDIFF:
2885 save_arg(ptrdiff_t);
2887 case FORMAT_TYPE_UBYTE:
2888 case FORMAT_TYPE_BYTE:
2891 case FORMAT_TYPE_USHORT:
2892 case FORMAT_TYPE_SHORT:
2902 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2905 EXPORT_SYMBOL_GPL(vbin_printf);
2908 * bstr_printf - Format a string from binary arguments and place it in a buffer
2909 * @buf: The buffer to place the result into
2910 * @size: The size of the buffer, including the trailing null space
2911 * @fmt: The format string to use
2912 * @bin_buf: Binary arguments for the format string
2914 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2915 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2916 * a binary buffer that generated by vbin_printf.
2918 * The format follows C99 vsnprintf, but has some extensions:
2919 * see vsnprintf comment for details.
2921 * The return value is the number of characters which would
2922 * be generated for the given input, excluding the trailing
2923 * '\0', as per ISO C99. If you want to have the exact
2924 * number of characters written into @buf as return value
2925 * (not including the trailing '\0'), use vscnprintf(). If the
2926 * return is greater than or equal to @size, the resulting
2927 * string is truncated.
2929 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2931 struct printf_spec spec = {0};
2933 const char *args = (const char *)bin_buf;
2935 if (WARN_ON_ONCE(size > INT_MAX))
2941 #define get_arg(type) \
2943 typeof(type) value; \
2944 if (sizeof(type) == 8) { \
2945 args = PTR_ALIGN(args, sizeof(u32)); \
2946 *(u32 *)&value = *(u32 *)args; \
2947 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2949 args = PTR_ALIGN(args, sizeof(type)); \
2950 value = *(typeof(type) *)args; \
2952 args += sizeof(type); \
2956 /* Make sure end is always >= buf */
2963 const char *old_fmt = fmt;
2964 int read = format_decode(fmt, &spec);
2968 switch (spec.type) {
2969 case FORMAT_TYPE_NONE: {
2972 if (copy > end - str)
2974 memcpy(str, old_fmt, copy);
2980 case FORMAT_TYPE_WIDTH:
2981 set_field_width(&spec, get_arg(int));
2984 case FORMAT_TYPE_PRECISION:
2985 set_precision(&spec, get_arg(int));
2988 case FORMAT_TYPE_CHAR: {
2991 if (!(spec.flags & LEFT)) {
2992 while (--spec.field_width > 0) {
2998 c = (unsigned char) get_arg(char);
3002 while (--spec.field_width > 0) {
3010 case FORMAT_TYPE_STR: {
3011 const char *str_arg = args;
3012 args += strlen(str_arg) + 1;
3013 str = string(str, end, (char *)str_arg, spec);
3017 case FORMAT_TYPE_PTR: {
3018 bool process = false;
3020 /* Non function dereferences were already done */
3032 if (!isalnum(*fmt)) {
3036 /* Pointer dereference was already processed */
3038 len = copy = strlen(args);
3039 if (copy > end - str)
3041 memcpy(str, args, copy);
3047 str = pointer(fmt, str, end, get_arg(void *), spec);
3049 while (isalnum(*fmt))
3054 case FORMAT_TYPE_PERCENT_CHAR:
3060 case FORMAT_TYPE_INVALID:
3064 unsigned long long num;
3066 switch (spec.type) {
3068 case FORMAT_TYPE_LONG_LONG:
3069 num = get_arg(long long);
3071 case FORMAT_TYPE_ULONG:
3072 case FORMAT_TYPE_LONG:
3073 num = get_arg(unsigned long);
3075 case FORMAT_TYPE_SIZE_T:
3076 num = get_arg(size_t);
3078 case FORMAT_TYPE_PTRDIFF:
3079 num = get_arg(ptrdiff_t);
3081 case FORMAT_TYPE_UBYTE:
3082 num = get_arg(unsigned char);
3084 case FORMAT_TYPE_BYTE:
3085 num = get_arg(signed char);
3087 case FORMAT_TYPE_USHORT:
3088 num = get_arg(unsigned short);
3090 case FORMAT_TYPE_SHORT:
3091 num = get_arg(short);
3093 case FORMAT_TYPE_UINT:
3094 num = get_arg(unsigned int);
3100 str = number(str, end, num, spec);
3102 } /* switch(spec.type) */
3115 /* the trailing null byte doesn't count towards the total */
3118 EXPORT_SYMBOL_GPL(bstr_printf);
3121 * bprintf - Parse a format string and place args' binary value in a buffer
3122 * @bin_buf: The buffer to place args' binary value
3123 * @size: The size of the buffer(by words(32bits), not characters)
3124 * @fmt: The format string to use
3125 * @...: Arguments for the format string
3127 * The function returns the number of words(u32) written
3130 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3135 va_start(args, fmt);
3136 ret = vbin_printf(bin_buf, size, fmt, args);
3141 EXPORT_SYMBOL_GPL(bprintf);
3143 #endif /* CONFIG_BINARY_PRINTF */
3146 * vsscanf - Unformat a buffer into a list of arguments
3147 * @buf: input buffer
3148 * @fmt: format of buffer
3151 int vsscanf(const char *buf, const char *fmt, va_list args)
3153 const char *str = buf;
3161 unsigned long long u;
3167 /* skip any white space in format */
3168 /* white space in format matchs any amount of
3169 * white space, including none, in the input.
3171 if (isspace(*fmt)) {
3172 fmt = skip_spaces(++fmt);
3173 str = skip_spaces(str);
3176 /* anything that is not a conversion must match exactly */
3177 if (*fmt != '%' && *fmt) {
3178 if (*fmt++ != *str++)
3187 /* skip this conversion.
3188 * advance both strings to next white space
3193 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3194 /* '%*[' not yet supported, invalid format */
3199 while (!isspace(*str) && *str)
3204 /* get field width */
3206 if (isdigit(*fmt)) {
3207 field_width = skip_atoi(&fmt);
3208 if (field_width <= 0)
3212 /* get conversion qualifier */
3214 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
3217 if (unlikely(qualifier == *fmt)) {
3218 if (qualifier == 'h') {
3221 } else if (qualifier == 'l') {
3232 /* return number of characters read so far */
3233 *va_arg(args, int *) = str - buf;
3247 char *s = (char *)va_arg(args, char*);
3248 if (field_width == -1)
3252 } while (--field_width > 0 && *str);
3258 char *s = (char *)va_arg(args, char *);
3259 if (field_width == -1)
3260 field_width = SHRT_MAX;
3261 /* first, skip leading white space in buffer */
3262 str = skip_spaces(str);
3264 /* now copy until next white space */
3265 while (*str && !isspace(*str) && field_width--)
3272 * Warning: This implementation of the '[' conversion specifier
3273 * deviates from its glibc counterpart in the following ways:
3274 * (1) It does NOT support ranges i.e. '-' is NOT a special
3276 * (2) It cannot match the closing bracket ']' itself
3277 * (3) A field width is required
3278 * (4) '%*[' (discard matching input) is currently not supported
3281 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3282 * buf1, buf2, buf3);
3288 char *s = (char *)va_arg(args, char *);
3289 DECLARE_BITMAP(set, 256) = {0};
3290 unsigned int len = 0;
3291 bool negate = (*fmt == '^');
3293 /* field width is required */
3294 if (field_width == -1)
3300 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3301 set_bit((u8)*fmt, set);
3303 /* no ']' or no character set found */
3309 bitmap_complement(set, set, 256);
3310 /* exclude null '\0' byte */
3314 /* match must be non-empty */
3315 if (!test_bit((u8)*str, set))
3318 while (test_bit((u8)*str, set) && field_width--)
3340 /* looking for '%' in str */
3345 /* invalid format; stop here */
3349 /* have some sort of integer conversion.
3350 * first, skip white space in buffer.
3352 str = skip_spaces(str);
3355 if (is_sign && digit == '-')
3359 || (base == 16 && !isxdigit(digit))
3360 || (base == 10 && !isdigit(digit))
3361 || (base == 8 && (!isdigit(digit) || digit > '7'))
3362 || (base == 0 && !isdigit(digit)))
3366 val.s = qualifier != 'L' ?
3367 simple_strtol(str, &next, base) :
3368 simple_strtoll(str, &next, base);
3370 val.u = qualifier != 'L' ?
3371 simple_strtoul(str, &next, base) :
3372 simple_strtoull(str, &next, base);
3374 if (field_width > 0 && next - str > field_width) {
3376 _parse_integer_fixup_radix(str, &base);
3377 while (next - str > field_width) {
3379 val.s = div_s64(val.s, base);
3381 val.u = div_u64(val.u, base);
3386 switch (qualifier) {
3387 case 'H': /* that's 'hh' in format */
3389 *va_arg(args, signed char *) = val.s;
3391 *va_arg(args, unsigned char *) = val.u;
3395 *va_arg(args, short *) = val.s;
3397 *va_arg(args, unsigned short *) = val.u;
3401 *va_arg(args, long *) = val.s;
3403 *va_arg(args, unsigned long *) = val.u;
3407 *va_arg(args, long long *) = val.s;
3409 *va_arg(args, unsigned long long *) = val.u;
3412 *va_arg(args, size_t *) = val.u;
3416 *va_arg(args, int *) = val.s;
3418 *va_arg(args, unsigned int *) = val.u;
3430 EXPORT_SYMBOL(vsscanf);
3433 * sscanf - Unformat a buffer into a list of arguments
3434 * @buf: input buffer
3435 * @fmt: formatting of buffer
3436 * @...: resulting arguments
3438 int sscanf(const char *buf, const char *fmt, ...)
3443 va_start(args, fmt);
3444 i = vsscanf(buf, fmt, args);
3449 EXPORT_SYMBOL(sscanf);