1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <stdarg.h>
20#include <linux/clk.h>
21#include <linux/clk-provider.h>
22#include <linux/module.h>
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"
43
44#include <asm/page.h>
45#include <asm/sections.h>
46#include <asm/byteorder.h>
47
48#include <linux/string_helpers.h>
49#include "kstrtox.h"
50
51
52
53
54
55
56
57
58
59unsigned 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
67 cp += (rv & ~KSTRTOX_OVERFLOW);
68
69 if (endp)
70 *endp = (char *)cp;
71
72 return result;
73}
74EXPORT_SYMBOL(simple_strtoull);
75
76
77
78
79
80
81
82
83
84unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
85{
86 return simple_strtoull(cp, endp, base);
87}
88EXPORT_SYMBOL(simple_strtoul);
89
90
91
92
93
94
95
96
97
98long 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}
105EXPORT_SYMBOL(simple_strtol);
106
107
108
109
110
111
112
113
114
115long 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}
122EXPORT_SYMBOL(simple_strtoll);
123
124static noinline_for_stack
125int 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161static 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
178
179
180
181
182static noinline_for_stack
183char *put_dec_trunc8(char *buf, unsigned r)
184{
185 unsigned q;
186
187
188 if (r < 100)
189 goto out_r;
190
191
192 q = (r * (u64)0x28f5c29) >> 32;
193 *((u16 *)buf) = decpair[r - 100*q];
194 buf += 2;
195
196
197 if (q < 100)
198 goto out_q;
199
200
201 r = (q * (u64)0x28f5c29) >> 32;
202 *((u16 *)buf) = decpair[q - 100*r];
203 buf += 2;
204
205
206 if (r < 100)
207 goto out_r;
208
209
210 q = (r * 0x147b) >> 19;
211 *((u16 *)buf) = decpair[r - 100*q];
212 buf += 2;
213out_q:
214
215 r = q;
216out_r:
217
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
224static noinline_for_stack
225char *put_dec_full8(char *buf, unsigned r)
226{
227 unsigned q;
228
229
230 q = (r * (u64)0x28f5c29) >> 32;
231 *((u16 *)buf) = decpair[r - 100*q];
232 buf += 2;
233
234
235 r = (q * (u64)0x28f5c29) >> 32;
236 *((u16 *)buf) = decpair[q - 100*r];
237 buf += 2;
238
239
240 q = (r * 0x147b) >> 19;
241 *((u16 *)buf) = decpair[r - 100*q];
242 buf += 2;
243
244
245 *((u16 *)buf) = decpair[q];
246 buf += 2;
247 return buf;
248}
249
250static noinline_for_stack
251char *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
256 if (n >= 100*1000*1000)
257 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
258
259 return put_dec_trunc8(buf, n);
260}
261
262#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
263
264static void
265put_dec_full4(char *buf, unsigned r)
266{
267 unsigned q;
268
269
270 q = (r * 0x147b) >> 19;
271 *((u16 *)buf) = decpair[r - 100*q];
272 buf += 2;
273
274 *((u16 *)buf) = decpair[q];
275}
276
277
278
279
280
281
282
283
284static noinline_for_stack
285unsigned 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
294
295
296
297
298static
299char *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);
307 h = (n >> 32);
308 d2 = (h ) & 0xffff;
309 d3 = (h >> 16);
310
311
312
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
336
337
338
339
340int num_to_str(char *buf, int size, unsigned long long num)
341{
342
343 char tmp[sizeof(num) * 3] __aligned(2);
344 int idx, len;
345
346
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
362#define LEFT 2
363#define PLUS 4
364#define SPACE 8
365#define ZEROPAD 16
366#define SMALL 32
367#define SPECIAL 64
368
369enum format_type {
370 FORMAT_TYPE_NONE,
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
391struct printf_spec {
392 unsigned int type:8;
393 signed int field_width:24;
394 unsigned int flags:8;
395 unsigned int base:8;
396 signed int precision:16;
397} __packed;
398#define FIELD_WIDTH_MAX ((1 << 23) - 1)
399#define PRECISION_MAX ((1 << 15) - 1)
400
401static noinline_for_stack
402char *number(char *buf, char *end, unsigned long long num,
403 struct printf_spec spec)
404{
405
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
418
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
444 i = 0;
445 if (num < spec.base)
446 tmp[i++] = hex_asc_upper[num] | locase;
447 else if (spec.base != 10) {
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 {
458 i = put_dec(tmp, num) - tmp;
459 }
460
461
462 if (i > precision)
463 precision = i;
464
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
474 if (sign) {
475 if (buf < end)
476 *buf = sign;
477 ++buf;
478 }
479
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
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
503 while (i <= --precision) {
504 if (buf < end)
505 *buf = '0';
506 ++buf;
507 }
508
509 while (--i >= 0) {
510 if (buf < end)
511 *buf = tmp[i];
512 ++buf;
513 }
514
515 while (--field_width >= 0) {
516 if (buf < end)
517 *buf = ' ';
518 ++buf;
519 }
520
521 return buf;
522}
523
524static noinline_for_stack
525char *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;
531 spec.flags = SPECIAL | SMALL | ZEROPAD;
532 spec.base = 16;
533 spec.precision = -1;
534
535 return number(buf, end, num, spec);
536}
537
538static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
539{
540 size_t size;
541 if (buf >= end)
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
558
559
560
561
562
563
564static noinline_for_stack
565char *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
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
585static noinline_for_stack
586char *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
606static noinline_for_stack
607char *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
651static noinline_for_stack
652char *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
670static noinline_for_stack
671char *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
697static noinline_for_stack
698char *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
743
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
805static noinline_for_stack
806char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
807 const char *fmt)
808{
809 int i, len = 1;
810
811 char separator;
812
813 if (spec.field_width == 0)
814
815 return buf;
816
817 if (ZERO_OR_NULL_PTR(addr))
818
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
857static noinline_for_stack
858char *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
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
898static noinline_for_stack
899char *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
904 int cur, rbot, rtop;
905 bool first = true;
906
907
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
938static noinline_for_stack
939char *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
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
976static noinline_for_stack
977char *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);
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
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
1026static noinline_for_stack
1027char *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
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)
1065 colonpos = -1;
1066
1067
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
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
1109static noinline_for_stack
1110char *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
1125static noinline_for_stack
1126char *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
1139static noinline_for_stack
1140char *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
1150static noinline_for_stack
1151char *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
1212static noinline_for_stack
1213char *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
1247static noinline_for_stack
1248char *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;
1258
1259 if (ZERO_OR_NULL_PTR(addr))
1260 return string(buf, end, NULL, spec);
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
1299
1300
1301
1302 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1303
1304 return buf;
1305}
1306
1307static noinline_for_stack
1308char *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;
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
1348int kptr_restrict __read_mostly;
1349
1350static noinline_for_stack
1351char *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
1364 break;
1365 case 1: {
1366 const struct cred *cred;
1367
1368
1369
1370
1371
1372 if (in_irq() || in_serving_softirq() || in_nmi())
1373 return string(buf, end, "pK-error", spec);
1374
1375
1376
1377
1378
1379
1380
1381
1382
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
1394 ptr = NULL;
1395 break;
1396 }
1397
1398 return number(buf, end, (unsigned long)ptr, spec);
1399}
1400
1401static noinline_for_stack
1402char *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
1421static noinline_for_stack
1422char *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
1442static noinline_for_stack
1443char *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
1463static
1464char *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
1500static noinline_for_stack
1501char *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
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
1529static 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
1537static noinline_for_stack
1538char *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
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
1562static noinline_for_stack
1563char *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
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':
1602 buf = device_node_gen_full_name(dn, buf, end);
1603 break;
1604 case 'n':
1605 buf = string(buf, end, dn->name, str_spec);
1606 break;
1607 case 'p':
1608 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1609 break;
1610 case 'P':
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':
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':
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':
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
1649static noinline_for_stack
1650char *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
1663static bool have_filled_random_ptr_key __read_mostly;
1664static siphash_key_t ptr_key __read_mostly;
1665
1666static void fill_random_ptr_key(struct random_ready_callback *unused)
1667{
1668 get_random_bytes(&ptr_key, sizeof(ptr_key));
1669
1670
1671
1672
1673
1674 smp_mb();
1675 WRITE_ONCE(have_filled_random_ptr_key, true);
1676}
1677
1678static struct random_ready_callback random_ready = {
1679 .func = fill_random_ptr_key
1680};
1681
1682static int __init initialize_ptr_random(void)
1683{
1684 int ret = add_random_ready_callback(&random_ready);
1685
1686 if (!ret) {
1687 return 0;
1688 } else if (ret == -EALREADY) {
1689 fill_random_ptr_key(&random_ready);
1690 return 0;
1691 }
1692
1693 return ret;
1694}
1695early_initcall(initialize_ptr_random);
1696
1697
1698static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1699{
1700 unsigned long hashval;
1701 const int default_width = 2 * sizeof(ptr);
1702
1703 if (unlikely(!have_filled_random_ptr_key)) {
1704 spec.field_width = default_width;
1705
1706 return string(buf, end, "(ptrval)", spec);
1707 }
1708
1709#ifdef CONFIG_64BIT
1710 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1711
1712
1713
1714
1715 hashval = hashval & 0xffffffff;
1716#else
1717 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1718#endif
1719
1720 spec.flags |= SMALL;
1721 if (spec.field_width == -1) {
1722 spec.field_width = default_width;
1723 spec.flags |= ZEROPAD;
1724 }
1725 spec.base = 16;
1726
1727 return number(buf, end, hashval, spec);
1728}
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846static noinline_for_stack
1847char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1848 struct printf_spec spec)
1849{
1850 const int default_width = 2 * sizeof(void *);
1851
1852 if (!ptr && *fmt != 'K') {
1853
1854
1855
1856
1857 if (spec.field_width == -1)
1858 spec.field_width = default_width;
1859 return string(buf, end, "(null)", spec);
1860 }
1861
1862 switch (*fmt) {
1863 case 'F':
1864 case 'f':
1865 ptr = dereference_function_descriptor(ptr);
1866
1867 case 'S':
1868 case 's':
1869 case 'B':
1870 return symbol_string(buf, end, ptr, spec, fmt);
1871 case 'R':
1872 case 'r':
1873 return resource_string(buf, end, ptr, spec, fmt);
1874 case 'h':
1875 return hex_string(buf, end, ptr, spec, fmt);
1876 case 'b':
1877 switch (fmt[1]) {
1878 case 'l':
1879 return bitmap_list_string(buf, end, ptr, spec, fmt);
1880 default:
1881 return bitmap_string(buf, end, ptr, spec, fmt);
1882 }
1883 case 'M':
1884 case 'm':
1885
1886
1887 return mac_address_string(buf, end, ptr, spec, fmt);
1888 case 'I':
1889
1890
1891
1892
1893 case 'i':
1894
1895
1896
1897 switch (fmt[1]) {
1898 case '6':
1899 return ip6_addr_string(buf, end, ptr, spec, fmt);
1900 case '4':
1901 return ip4_addr_string(buf, end, ptr, spec, fmt);
1902 case 'S': {
1903 const union {
1904 struct sockaddr raw;
1905 struct sockaddr_in v4;
1906 struct sockaddr_in6 v6;
1907 } *sa = ptr;
1908
1909 switch (sa->raw.sa_family) {
1910 case AF_INET:
1911 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1912 case AF_INET6:
1913 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1914 default:
1915 return string(buf, end, "(invalid address)", spec);
1916 }}
1917 }
1918 break;
1919 case 'E':
1920 return escaped_string(buf, end, ptr, spec, fmt);
1921 case 'U':
1922 return uuid_string(buf, end, ptr, spec, fmt);
1923 case 'V':
1924 {
1925 va_list va;
1926
1927 va_copy(va, *((struct va_format *)ptr)->va);
1928 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1929 ((struct va_format *)ptr)->fmt, va);
1930 va_end(va);
1931 return buf;
1932 }
1933 case 'K':
1934 if (!kptr_restrict)
1935 break;
1936 return restricted_pointer(buf, end, ptr, spec);
1937 case 'N':
1938 return netdev_bits(buf, end, ptr, fmt);
1939 case 'a':
1940 return address_val(buf, end, ptr, fmt);
1941 case 'd':
1942 return dentry_name(buf, end, ptr, spec, fmt);
1943 case 'C':
1944 return clock(buf, end, ptr, spec, fmt);
1945 case 'D':
1946 return dentry_name(buf, end,
1947 ((const struct file *)ptr)->f_path.dentry,
1948 spec, fmt);
1949#ifdef CONFIG_BLOCK
1950 case 'g':
1951 return bdev_name(buf, end, ptr, spec, fmt);
1952#endif
1953
1954 case 'G':
1955 return flags_string(buf, end, ptr, fmt);
1956 case 'O':
1957 switch (fmt[1]) {
1958 case 'F':
1959 return device_node_string(buf, end, ptr, spec, fmt + 1);
1960 }
1961 case 'x':
1962 return pointer_string(buf, end, ptr, spec);
1963 }
1964
1965
1966 return ptr_to_id(buf, end, ptr, spec);
1967}
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990static noinline_for_stack
1991int format_decode(const char *fmt, struct printf_spec *spec)
1992{
1993 const char *start = fmt;
1994 char qualifier;
1995
1996
1997 if (spec->type == FORMAT_TYPE_WIDTH) {
1998 if (spec->field_width < 0) {
1999 spec->field_width = -spec->field_width;
2000 spec->flags |= LEFT;
2001 }
2002 spec->type = FORMAT_TYPE_NONE;
2003 goto precision;
2004 }
2005
2006
2007 if (spec->type == FORMAT_TYPE_PRECISION) {
2008 if (spec->precision < 0)
2009 spec->precision = 0;
2010
2011 spec->type = FORMAT_TYPE_NONE;
2012 goto qualifier;
2013 }
2014
2015
2016 spec->type = FORMAT_TYPE_NONE;
2017
2018 for (; *fmt ; ++fmt) {
2019 if (*fmt == '%')
2020 break;
2021 }
2022
2023
2024 if (fmt != start || !*fmt)
2025 return fmt - start;
2026
2027
2028 spec->flags = 0;
2029
2030 while (1) {
2031 bool found = true;
2032
2033 ++fmt;
2034
2035 switch (*fmt) {
2036 case '-': spec->flags |= LEFT; break;
2037 case '+': spec->flags |= PLUS; break;
2038 case ' ': spec->flags |= SPACE; break;
2039 case '#': spec->flags |= SPECIAL; break;
2040 case '0': spec->flags |= ZEROPAD; break;
2041 default: found = false;
2042 }
2043
2044 if (!found)
2045 break;
2046 }
2047
2048
2049 spec->field_width = -1;
2050
2051 if (isdigit(*fmt))
2052 spec->field_width = skip_atoi(&fmt);
2053 else if (*fmt == '*') {
2054
2055 spec->type = FORMAT_TYPE_WIDTH;
2056 return ++fmt - start;
2057 }
2058
2059precision:
2060
2061 spec->precision = -1;
2062 if (*fmt == '.') {
2063 ++fmt;
2064 if (isdigit(*fmt)) {
2065 spec->precision = skip_atoi(&fmt);
2066 if (spec->precision < 0)
2067 spec->precision = 0;
2068 } else if (*fmt == '*') {
2069
2070 spec->type = FORMAT_TYPE_PRECISION;
2071 return ++fmt - start;
2072 }
2073 }
2074
2075qualifier:
2076
2077 qualifier = 0;
2078 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2079 *fmt == 'z' || *fmt == 't') {
2080 qualifier = *fmt++;
2081 if (unlikely(qualifier == *fmt)) {
2082 if (qualifier == 'l') {
2083 qualifier = 'L';
2084 ++fmt;
2085 } else if (qualifier == 'h') {
2086 qualifier = 'H';
2087 ++fmt;
2088 }
2089 }
2090 }
2091
2092
2093 spec->base = 10;
2094 switch (*fmt) {
2095 case 'c':
2096 spec->type = FORMAT_TYPE_CHAR;
2097 return ++fmt - start;
2098
2099 case 's':
2100 spec->type = FORMAT_TYPE_STR;
2101 return ++fmt - start;
2102
2103 case 'p':
2104 spec->type = FORMAT_TYPE_PTR;
2105 return ++fmt - start;
2106
2107 case '%':
2108 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2109 return ++fmt - start;
2110
2111
2112 case 'o':
2113 spec->base = 8;
2114 break;
2115
2116 case 'x':
2117 spec->flags |= SMALL;
2118
2119 case 'X':
2120 spec->base = 16;
2121 break;
2122
2123 case 'd':
2124 case 'i':
2125 spec->flags |= SIGN;
2126 case 'u':
2127 break;
2128
2129 case 'n':
2130
2131
2132
2133
2134
2135
2136
2137 default:
2138 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2139 spec->type = FORMAT_TYPE_INVALID;
2140 return fmt - start;
2141 }
2142
2143 if (qualifier == 'L')
2144 spec->type = FORMAT_TYPE_LONG_LONG;
2145 else if (qualifier == 'l') {
2146 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2147 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2148 } else if (qualifier == 'z') {
2149 spec->type = FORMAT_TYPE_SIZE_T;
2150 } else if (qualifier == 't') {
2151 spec->type = FORMAT_TYPE_PTRDIFF;
2152 } else if (qualifier == 'H') {
2153 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2154 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2155 } else if (qualifier == 'h') {
2156 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2157 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2158 } else {
2159 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2160 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2161 }
2162
2163 return ++fmt - start;
2164}
2165
2166static void
2167set_field_width(struct printf_spec *spec, int width)
2168{
2169 spec->field_width = width;
2170 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2171 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2172 }
2173}
2174
2175static void
2176set_precision(struct printf_spec *spec, int prec)
2177{
2178 spec->precision = prec;
2179 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2180 spec->precision = clamp(prec, 0, PRECISION_MAX);
2181 }
2182}
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2213{
2214 unsigned long long num;
2215 char *str, *end;
2216 struct printf_spec spec = {0};
2217
2218
2219
2220 if (WARN_ON_ONCE(size > INT_MAX))
2221 return 0;
2222
2223 str = buf;
2224 end = buf + size;
2225
2226
2227 if (end < buf) {
2228 end = ((void *)-1);
2229 size = end - buf;
2230 }
2231
2232 while (*fmt) {
2233 const char *old_fmt = fmt;
2234 int read = format_decode(fmt, &spec);
2235
2236 fmt += read;
2237
2238 switch (spec.type) {
2239 case FORMAT_TYPE_NONE: {
2240 int copy = read;
2241 if (str < end) {
2242 if (copy > end - str)
2243 copy = end - str;
2244 memcpy(str, old_fmt, copy);
2245 }
2246 str += read;
2247 break;
2248 }
2249
2250 case FORMAT_TYPE_WIDTH:
2251 set_field_width(&spec, va_arg(args, int));
2252 break;
2253
2254 case FORMAT_TYPE_PRECISION:
2255 set_precision(&spec, va_arg(args, int));
2256 break;
2257
2258 case FORMAT_TYPE_CHAR: {
2259 char c;
2260
2261 if (!(spec.flags & LEFT)) {
2262 while (--spec.field_width > 0) {
2263 if (str < end)
2264 *str = ' ';
2265 ++str;
2266
2267 }
2268 }
2269 c = (unsigned char) va_arg(args, int);
2270 if (str < end)
2271 *str = c;
2272 ++str;
2273 while (--spec.field_width > 0) {
2274 if (str < end)
2275 *str = ' ';
2276 ++str;
2277 }
2278 break;
2279 }
2280
2281 case FORMAT_TYPE_STR:
2282 str = string(str, end, va_arg(args, char *), spec);
2283 break;
2284
2285 case FORMAT_TYPE_PTR:
2286 str = pointer(fmt, str, end, va_arg(args, void *),
2287 spec);
2288 while (isalnum(*fmt))
2289 fmt++;
2290 break;
2291
2292 case FORMAT_TYPE_PERCENT_CHAR:
2293 if (str < end)
2294 *str = '%';
2295 ++str;
2296 break;
2297
2298 case FORMAT_TYPE_INVALID:
2299
2300
2301
2302
2303
2304
2305
2306
2307 goto out;
2308
2309 default:
2310 switch (spec.type) {
2311 case FORMAT_TYPE_LONG_LONG:
2312 num = va_arg(args, long long);
2313 break;
2314 case FORMAT_TYPE_ULONG:
2315 num = va_arg(args, unsigned long);
2316 break;
2317 case FORMAT_TYPE_LONG:
2318 num = va_arg(args, long);
2319 break;
2320 case FORMAT_TYPE_SIZE_T:
2321 if (spec.flags & SIGN)
2322 num = va_arg(args, ssize_t);
2323 else
2324 num = va_arg(args, size_t);
2325 break;
2326 case FORMAT_TYPE_PTRDIFF:
2327 num = va_arg(args, ptrdiff_t);
2328 break;
2329 case FORMAT_TYPE_UBYTE:
2330 num = (unsigned char) va_arg(args, int);
2331 break;
2332 case FORMAT_TYPE_BYTE:
2333 num = (signed char) va_arg(args, int);
2334 break;
2335 case FORMAT_TYPE_USHORT:
2336 num = (unsigned short) va_arg(args, int);
2337 break;
2338 case FORMAT_TYPE_SHORT:
2339 num = (short) va_arg(args, int);
2340 break;
2341 case FORMAT_TYPE_INT:
2342 num = (int) va_arg(args, int);
2343 break;
2344 default:
2345 num = va_arg(args, unsigned int);
2346 }
2347
2348 str = number(str, end, num, spec);
2349 }
2350 }
2351
2352out:
2353 if (size > 0) {
2354 if (str < end)
2355 *str = '\0';
2356 else
2357 end[-1] = '\0';
2358 }
2359
2360
2361 return str-buf;
2362
2363}
2364EXPORT_SYMBOL(vsnprintf);
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2382{
2383 int i;
2384
2385 i = vsnprintf(buf, size, fmt, args);
2386
2387 if (likely(i < size))
2388 return i;
2389 if (size != 0)
2390 return size - 1;
2391 return 0;
2392}
2393EXPORT_SYMBOL(vscnprintf);
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409int snprintf(char *buf, size_t size, const char *fmt, ...)
2410{
2411 va_list args;
2412 int i;
2413
2414 va_start(args, fmt);
2415 i = vsnprintf(buf, size, fmt, args);
2416 va_end(args);
2417
2418 return i;
2419}
2420EXPORT_SYMBOL(snprintf);
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433int scnprintf(char *buf, size_t size, const char *fmt, ...)
2434{
2435 va_list args;
2436 int i;
2437
2438 va_start(args, fmt);
2439 i = vscnprintf(buf, size, fmt, args);
2440 va_end(args);
2441
2442 return i;
2443}
2444EXPORT_SYMBOL(scnprintf);
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460int vsprintf(char *buf, const char *fmt, va_list args)
2461{
2462 return vsnprintf(buf, INT_MAX, fmt, args);
2463}
2464EXPORT_SYMBOL(vsprintf);
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478int sprintf(char *buf, const char *fmt, ...)
2479{
2480 va_list args;
2481 int i;
2482
2483 va_start(args, fmt);
2484 i = vsnprintf(buf, INT_MAX, fmt, args);
2485 va_end(args);
2486
2487 return i;
2488}
2489EXPORT_SYMBOL(sprintf);
2490
2491#ifdef CONFIG_BINARY_PRINTF
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2516{
2517 struct printf_spec spec = {0};
2518 char *str, *end;
2519
2520 str = (char *)bin_buf;
2521 end = (char *)(bin_buf + size);
2522
2523#define save_arg(type) \
2524do { \
2525 if (sizeof(type) == 8) { \
2526 unsigned long long value; \
2527 str = PTR_ALIGN(str, sizeof(u32)); \
2528 value = va_arg(args, unsigned long long); \
2529 if (str + sizeof(type) <= end) { \
2530 *(u32 *)str = *(u32 *)&value; \
2531 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2532 } \
2533 } else { \
2534 unsigned long value; \
2535 str = PTR_ALIGN(str, sizeof(type)); \
2536 value = va_arg(args, int); \
2537 if (str + sizeof(type) <= end) \
2538 *(typeof(type) *)str = (type)value; \
2539 } \
2540 str += sizeof(type); \
2541} while (0)
2542
2543 while (*fmt) {
2544 int read = format_decode(fmt, &spec);
2545
2546 fmt += read;
2547
2548 switch (spec.type) {
2549 case FORMAT_TYPE_NONE:
2550 case FORMAT_TYPE_PERCENT_CHAR:
2551 break;
2552 case FORMAT_TYPE_INVALID:
2553 goto out;
2554
2555 case FORMAT_TYPE_WIDTH:
2556 case FORMAT_TYPE_PRECISION:
2557 save_arg(int);
2558 break;
2559
2560 case FORMAT_TYPE_CHAR:
2561 save_arg(char);
2562 break;
2563
2564 case FORMAT_TYPE_STR: {
2565 const char *save_str = va_arg(args, char *);
2566 size_t len;
2567
2568 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2569 || (unsigned long)save_str < PAGE_SIZE)
2570 save_str = "(null)";
2571 len = strlen(save_str) + 1;
2572 if (str + len < end)
2573 memcpy(str, save_str, len);
2574 str += len;
2575 break;
2576 }
2577
2578 case FORMAT_TYPE_PTR:
2579 save_arg(void *);
2580
2581 while (isalnum(*fmt))
2582 fmt++;
2583 break;
2584
2585 default:
2586 switch (spec.type) {
2587
2588 case FORMAT_TYPE_LONG_LONG:
2589 save_arg(long long);
2590 break;
2591 case FORMAT_TYPE_ULONG:
2592 case FORMAT_TYPE_LONG:
2593 save_arg(unsigned long);
2594 break;
2595 case FORMAT_TYPE_SIZE_T:
2596 save_arg(size_t);
2597 break;
2598 case FORMAT_TYPE_PTRDIFF:
2599 save_arg(ptrdiff_t);
2600 break;
2601 case FORMAT_TYPE_UBYTE:
2602 case FORMAT_TYPE_BYTE:
2603 save_arg(char);
2604 break;
2605 case FORMAT_TYPE_USHORT:
2606 case FORMAT_TYPE_SHORT:
2607 save_arg(short);
2608 break;
2609 default:
2610 save_arg(int);
2611 }
2612 }
2613 }
2614
2615out:
2616 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2617#undef save_arg
2618}
2619EXPORT_SYMBOL_GPL(vbin_printf);
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2644{
2645 struct printf_spec spec = {0};
2646 char *str, *end;
2647 const char *args = (const char *)bin_buf;
2648
2649 if (WARN_ON_ONCE(size > INT_MAX))
2650 return 0;
2651
2652 str = buf;
2653 end = buf + size;
2654
2655#define get_arg(type) \
2656({ \
2657 typeof(type) value; \
2658 if (sizeof(type) == 8) { \
2659 args = PTR_ALIGN(args, sizeof(u32)); \
2660 *(u32 *)&value = *(u32 *)args; \
2661 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2662 } else { \
2663 args = PTR_ALIGN(args, sizeof(type)); \
2664 value = *(typeof(type) *)args; \
2665 } \
2666 args += sizeof(type); \
2667 value; \
2668})
2669
2670
2671 if (end < buf) {
2672 end = ((void *)-1);
2673 size = end - buf;
2674 }
2675
2676 while (*fmt) {
2677 const char *old_fmt = fmt;
2678 int read = format_decode(fmt, &spec);
2679
2680 fmt += read;
2681
2682 switch (spec.type) {
2683 case FORMAT_TYPE_NONE: {
2684 int copy = read;
2685 if (str < end) {
2686 if (copy > end - str)
2687 copy = end - str;
2688 memcpy(str, old_fmt, copy);
2689 }
2690 str += read;
2691 break;
2692 }
2693
2694 case FORMAT_TYPE_WIDTH:
2695 set_field_width(&spec, get_arg(int));
2696 break;
2697
2698 case FORMAT_TYPE_PRECISION:
2699 set_precision(&spec, get_arg(int));
2700 break;
2701
2702 case FORMAT_TYPE_CHAR: {
2703 char c;
2704
2705 if (!(spec.flags & LEFT)) {
2706 while (--spec.field_width > 0) {
2707 if (str < end)
2708 *str = ' ';
2709 ++str;
2710 }
2711 }
2712 c = (unsigned char) get_arg(char);
2713 if (str < end)
2714 *str = c;
2715 ++str;
2716 while (--spec.field_width > 0) {
2717 if (str < end)
2718 *str = ' ';
2719 ++str;
2720 }
2721 break;
2722 }
2723
2724 case FORMAT_TYPE_STR: {
2725 const char *str_arg = args;
2726 args += strlen(str_arg) + 1;
2727 str = string(str, end, (char *)str_arg, spec);
2728 break;
2729 }
2730
2731 case FORMAT_TYPE_PTR:
2732 str = pointer(fmt, str, end, get_arg(void *), spec);
2733 while (isalnum(*fmt))
2734 fmt++;
2735 break;
2736
2737 case FORMAT_TYPE_PERCENT_CHAR:
2738 if (str < end)
2739 *str = '%';
2740 ++str;
2741 break;
2742
2743 case FORMAT_TYPE_INVALID:
2744 goto out;
2745
2746 default: {
2747 unsigned long long num;
2748
2749 switch (spec.type) {
2750
2751 case FORMAT_TYPE_LONG_LONG:
2752 num = get_arg(long long);
2753 break;
2754 case FORMAT_TYPE_ULONG:
2755 case FORMAT_TYPE_LONG:
2756 num = get_arg(unsigned long);
2757 break;
2758 case FORMAT_TYPE_SIZE_T:
2759 num = get_arg(size_t);
2760 break;
2761 case FORMAT_TYPE_PTRDIFF:
2762 num = get_arg(ptrdiff_t);
2763 break;
2764 case FORMAT_TYPE_UBYTE:
2765 num = get_arg(unsigned char);
2766 break;
2767 case FORMAT_TYPE_BYTE:
2768 num = get_arg(signed char);
2769 break;
2770 case FORMAT_TYPE_USHORT:
2771 num = get_arg(unsigned short);
2772 break;
2773 case FORMAT_TYPE_SHORT:
2774 num = get_arg(short);
2775 break;
2776 case FORMAT_TYPE_UINT:
2777 num = get_arg(unsigned int);
2778 break;
2779 default:
2780 num = get_arg(int);
2781 }
2782
2783 str = number(str, end, num, spec);
2784 }
2785 }
2786 }
2787
2788out:
2789 if (size > 0) {
2790 if (str < end)
2791 *str = '\0';
2792 else
2793 end[-1] = '\0';
2794 }
2795
2796#undef get_arg
2797
2798
2799 return str - buf;
2800}
2801EXPORT_SYMBOL_GPL(bstr_printf);
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2814{
2815 va_list args;
2816 int ret;
2817
2818 va_start(args, fmt);
2819 ret = vbin_printf(bin_buf, size, fmt, args);
2820 va_end(args);
2821
2822 return ret;
2823}
2824EXPORT_SYMBOL_GPL(bprintf);
2825
2826#endif
2827
2828
2829
2830
2831
2832
2833
2834int vsscanf(const char *buf, const char *fmt, va_list args)
2835{
2836 const char *str = buf;
2837 char *next;
2838 char digit;
2839 int num = 0;
2840 u8 qualifier;
2841 unsigned int base;
2842 union {
2843 long long s;
2844 unsigned long long u;
2845 } val;
2846 s16 field_width;
2847 bool is_sign;
2848
2849 while (*fmt) {
2850
2851
2852
2853
2854 if (isspace(*fmt)) {
2855 fmt = skip_spaces(++fmt);
2856 str = skip_spaces(str);
2857 }
2858
2859
2860 if (*fmt != '%' && *fmt) {
2861 if (*fmt++ != *str++)
2862 break;
2863 continue;
2864 }
2865
2866 if (!*fmt)
2867 break;
2868 ++fmt;
2869
2870
2871
2872
2873 if (*fmt == '*') {
2874 if (!*str)
2875 break;
2876 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2877
2878 if (*fmt == '[')
2879 return num;
2880 fmt++;
2881 }
2882 while (!isspace(*str) && *str)
2883 str++;
2884 continue;
2885 }
2886
2887
2888 field_width = -1;
2889 if (isdigit(*fmt)) {
2890 field_width = skip_atoi(&fmt);
2891 if (field_width <= 0)
2892 break;
2893 }
2894
2895
2896 qualifier = -1;
2897 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2898 *fmt == 'z') {
2899 qualifier = *fmt++;
2900 if (unlikely(qualifier == *fmt)) {
2901 if (qualifier == 'h') {
2902 qualifier = 'H';
2903 fmt++;
2904 } else if (qualifier == 'l') {
2905 qualifier = 'L';
2906 fmt++;
2907 }
2908 }
2909 }
2910
2911 if (!*fmt)
2912 break;
2913
2914 if (*fmt == 'n') {
2915
2916 *va_arg(args, int *) = str - buf;
2917 ++fmt;
2918 continue;
2919 }
2920
2921 if (!*str)
2922 break;
2923
2924 base = 10;
2925 is_sign = false;
2926
2927 switch (*fmt++) {
2928 case 'c':
2929 {
2930 char *s = (char *)va_arg(args, char*);
2931 if (field_width == -1)
2932 field_width = 1;
2933 do {
2934 *s++ = *str++;
2935 } while (--field_width > 0 && *str);
2936 num++;
2937 }
2938 continue;
2939 case 's':
2940 {
2941 char *s = (char *)va_arg(args, char *);
2942 if (field_width == -1)
2943 field_width = SHRT_MAX;
2944
2945 str = skip_spaces(str);
2946
2947
2948 while (*str && !isspace(*str) && field_width--)
2949 *s++ = *str++;
2950 *s = '\0';
2951 num++;
2952 }
2953 continue;
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969 case '[':
2970 {
2971 char *s = (char *)va_arg(args, char *);
2972 DECLARE_BITMAP(set, 256) = {0};
2973 unsigned int len = 0;
2974 bool negate = (*fmt == '^');
2975
2976
2977 if (field_width == -1)
2978 return num;
2979
2980 if (negate)
2981 ++fmt;
2982
2983 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
2984 set_bit((u8)*fmt, set);
2985
2986
2987 if (!*fmt || !len)
2988 return num;
2989 ++fmt;
2990
2991 if (negate) {
2992 bitmap_complement(set, set, 256);
2993
2994 clear_bit(0, set);
2995 }
2996
2997
2998 if (!test_bit((u8)*str, set))
2999 return num;
3000
3001 while (test_bit((u8)*str, set) && field_width--)
3002 *s++ = *str++;
3003 *s = '\0';
3004 ++num;
3005 }
3006 continue;
3007 case 'o':
3008 base = 8;
3009 break;
3010 case 'x':
3011 case 'X':
3012 base = 16;
3013 break;
3014 case 'i':
3015 base = 0;
3016 case 'd':
3017 is_sign = true;
3018 case 'u':
3019 break;
3020 case '%':
3021
3022 if (*str++ != '%')
3023 return num;
3024 continue;
3025 default:
3026
3027 return num;
3028 }
3029
3030
3031
3032
3033 str = skip_spaces(str);
3034
3035 digit = *str;
3036 if (is_sign && digit == '-')
3037 digit = *(str + 1);
3038
3039 if (!digit
3040 || (base == 16 && !isxdigit(digit))
3041 || (base == 10 && !isdigit(digit))
3042 || (base == 8 && (!isdigit(digit) || digit > '7'))
3043 || (base == 0 && !isdigit(digit)))
3044 break;
3045
3046 if (is_sign)
3047 val.s = qualifier != 'L' ?
3048 simple_strtol(str, &next, base) :
3049 simple_strtoll(str, &next, base);
3050 else
3051 val.u = qualifier != 'L' ?
3052 simple_strtoul(str, &next, base) :
3053 simple_strtoull(str, &next, base);
3054
3055 if (field_width > 0 && next - str > field_width) {
3056 if (base == 0)
3057 _parse_integer_fixup_radix(str, &base);
3058 while (next - str > field_width) {
3059 if (is_sign)
3060 val.s = div_s64(val.s, base);
3061 else
3062 val.u = div_u64(val.u, base);
3063 --next;
3064 }
3065 }
3066
3067 switch (qualifier) {
3068 case 'H':
3069 if (is_sign)
3070 *va_arg(args, signed char *) = val.s;
3071 else
3072 *va_arg(args, unsigned char *) = val.u;
3073 break;
3074 case 'h':
3075 if (is_sign)
3076 *va_arg(args, short *) = val.s;
3077 else
3078 *va_arg(args, unsigned short *) = val.u;
3079 break;
3080 case 'l':
3081 if (is_sign)
3082 *va_arg(args, long *) = val.s;
3083 else
3084 *va_arg(args, unsigned long *) = val.u;
3085 break;
3086 case 'L':
3087 if (is_sign)
3088 *va_arg(args, long long *) = val.s;
3089 else
3090 *va_arg(args, unsigned long long *) = val.u;
3091 break;
3092 case 'z':
3093 *va_arg(args, size_t *) = val.u;
3094 break;
3095 default:
3096 if (is_sign)
3097 *va_arg(args, int *) = val.s;
3098 else
3099 *va_arg(args, unsigned int *) = val.u;
3100 break;
3101 }
3102 num++;
3103
3104 if (!next)
3105 break;
3106 str = next;
3107 }
3108
3109 return num;
3110}
3111EXPORT_SYMBOL(vsscanf);
3112
3113
3114
3115
3116
3117
3118
3119int sscanf(const char *buf, const char *fmt, ...)
3120{
3121 va_list args;
3122 int i;
3123
3124 va_start(args, fmt);
3125 i = vsscanf(buf, fmt, args);
3126 va_end(args);
3127
3128 return i;
3129}
3130EXPORT_SYMBOL(sscanf);
3131