]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - lib/vsprintf.c
lib/test_kmod.c: fix limit check on number of test devices created
[mirror_ubuntu-bionic-kernel.git] / lib / vsprintf.c
1 /*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
12 /*
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
17 */
18
19 #include <stdarg.h>
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/uuid.h>
34 #include <linux/of.h>
35 #include <net/addrconf.h>
36 #include <linux/siphash.h>
37 #include <linux/compiler.h>
38 #ifdef CONFIG_BLOCK
39 #include <linux/blkdev.h>
40 #endif
41
42 #include "../mm/internal.h" /* For the trace_print_flags arrays */
43
44 #include <asm/page.h> /* for PAGE_SIZE */
45 #include <asm/sections.h> /* for dereference_function_descriptor() */
46 #include <asm/byteorder.h> /* cpu_to_le16 */
47
48 #include <linux/string_helpers.h>
49 #include "kstrtox.h"
50
51 /**
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
56 *
57 * This function is obsolete. Please use kstrtoull instead.
58 */
59 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
60 {
61 unsigned long long result;
62 unsigned int rv;
63
64 cp = _parse_integer_fixup_radix(cp, &base);
65 rv = _parse_integer(cp, base, &result);
66 /* FIXME */
67 cp += (rv & ~KSTRTOX_OVERFLOW);
68
69 if (endp)
70 *endp = (char *)cp;
71
72 return result;
73 }
74 EXPORT_SYMBOL(simple_strtoull);
75
76 /**
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
81 *
82 * This function is obsolete. Please use kstrtoul instead.
83 */
84 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
85 {
86 return simple_strtoull(cp, endp, base);
87 }
88 EXPORT_SYMBOL(simple_strtoul);
89
90 /**
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
95 *
96 * This function is obsolete. Please use kstrtol instead.
97 */
98 long simple_strtol(const char *cp, char **endp, unsigned int base)
99 {
100 if (*cp == '-')
101 return -simple_strtoul(cp + 1, endp, base);
102
103 return simple_strtoul(cp, endp, base);
104 }
105 EXPORT_SYMBOL(simple_strtol);
106
107 /**
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
112 *
113 * This function is obsolete. Please use kstrtoll instead.
114 */
115 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
116 {
117 if (*cp == '-')
118 return -simple_strtoull(cp + 1, endp, base);
119
120 return simple_strtoull(cp, endp, base);
121 }
122 EXPORT_SYMBOL(simple_strtoll);
123
124 static noinline_for_stack
125 int skip_atoi(const char **s)
126 {
127 int i = 0;
128
129 do {
130 i = i*10 + *((*s)++) - '0';
131 } while (isdigit(**s));
132
133 return i;
134 }
135
136 /*
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).
146 *
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.
152 *
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.
155 *
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.
159 */
160
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),
173 #undef _
174 };
175
176 /*
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.
181 */
182 static noinline_for_stack
183 char *put_dec_trunc8(char *buf, unsigned r)
184 {
185 unsigned q;
186
187 /* 1 <= r < 10^8 */
188 if (r < 100)
189 goto out_r;
190
191 /* 100 <= r < 10^8 */
192 q = (r * (u64)0x28f5c29) >> 32;
193 *((u16 *)buf) = decpair[r - 100*q];
194 buf += 2;
195
196 /* 1 <= q < 10^6 */
197 if (q < 100)
198 goto out_q;
199
200 /* 100 <= q < 10^6 */
201 r = (q * (u64)0x28f5c29) >> 32;
202 *((u16 *)buf) = decpair[q - 100*r];
203 buf += 2;
204
205 /* 1 <= r < 10^4 */
206 if (r < 100)
207 goto out_r;
208
209 /* 100 <= r < 10^4 */
210 q = (r * 0x147b) >> 19;
211 *((u16 *)buf) = decpair[r - 100*q];
212 buf += 2;
213 out_q:
214 /* 1 <= q < 100 */
215 r = q;
216 out_r:
217 /* 1 <= r < 100 */
218 *((u16 *)buf) = decpair[r];
219 buf += r < 10 ? 1 : 2;
220 return buf;
221 }
222
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)
226 {
227 unsigned q;
228
229 /* 0 <= r < 10^8 */
230 q = (r * (u64)0x28f5c29) >> 32;
231 *((u16 *)buf) = decpair[r - 100*q];
232 buf += 2;
233
234 /* 0 <= q < 10^6 */
235 r = (q * (u64)0x28f5c29) >> 32;
236 *((u16 *)buf) = decpair[q - 100*r];
237 buf += 2;
238
239 /* 0 <= r < 10^4 */
240 q = (r * 0x147b) >> 19;
241 *((u16 *)buf) = decpair[r - 100*q];
242 buf += 2;
243
244 /* 0 <= q < 100 */
245 *((u16 *)buf) = decpair[q];
246 buf += 2;
247 return buf;
248 }
249
250 static noinline_for_stack
251 char *put_dec(char *buf, unsigned long long n)
252 {
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));
258 /* 1 <= n < 1e8 */
259 return put_dec_trunc8(buf, n);
260 }
261
262 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
263
264 static void
265 put_dec_full4(char *buf, unsigned r)
266 {
267 unsigned q;
268
269 /* 0 <= r < 10^4 */
270 q = (r * 0x147b) >> 19;
271 *((u16 *)buf) = decpair[r - 100*q];
272 buf += 2;
273 /* 0 <= q < 100 */
274 *((u16 *)buf) = decpair[q];
275 }
276
277 /*
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).
283 */
284 static noinline_for_stack
285 unsigned put_dec_helper4(char *buf, unsigned x)
286 {
287 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
288
289 put_dec_full4(buf, x - q * 10000);
290 return q;
291 }
292
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.
297 */
298 static
299 char *put_dec(char *buf, unsigned long long n)
300 {
301 uint32_t d3, d2, d1, q, h;
302
303 if (n < 100*1000*1000)
304 return put_dec_trunc8(buf, n);
305
306 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
307 h = (n >> 32);
308 d2 = (h ) & 0xffff;
309 d3 = (h >> 16); /* implicit "& 0xffff" */
310
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);
315
316 q += 7671 * d3 + 9496 * d2 + 6 * d1;
317 q = put_dec_helper4(buf+4, q);
318
319 q += 4749 * d3 + 42 * d2;
320 q = put_dec_helper4(buf+8, q);
321
322 q += 281 * d3;
323 buf += 12;
324 if (q)
325 buf = put_dec_trunc8(buf, q);
326 else while (buf[-1] == '0')
327 --buf;
328
329 return buf;
330 }
331
332 #endif
333
334 /*
335 * Convert passed number to decimal string.
336 * Returns the length of string. On buffer overflow, returns 0.
337 *
338 * If speed is not important, use snprintf(). It's easy to read the code.
339 */
340 int num_to_str(char *buf, int size, unsigned long long num)
341 {
342 /* put_dec requires 2-byte alignment of the buffer. */
343 char tmp[sizeof(num) * 3] __aligned(2);
344 int idx, len;
345
346 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
347 if (num <= 9) {
348 tmp[0] = '0' + num;
349 len = 1;
350 } else {
351 len = put_dec(tmp, num) - tmp;
352 }
353
354 if (len > size)
355 return 0;
356 for (idx = 0; idx < len; ++idx)
357 buf[idx] = tmp[len - idx - 1];
358 return len;
359 }
360
361 #define SIGN 1 /* unsigned/signed, must be 1 */
362 #define LEFT 2 /* left justified */
363 #define PLUS 4 /* show plus */
364 #define SPACE 8 /* space if plus */
365 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
366 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
367 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
368
369 enum format_type {
370 FORMAT_TYPE_NONE, /* Just a string part */
371 FORMAT_TYPE_WIDTH,
372 FORMAT_TYPE_PRECISION,
373 FORMAT_TYPE_CHAR,
374 FORMAT_TYPE_STR,
375 FORMAT_TYPE_PTR,
376 FORMAT_TYPE_PERCENT_CHAR,
377 FORMAT_TYPE_INVALID,
378 FORMAT_TYPE_LONG_LONG,
379 FORMAT_TYPE_ULONG,
380 FORMAT_TYPE_LONG,
381 FORMAT_TYPE_UBYTE,
382 FORMAT_TYPE_BYTE,
383 FORMAT_TYPE_USHORT,
384 FORMAT_TYPE_SHORT,
385 FORMAT_TYPE_UINT,
386 FORMAT_TYPE_INT,
387 FORMAT_TYPE_SIZE_T,
388 FORMAT_TYPE_PTRDIFF
389 };
390
391 struct printf_spec {
392 unsigned int type:8; /* format_type enum */
393 signed int field_width:24; /* width of output field */
394 unsigned int flags:8; /* flags to number() */
395 unsigned int base:8; /* number base, 8, 10 or 16 only */
396 signed int precision:16; /* # of digits/chars */
397 } __packed;
398 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
399 #define PRECISION_MAX ((1 << 15) - 1)
400
401 static noinline_for_stack
402 char *number(char *buf, char *end, unsigned long long num,
403 struct printf_spec spec)
404 {
405 /* put_dec requires 2-byte alignment of the buffer. */
406 char tmp[3 * sizeof(num)] __aligned(2);
407 char sign;
408 char locase;
409 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
410 int i;
411 bool is_zero = num == 0LL;
412 int field_width = spec.field_width;
413 int precision = spec.precision;
414
415 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
416
417 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
418 * produces same digits or (maybe lowercased) letters */
419 locase = (spec.flags & SMALL);
420 if (spec.flags & LEFT)
421 spec.flags &= ~ZEROPAD;
422 sign = 0;
423 if (spec.flags & SIGN) {
424 if ((signed long long)num < 0) {
425 sign = '-';
426 num = -(signed long long)num;
427 field_width--;
428 } else if (spec.flags & PLUS) {
429 sign = '+';
430 field_width--;
431 } else if (spec.flags & SPACE) {
432 sign = ' ';
433 field_width--;
434 }
435 }
436 if (need_pfx) {
437 if (spec.base == 16)
438 field_width -= 2;
439 else if (!is_zero)
440 field_width--;
441 }
442
443 /* generate full string in tmp[], in reverse order */
444 i = 0;
445 if (num < spec.base)
446 tmp[i++] = hex_asc_upper[num] | locase;
447 else if (spec.base != 10) { /* 8 or 16 */
448 int mask = spec.base - 1;
449 int shift = 3;
450
451 if (spec.base == 16)
452 shift = 4;
453 do {
454 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
455 num >>= shift;
456 } while (num);
457 } else { /* base 10 */
458 i = put_dec(tmp, num) - tmp;
459 }
460
461 /* printing 100 using %2d gives "100", not "00" */
462 if (i > precision)
463 precision = i;
464 /* leading space padding */
465 field_width -= precision;
466 if (!(spec.flags & (ZEROPAD | LEFT))) {
467 while (--field_width >= 0) {
468 if (buf < end)
469 *buf = ' ';
470 ++buf;
471 }
472 }
473 /* sign */
474 if (sign) {
475 if (buf < end)
476 *buf = sign;
477 ++buf;
478 }
479 /* "0x" / "0" prefix */
480 if (need_pfx) {
481 if (spec.base == 16 || !is_zero) {
482 if (buf < end)
483 *buf = '0';
484 ++buf;
485 }
486 if (spec.base == 16) {
487 if (buf < end)
488 *buf = ('X' | locase);
489 ++buf;
490 }
491 }
492 /* zero or space padding */
493 if (!(spec.flags & LEFT)) {
494 char c = ' ' + (spec.flags & ZEROPAD);
495 BUILD_BUG_ON(' ' + ZEROPAD != '0');
496 while (--field_width >= 0) {
497 if (buf < end)
498 *buf = c;
499 ++buf;
500 }
501 }
502 /* hmm even more zero padding? */
503 while (i <= --precision) {
504 if (buf < end)
505 *buf = '0';
506 ++buf;
507 }
508 /* actual digits of result */
509 while (--i >= 0) {
510 if (buf < end)
511 *buf = tmp[i];
512 ++buf;
513 }
514 /* trailing space padding */
515 while (--field_width >= 0) {
516 if (buf < end)
517 *buf = ' ';
518 ++buf;
519 }
520
521 return buf;
522 }
523
524 static noinline_for_stack
525 char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
526 {
527 struct printf_spec spec;
528
529 spec.type = FORMAT_TYPE_PTR;
530 spec.field_width = 2 + 2 * size; /* 0x + hex */
531 spec.flags = SPECIAL | SMALL | ZEROPAD;
532 spec.base = 16;
533 spec.precision = -1;
534
535 return number(buf, end, num, spec);
536 }
537
538 static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
539 {
540 size_t size;
541 if (buf >= end) /* nowhere to put anything */
542 return;
543 size = end - buf;
544 if (size <= spaces) {
545 memset(buf, ' ', size);
546 return;
547 }
548 if (len) {
549 if (len > size - spaces)
550 len = size - spaces;
551 memmove(buf + spaces, buf, len);
552 }
553 memset(buf, ' ', spaces);
554 }
555
556 /*
557 * Handle field width padding for a string.
558 * @buf: current buffer position
559 * @n: length of string
560 * @end: end of output buffer
561 * @spec: for field width and flags
562 * Returns: new buffer position after padding.
563 */
564 static noinline_for_stack
565 char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
566 {
567 unsigned spaces;
568
569 if (likely(n >= spec.field_width))
570 return buf;
571 /* we want to pad the sucker */
572 spaces = spec.field_width - n;
573 if (!(spec.flags & LEFT)) {
574 move_right(buf - n, end, n, spaces);
575 return buf + spaces;
576 }
577 while (spaces--) {
578 if (buf < end)
579 *buf = ' ';
580 ++buf;
581 }
582 return buf;
583 }
584
585 static noinline_for_stack
586 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
587 {
588 int len = 0;
589 size_t lim = spec.precision;
590
591 if ((unsigned long)s < PAGE_SIZE)
592 s = "(null)";
593
594 while (lim--) {
595 char c = *s++;
596 if (!c)
597 break;
598 if (buf < end)
599 *buf = c;
600 ++buf;
601 ++len;
602 }
603 return widen_string(buf, len, end, spec);
604 }
605
606 static noinline_for_stack
607 char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
608 const char *fmt)
609 {
610 const char *array[4], *s;
611 const struct dentry *p;
612 int depth;
613 int i, n;
614
615 switch (fmt[1]) {
616 case '2': case '3': case '4':
617 depth = fmt[1] - '0';
618 break;
619 default:
620 depth = 1;
621 }
622
623 rcu_read_lock();
624 for (i = 0; i < depth; i++, d = p) {
625 p = READ_ONCE(d->d_parent);
626 array[i] = READ_ONCE(d->d_name.name);
627 if (p == d) {
628 if (i)
629 array[i] = "";
630 i++;
631 break;
632 }
633 }
634 s = array[--i];
635 for (n = 0; n != spec.precision; n++, buf++) {
636 char c = *s++;
637 if (!c) {
638 if (!i)
639 break;
640 c = '/';
641 s = array[--i];
642 }
643 if (buf < end)
644 *buf = c;
645 }
646 rcu_read_unlock();
647 return widen_string(buf, n, end, spec);
648 }
649
650 #ifdef CONFIG_BLOCK
651 static noinline_for_stack
652 char *bdev_name(char *buf, char *end, struct block_device *bdev,
653 struct printf_spec spec, const char *fmt)
654 {
655 struct gendisk *hd = bdev->bd_disk;
656
657 buf = string(buf, end, hd->disk_name, spec);
658 if (bdev->bd_part->partno) {
659 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
660 if (buf < end)
661 *buf = 'p';
662 buf++;
663 }
664 buf = number(buf, end, bdev->bd_part->partno, spec);
665 }
666 return buf;
667 }
668 #endif
669
670 static noinline_for_stack
671 char *symbol_string(char *buf, char *end, void *ptr,
672 struct printf_spec spec, const char *fmt)
673 {
674 unsigned long value;
675 #ifdef CONFIG_KALLSYMS
676 char sym[KSYM_SYMBOL_LEN];
677 #endif
678
679 if (fmt[1] == 'R')
680 ptr = __builtin_extract_return_addr(ptr);
681 value = (unsigned long)ptr;
682
683 #ifdef CONFIG_KALLSYMS
684 if (*fmt == 'B')
685 sprint_backtrace(sym, value);
686 else if (*fmt != 'f' && *fmt != 's')
687 sprint_symbol(sym, value);
688 else
689 sprint_symbol_no_offset(sym, value);
690
691 return string(buf, end, sym, spec);
692 #else
693 return special_hex_number(buf, end, value, sizeof(void *));
694 #endif
695 }
696
697 static noinline_for_stack
698 char *resource_string(char *buf, char *end, struct resource *res,
699 struct printf_spec spec, const char *fmt)
700 {
701 #ifndef IO_RSRC_PRINTK_SIZE
702 #define IO_RSRC_PRINTK_SIZE 6
703 #endif
704
705 #ifndef MEM_RSRC_PRINTK_SIZE
706 #define MEM_RSRC_PRINTK_SIZE 10
707 #endif
708 static const struct printf_spec io_spec = {
709 .base = 16,
710 .field_width = IO_RSRC_PRINTK_SIZE,
711 .precision = -1,
712 .flags = SPECIAL | SMALL | ZEROPAD,
713 };
714 static const struct printf_spec mem_spec = {
715 .base = 16,
716 .field_width = MEM_RSRC_PRINTK_SIZE,
717 .precision = -1,
718 .flags = SPECIAL | SMALL | ZEROPAD,
719 };
720 static const struct printf_spec bus_spec = {
721 .base = 16,
722 .field_width = 2,
723 .precision = -1,
724 .flags = SMALL | ZEROPAD,
725 };
726 static const struct printf_spec dec_spec = {
727 .base = 10,
728 .precision = -1,
729 .flags = 0,
730 };
731 static const struct printf_spec str_spec = {
732 .field_width = -1,
733 .precision = 10,
734 .flags = LEFT,
735 };
736 static const struct printf_spec flag_spec = {
737 .base = 16,
738 .precision = -1,
739 .flags = SPECIAL | SMALL,
740 };
741
742 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
743 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
744 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
745 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
746 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
747 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
748 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
749 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
750
751 char *p = sym, *pend = sym + sizeof(sym);
752 int decode = (fmt[0] == 'R') ? 1 : 0;
753 const struct printf_spec *specp;
754
755 *p++ = '[';
756 if (res->flags & IORESOURCE_IO) {
757 p = string(p, pend, "io ", str_spec);
758 specp = &io_spec;
759 } else if (res->flags & IORESOURCE_MEM) {
760 p = string(p, pend, "mem ", str_spec);
761 specp = &mem_spec;
762 } else if (res->flags & IORESOURCE_IRQ) {
763 p = string(p, pend, "irq ", str_spec);
764 specp = &dec_spec;
765 } else if (res->flags & IORESOURCE_DMA) {
766 p = string(p, pend, "dma ", str_spec);
767 specp = &dec_spec;
768 } else if (res->flags & IORESOURCE_BUS) {
769 p = string(p, pend, "bus ", str_spec);
770 specp = &bus_spec;
771 } else {
772 p = string(p, pend, "??? ", str_spec);
773 specp = &mem_spec;
774 decode = 0;
775 }
776 if (decode && res->flags & IORESOURCE_UNSET) {
777 p = string(p, pend, "size ", str_spec);
778 p = number(p, pend, resource_size(res), *specp);
779 } else {
780 p = number(p, pend, res->start, *specp);
781 if (res->start != res->end) {
782 *p++ = '-';
783 p = number(p, pend, res->end, *specp);
784 }
785 }
786 if (decode) {
787 if (res->flags & IORESOURCE_MEM_64)
788 p = string(p, pend, " 64bit", str_spec);
789 if (res->flags & IORESOURCE_PREFETCH)
790 p = string(p, pend, " pref", str_spec);
791 if (res->flags & IORESOURCE_WINDOW)
792 p = string(p, pend, " window", str_spec);
793 if (res->flags & IORESOURCE_DISABLED)
794 p = string(p, pend, " disabled", str_spec);
795 } else {
796 p = string(p, pend, " flags ", str_spec);
797 p = number(p, pend, res->flags, flag_spec);
798 }
799 *p++ = ']';
800 *p = '\0';
801
802 return string(buf, end, sym, spec);
803 }
804
805 static noinline_for_stack
806 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
807 const char *fmt)
808 {
809 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
810 negative value, fallback to the default */
811 char separator;
812
813 if (spec.field_width == 0)
814 /* nothing to print */
815 return buf;
816
817 if (ZERO_OR_NULL_PTR(addr))
818 /* NULL pointer */
819 return string(buf, end, NULL, spec);
820
821 switch (fmt[1]) {
822 case 'C':
823 separator = ':';
824 break;
825 case 'D':
826 separator = '-';
827 break;
828 case 'N':
829 separator = 0;
830 break;
831 default:
832 separator = ' ';
833 break;
834 }
835
836 if (spec.field_width > 0)
837 len = min_t(int, spec.field_width, 64);
838
839 for (i = 0; i < len; ++i) {
840 if (buf < end)
841 *buf = hex_asc_hi(addr[i]);
842 ++buf;
843 if (buf < end)
844 *buf = hex_asc_lo(addr[i]);
845 ++buf;
846
847 if (separator && i != len - 1) {
848 if (buf < end)
849 *buf = separator;
850 ++buf;
851 }
852 }
853
854 return buf;
855 }
856
857 static noinline_for_stack
858 char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
859 struct printf_spec spec, const char *fmt)
860 {
861 const int CHUNKSZ = 32;
862 int nr_bits = max_t(int, spec.field_width, 0);
863 int i, chunksz;
864 bool first = true;
865
866 /* reused to print numbers */
867 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
868
869 chunksz = nr_bits & (CHUNKSZ - 1);
870 if (chunksz == 0)
871 chunksz = CHUNKSZ;
872
873 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
874 for (; i >= 0; i -= CHUNKSZ) {
875 u32 chunkmask, val;
876 int word, bit;
877
878 chunkmask = ((1ULL << chunksz) - 1);
879 word = i / BITS_PER_LONG;
880 bit = i % BITS_PER_LONG;
881 val = (bitmap[word] >> bit) & chunkmask;
882
883 if (!first) {
884 if (buf < end)
885 *buf = ',';
886 buf++;
887 }
888 first = false;
889
890 spec.field_width = DIV_ROUND_UP(chunksz, 4);
891 buf = number(buf, end, val, spec);
892
893 chunksz = CHUNKSZ;
894 }
895 return buf;
896 }
897
898 static noinline_for_stack
899 char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
900 struct printf_spec spec, const char *fmt)
901 {
902 int nr_bits = max_t(int, spec.field_width, 0);
903 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
904 int cur, rbot, rtop;
905 bool first = true;
906
907 /* reused to print numbers */
908 spec = (struct printf_spec){ .base = 10 };
909
910 rbot = cur = find_first_bit(bitmap, nr_bits);
911 while (cur < nr_bits) {
912 rtop = cur;
913 cur = find_next_bit(bitmap, nr_bits, cur + 1);
914 if (cur < nr_bits && cur <= rtop + 1)
915 continue;
916
917 if (!first) {
918 if (buf < end)
919 *buf = ',';
920 buf++;
921 }
922 first = false;
923
924 buf = number(buf, end, rbot, spec);
925 if (rbot < rtop) {
926 if (buf < end)
927 *buf = '-';
928 buf++;
929
930 buf = number(buf, end, rtop, spec);
931 }
932
933 rbot = cur;
934 }
935 return buf;
936 }
937
938 static noinline_for_stack
939 char *mac_address_string(char *buf, char *end, u8 *addr,
940 struct printf_spec spec, const char *fmt)
941 {
942 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
943 char *p = mac_addr;
944 int i;
945 char separator;
946 bool reversed = false;
947
948 switch (fmt[1]) {
949 case 'F':
950 separator = '-';
951 break;
952
953 case 'R':
954 reversed = true;
955 /* fall through */
956
957 default:
958 separator = ':';
959 break;
960 }
961
962 for (i = 0; i < 6; i++) {
963 if (reversed)
964 p = hex_byte_pack(p, addr[5 - i]);
965 else
966 p = hex_byte_pack(p, addr[i]);
967
968 if (fmt[0] == 'M' && i != 5)
969 *p++ = separator;
970 }
971 *p = '\0';
972
973 return string(buf, end, mac_addr, spec);
974 }
975
976 static noinline_for_stack
977 char *ip4_string(char *p, const u8 *addr, const char *fmt)
978 {
979 int i;
980 bool leading_zeros = (fmt[0] == 'i');
981 int index;
982 int step;
983
984 switch (fmt[2]) {
985 case 'h':
986 #ifdef __BIG_ENDIAN
987 index = 0;
988 step = 1;
989 #else
990 index = 3;
991 step = -1;
992 #endif
993 break;
994 case 'l':
995 index = 3;
996 step = -1;
997 break;
998 case 'n':
999 case 'b':
1000 default:
1001 index = 0;
1002 step = 1;
1003 break;
1004 }
1005 for (i = 0; i < 4; i++) {
1006 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
1007 int digits = put_dec_trunc8(temp, addr[index]) - temp;
1008 if (leading_zeros) {
1009 if (digits < 3)
1010 *p++ = '0';
1011 if (digits < 2)
1012 *p++ = '0';
1013 }
1014 /* reverse the digits in the quad */
1015 while (digits--)
1016 *p++ = temp[digits];
1017 if (i < 3)
1018 *p++ = '.';
1019 index += step;
1020 }
1021 *p = '\0';
1022
1023 return p;
1024 }
1025
1026 static noinline_for_stack
1027 char *ip6_compressed_string(char *p, const char *addr)
1028 {
1029 int i, j, range;
1030 unsigned char zerolength[8];
1031 int longest = 1;
1032 int colonpos = -1;
1033 u16 word;
1034 u8 hi, lo;
1035 bool needcolon = false;
1036 bool useIPv4;
1037 struct in6_addr in6;
1038
1039 memcpy(&in6, addr, sizeof(struct in6_addr));
1040
1041 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1042
1043 memset(zerolength, 0, sizeof(zerolength));
1044
1045 if (useIPv4)
1046 range = 6;
1047 else
1048 range = 8;
1049
1050 /* find position of longest 0 run */
1051 for (i = 0; i < range; i++) {
1052 for (j = i; j < range; j++) {
1053 if (in6.s6_addr16[j] != 0)
1054 break;
1055 zerolength[i]++;
1056 }
1057 }
1058 for (i = 0; i < range; i++) {
1059 if (zerolength[i] > longest) {
1060 longest = zerolength[i];
1061 colonpos = i;
1062 }
1063 }
1064 if (longest == 1) /* don't compress a single 0 */
1065 colonpos = -1;
1066
1067 /* emit address */
1068 for (i = 0; i < range; i++) {
1069 if (i == colonpos) {
1070 if (needcolon || i == 0)
1071 *p++ = ':';
1072 *p++ = ':';
1073 needcolon = false;
1074 i += longest - 1;
1075 continue;
1076 }
1077 if (needcolon) {
1078 *p++ = ':';
1079 needcolon = false;
1080 }
1081 /* hex u16 without leading 0s */
1082 word = ntohs(in6.s6_addr16[i]);
1083 hi = word >> 8;
1084 lo = word & 0xff;
1085 if (hi) {
1086 if (hi > 0x0f)
1087 p = hex_byte_pack(p, hi);
1088 else
1089 *p++ = hex_asc_lo(hi);
1090 p = hex_byte_pack(p, lo);
1091 }
1092 else if (lo > 0x0f)
1093 p = hex_byte_pack(p, lo);
1094 else
1095 *p++ = hex_asc_lo(lo);
1096 needcolon = true;
1097 }
1098
1099 if (useIPv4) {
1100 if (needcolon)
1101 *p++ = ':';
1102 p = ip4_string(p, &in6.s6_addr[12], "I4");
1103 }
1104 *p = '\0';
1105
1106 return p;
1107 }
1108
1109 static noinline_for_stack
1110 char *ip6_string(char *p, const char *addr, const char *fmt)
1111 {
1112 int i;
1113
1114 for (i = 0; i < 8; i++) {
1115 p = hex_byte_pack(p, *addr++);
1116 p = hex_byte_pack(p, *addr++);
1117 if (fmt[0] == 'I' && i != 7)
1118 *p++ = ':';
1119 }
1120 *p = '\0';
1121
1122 return p;
1123 }
1124
1125 static noinline_for_stack
1126 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1127 struct printf_spec spec, const char *fmt)
1128 {
1129 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1130
1131 if (fmt[0] == 'I' && fmt[2] == 'c')
1132 ip6_compressed_string(ip6_addr, addr);
1133 else
1134 ip6_string(ip6_addr, addr, fmt);
1135
1136 return string(buf, end, ip6_addr, spec);
1137 }
1138
1139 static noinline_for_stack
1140 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1141 struct printf_spec spec, const char *fmt)
1142 {
1143 char ip4_addr[sizeof("255.255.255.255")];
1144
1145 ip4_string(ip4_addr, addr, fmt);
1146
1147 return string(buf, end, ip4_addr, spec);
1148 }
1149
1150 static noinline_for_stack
1151 char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1152 struct printf_spec spec, const char *fmt)
1153 {
1154 bool have_p = false, have_s = false, have_f = false, have_c = false;
1155 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1156 sizeof(":12345") + sizeof("/123456789") +
1157 sizeof("%1234567890")];
1158 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1159 const u8 *addr = (const u8 *) &sa->sin6_addr;
1160 char fmt6[2] = { fmt[0], '6' };
1161 u8 off = 0;
1162
1163 fmt++;
1164 while (isalpha(*++fmt)) {
1165 switch (*fmt) {
1166 case 'p':
1167 have_p = true;
1168 break;
1169 case 'f':
1170 have_f = true;
1171 break;
1172 case 's':
1173 have_s = true;
1174 break;
1175 case 'c':
1176 have_c = true;
1177 break;
1178 }
1179 }
1180
1181 if (have_p || have_s || have_f) {
1182 *p = '[';
1183 off = 1;
1184 }
1185
1186 if (fmt6[0] == 'I' && have_c)
1187 p = ip6_compressed_string(ip6_addr + off, addr);
1188 else
1189 p = ip6_string(ip6_addr + off, addr, fmt6);
1190
1191 if (have_p || have_s || have_f)
1192 *p++ = ']';
1193
1194 if (have_p) {
1195 *p++ = ':';
1196 p = number(p, pend, ntohs(sa->sin6_port), spec);
1197 }
1198 if (have_f) {
1199 *p++ = '/';
1200 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1201 IPV6_FLOWINFO_MASK), spec);
1202 }
1203 if (have_s) {
1204 *p++ = '%';
1205 p = number(p, pend, sa->sin6_scope_id, spec);
1206 }
1207 *p = '\0';
1208
1209 return string(buf, end, ip6_addr, spec);
1210 }
1211
1212 static noinline_for_stack
1213 char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1214 struct printf_spec spec, const char *fmt)
1215 {
1216 bool have_p = false;
1217 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1218 char *pend = ip4_addr + sizeof(ip4_addr);
1219 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1220 char fmt4[3] = { fmt[0], '4', 0 };
1221
1222 fmt++;
1223 while (isalpha(*++fmt)) {
1224 switch (*fmt) {
1225 case 'p':
1226 have_p = true;
1227 break;
1228 case 'h':
1229 case 'l':
1230 case 'n':
1231 case 'b':
1232 fmt4[2] = *fmt;
1233 break;
1234 }
1235 }
1236
1237 p = ip4_string(ip4_addr, addr, fmt4);
1238 if (have_p) {
1239 *p++ = ':';
1240 p = number(p, pend, ntohs(sa->sin_port), spec);
1241 }
1242 *p = '\0';
1243
1244 return string(buf, end, ip4_addr, spec);
1245 }
1246
1247 static noinline_for_stack
1248 char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1249 const char *fmt)
1250 {
1251 bool found = true;
1252 int count = 1;
1253 unsigned int flags = 0;
1254 int len;
1255
1256 if (spec.field_width == 0)
1257 return buf; /* nothing to print */
1258
1259 if (ZERO_OR_NULL_PTR(addr))
1260 return string(buf, end, NULL, spec); /* NULL pointer */
1261
1262
1263 do {
1264 switch (fmt[count++]) {
1265 case 'a':
1266 flags |= ESCAPE_ANY;
1267 break;
1268 case 'c':
1269 flags |= ESCAPE_SPECIAL;
1270 break;
1271 case 'h':
1272 flags |= ESCAPE_HEX;
1273 break;
1274 case 'n':
1275 flags |= ESCAPE_NULL;
1276 break;
1277 case 'o':
1278 flags |= ESCAPE_OCTAL;
1279 break;
1280 case 'p':
1281 flags |= ESCAPE_NP;
1282 break;
1283 case 's':
1284 flags |= ESCAPE_SPACE;
1285 break;
1286 default:
1287 found = false;
1288 break;
1289 }
1290 } while (found);
1291
1292 if (!flags)
1293 flags = ESCAPE_ANY_NP;
1294
1295 len = spec.field_width < 0 ? 1 : spec.field_width;
1296
1297 /*
1298 * string_escape_mem() writes as many characters as it can to
1299 * the given buffer, and returns the total size of the output
1300 * had the buffer been big enough.
1301 */
1302 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1303
1304 return buf;
1305 }
1306
1307 static noinline_for_stack
1308 char *uuid_string(char *buf, char *end, const u8 *addr,
1309 struct printf_spec spec, const char *fmt)
1310 {
1311 char uuid[UUID_STRING_LEN + 1];
1312 char *p = uuid;
1313 int i;
1314 const u8 *index = uuid_index;
1315 bool uc = false;
1316
1317 switch (*(++fmt)) {
1318 case 'L':
1319 uc = true; /* fall-through */
1320 case 'l':
1321 index = guid_index;
1322 break;
1323 case 'B':
1324 uc = true;
1325 break;
1326 }
1327
1328 for (i = 0; i < 16; i++) {
1329 if (uc)
1330 p = hex_byte_pack_upper(p, addr[index[i]]);
1331 else
1332 p = hex_byte_pack(p, addr[index[i]]);
1333 switch (i) {
1334 case 3:
1335 case 5:
1336 case 7:
1337 case 9:
1338 *p++ = '-';
1339 break;
1340 }
1341 }
1342
1343 *p = 0;
1344
1345 return string(buf, end, uuid, spec);
1346 }
1347
1348 int kptr_restrict __read_mostly;
1349
1350 static noinline_for_stack
1351 char *restricted_pointer(char *buf, char *end, const void *ptr,
1352 struct printf_spec spec)
1353 {
1354 spec.base = 16;
1355 spec.flags |= SMALL;
1356 if (spec.field_width == -1) {
1357 spec.field_width = 2 * sizeof(ptr);
1358 spec.flags |= ZEROPAD;
1359 }
1360
1361 switch (kptr_restrict) {
1362 case 0:
1363 /* Always print %pK values */
1364 break;
1365 case 1: {
1366 const struct cred *cred;
1367
1368 /*
1369 * kptr_restrict==1 cannot be used in IRQ context
1370 * because its test for CAP_SYSLOG would be meaningless.
1371 */
1372 if (in_irq() || in_serving_softirq() || in_nmi())
1373 return string(buf, end, "pK-error", spec);
1374
1375 /*
1376 * Only print the real pointer value if the current
1377 * process has CAP_SYSLOG and is running with the
1378 * same credentials it started with. This is because
1379 * access to files is checked at open() time, but %pK
1380 * checks permission at read() time. We don't want to
1381 * leak pointer values if a binary opens a file using
1382 * %pK and then elevates privileges before reading it.
1383 */
1384 cred = current_cred();
1385 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1386 !uid_eq(cred->euid, cred->uid) ||
1387 !gid_eq(cred->egid, cred->gid))
1388 ptr = NULL;
1389 break;
1390 }
1391 case 2:
1392 default:
1393 /* Always print 0's for %pK */
1394 ptr = NULL;
1395 break;
1396 }
1397
1398 return number(buf, end, (unsigned long)ptr, spec);
1399 }
1400
1401 static noinline_for_stack
1402 char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
1403 {
1404 unsigned long long num;
1405 int size;
1406
1407 switch (fmt[1]) {
1408 case 'F':
1409 num = *(const netdev_features_t *)addr;
1410 size = sizeof(netdev_features_t);
1411 break;
1412 default:
1413 num = (unsigned long)addr;
1414 size = sizeof(unsigned long);
1415 break;
1416 }
1417
1418 return special_hex_number(buf, end, num, size);
1419 }
1420
1421 static noinline_for_stack
1422 char *address_val(char *buf, char *end, const void *addr, const char *fmt)
1423 {
1424 unsigned long long num;
1425 int size;
1426
1427 switch (fmt[1]) {
1428 case 'd':
1429 num = *(const dma_addr_t *)addr;
1430 size = sizeof(dma_addr_t);
1431 break;
1432 case 'p':
1433 default:
1434 num = *(const phys_addr_t *)addr;
1435 size = sizeof(phys_addr_t);
1436 break;
1437 }
1438
1439 return special_hex_number(buf, end, num, size);
1440 }
1441
1442 static noinline_for_stack
1443 char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1444 const char *fmt)
1445 {
1446 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1447 return string(buf, end, NULL, spec);
1448
1449 switch (fmt[1]) {
1450 case 'r':
1451 return number(buf, end, clk_get_rate(clk), spec);
1452
1453 case 'n':
1454 default:
1455 #ifdef CONFIG_COMMON_CLK
1456 return string(buf, end, __clk_get_name(clk), spec);
1457 #else
1458 return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
1459 #endif
1460 }
1461 }
1462
1463 static
1464 char *format_flags(char *buf, char *end, unsigned long flags,
1465 const struct trace_print_flags *names)
1466 {
1467 unsigned long mask;
1468 const struct printf_spec strspec = {
1469 .field_width = -1,
1470 .precision = -1,
1471 };
1472 const struct printf_spec numspec = {
1473 .flags = SPECIAL|SMALL,
1474 .field_width = -1,
1475 .precision = -1,
1476 .base = 16,
1477 };
1478
1479 for ( ; flags && names->name; names++) {
1480 mask = names->mask;
1481 if ((flags & mask) != mask)
1482 continue;
1483
1484 buf = string(buf, end, names->name, strspec);
1485
1486 flags &= ~mask;
1487 if (flags) {
1488 if (buf < end)
1489 *buf = '|';
1490 buf++;
1491 }
1492 }
1493
1494 if (flags)
1495 buf = number(buf, end, flags, numspec);
1496
1497 return buf;
1498 }
1499
1500 static noinline_for_stack
1501 char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1502 {
1503 unsigned long flags;
1504 const struct trace_print_flags *names;
1505
1506 switch (fmt[1]) {
1507 case 'p':
1508 flags = *(unsigned long *)flags_ptr;
1509 /* Remove zone id */
1510 flags &= (1UL << NR_PAGEFLAGS) - 1;
1511 names = pageflag_names;
1512 break;
1513 case 'v':
1514 flags = *(unsigned long *)flags_ptr;
1515 names = vmaflag_names;
1516 break;
1517 case 'g':
1518 flags = *(gfp_t *)flags_ptr;
1519 names = gfpflag_names;
1520 break;
1521 default:
1522 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1523 return buf;
1524 }
1525
1526 return format_flags(buf, end, flags, names);
1527 }
1528
1529 static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1530 {
1531 for ( ; np && depth; depth--)
1532 np = np->parent;
1533
1534 return kbasename(np->full_name);
1535 }
1536
1537 static noinline_for_stack
1538 char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1539 {
1540 int depth;
1541 const struct device_node *parent = np->parent;
1542 static const struct printf_spec strspec = {
1543 .field_width = -1,
1544 .precision = -1,
1545 };
1546
1547 /* special case for root node */
1548 if (!parent)
1549 return string(buf, end, "/", strspec);
1550
1551 for (depth = 0; parent->parent; depth++)
1552 parent = parent->parent;
1553
1554 for ( ; depth >= 0; depth--) {
1555 buf = string(buf, end, "/", strspec);
1556 buf = string(buf, end, device_node_name_for_depth(np, depth),
1557 strspec);
1558 }
1559 return buf;
1560 }
1561
1562 static noinline_for_stack
1563 char *device_node_string(char *buf, char *end, struct device_node *dn,
1564 struct printf_spec spec, const char *fmt)
1565 {
1566 char tbuf[sizeof("xxxx") + 1];
1567 const char *p;
1568 int ret;
1569 char *buf_start = buf;
1570 struct property *prop;
1571 bool has_mult, pass;
1572 static const struct printf_spec num_spec = {
1573 .flags = SMALL,
1574 .field_width = -1,
1575 .precision = -1,
1576 .base = 10,
1577 };
1578
1579 struct printf_spec str_spec = spec;
1580 str_spec.field_width = -1;
1581
1582 if (!IS_ENABLED(CONFIG_OF))
1583 return string(buf, end, "(!OF)", spec);
1584
1585 if ((unsigned long)dn < PAGE_SIZE)
1586 return string(buf, end, "(null)", spec);
1587
1588 /* simple case without anything any more format specifiers */
1589 fmt++;
1590 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1591 fmt = "f";
1592
1593 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1594 if (pass) {
1595 if (buf < end)
1596 *buf = ':';
1597 buf++;
1598 }
1599
1600 switch (*fmt) {
1601 case 'f': /* full_name */
1602 buf = device_node_gen_full_name(dn, buf, end);
1603 break;
1604 case 'n': /* name */
1605 buf = string(buf, end, dn->name, str_spec);
1606 break;
1607 case 'p': /* phandle */
1608 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1609 break;
1610 case 'P': /* path-spec */
1611 p = kbasename(of_node_full_name(dn));
1612 if (!p[1])
1613 p = "/";
1614 buf = string(buf, end, p, str_spec);
1615 break;
1616 case 'F': /* flags */
1617 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1618 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1619 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1620 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1621 tbuf[4] = 0;
1622 buf = string(buf, end, tbuf, str_spec);
1623 break;
1624 case 'c': /* major compatible string */
1625 ret = of_property_read_string(dn, "compatible", &p);
1626 if (!ret)
1627 buf = string(buf, end, p, str_spec);
1628 break;
1629 case 'C': /* full compatible string */
1630 has_mult = false;
1631 of_property_for_each_string(dn, "compatible", prop, p) {
1632 if (has_mult)
1633 buf = string(buf, end, ",", str_spec);
1634 buf = string(buf, end, "\"", str_spec);
1635 buf = string(buf, end, p, str_spec);
1636 buf = string(buf, end, "\"", str_spec);
1637
1638 has_mult = true;
1639 }
1640 break;
1641 default:
1642 break;
1643 }
1644 }
1645
1646 return widen_string(buf, buf - buf_start, end, spec);
1647 }
1648
1649 static noinline_for_stack
1650 char *pointer_string(char *buf, char *end, const void *ptr,
1651 struct printf_spec spec)
1652 {
1653 spec.base = 16;
1654 spec.flags |= SMALL;
1655 if (spec.field_width == -1) {
1656 spec.field_width = 2 * sizeof(ptr);
1657 spec.flags |= ZEROPAD;
1658 }
1659
1660 return number(buf, end, (unsigned long int)ptr, spec);
1661 }
1662
1663 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
1664 static siphash_key_t ptr_key __read_mostly;
1665
1666 static void enable_ptr_key_workfn(struct work_struct *work)
1667 {
1668 get_random_bytes(&ptr_key, sizeof(ptr_key));
1669 /* Needs to run from preemptible context */
1670 static_branch_disable(&not_filled_random_ptr_key);
1671 }
1672
1673 static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
1674
1675 static void fill_random_ptr_key(struct random_ready_callback *unused)
1676 {
1677 /* This may be in an interrupt handler. */
1678 queue_work(system_unbound_wq, &enable_ptr_key_work);
1679 }
1680
1681 static struct random_ready_callback random_ready = {
1682 .func = fill_random_ptr_key
1683 };
1684
1685 static int __init initialize_ptr_random(void)
1686 {
1687 int ret = add_random_ready_callback(&random_ready);
1688
1689 if (!ret) {
1690 return 0;
1691 } else if (ret == -EALREADY) {
1692 /* This is in preemptible context */
1693 enable_ptr_key_workfn(&enable_ptr_key_work);
1694 return 0;
1695 }
1696
1697 return ret;
1698 }
1699 early_initcall(initialize_ptr_random);
1700
1701 /* Maps a pointer to a 32 bit unique identifier. */
1702 static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1703 {
1704 unsigned long hashval;
1705 const int default_width = 2 * sizeof(ptr);
1706
1707 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
1708 spec.field_width = default_width;
1709 /* string length must be less than default_width */
1710 return string(buf, end, "(ptrval)", spec);
1711 }
1712
1713 #ifdef CONFIG_64BIT
1714 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1715 /*
1716 * Mask off the first 32 bits, this makes explicit that we have
1717 * modified the address (and 32 bits is plenty for a unique ID).
1718 */
1719 hashval = hashval & 0xffffffff;
1720 #else
1721 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1722 #endif
1723
1724 spec.flags |= SMALL;
1725 if (spec.field_width == -1) {
1726 spec.field_width = default_width;
1727 spec.flags |= ZEROPAD;
1728 }
1729 spec.base = 16;
1730
1731 return number(buf, end, hashval, spec);
1732 }
1733
1734 #ifdef CONFIG_KMSG_IDS
1735
1736 unsigned long long __jhash_string(const char *str);
1737
1738 static noinline_for_stack
1739 char *jhash_string(char *buf, char *end, const char *str, const char *fmt)
1740 {
1741 struct printf_spec spec;
1742 unsigned long long num;
1743
1744 num = __jhash_string(str);
1745
1746 spec.type = FORMAT_TYPE_PTR;
1747 spec.field_width = 6;
1748 spec.flags = SMALL | ZEROPAD;
1749 spec.base = 16;
1750 spec.precision = -1;
1751
1752 return number(buf, end, num, spec);
1753 }
1754
1755 #endif
1756
1757 /*
1758 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1759 * by an extra set of alphanumeric characters that are extended format
1760 * specifiers.
1761 *
1762 * Please update scripts/checkpatch.pl when adding/removing conversion
1763 * characters. (Search for "check for vsprintf extension").
1764 *
1765 * Right now we handle:
1766 *
1767 * - 'F' For symbolic function descriptor pointers with offset
1768 * - 'f' For simple symbolic function names without offset
1769 * - 'S' For symbolic direct pointers with offset
1770 * - 's' For symbolic direct pointers without offset
1771 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1772 * - 'B' For backtraced symbolic direct pointers with offset
1773 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1774 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1775 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1776 * width which must be explicitly specified either as part of the
1777 * format string '%32b[l]' or through '%*b[l]', [l] selects
1778 * range-list format instead of hex format
1779 * - 'M' For a 6-byte MAC address, it prints the address in the
1780 * usual colon-separated hex notation
1781 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1782 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1783 * with a dash-separated hex notation
1784 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1785 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1786 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1787 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1788 * [S][pfs]
1789 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1790 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1791 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1792 * IPv6 omits the colons (01020304...0f)
1793 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1794 * [S][pfs]
1795 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1796 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1797 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1798 * - 'I[6S]c' for IPv6 addresses printed as specified by
1799 * http://tools.ietf.org/html/rfc5952
1800 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1801 * of the following flags (see string_escape_mem() for the
1802 * details):
1803 * a - ESCAPE_ANY
1804 * c - ESCAPE_SPECIAL
1805 * h - ESCAPE_HEX
1806 * n - ESCAPE_NULL
1807 * o - ESCAPE_OCTAL
1808 * p - ESCAPE_NP
1809 * s - ESCAPE_SPACE
1810 * By default ESCAPE_ANY_NP is used.
1811 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1812 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1813 * Options for %pU are:
1814 * b big endian lower case hex (default)
1815 * B big endian UPPER case hex
1816 * l little endian lower case hex
1817 * L little endian UPPER case hex
1818 * big endian output byte order is:
1819 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1820 * little endian output byte order is:
1821 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1822 * - 'V' For a struct va_format which contains a format string * and va_list *,
1823 * call vsnprintf(->format, *->va_list).
1824 * Implements a "recursive vsnprintf".
1825 * Do not use this feature without some mechanism to verify the
1826 * correctness of the format string and va_list arguments.
1827 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1828 * - 'NF' For a netdev_features_t
1829 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1830 * a certain separator (' ' by default):
1831 * C colon
1832 * D dash
1833 * N no separator
1834 * The maximum supported length is 64 bytes of the input. Consider
1835 * to use print_hex_dump() for the larger input.
1836 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1837 * (default assumed to be phys_addr_t, passed by reference)
1838 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1839 * - 'D[234]' Same as 'd' but for a struct file
1840 * - 'g' For block_device name (gendisk + partition number)
1841 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1842 * (legacy clock framework) of the clock
1843 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1844 * (legacy clock framework) of the clock
1845 * - 'Cr' For a clock, it prints the current rate of the clock
1846 * - 'G' For flags to be printed as a collection of symbolic strings that would
1847 * construct the specific value. Supported flags given by option:
1848 * p page flags (see struct page) given as pointer to unsigned long
1849 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1850 * v vma flags (VM_*) given as pointer to unsigned long
1851 * - 'j' Kernel message catalog jhash for System z
1852 * - 'O' For a kobject based struct. Must be one of the following:
1853 * - 'OF[fnpPcCF]' For a device tree object
1854 * Without any optional arguments prints the full_name
1855 * f device node full_name
1856 * n device node name
1857 * p device node phandle
1858 * P device node path spec (name + @unit)
1859 * F device node flags
1860 * c major compatible string
1861 * C full compatible string
1862 *
1863 * - 'x' For printing the address. Equivalent to "%lx".
1864 *
1865 * ** Please update also Documentation/printk-formats.txt when making changes **
1866 *
1867 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1868 * function pointers are really function descriptors, which contain a
1869 * pointer to the real address.
1870 *
1871 * Note: The default behaviour (unadorned %p) is to hash the address,
1872 * rendering it useful as a unique identifier.
1873 */
1874 static noinline_for_stack
1875 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1876 struct printf_spec spec)
1877 {
1878 const int default_width = 2 * sizeof(void *);
1879
1880 if (!ptr && *fmt != 'K' && *fmt != 'x') {
1881 /*
1882 * Print (null) with the same width as a pointer so it makes
1883 * tabular output look nice.
1884 */
1885 if (spec.field_width == -1)
1886 spec.field_width = default_width;
1887 return string(buf, end, "(null)", spec);
1888 }
1889
1890 switch (*fmt) {
1891 case 'F':
1892 case 'f':
1893 ptr = dereference_function_descriptor(ptr);
1894 /* Fallthrough */
1895 case 'S':
1896 case 's':
1897 case 'B':
1898 return symbol_string(buf, end, ptr, spec, fmt);
1899 case 'R':
1900 case 'r':
1901 return resource_string(buf, end, ptr, spec, fmt);
1902 case 'h':
1903 return hex_string(buf, end, ptr, spec, fmt);
1904 case 'b':
1905 switch (fmt[1]) {
1906 case 'l':
1907 return bitmap_list_string(buf, end, ptr, spec, fmt);
1908 default:
1909 return bitmap_string(buf, end, ptr, spec, fmt);
1910 }
1911 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1912 case 'm': /* Contiguous: 000102030405 */
1913 /* [mM]F (FDDI) */
1914 /* [mM]R (Reverse order; Bluetooth) */
1915 return mac_address_string(buf, end, ptr, spec, fmt);
1916 case 'I': /* Formatted IP supported
1917 * 4: 1.2.3.4
1918 * 6: 0001:0203:...:0708
1919 * 6c: 1::708 or 1::1.2.3.4
1920 */
1921 case 'i': /* Contiguous:
1922 * 4: 001.002.003.004
1923 * 6: 000102...0f
1924 */
1925 switch (fmt[1]) {
1926 case '6':
1927 return ip6_addr_string(buf, end, ptr, spec, fmt);
1928 case '4':
1929 return ip4_addr_string(buf, end, ptr, spec, fmt);
1930 case 'S': {
1931 const union {
1932 struct sockaddr raw;
1933 struct sockaddr_in v4;
1934 struct sockaddr_in6 v6;
1935 } *sa = ptr;
1936
1937 switch (sa->raw.sa_family) {
1938 case AF_INET:
1939 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1940 case AF_INET6:
1941 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1942 default:
1943 return string(buf, end, "(invalid address)", spec);
1944 }}
1945 }
1946 break;
1947 case 'E':
1948 return escaped_string(buf, end, ptr, spec, fmt);
1949 case 'U':
1950 return uuid_string(buf, end, ptr, spec, fmt);
1951 case 'V':
1952 {
1953 va_list va;
1954
1955 va_copy(va, *((struct va_format *)ptr)->va);
1956 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1957 ((struct va_format *)ptr)->fmt, va);
1958 va_end(va);
1959 return buf;
1960 }
1961 case 'K':
1962 if (!kptr_restrict)
1963 break;
1964 return restricted_pointer(buf, end, ptr, spec);
1965 case 'N':
1966 return netdev_bits(buf, end, ptr, fmt);
1967 case 'a':
1968 return address_val(buf, end, ptr, fmt);
1969 case 'd':
1970 return dentry_name(buf, end, ptr, spec, fmt);
1971 case 'C':
1972 return clock(buf, end, ptr, spec, fmt);
1973 case 'D':
1974 return dentry_name(buf, end,
1975 ((const struct file *)ptr)->f_path.dentry,
1976 spec, fmt);
1977 #ifdef CONFIG_BLOCK
1978 case 'g':
1979 return bdev_name(buf, end, ptr, spec, fmt);
1980 #endif
1981
1982 case 'G':
1983 return flags_string(buf, end, ptr, fmt);
1984 case 'O':
1985 switch (fmt[1]) {
1986 case 'F':
1987 return device_node_string(buf, end, ptr, spec, fmt + 1);
1988 }
1989 case 'x':
1990 return pointer_string(buf, end, ptr, spec);
1991 #ifdef CONFIG_KMSG_IDS
1992 case 'j':
1993 return jhash_string(buf, end, ptr, fmt);
1994 #endif
1995 }
1996
1997 /* default is to _not_ leak addresses, hash before printing */
1998 return ptr_to_id(buf, end, ptr, spec);
1999 }
2000
2001 /*
2002 * Helper function to decode printf style format.
2003 * Each call decode a token from the format and return the
2004 * number of characters read (or likely the delta where it wants
2005 * to go on the next call).
2006 * The decoded token is returned through the parameters
2007 *
2008 * 'h', 'l', or 'L' for integer fields
2009 * 'z' support added 23/7/1999 S.H.
2010 * 'z' changed to 'Z' --davidm 1/25/99
2011 * 'Z' changed to 'z' --adobriyan 2017-01-25
2012 * 't' added for ptrdiff_t
2013 *
2014 * @fmt: the format string
2015 * @type of the token returned
2016 * @flags: various flags such as +, -, # tokens..
2017 * @field_width: overwritten width
2018 * @base: base of the number (octal, hex, ...)
2019 * @precision: precision of a number
2020 * @qualifier: qualifier of a number (long, size_t, ...)
2021 */
2022 static noinline_for_stack
2023 int format_decode(const char *fmt, struct printf_spec *spec)
2024 {
2025 const char *start = fmt;
2026 char qualifier;
2027
2028 /* we finished early by reading the field width */
2029 if (spec->type == FORMAT_TYPE_WIDTH) {
2030 if (spec->field_width < 0) {
2031 spec->field_width = -spec->field_width;
2032 spec->flags |= LEFT;
2033 }
2034 spec->type = FORMAT_TYPE_NONE;
2035 goto precision;
2036 }
2037
2038 /* we finished early by reading the precision */
2039 if (spec->type == FORMAT_TYPE_PRECISION) {
2040 if (spec->precision < 0)
2041 spec->precision = 0;
2042
2043 spec->type = FORMAT_TYPE_NONE;
2044 goto qualifier;
2045 }
2046
2047 /* By default */
2048 spec->type = FORMAT_TYPE_NONE;
2049
2050 for (; *fmt ; ++fmt) {
2051 if (*fmt == '%')
2052 break;
2053 }
2054
2055 /* Return the current non-format string */
2056 if (fmt != start || !*fmt)
2057 return fmt - start;
2058
2059 /* Process flags */
2060 spec->flags = 0;
2061
2062 while (1) { /* this also skips first '%' */
2063 bool found = true;
2064
2065 ++fmt;
2066
2067 switch (*fmt) {
2068 case '-': spec->flags |= LEFT; break;
2069 case '+': spec->flags |= PLUS; break;
2070 case ' ': spec->flags |= SPACE; break;
2071 case '#': spec->flags |= SPECIAL; break;
2072 case '0': spec->flags |= ZEROPAD; break;
2073 default: found = false;
2074 }
2075
2076 if (!found)
2077 break;
2078 }
2079
2080 /* get field width */
2081 spec->field_width = -1;
2082
2083 if (isdigit(*fmt))
2084 spec->field_width = skip_atoi(&fmt);
2085 else if (*fmt == '*') {
2086 /* it's the next argument */
2087 spec->type = FORMAT_TYPE_WIDTH;
2088 return ++fmt - start;
2089 }
2090
2091 precision:
2092 /* get the precision */
2093 spec->precision = -1;
2094 if (*fmt == '.') {
2095 ++fmt;
2096 if (isdigit(*fmt)) {
2097 spec->precision = skip_atoi(&fmt);
2098 if (spec->precision < 0)
2099 spec->precision = 0;
2100 } else if (*fmt == '*') {
2101 /* it's the next argument */
2102 spec->type = FORMAT_TYPE_PRECISION;
2103 return ++fmt - start;
2104 }
2105 }
2106
2107 qualifier:
2108 /* get the conversion qualifier */
2109 qualifier = 0;
2110 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2111 *fmt == 'z' || *fmt == 't') {
2112 qualifier = *fmt++;
2113 if (unlikely(qualifier == *fmt)) {
2114 if (qualifier == 'l') {
2115 qualifier = 'L';
2116 ++fmt;
2117 } else if (qualifier == 'h') {
2118 qualifier = 'H';
2119 ++fmt;
2120 }
2121 }
2122 }
2123
2124 /* default base */
2125 spec->base = 10;
2126 switch (*fmt) {
2127 case 'c':
2128 spec->type = FORMAT_TYPE_CHAR;
2129 return ++fmt - start;
2130
2131 case 's':
2132 spec->type = FORMAT_TYPE_STR;
2133 return ++fmt - start;
2134
2135 case 'p':
2136 spec->type = FORMAT_TYPE_PTR;
2137 return ++fmt - start;
2138
2139 case '%':
2140 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2141 return ++fmt - start;
2142
2143 /* integer number formats - set up the flags and "break" */
2144 case 'o':
2145 spec->base = 8;
2146 break;
2147
2148 case 'x':
2149 spec->flags |= SMALL;
2150
2151 case 'X':
2152 spec->base = 16;
2153 break;
2154
2155 case 'd':
2156 case 'i':
2157 spec->flags |= SIGN;
2158 case 'u':
2159 break;
2160
2161 case 'n':
2162 /*
2163 * Since %n poses a greater security risk than
2164 * utility, treat it as any other invalid or
2165 * unsupported format specifier.
2166 */
2167 /* Fall-through */
2168
2169 default:
2170 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2171 spec->type = FORMAT_TYPE_INVALID;
2172 return fmt - start;
2173 }
2174
2175 if (qualifier == 'L')
2176 spec->type = FORMAT_TYPE_LONG_LONG;
2177 else if (qualifier == 'l') {
2178 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2179 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2180 } else if (qualifier == 'z') {
2181 spec->type = FORMAT_TYPE_SIZE_T;
2182 } else if (qualifier == 't') {
2183 spec->type = FORMAT_TYPE_PTRDIFF;
2184 } else if (qualifier == 'H') {
2185 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2186 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2187 } else if (qualifier == 'h') {
2188 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2189 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2190 } else {
2191 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2192 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2193 }
2194
2195 return ++fmt - start;
2196 }
2197
2198 static void
2199 set_field_width(struct printf_spec *spec, int width)
2200 {
2201 spec->field_width = width;
2202 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2203 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2204 }
2205 }
2206
2207 static void
2208 set_precision(struct printf_spec *spec, int prec)
2209 {
2210 spec->precision = prec;
2211 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2212 spec->precision = clamp(prec, 0, PRECISION_MAX);
2213 }
2214 }
2215
2216 /**
2217 * vsnprintf - Format a string and place it in a buffer
2218 * @buf: The buffer to place the result into
2219 * @size: The size of the buffer, including the trailing null space
2220 * @fmt: The format string to use
2221 * @args: Arguments for the format string
2222 *
2223 * This function generally follows C99 vsnprintf, but has some
2224 * extensions and a few limitations:
2225 *
2226 * - ``%n`` is unsupported
2227 * - ``%p*`` is handled by pointer()
2228 *
2229 * See pointer() or Documentation/printk-formats.txt for more
2230 * extensive description.
2231 *
2232 * **Please update the documentation in both places when making changes**
2233 *
2234 * The return value is the number of characters which would
2235 * be generated for the given input, excluding the trailing
2236 * '\0', as per ISO C99. If you want to have the exact
2237 * number of characters written into @buf as return value
2238 * (not including the trailing '\0'), use vscnprintf(). If the
2239 * return is greater than or equal to @size, the resulting
2240 * string is truncated.
2241 *
2242 * If you're not already dealing with a va_list consider using snprintf().
2243 */
2244 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2245 {
2246 unsigned long long num;
2247 char *str, *end;
2248 struct printf_spec spec = {0};
2249
2250 /* Reject out-of-range values early. Large positive sizes are
2251 used for unknown buffer sizes. */
2252 if (WARN_ON_ONCE(size > INT_MAX))
2253 return 0;
2254
2255 str = buf;
2256 end = buf + size;
2257
2258 /* Make sure end is always >= buf */
2259 if (end < buf) {
2260 end = ((void *)-1);
2261 size = end - buf;
2262 }
2263
2264 while (*fmt) {
2265 const char *old_fmt = fmt;
2266 int read = format_decode(fmt, &spec);
2267
2268 fmt += read;
2269
2270 switch (spec.type) {
2271 case FORMAT_TYPE_NONE: {
2272 int copy = read;
2273 if (str < end) {
2274 if (copy > end - str)
2275 copy = end - str;
2276 memcpy(str, old_fmt, copy);
2277 }
2278 str += read;
2279 break;
2280 }
2281
2282 case FORMAT_TYPE_WIDTH:
2283 set_field_width(&spec, va_arg(args, int));
2284 break;
2285
2286 case FORMAT_TYPE_PRECISION:
2287 set_precision(&spec, va_arg(args, int));
2288 break;
2289
2290 case FORMAT_TYPE_CHAR: {
2291 char c;
2292
2293 if (!(spec.flags & LEFT)) {
2294 while (--spec.field_width > 0) {
2295 if (str < end)
2296 *str = ' ';
2297 ++str;
2298
2299 }
2300 }
2301 c = (unsigned char) va_arg(args, int);
2302 if (str < end)
2303 *str = c;
2304 ++str;
2305 while (--spec.field_width > 0) {
2306 if (str < end)
2307 *str = ' ';
2308 ++str;
2309 }
2310 break;
2311 }
2312
2313 case FORMAT_TYPE_STR:
2314 str = string(str, end, va_arg(args, char *), spec);
2315 break;
2316
2317 case FORMAT_TYPE_PTR:
2318 str = pointer(fmt, str, end, va_arg(args, void *),
2319 spec);
2320 while (isalnum(*fmt))
2321 fmt++;
2322 break;
2323
2324 case FORMAT_TYPE_PERCENT_CHAR:
2325 if (str < end)
2326 *str = '%';
2327 ++str;
2328 break;
2329
2330 case FORMAT_TYPE_INVALID:
2331 /*
2332 * Presumably the arguments passed gcc's type
2333 * checking, but there is no safe or sane way
2334 * for us to continue parsing the format and
2335 * fetching from the va_list; the remaining
2336 * specifiers and arguments would be out of
2337 * sync.
2338 */
2339 goto out;
2340
2341 default:
2342 switch (spec.type) {
2343 case FORMAT_TYPE_LONG_LONG:
2344 num = va_arg(args, long long);
2345 break;
2346 case FORMAT_TYPE_ULONG:
2347 num = va_arg(args, unsigned long);
2348 break;
2349 case FORMAT_TYPE_LONG:
2350 num = va_arg(args, long);
2351 break;
2352 case FORMAT_TYPE_SIZE_T:
2353 if (spec.flags & SIGN)
2354 num = va_arg(args, ssize_t);
2355 else
2356 num = va_arg(args, size_t);
2357 break;
2358 case FORMAT_TYPE_PTRDIFF:
2359 num = va_arg(args, ptrdiff_t);
2360 break;
2361 case FORMAT_TYPE_UBYTE:
2362 num = (unsigned char) va_arg(args, int);
2363 break;
2364 case FORMAT_TYPE_BYTE:
2365 num = (signed char) va_arg(args, int);
2366 break;
2367 case FORMAT_TYPE_USHORT:
2368 num = (unsigned short) va_arg(args, int);
2369 break;
2370 case FORMAT_TYPE_SHORT:
2371 num = (short) va_arg(args, int);
2372 break;
2373 case FORMAT_TYPE_INT:
2374 num = (int) va_arg(args, int);
2375 break;
2376 default:
2377 num = va_arg(args, unsigned int);
2378 }
2379
2380 str = number(str, end, num, spec);
2381 }
2382 }
2383
2384 out:
2385 if (size > 0) {
2386 if (str < end)
2387 *str = '\0';
2388 else
2389 end[-1] = '\0';
2390 }
2391
2392 /* the trailing null byte doesn't count towards the total */
2393 return str-buf;
2394
2395 }
2396 EXPORT_SYMBOL(vsnprintf);
2397
2398 /**
2399 * vscnprintf - Format a string and place it in a buffer
2400 * @buf: The buffer to place the result into
2401 * @size: The size of the buffer, including the trailing null space
2402 * @fmt: The format string to use
2403 * @args: Arguments for the format string
2404 *
2405 * The return value is the number of characters which have been written into
2406 * the @buf not including the trailing '\0'. If @size is == 0 the function
2407 * returns 0.
2408 *
2409 * If you're not already dealing with a va_list consider using scnprintf().
2410 *
2411 * See the vsnprintf() documentation for format string extensions over C99.
2412 */
2413 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2414 {
2415 int i;
2416
2417 i = vsnprintf(buf, size, fmt, args);
2418
2419 if (likely(i < size))
2420 return i;
2421 if (size != 0)
2422 return size - 1;
2423 return 0;
2424 }
2425 EXPORT_SYMBOL(vscnprintf);
2426
2427 /**
2428 * snprintf - Format a string and place it in a buffer
2429 * @buf: The buffer to place the result into
2430 * @size: The size of the buffer, including the trailing null space
2431 * @fmt: The format string to use
2432 * @...: Arguments for the format string
2433 *
2434 * The return value is the number of characters which would be
2435 * generated for the given input, excluding the trailing null,
2436 * as per ISO C99. If the return is greater than or equal to
2437 * @size, the resulting string is truncated.
2438 *
2439 * See the vsnprintf() documentation for format string extensions over C99.
2440 */
2441 int snprintf(char *buf, size_t size, const char *fmt, ...)
2442 {
2443 va_list args;
2444 int i;
2445
2446 va_start(args, fmt);
2447 i = vsnprintf(buf, size, fmt, args);
2448 va_end(args);
2449
2450 return i;
2451 }
2452 EXPORT_SYMBOL(snprintf);
2453
2454 /**
2455 * scnprintf - Format a string and place it in a buffer
2456 * @buf: The buffer to place the result into
2457 * @size: The size of the buffer, including the trailing null space
2458 * @fmt: The format string to use
2459 * @...: Arguments for the format string
2460 *
2461 * The return value is the number of characters written into @buf not including
2462 * the trailing '\0'. If @size is == 0 the function returns 0.
2463 */
2464
2465 int scnprintf(char *buf, size_t size, const char *fmt, ...)
2466 {
2467 va_list args;
2468 int i;
2469
2470 va_start(args, fmt);
2471 i = vscnprintf(buf, size, fmt, args);
2472 va_end(args);
2473
2474 return i;
2475 }
2476 EXPORT_SYMBOL(scnprintf);
2477
2478 /**
2479 * vsprintf - Format a string and place it in a buffer
2480 * @buf: The buffer to place the result into
2481 * @fmt: The format string to use
2482 * @args: Arguments for the format string
2483 *
2484 * The function returns the number of characters written
2485 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2486 * buffer overflows.
2487 *
2488 * If you're not already dealing with a va_list consider using sprintf().
2489 *
2490 * See the vsnprintf() documentation for format string extensions over C99.
2491 */
2492 int vsprintf(char *buf, const char *fmt, va_list args)
2493 {
2494 return vsnprintf(buf, INT_MAX, fmt, args);
2495 }
2496 EXPORT_SYMBOL(vsprintf);
2497
2498 /**
2499 * sprintf - Format a string and place it in a buffer
2500 * @buf: The buffer to place the result into
2501 * @fmt: The format string to use
2502 * @...: Arguments for the format string
2503 *
2504 * The function returns the number of characters written
2505 * into @buf. Use snprintf() or scnprintf() in order to avoid
2506 * buffer overflows.
2507 *
2508 * See the vsnprintf() documentation for format string extensions over C99.
2509 */
2510 int sprintf(char *buf, const char *fmt, ...)
2511 {
2512 va_list args;
2513 int i;
2514
2515 va_start(args, fmt);
2516 i = vsnprintf(buf, INT_MAX, fmt, args);
2517 va_end(args);
2518
2519 return i;
2520 }
2521 EXPORT_SYMBOL(sprintf);
2522
2523 #ifdef CONFIG_BINARY_PRINTF
2524 /*
2525 * bprintf service:
2526 * vbin_printf() - VA arguments to binary data
2527 * bstr_printf() - Binary data to text string
2528 */
2529
2530 /**
2531 * vbin_printf - Parse a format string and place args' binary value in a buffer
2532 * @bin_buf: The buffer to place args' binary value
2533 * @size: The size of the buffer(by words(32bits), not characters)
2534 * @fmt: The format string to use
2535 * @args: Arguments for the format string
2536 *
2537 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2538 * is skipped.
2539 *
2540 * The return value is the number of words(32bits) which would be generated for
2541 * the given input.
2542 *
2543 * NOTE:
2544 * If the return value is greater than @size, the resulting bin_buf is NOT
2545 * valid for bstr_printf().
2546 */
2547 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2548 {
2549 struct printf_spec spec = {0};
2550 char *str, *end;
2551
2552 str = (char *)bin_buf;
2553 end = (char *)(bin_buf + size);
2554
2555 #define save_arg(type) \
2556 do { \
2557 if (sizeof(type) == 8) { \
2558 unsigned long long value; \
2559 str = PTR_ALIGN(str, sizeof(u32)); \
2560 value = va_arg(args, unsigned long long); \
2561 if (str + sizeof(type) <= end) { \
2562 *(u32 *)str = *(u32 *)&value; \
2563 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2564 } \
2565 } else { \
2566 unsigned long value; \
2567 str = PTR_ALIGN(str, sizeof(type)); \
2568 value = va_arg(args, int); \
2569 if (str + sizeof(type) <= end) \
2570 *(typeof(type) *)str = (type)value; \
2571 } \
2572 str += sizeof(type); \
2573 } while (0)
2574
2575 while (*fmt) {
2576 int read = format_decode(fmt, &spec);
2577
2578 fmt += read;
2579
2580 switch (spec.type) {
2581 case FORMAT_TYPE_NONE:
2582 case FORMAT_TYPE_PERCENT_CHAR:
2583 break;
2584 case FORMAT_TYPE_INVALID:
2585 goto out;
2586
2587 case FORMAT_TYPE_WIDTH:
2588 case FORMAT_TYPE_PRECISION:
2589 save_arg(int);
2590 break;
2591
2592 case FORMAT_TYPE_CHAR:
2593 save_arg(char);
2594 break;
2595
2596 case FORMAT_TYPE_STR: {
2597 const char *save_str = va_arg(args, char *);
2598 size_t len;
2599
2600 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2601 || (unsigned long)save_str < PAGE_SIZE)
2602 save_str = "(null)";
2603 len = strlen(save_str) + 1;
2604 if (str + len < end)
2605 memcpy(str, save_str, len);
2606 str += len;
2607 break;
2608 }
2609
2610 case FORMAT_TYPE_PTR:
2611 save_arg(void *);
2612 /* skip all alphanumeric pointer suffixes */
2613 while (isalnum(*fmt))
2614 fmt++;
2615 break;
2616
2617 default:
2618 switch (spec.type) {
2619
2620 case FORMAT_TYPE_LONG_LONG:
2621 save_arg(long long);
2622 break;
2623 case FORMAT_TYPE_ULONG:
2624 case FORMAT_TYPE_LONG:
2625 save_arg(unsigned long);
2626 break;
2627 case FORMAT_TYPE_SIZE_T:
2628 save_arg(size_t);
2629 break;
2630 case FORMAT_TYPE_PTRDIFF:
2631 save_arg(ptrdiff_t);
2632 break;
2633 case FORMAT_TYPE_UBYTE:
2634 case FORMAT_TYPE_BYTE:
2635 save_arg(char);
2636 break;
2637 case FORMAT_TYPE_USHORT:
2638 case FORMAT_TYPE_SHORT:
2639 save_arg(short);
2640 break;
2641 default:
2642 save_arg(int);
2643 }
2644 }
2645 }
2646
2647 out:
2648 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2649 #undef save_arg
2650 }
2651 EXPORT_SYMBOL_GPL(vbin_printf);
2652
2653 /**
2654 * bstr_printf - Format a string from binary arguments and place it in a buffer
2655 * @buf: The buffer to place the result into
2656 * @size: The size of the buffer, including the trailing null space
2657 * @fmt: The format string to use
2658 * @bin_buf: Binary arguments for the format string
2659 *
2660 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2661 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2662 * a binary buffer that generated by vbin_printf.
2663 *
2664 * The format follows C99 vsnprintf, but has some extensions:
2665 * see vsnprintf comment for details.
2666 *
2667 * The return value is the number of characters which would
2668 * be generated for the given input, excluding the trailing
2669 * '\0', as per ISO C99. If you want to have the exact
2670 * number of characters written into @buf as return value
2671 * (not including the trailing '\0'), use vscnprintf(). If the
2672 * return is greater than or equal to @size, the resulting
2673 * string is truncated.
2674 */
2675 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2676 {
2677 struct printf_spec spec = {0};
2678 char *str, *end;
2679 const char *args = (const char *)bin_buf;
2680
2681 if (WARN_ON_ONCE(size > INT_MAX))
2682 return 0;
2683
2684 str = buf;
2685 end = buf + size;
2686
2687 #define get_arg(type) \
2688 ({ \
2689 typeof(type) value; \
2690 if (sizeof(type) == 8) { \
2691 args = PTR_ALIGN(args, sizeof(u32)); \
2692 *(u32 *)&value = *(u32 *)args; \
2693 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2694 } else { \
2695 args = PTR_ALIGN(args, sizeof(type)); \
2696 value = *(typeof(type) *)args; \
2697 } \
2698 args += sizeof(type); \
2699 value; \
2700 })
2701
2702 /* Make sure end is always >= buf */
2703 if (end < buf) {
2704 end = ((void *)-1);
2705 size = end - buf;
2706 }
2707
2708 while (*fmt) {
2709 const char *old_fmt = fmt;
2710 int read = format_decode(fmt, &spec);
2711
2712 fmt += read;
2713
2714 switch (spec.type) {
2715 case FORMAT_TYPE_NONE: {
2716 int copy = read;
2717 if (str < end) {
2718 if (copy > end - str)
2719 copy = end - str;
2720 memcpy(str, old_fmt, copy);
2721 }
2722 str += read;
2723 break;
2724 }
2725
2726 case FORMAT_TYPE_WIDTH:
2727 set_field_width(&spec, get_arg(int));
2728 break;
2729
2730 case FORMAT_TYPE_PRECISION:
2731 set_precision(&spec, get_arg(int));
2732 break;
2733
2734 case FORMAT_TYPE_CHAR: {
2735 char c;
2736
2737 if (!(spec.flags & LEFT)) {
2738 while (--spec.field_width > 0) {
2739 if (str < end)
2740 *str = ' ';
2741 ++str;
2742 }
2743 }
2744 c = (unsigned char) get_arg(char);
2745 if (str < end)
2746 *str = c;
2747 ++str;
2748 while (--spec.field_width > 0) {
2749 if (str < end)
2750 *str = ' ';
2751 ++str;
2752 }
2753 break;
2754 }
2755
2756 case FORMAT_TYPE_STR: {
2757 const char *str_arg = args;
2758 args += strlen(str_arg) + 1;
2759 str = string(str, end, (char *)str_arg, spec);
2760 break;
2761 }
2762
2763 case FORMAT_TYPE_PTR:
2764 str = pointer(fmt, str, end, get_arg(void *), spec);
2765 while (isalnum(*fmt))
2766 fmt++;
2767 break;
2768
2769 case FORMAT_TYPE_PERCENT_CHAR:
2770 if (str < end)
2771 *str = '%';
2772 ++str;
2773 break;
2774
2775 case FORMAT_TYPE_INVALID:
2776 goto out;
2777
2778 default: {
2779 unsigned long long num;
2780
2781 switch (spec.type) {
2782
2783 case FORMAT_TYPE_LONG_LONG:
2784 num = get_arg(long long);
2785 break;
2786 case FORMAT_TYPE_ULONG:
2787 case FORMAT_TYPE_LONG:
2788 num = get_arg(unsigned long);
2789 break;
2790 case FORMAT_TYPE_SIZE_T:
2791 num = get_arg(size_t);
2792 break;
2793 case FORMAT_TYPE_PTRDIFF:
2794 num = get_arg(ptrdiff_t);
2795 break;
2796 case FORMAT_TYPE_UBYTE:
2797 num = get_arg(unsigned char);
2798 break;
2799 case FORMAT_TYPE_BYTE:
2800 num = get_arg(signed char);
2801 break;
2802 case FORMAT_TYPE_USHORT:
2803 num = get_arg(unsigned short);
2804 break;
2805 case FORMAT_TYPE_SHORT:
2806 num = get_arg(short);
2807 break;
2808 case FORMAT_TYPE_UINT:
2809 num = get_arg(unsigned int);
2810 break;
2811 default:
2812 num = get_arg(int);
2813 }
2814
2815 str = number(str, end, num, spec);
2816 } /* default: */
2817 } /* switch(spec.type) */
2818 } /* while(*fmt) */
2819
2820 out:
2821 if (size > 0) {
2822 if (str < end)
2823 *str = '\0';
2824 else
2825 end[-1] = '\0';
2826 }
2827
2828 #undef get_arg
2829
2830 /* the trailing null byte doesn't count towards the total */
2831 return str - buf;
2832 }
2833 EXPORT_SYMBOL_GPL(bstr_printf);
2834
2835 /**
2836 * bprintf - Parse a format string and place args' binary value in a buffer
2837 * @bin_buf: The buffer to place args' binary value
2838 * @size: The size of the buffer(by words(32bits), not characters)
2839 * @fmt: The format string to use
2840 * @...: Arguments for the format string
2841 *
2842 * The function returns the number of words(u32) written
2843 * into @bin_buf.
2844 */
2845 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2846 {
2847 va_list args;
2848 int ret;
2849
2850 va_start(args, fmt);
2851 ret = vbin_printf(bin_buf, size, fmt, args);
2852 va_end(args);
2853
2854 return ret;
2855 }
2856 EXPORT_SYMBOL_GPL(bprintf);
2857
2858 #endif /* CONFIG_BINARY_PRINTF */
2859
2860 /**
2861 * vsscanf - Unformat a buffer into a list of arguments
2862 * @buf: input buffer
2863 * @fmt: format of buffer
2864 * @args: arguments
2865 */
2866 int vsscanf(const char *buf, const char *fmt, va_list args)
2867 {
2868 const char *str = buf;
2869 char *next;
2870 char digit;
2871 int num = 0;
2872 u8 qualifier;
2873 unsigned int base;
2874 union {
2875 long long s;
2876 unsigned long long u;
2877 } val;
2878 s16 field_width;
2879 bool is_sign;
2880
2881 while (*fmt) {
2882 /* skip any white space in format */
2883 /* white space in format matchs any amount of
2884 * white space, including none, in the input.
2885 */
2886 if (isspace(*fmt)) {
2887 fmt = skip_spaces(++fmt);
2888 str = skip_spaces(str);
2889 }
2890
2891 /* anything that is not a conversion must match exactly */
2892 if (*fmt != '%' && *fmt) {
2893 if (*fmt++ != *str++)
2894 break;
2895 continue;
2896 }
2897
2898 if (!*fmt)
2899 break;
2900 ++fmt;
2901
2902 /* skip this conversion.
2903 * advance both strings to next white space
2904 */
2905 if (*fmt == '*') {
2906 if (!*str)
2907 break;
2908 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2909 /* '%*[' not yet supported, invalid format */
2910 if (*fmt == '[')
2911 return num;
2912 fmt++;
2913 }
2914 while (!isspace(*str) && *str)
2915 str++;
2916 continue;
2917 }
2918
2919 /* get field width */
2920 field_width = -1;
2921 if (isdigit(*fmt)) {
2922 field_width = skip_atoi(&fmt);
2923 if (field_width <= 0)
2924 break;
2925 }
2926
2927 /* get conversion qualifier */
2928 qualifier = -1;
2929 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2930 *fmt == 'z') {
2931 qualifier = *fmt++;
2932 if (unlikely(qualifier == *fmt)) {
2933 if (qualifier == 'h') {
2934 qualifier = 'H';
2935 fmt++;
2936 } else if (qualifier == 'l') {
2937 qualifier = 'L';
2938 fmt++;
2939 }
2940 }
2941 }
2942
2943 if (!*fmt)
2944 break;
2945
2946 if (*fmt == 'n') {
2947 /* return number of characters read so far */
2948 *va_arg(args, int *) = str - buf;
2949 ++fmt;
2950 continue;
2951 }
2952
2953 if (!*str)
2954 break;
2955
2956 base = 10;
2957 is_sign = false;
2958
2959 switch (*fmt++) {
2960 case 'c':
2961 {
2962 char *s = (char *)va_arg(args, char*);
2963 if (field_width == -1)
2964 field_width = 1;
2965 do {
2966 *s++ = *str++;
2967 } while (--field_width > 0 && *str);
2968 num++;
2969 }
2970 continue;
2971 case 's':
2972 {
2973 char *s = (char *)va_arg(args, char *);
2974 if (field_width == -1)
2975 field_width = SHRT_MAX;
2976 /* first, skip leading white space in buffer */
2977 str = skip_spaces(str);
2978
2979 /* now copy until next white space */
2980 while (*str && !isspace(*str) && field_width--)
2981 *s++ = *str++;
2982 *s = '\0';
2983 num++;
2984 }
2985 continue;
2986 /*
2987 * Warning: This implementation of the '[' conversion specifier
2988 * deviates from its glibc counterpart in the following ways:
2989 * (1) It does NOT support ranges i.e. '-' is NOT a special
2990 * character
2991 * (2) It cannot match the closing bracket ']' itself
2992 * (3) A field width is required
2993 * (4) '%*[' (discard matching input) is currently not supported
2994 *
2995 * Example usage:
2996 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
2997 * buf1, buf2, buf3);
2998 * if (ret < 3)
2999 * // etc..
3000 */
3001 case '[':
3002 {
3003 char *s = (char *)va_arg(args, char *);
3004 DECLARE_BITMAP(set, 256) = {0};
3005 unsigned int len = 0;
3006 bool negate = (*fmt == '^');
3007
3008 /* field width is required */
3009 if (field_width == -1)
3010 return num;
3011
3012 if (negate)
3013 ++fmt;
3014
3015 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3016 set_bit((u8)*fmt, set);
3017
3018 /* no ']' or no character set found */
3019 if (!*fmt || !len)
3020 return num;
3021 ++fmt;
3022
3023 if (negate) {
3024 bitmap_complement(set, set, 256);
3025 /* exclude null '\0' byte */
3026 clear_bit(0, set);
3027 }
3028
3029 /* match must be non-empty */
3030 if (!test_bit((u8)*str, set))
3031 return num;
3032
3033 while (test_bit((u8)*str, set) && field_width--)
3034 *s++ = *str++;
3035 *s = '\0';
3036 ++num;
3037 }
3038 continue;
3039 case 'o':
3040 base = 8;
3041 break;
3042 case 'x':
3043 case 'X':
3044 base = 16;
3045 break;
3046 case 'i':
3047 base = 0;
3048 case 'd':
3049 is_sign = true;
3050 case 'u':
3051 break;
3052 case '%':
3053 /* looking for '%' in str */
3054 if (*str++ != '%')
3055 return num;
3056 continue;
3057 default:
3058 /* invalid format; stop here */
3059 return num;
3060 }
3061
3062 /* have some sort of integer conversion.
3063 * first, skip white space in buffer.
3064 */
3065 str = skip_spaces(str);
3066
3067 digit = *str;
3068 if (is_sign && digit == '-')
3069 digit = *(str + 1);
3070
3071 if (!digit
3072 || (base == 16 && !isxdigit(digit))
3073 || (base == 10 && !isdigit(digit))
3074 || (base == 8 && (!isdigit(digit) || digit > '7'))
3075 || (base == 0 && !isdigit(digit)))
3076 break;
3077
3078 if (is_sign)
3079 val.s = qualifier != 'L' ?
3080 simple_strtol(str, &next, base) :
3081 simple_strtoll(str, &next, base);
3082 else
3083 val.u = qualifier != 'L' ?
3084 simple_strtoul(str, &next, base) :
3085 simple_strtoull(str, &next, base);
3086
3087 if (field_width > 0 && next - str > field_width) {
3088 if (base == 0)
3089 _parse_integer_fixup_radix(str, &base);
3090 while (next - str > field_width) {
3091 if (is_sign)
3092 val.s = div_s64(val.s, base);
3093 else
3094 val.u = div_u64(val.u, base);
3095 --next;
3096 }
3097 }
3098
3099 switch (qualifier) {
3100 case 'H': /* that's 'hh' in format */
3101 if (is_sign)
3102 *va_arg(args, signed char *) = val.s;
3103 else
3104 *va_arg(args, unsigned char *) = val.u;
3105 break;
3106 case 'h':
3107 if (is_sign)
3108 *va_arg(args, short *) = val.s;
3109 else
3110 *va_arg(args, unsigned short *) = val.u;
3111 break;
3112 case 'l':
3113 if (is_sign)
3114 *va_arg(args, long *) = val.s;
3115 else
3116 *va_arg(args, unsigned long *) = val.u;
3117 break;
3118 case 'L':
3119 if (is_sign)
3120 *va_arg(args, long long *) = val.s;
3121 else
3122 *va_arg(args, unsigned long long *) = val.u;
3123 break;
3124 case 'z':
3125 *va_arg(args, size_t *) = val.u;
3126 break;
3127 default:
3128 if (is_sign)
3129 *va_arg(args, int *) = val.s;
3130 else
3131 *va_arg(args, unsigned int *) = val.u;
3132 break;
3133 }
3134 num++;
3135
3136 if (!next)
3137 break;
3138 str = next;
3139 }
3140
3141 return num;
3142 }
3143 EXPORT_SYMBOL(vsscanf);
3144
3145 /**
3146 * sscanf - Unformat a buffer into a list of arguments
3147 * @buf: input buffer
3148 * @fmt: formatting of buffer
3149 * @...: resulting arguments
3150 */
3151 int sscanf(const char *buf, const char *fmt, ...)
3152 {
3153 va_list args;
3154 int i;
3155
3156 va_start(args, fmt);
3157 i = vsscanf(buf, fmt, args);
3158 va_end(args);
3159
3160 return i;
3161 }
3162 EXPORT_SYMBOL(sscanf);