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/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
25#include <linux/kallsyms.h>
26#include <linux/math64.h>
27#include <linux/uaccess.h>
28#include <linux/ioport.h>
29#include <linux/cpumask.h>
30#include <linux/dcache.h>
31#include <linux/cred.h>
32#include <linux/uuid.h>
33#include <net/addrconf.h>
34
35#include <asm/page.h>
36#include <asm/sections.h>
37
38#include "kstrtox.h"
39
40
41
42
43
44
45
46
47
48unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
49{
50 unsigned long long result;
51 unsigned int rv;
52
53 cp = _parse_integer_fixup_radix(cp, &base);
54 rv = _parse_integer(cp, base, &result);
55
56 cp += (rv & ~KSTRTOX_OVERFLOW);
57
58 if (endp)
59 *endp = (char *)cp;
60
61 return result;
62}
63EXPORT_SYMBOL(simple_strtoull);
64
65
66
67
68
69
70
71
72
73unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
74{
75 return simple_strtoull(cp, endp, base);
76}
77EXPORT_SYMBOL(simple_strtoul);
78
79
80
81
82
83
84
85
86
87long simple_strtol(const char *cp, char **endp, unsigned int base)
88{
89 if (*cp == '-')
90 return -simple_strtoul(cp + 1, endp, base);
91
92 return simple_strtoul(cp, endp, base);
93}
94EXPORT_SYMBOL(simple_strtol);
95
96
97
98
99
100
101
102
103
104long long simple_strtoll(const char *cp, char **endp, unsigned int base)
105{
106 if (*cp == '-')
107 return -simple_strtoull(cp + 1, endp, base);
108
109 return simple_strtoull(cp, endp, base);
110}
111EXPORT_SYMBOL(simple_strtoll);
112
113static noinline_for_stack
114int skip_atoi(const char **s)
115{
116 int i = 0;
117
118 while (isdigit(**s))
119 i = i*10 + *((*s)++) - '0';
120
121 return i;
122}
123
124
125
126
127
128
129
130
131#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
132
133static noinline_for_stack
134char *put_dec_full9(char *buf, unsigned q)
135{
136 unsigned r;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 r = (q * (uint64_t)0x1999999a) >> 32;
158 *buf++ = (q - 10 * r) + '0';
159 q = (r * (uint64_t)0x1999999a) >> 32;
160 *buf++ = (r - 10 * q) + '0';
161 r = (q * (uint64_t)0x1999999a) >> 32;
162 *buf++ = (q - 10 * r) + '0';
163 q = (r * (uint64_t)0x1999999a) >> 32;
164 *buf++ = (r - 10 * q) + '0';
165 r = (q * (uint64_t)0x1999999a) >> 32;
166 *buf++ = (q - 10 * r) + '0';
167
168 q = (r * 0x199a) >> 16;
169 *buf++ = (r - 10 * q) + '0';
170 r = (q * 0xcd) >> 11;
171 *buf++ = (q - 10 * r) + '0';
172 q = (r * 0xcd) >> 11;
173 *buf++ = (r - 10 * q) + '0';
174 *buf++ = q + '0';
175 return buf;
176}
177#endif
178
179
180
181
182
183static noinline_for_stack
184char *put_dec_trunc8(char *buf, unsigned r)
185{
186 unsigned q;
187
188
189 while (r >= 10000) {
190 q = r + '0';
191 r = (r * (uint64_t)0x1999999a) >> 32;
192 *buf++ = q - 10*r;
193 }
194
195 q = (r * 0x199a) >> 16;
196 *buf++ = (r - 10 * q) + '0';
197 if (q == 0)
198 return buf;
199 r = (q * 0xcd) >> 11;
200 *buf++ = (q - 10 * r) + '0';
201 if (r == 0)
202 return buf;
203 q = (r * 0xcd) >> 11;
204 *buf++ = (r - 10 * q) + '0';
205 if (q == 0)
206 return buf;
207 *buf++ = q + '0';
208 return buf;
209}
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
229
230
231
232static
233char *put_dec(char *buf, unsigned long long n)
234{
235 if (n >= 100*1000*1000) {
236 while (n >= 1000*1000*1000)
237 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
238 if (n >= 100*1000*1000)
239 return put_dec_full9(buf, n);
240 }
241 return put_dec_trunc8(buf, n);
242}
243
244#else
245
246
247
248
249static noinline_for_stack
250void put_dec_full4(char *buf, unsigned q)
251{
252 unsigned r;
253 r = (q * 0xccd) >> 15;
254 buf[0] = (q - 10 * r) + '0';
255 q = (r * 0xcd) >> 11;
256 buf[1] = (r - 10 * q) + '0';
257 r = (q * 0xcd) >> 11;
258 buf[2] = (q - 10 * r) + '0';
259 buf[3] = r + '0';
260}
261
262
263
264
265
266
267
268
269static
270unsigned put_dec_helper4(char *buf, unsigned x)
271{
272 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
273
274 put_dec_full4(buf, x - q * 10000);
275 return q;
276}
277
278
279
280
281
282
283static
284char *put_dec(char *buf, unsigned long long n)
285{
286 uint32_t d3, d2, d1, q, h;
287
288 if (n < 100*1000*1000)
289 return put_dec_trunc8(buf, n);
290
291 d1 = ((uint32_t)n >> 16);
292 h = (n >> 32);
293 d2 = (h ) & 0xffff;
294 d3 = (h >> 16);
295
296 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
297 q = put_dec_helper4(buf, q);
298
299 q += 7671 * d3 + 9496 * d2 + 6 * d1;
300 q = put_dec_helper4(buf+4, q);
301
302 q += 4749 * d3 + 42 * d2;
303 q = put_dec_helper4(buf+8, q);
304
305 q += 281 * d3;
306 buf += 12;
307 if (q)
308 buf = put_dec_trunc8(buf, q);
309 else while (buf[-1] == '0')
310 --buf;
311
312 return buf;
313}
314
315#endif
316
317
318
319
320
321
322
323int num_to_str(char *buf, int size, unsigned long long num)
324{
325 char tmp[sizeof(num) * 3];
326 int idx, len;
327
328
329 if (num <= 9) {
330 tmp[0] = '0' + num;
331 len = 1;
332 } else {
333 len = put_dec(tmp, num) - tmp;
334 }
335
336 if (len > size)
337 return 0;
338 for (idx = 0; idx < len; ++idx)
339 buf[idx] = tmp[len - idx - 1];
340 return len;
341}
342
343#define ZEROPAD 1
344#define SIGN 2
345#define PLUS 4
346#define SPACE 8
347#define LEFT 16
348#define SMALL 32
349#define SPECIAL 64
350
351enum format_type {
352 FORMAT_TYPE_NONE,
353 FORMAT_TYPE_WIDTH,
354 FORMAT_TYPE_PRECISION,
355 FORMAT_TYPE_CHAR,
356 FORMAT_TYPE_STR,
357 FORMAT_TYPE_PTR,
358 FORMAT_TYPE_PERCENT_CHAR,
359 FORMAT_TYPE_INVALID,
360 FORMAT_TYPE_LONG_LONG,
361 FORMAT_TYPE_ULONG,
362 FORMAT_TYPE_LONG,
363 FORMAT_TYPE_UBYTE,
364 FORMAT_TYPE_BYTE,
365 FORMAT_TYPE_USHORT,
366 FORMAT_TYPE_SHORT,
367 FORMAT_TYPE_UINT,
368 FORMAT_TYPE_INT,
369 FORMAT_TYPE_NRCHARS,
370 FORMAT_TYPE_SIZE_T,
371 FORMAT_TYPE_PTRDIFF
372};
373
374struct printf_spec {
375 u8 type;
376 u8 flags;
377 u8 base;
378 u8 qualifier;
379 s16 field_width;
380 s16 precision;
381};
382
383static noinline_for_stack
384char *number(char *buf, char *end, unsigned long long num,
385 struct printf_spec spec)
386{
387
388 static const char digits[16] = "0123456789ABCDEF";
389
390 char tmp[66];
391 char sign;
392 char locase;
393 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
394 int i;
395 bool is_zero = num == 0LL;
396
397
398
399 locase = (spec.flags & SMALL);
400 if (spec.flags & LEFT)
401 spec.flags &= ~ZEROPAD;
402 sign = 0;
403 if (spec.flags & SIGN) {
404 if ((signed long long)num < 0) {
405 sign = '-';
406 num = -(signed long long)num;
407 spec.field_width--;
408 } else if (spec.flags & PLUS) {
409 sign = '+';
410 spec.field_width--;
411 } else if (spec.flags & SPACE) {
412 sign = ' ';
413 spec.field_width--;
414 }
415 }
416 if (need_pfx) {
417 if (spec.base == 16)
418 spec.field_width -= 2;
419 else if (!is_zero)
420 spec.field_width--;
421 }
422
423
424 i = 0;
425 if (num < spec.base)
426 tmp[i++] = digits[num] | locase;
427
428
429
430
431
432 else if (spec.base != 10) {
433 int mask = spec.base - 1;
434 int shift = 3;
435
436 if (spec.base == 16)
437 shift = 4;
438 do {
439 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
440 num >>= shift;
441 } while (num);
442 } else {
443 i = put_dec(tmp, num) - tmp;
444 }
445
446
447 if (i > spec.precision)
448 spec.precision = i;
449
450 spec.field_width -= spec.precision;
451 if (!(spec.flags & (ZEROPAD+LEFT))) {
452 while (--spec.field_width >= 0) {
453 if (buf < end)
454 *buf = ' ';
455 ++buf;
456 }
457 }
458
459 if (sign) {
460 if (buf < end)
461 *buf = sign;
462 ++buf;
463 }
464
465 if (need_pfx) {
466 if (spec.base == 16 || !is_zero) {
467 if (buf < end)
468 *buf = '0';
469 ++buf;
470 }
471 if (spec.base == 16) {
472 if (buf < end)
473 *buf = ('X' | locase);
474 ++buf;
475 }
476 }
477
478 if (!(spec.flags & LEFT)) {
479 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
480 while (--spec.field_width >= 0) {
481 if (buf < end)
482 *buf = c;
483 ++buf;
484 }
485 }
486
487 while (i <= --spec.precision) {
488 if (buf < end)
489 *buf = '0';
490 ++buf;
491 }
492
493 while (--i >= 0) {
494 if (buf < end)
495 *buf = tmp[i];
496 ++buf;
497 }
498
499 while (--spec.field_width >= 0) {
500 if (buf < end)
501 *buf = ' ';
502 ++buf;
503 }
504
505 return buf;
506}
507
508static noinline_for_stack
509char *string(char *buf, char *end, const char *s, struct printf_spec spec)
510{
511 int len, i;
512
513 if ((unsigned long)s < PAGE_SIZE)
514 s = "(null)";
515
516 len = strnlen(s, spec.precision);
517
518 if (!(spec.flags & LEFT)) {
519 while (len < spec.field_width--) {
520 if (buf < end)
521 *buf = ' ';
522 ++buf;
523 }
524 }
525 for (i = 0; i < len; ++i) {
526 if (buf < end)
527 *buf = *s;
528 ++buf; ++s;
529 }
530 while (len < spec.field_width--) {
531 if (buf < end)
532 *buf = ' ';
533 ++buf;
534 }
535
536 return buf;
537}
538
539static void widen(char *buf, char *end, unsigned len, unsigned spaces)
540{
541 size_t size;
542 if (buf >= end)
543 return;
544 size = end - buf;
545 if (size <= spaces) {
546 memset(buf, ' ', size);
547 return;
548 }
549 if (len) {
550 if (len > size - spaces)
551 len = size - spaces;
552 memmove(buf + spaces, buf, len);
553 }
554 memset(buf, ' ', spaces);
555}
556
557static noinline_for_stack
558char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
559 const char *fmt)
560{
561 const char *array[4], *s;
562 const struct dentry *p;
563 int depth;
564 int i, n;
565
566 switch (fmt[1]) {
567 case '2': case '3': case '4':
568 depth = fmt[1] - '0';
569 break;
570 default:
571 depth = 1;
572 }
573
574 rcu_read_lock();
575 for (i = 0; i < depth; i++, d = p) {
576 p = ACCESS_ONCE(d->d_parent);
577 array[i] = ACCESS_ONCE(d->d_name.name);
578 if (p == d) {
579 if (i)
580 array[i] = "";
581 i++;
582 break;
583 }
584 }
585 s = array[--i];
586 for (n = 0; n != spec.precision; n++, buf++) {
587 char c = *s++;
588 if (!c) {
589 if (!i)
590 break;
591 c = '/';
592 s = array[--i];
593 }
594 if (buf < end)
595 *buf = c;
596 }
597 rcu_read_unlock();
598 if (n < spec.field_width) {
599
600 unsigned spaces = spec.field_width - n;
601 if (!(spec.flags & LEFT)) {
602 widen(buf - n, end, n, spaces);
603 return buf + spaces;
604 }
605 while (spaces--) {
606 if (buf < end)
607 *buf = ' ';
608 ++buf;
609 }
610 }
611 return buf;
612}
613
614static noinline_for_stack
615char *symbol_string(char *buf, char *end, void *ptr,
616 struct printf_spec spec, const char *fmt)
617{
618 unsigned long value;
619#ifdef CONFIG_KALLSYMS
620 char sym[KSYM_SYMBOL_LEN];
621#endif
622
623 if (fmt[1] == 'R')
624 ptr = __builtin_extract_return_addr(ptr);
625 value = (unsigned long)ptr;
626
627#ifdef CONFIG_KALLSYMS
628 if (*fmt == 'B')
629 sprint_backtrace(sym, value);
630 else if (*fmt != 'f' && *fmt != 's')
631 sprint_symbol(sym, value);
632 else
633 sprint_symbol_no_offset(sym, value);
634
635 return string(buf, end, sym, spec);
636#else
637 spec.field_width = 2 * sizeof(void *);
638 spec.flags |= SPECIAL | SMALL | ZEROPAD;
639 spec.base = 16;
640
641 return number(buf, end, value, spec);
642#endif
643}
644
645static noinline_for_stack
646char *resource_string(char *buf, char *end, struct resource *res,
647 struct printf_spec spec, const char *fmt)
648{
649#ifndef IO_RSRC_PRINTK_SIZE
650#define IO_RSRC_PRINTK_SIZE 6
651#endif
652
653#ifndef MEM_RSRC_PRINTK_SIZE
654#define MEM_RSRC_PRINTK_SIZE 10
655#endif
656 static const struct printf_spec io_spec = {
657 .base = 16,
658 .field_width = IO_RSRC_PRINTK_SIZE,
659 .precision = -1,
660 .flags = SPECIAL | SMALL | ZEROPAD,
661 };
662 static const struct printf_spec mem_spec = {
663 .base = 16,
664 .field_width = MEM_RSRC_PRINTK_SIZE,
665 .precision = -1,
666 .flags = SPECIAL | SMALL | ZEROPAD,
667 };
668 static const struct printf_spec bus_spec = {
669 .base = 16,
670 .field_width = 2,
671 .precision = -1,
672 .flags = SMALL | ZEROPAD,
673 };
674 static const struct printf_spec dec_spec = {
675 .base = 10,
676 .precision = -1,
677 .flags = 0,
678 };
679 static const struct printf_spec str_spec = {
680 .field_width = -1,
681 .precision = 10,
682 .flags = LEFT,
683 };
684 static const struct printf_spec flag_spec = {
685 .base = 16,
686 .precision = -1,
687 .flags = SPECIAL | SMALL,
688 };
689
690
691
692#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
693#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
694#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
695#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
696 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
697 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
698
699 char *p = sym, *pend = sym + sizeof(sym);
700 int decode = (fmt[0] == 'R') ? 1 : 0;
701 const struct printf_spec *specp;
702
703 *p++ = '[';
704 if (res->flags & IORESOURCE_IO) {
705 p = string(p, pend, "io ", str_spec);
706 specp = &io_spec;
707 } else if (res->flags & IORESOURCE_MEM) {
708 p = string(p, pend, "mem ", str_spec);
709 specp = &mem_spec;
710 } else if (res->flags & IORESOURCE_IRQ) {
711 p = string(p, pend, "irq ", str_spec);
712 specp = &dec_spec;
713 } else if (res->flags & IORESOURCE_DMA) {
714 p = string(p, pend, "dma ", str_spec);
715 specp = &dec_spec;
716 } else if (res->flags & IORESOURCE_BUS) {
717 p = string(p, pend, "bus ", str_spec);
718 specp = &bus_spec;
719 } else {
720 p = string(p, pend, "??? ", str_spec);
721 specp = &mem_spec;
722 decode = 0;
723 }
724 if (decode && res->flags & IORESOURCE_UNSET) {
725 p = string(p, pend, "size ", str_spec);
726 p = number(p, pend, resource_size(res), *specp);
727 } else {
728 p = number(p, pend, res->start, *specp);
729 if (res->start != res->end) {
730 *p++ = '-';
731 p = number(p, pend, res->end, *specp);
732 }
733 }
734 if (decode) {
735 if (res->flags & IORESOURCE_MEM_64)
736 p = string(p, pend, " 64bit", str_spec);
737 if (res->flags & IORESOURCE_PREFETCH)
738 p = string(p, pend, " pref", str_spec);
739 if (res->flags & IORESOURCE_WINDOW)
740 p = string(p, pend, " window", str_spec);
741 if (res->flags & IORESOURCE_DISABLED)
742 p = string(p, pend, " disabled", str_spec);
743 } else {
744 p = string(p, pend, " flags ", str_spec);
745 p = number(p, pend, res->flags, flag_spec);
746 }
747 *p++ = ']';
748 *p = '\0';
749
750 return string(buf, end, sym, spec);
751}
752
753static noinline_for_stack
754char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
755 const char *fmt)
756{
757 int i, len = 1;
758
759 char separator;
760
761 if (spec.field_width == 0)
762
763 return buf;
764
765 if (ZERO_OR_NULL_PTR(addr))
766
767 return string(buf, end, NULL, spec);
768
769 switch (fmt[1]) {
770 case 'C':
771 separator = ':';
772 break;
773 case 'D':
774 separator = '-';
775 break;
776 case 'N':
777 separator = 0;
778 break;
779 default:
780 separator = ' ';
781 break;
782 }
783
784 if (spec.field_width > 0)
785 len = min_t(int, spec.field_width, 64);
786
787 for (i = 0; i < len; ++i) {
788 if (buf < end)
789 *buf = hex_asc_hi(addr[i]);
790 ++buf;
791 if (buf < end)
792 *buf = hex_asc_lo(addr[i]);
793 ++buf;
794
795 if (separator && i != len - 1) {
796 if (buf < end)
797 *buf = separator;
798 ++buf;
799 }
800 }
801
802 return buf;
803}
804
805static noinline_for_stack
806char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
807 struct printf_spec spec, const char *fmt)
808{
809 const int CHUNKSZ = 32;
810 int nr_bits = max_t(int, spec.field_width, 0);
811 int i, chunksz;
812 bool first = true;
813
814
815 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
816
817 chunksz = nr_bits & (CHUNKSZ - 1);
818 if (chunksz == 0)
819 chunksz = CHUNKSZ;
820
821 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
822 for (; i >= 0; i -= CHUNKSZ) {
823 u32 chunkmask, val;
824 int word, bit;
825
826 chunkmask = ((1ULL << chunksz) - 1);
827 word = i / BITS_PER_LONG;
828 bit = i % BITS_PER_LONG;
829 val = (bitmap[word] >> bit) & chunkmask;
830
831 if (!first) {
832 if (buf < end)
833 *buf = ',';
834 buf++;
835 }
836 first = false;
837
838 spec.field_width = DIV_ROUND_UP(chunksz, 4);
839 buf = number(buf, end, val, spec);
840
841 chunksz = CHUNKSZ;
842 }
843 return buf;
844}
845
846static noinline_for_stack
847char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
848 struct printf_spec spec, const char *fmt)
849{
850 int nr_bits = max_t(int, spec.field_width, 0);
851
852 int cur, rbot, rtop;
853 bool first = true;
854
855
856 spec = (struct printf_spec){ .base = 10 };
857
858 rbot = cur = find_first_bit(bitmap, nr_bits);
859 while (cur < nr_bits) {
860 rtop = cur;
861 cur = find_next_bit(bitmap, nr_bits, cur + 1);
862 if (cur < nr_bits && cur <= rtop + 1)
863 continue;
864
865 if (!first) {
866 if (buf < end)
867 *buf = ',';
868 buf++;
869 }
870 first = false;
871
872 buf = number(buf, end, rbot, spec);
873 if (rbot < rtop) {
874 if (buf < end)
875 *buf = '-';
876 buf++;
877
878 buf = number(buf, end, rtop, spec);
879 }
880
881 rbot = cur;
882 }
883 return buf;
884}
885
886static noinline_for_stack
887char *mac_address_string(char *buf, char *end, u8 *addr,
888 struct printf_spec spec, const char *fmt)
889{
890 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
891 char *p = mac_addr;
892 int i;
893 char separator;
894 bool reversed = false;
895
896 switch (fmt[1]) {
897 case 'F':
898 separator = '-';
899 break;
900
901 case 'R':
902 reversed = true;
903
904
905 default:
906 separator = ':';
907 break;
908 }
909
910 for (i = 0; i < 6; i++) {
911 if (reversed)
912 p = hex_byte_pack(p, addr[5 - i]);
913 else
914 p = hex_byte_pack(p, addr[i]);
915
916 if (fmt[0] == 'M' && i != 5)
917 *p++ = separator;
918 }
919 *p = '\0';
920
921 return string(buf, end, mac_addr, spec);
922}
923
924static noinline_for_stack
925char *ip4_string(char *p, const u8 *addr, const char *fmt)
926{
927 int i;
928 bool leading_zeros = (fmt[0] == 'i');
929 int index;
930 int step;
931
932 switch (fmt[2]) {
933 case 'h':
934#ifdef __BIG_ENDIAN
935 index = 0;
936 step = 1;
937#else
938 index = 3;
939 step = -1;
940#endif
941 break;
942 case 'l':
943 index = 3;
944 step = -1;
945 break;
946 case 'n':
947 case 'b':
948 default:
949 index = 0;
950 step = 1;
951 break;
952 }
953 for (i = 0; i < 4; i++) {
954 char temp[3];
955 int digits = put_dec_trunc8(temp, addr[index]) - temp;
956 if (leading_zeros) {
957 if (digits < 3)
958 *p++ = '0';
959 if (digits < 2)
960 *p++ = '0';
961 }
962
963 while (digits--)
964 *p++ = temp[digits];
965 if (i < 3)
966 *p++ = '.';
967 index += step;
968 }
969 *p = '\0';
970
971 return p;
972}
973
974static noinline_for_stack
975char *ip6_compressed_string(char *p, const char *addr)
976{
977 int i, j, range;
978 unsigned char zerolength[8];
979 int longest = 1;
980 int colonpos = -1;
981 u16 word;
982 u8 hi, lo;
983 bool needcolon = false;
984 bool useIPv4;
985 struct in6_addr in6;
986
987 memcpy(&in6, addr, sizeof(struct in6_addr));
988
989 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
990
991 memset(zerolength, 0, sizeof(zerolength));
992
993 if (useIPv4)
994 range = 6;
995 else
996 range = 8;
997
998
999 for (i = 0; i < range; i++) {
1000 for (j = i; j < range; j++) {
1001 if (in6.s6_addr16[j] != 0)
1002 break;
1003 zerolength[i]++;
1004 }
1005 }
1006 for (i = 0; i < range; i++) {
1007 if (zerolength[i] > longest) {
1008 longest = zerolength[i];
1009 colonpos = i;
1010 }
1011 }
1012 if (longest == 1)
1013 colonpos = -1;
1014
1015
1016 for (i = 0; i < range; i++) {
1017 if (i == colonpos) {
1018 if (needcolon || i == 0)
1019 *p++ = ':';
1020 *p++ = ':';
1021 needcolon = false;
1022 i += longest - 1;
1023 continue;
1024 }
1025 if (needcolon) {
1026 *p++ = ':';
1027 needcolon = false;
1028 }
1029
1030 word = ntohs(in6.s6_addr16[i]);
1031 hi = word >> 8;
1032 lo = word & 0xff;
1033 if (hi) {
1034 if (hi > 0x0f)
1035 p = hex_byte_pack(p, hi);
1036 else
1037 *p++ = hex_asc_lo(hi);
1038 p = hex_byte_pack(p, lo);
1039 }
1040 else if (lo > 0x0f)
1041 p = hex_byte_pack(p, lo);
1042 else
1043 *p++ = hex_asc_lo(lo);
1044 needcolon = true;
1045 }
1046
1047 if (useIPv4) {
1048 if (needcolon)
1049 *p++ = ':';
1050 p = ip4_string(p, &in6.s6_addr[12], "I4");
1051 }
1052 *p = '\0';
1053
1054 return p;
1055}
1056
1057static noinline_for_stack
1058char *ip6_string(char *p, const char *addr, const char *fmt)
1059{
1060 int i;
1061
1062 for (i = 0; i < 8; i++) {
1063 p = hex_byte_pack(p, *addr++);
1064 p = hex_byte_pack(p, *addr++);
1065 if (fmt[0] == 'I' && i != 7)
1066 *p++ = ':';
1067 }
1068 *p = '\0';
1069
1070 return p;
1071}
1072
1073static noinline_for_stack
1074char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1075 struct printf_spec spec, const char *fmt)
1076{
1077 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1078
1079 if (fmt[0] == 'I' && fmt[2] == 'c')
1080 ip6_compressed_string(ip6_addr, addr);
1081 else
1082 ip6_string(ip6_addr, addr, fmt);
1083
1084 return string(buf, end, ip6_addr, spec);
1085}
1086
1087static noinline_for_stack
1088char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1089 struct printf_spec spec, const char *fmt)
1090{
1091 char ip4_addr[sizeof("255.255.255.255")];
1092
1093 ip4_string(ip4_addr, addr, fmt);
1094
1095 return string(buf, end, ip4_addr, spec);
1096}
1097
1098static noinline_for_stack
1099char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1100 struct printf_spec spec, const char *fmt)
1101{
1102 bool have_p = false, have_s = false, have_f = false, have_c = false;
1103 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1104 sizeof(":12345") + sizeof("/123456789") +
1105 sizeof("%1234567890")];
1106 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1107 const u8 *addr = (const u8 *) &sa->sin6_addr;
1108 char fmt6[2] = { fmt[0], '6' };
1109 u8 off = 0;
1110
1111 fmt++;
1112 while (isalpha(*++fmt)) {
1113 switch (*fmt) {
1114 case 'p':
1115 have_p = true;
1116 break;
1117 case 'f':
1118 have_f = true;
1119 break;
1120 case 's':
1121 have_s = true;
1122 break;
1123 case 'c':
1124 have_c = true;
1125 break;
1126 }
1127 }
1128
1129 if (have_p || have_s || have_f) {
1130 *p = '[';
1131 off = 1;
1132 }
1133
1134 if (fmt6[0] == 'I' && have_c)
1135 p = ip6_compressed_string(ip6_addr + off, addr);
1136 else
1137 p = ip6_string(ip6_addr + off, addr, fmt6);
1138
1139 if (have_p || have_s || have_f)
1140 *p++ = ']';
1141
1142 if (have_p) {
1143 *p++ = ':';
1144 p = number(p, pend, ntohs(sa->sin6_port), spec);
1145 }
1146 if (have_f) {
1147 *p++ = '/';
1148 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1149 IPV6_FLOWINFO_MASK), spec);
1150 }
1151 if (have_s) {
1152 *p++ = '%';
1153 p = number(p, pend, sa->sin6_scope_id, spec);
1154 }
1155 *p = '\0';
1156
1157 return string(buf, end, ip6_addr, spec);
1158}
1159
1160static noinline_for_stack
1161char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1162 struct printf_spec spec, const char *fmt)
1163{
1164 bool have_p = false;
1165 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1166 char *pend = ip4_addr + sizeof(ip4_addr);
1167 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1168 char fmt4[3] = { fmt[0], '4', 0 };
1169
1170 fmt++;
1171 while (isalpha(*++fmt)) {
1172 switch (*fmt) {
1173 case 'p':
1174 have_p = true;
1175 break;
1176 case 'h':
1177 case 'l':
1178 case 'n':
1179 case 'b':
1180 fmt4[2] = *fmt;
1181 break;
1182 }
1183 }
1184
1185 p = ip4_string(ip4_addr, addr, fmt4);
1186 if (have_p) {
1187 *p++ = ':';
1188 p = number(p, pend, ntohs(sa->sin_port), spec);
1189 }
1190 *p = '\0';
1191
1192 return string(buf, end, ip4_addr, spec);
1193}
1194
1195static noinline_for_stack
1196char *uuid_string(char *buf, char *end, const u8 *addr,
1197 struct printf_spec spec, const char *fmt)
1198{
1199 char uuid[UUID_STRING_LEN + 1];
1200 char *p = uuid;
1201 int i;
1202 const u8 *index = uuid_index;
1203 bool uc = false;
1204
1205 switch (*(++fmt)) {
1206 case 'L':
1207 uc = true;
1208 case 'l':
1209 index = guid_index;
1210 break;
1211 case 'B':
1212 uc = true;
1213 break;
1214 }
1215
1216 for (i = 0; i < 16; i++) {
1217 p = hex_byte_pack(p, addr[index[i]]);
1218 switch (i) {
1219 case 3:
1220 case 5:
1221 case 7:
1222 case 9:
1223 *p++ = '-';
1224 break;
1225 }
1226 }
1227
1228 *p = 0;
1229
1230 if (uc) {
1231 p = uuid;
1232 do {
1233 *p = toupper(*p);
1234 } while (*(++p));
1235 }
1236
1237 return string(buf, end, uuid, spec);
1238}
1239
1240static
1241char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1242 struct printf_spec spec)
1243{
1244 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1245 if (spec.field_width == -1)
1246 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1247 spec.base = 16;
1248
1249 return number(buf, end, *(const netdev_features_t *)addr, spec);
1250}
1251
1252int kptr_restrict __read_mostly;
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328static noinline_for_stack
1329char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1330 struct printf_spec spec)
1331{
1332 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1333
1334 if (!ptr && *fmt != 'K') {
1335
1336
1337
1338
1339 if (spec.field_width == -1)
1340 spec.field_width = default_width;
1341 return string(buf, end, "(null)", spec);
1342 }
1343
1344 switch (*fmt) {
1345 case 'F':
1346 case 'f':
1347 ptr = dereference_function_descriptor(ptr);
1348
1349 case 'S':
1350 case 's':
1351 case 'B':
1352 return symbol_string(buf, end, ptr, spec, fmt);
1353 case 'R':
1354 case 'r':
1355 return resource_string(buf, end, ptr, spec, fmt);
1356 case 'h':
1357 return hex_string(buf, end, ptr, spec, fmt);
1358 case 'b':
1359 switch (fmt[1]) {
1360 case 'l':
1361 return bitmap_list_string(buf, end, ptr, spec, fmt);
1362 default:
1363 return bitmap_string(buf, end, ptr, spec, fmt);
1364 }
1365 case 'M':
1366 case 'm':
1367
1368
1369 return mac_address_string(buf, end, ptr, spec, fmt);
1370 case 'I':
1371
1372
1373
1374
1375 case 'i':
1376
1377
1378
1379 switch (fmt[1]) {
1380 case '6':
1381 return ip6_addr_string(buf, end, ptr, spec, fmt);
1382 case '4':
1383 return ip4_addr_string(buf, end, ptr, spec, fmt);
1384 case 'S': {
1385 const union {
1386 struct sockaddr raw;
1387 struct sockaddr_in v4;
1388 struct sockaddr_in6 v6;
1389 } *sa = ptr;
1390
1391 switch (sa->raw.sa_family) {
1392 case AF_INET:
1393 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1394 case AF_INET6:
1395 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1396 default:
1397 return string(buf, end, "(invalid address)", spec);
1398 }}
1399 }
1400 break;
1401 case 'U':
1402 return uuid_string(buf, end, ptr, spec, fmt);
1403 case 'V':
1404 {
1405 va_list va;
1406
1407 va_copy(va, *((struct va_format *)ptr)->va);
1408 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1409 ((struct va_format *)ptr)->fmt, va);
1410 va_end(va);
1411 return buf;
1412 }
1413 case 'K':
1414
1415
1416
1417
1418 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1419 in_nmi())) {
1420 if (spec.field_width == -1)
1421 spec.field_width = default_width;
1422 return string(buf, end, "pK-error", spec);
1423 }
1424
1425 switch (kptr_restrict) {
1426 case 0:
1427
1428 break;
1429 case 1: {
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439 const struct cred *cred = current_cred();
1440
1441 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1442 !uid_eq(cred->euid, cred->uid) ||
1443 !gid_eq(cred->egid, cred->gid))
1444 ptr = NULL;
1445 break;
1446 }
1447 case 2:
1448 default:
1449
1450 ptr = NULL;
1451 break;
1452 }
1453 break;
1454
1455 case 'N':
1456 switch (fmt[1]) {
1457 case 'F':
1458 return netdev_feature_string(buf, end, ptr, spec);
1459 }
1460 break;
1461 case 'a':
1462 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1463 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1464 spec.base = 16;
1465 return number(buf, end,
1466 (unsigned long long) *((phys_addr_t *)ptr), spec);
1467 case 'c':
1468 return buf + cpulist_scnprintf(buf, end - buf, ptr);
1469 case 'd':
1470 return dentry_name(buf, end, ptr, spec, fmt);
1471 case 'D':
1472 return dentry_name(buf, end,
1473 ((const struct file *)ptr)->f_path.dentry,
1474 spec, fmt);
1475 }
1476 spec.flags |= SMALL;
1477 if (spec.field_width == -1) {
1478 spec.field_width = default_width;
1479 spec.flags |= ZEROPAD;
1480 }
1481 spec.base = 16;
1482
1483 return number(buf, end, (unsigned long) ptr, spec);
1484}
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506static noinline_for_stack
1507int format_decode(const char *fmt, struct printf_spec *spec)
1508{
1509 const char *start = fmt;
1510
1511
1512 if (spec->type == FORMAT_TYPE_WIDTH) {
1513 if (spec->field_width < 0) {
1514 spec->field_width = -spec->field_width;
1515 spec->flags |= LEFT;
1516 }
1517 spec->type = FORMAT_TYPE_NONE;
1518 goto precision;
1519 }
1520
1521
1522 if (spec->type == FORMAT_TYPE_PRECISION) {
1523 if (spec->precision < 0)
1524 spec->precision = 0;
1525
1526 spec->type = FORMAT_TYPE_NONE;
1527 goto qualifier;
1528 }
1529
1530
1531 spec->type = FORMAT_TYPE_NONE;
1532
1533 for (; *fmt ; ++fmt) {
1534 if (*fmt == '%')
1535 break;
1536 }
1537
1538
1539 if (fmt != start || !*fmt)
1540 return fmt - start;
1541
1542
1543 spec->flags = 0;
1544
1545 while (1) {
1546 bool found = true;
1547
1548 ++fmt;
1549
1550 switch (*fmt) {
1551 case '-': spec->flags |= LEFT; break;
1552 case '+': spec->flags |= PLUS; break;
1553 case ' ': spec->flags |= SPACE; break;
1554 case '#': spec->flags |= SPECIAL; break;
1555 case '0': spec->flags |= ZEROPAD; break;
1556 default: found = false;
1557 }
1558
1559 if (!found)
1560 break;
1561 }
1562
1563
1564 spec->field_width = -1;
1565
1566 if (isdigit(*fmt))
1567 spec->field_width = skip_atoi(&fmt);
1568 else if (*fmt == '*') {
1569
1570 spec->type = FORMAT_TYPE_WIDTH;
1571 return ++fmt - start;
1572 }
1573
1574precision:
1575
1576 spec->precision = -1;
1577 if (*fmt == '.') {
1578 ++fmt;
1579 if (isdigit(*fmt)) {
1580 spec->precision = skip_atoi(&fmt);
1581 if (spec->precision < 0)
1582 spec->precision = 0;
1583 } else if (*fmt == '*') {
1584
1585 spec->type = FORMAT_TYPE_PRECISION;
1586 return ++fmt - start;
1587 }
1588 }
1589
1590qualifier:
1591
1592 spec->qualifier = -1;
1593 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1594 _tolower(*fmt) == 'z' || *fmt == 't') {
1595 spec->qualifier = *fmt++;
1596 if (unlikely(spec->qualifier == *fmt)) {
1597 if (spec->qualifier == 'l') {
1598 spec->qualifier = 'L';
1599 ++fmt;
1600 } else if (spec->qualifier == 'h') {
1601 spec->qualifier = 'H';
1602 ++fmt;
1603 }
1604 }
1605 }
1606
1607
1608 spec->base = 10;
1609 switch (*fmt) {
1610 case 'c':
1611 spec->type = FORMAT_TYPE_CHAR;
1612 return ++fmt - start;
1613
1614 case 's':
1615 spec->type = FORMAT_TYPE_STR;
1616 return ++fmt - start;
1617
1618 case 'p':
1619 spec->type = FORMAT_TYPE_PTR;
1620 return fmt - start;
1621
1622
1623 case 'n':
1624 spec->type = FORMAT_TYPE_NRCHARS;
1625 return ++fmt - start;
1626
1627 case '%':
1628 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1629 return ++fmt - start;
1630
1631
1632 case 'o':
1633 spec->base = 8;
1634 break;
1635
1636 case 'x':
1637 spec->flags |= SMALL;
1638
1639 case 'X':
1640 spec->base = 16;
1641 break;
1642
1643 case 'd':
1644 case 'i':
1645 spec->flags |= SIGN;
1646 case 'u':
1647 break;
1648
1649 default:
1650 spec->type = FORMAT_TYPE_INVALID;
1651 return fmt - start;
1652 }
1653
1654 if (spec->qualifier == 'L')
1655 spec->type = FORMAT_TYPE_LONG_LONG;
1656 else if (spec->qualifier == 'l') {
1657 if (spec->flags & SIGN)
1658 spec->type = FORMAT_TYPE_LONG;
1659 else
1660 spec->type = FORMAT_TYPE_ULONG;
1661 } else if (_tolower(spec->qualifier) == 'z') {
1662 spec->type = FORMAT_TYPE_SIZE_T;
1663 } else if (spec->qualifier == 't') {
1664 spec->type = FORMAT_TYPE_PTRDIFF;
1665 } else if (spec->qualifier == 'H') {
1666 if (spec->flags & SIGN)
1667 spec->type = FORMAT_TYPE_BYTE;
1668 else
1669 spec->type = FORMAT_TYPE_UBYTE;
1670 } else if (spec->qualifier == 'h') {
1671 if (spec->flags & SIGN)
1672 spec->type = FORMAT_TYPE_SHORT;
1673 else
1674 spec->type = FORMAT_TYPE_USHORT;
1675 } else {
1676 if (spec->flags & SIGN)
1677 spec->type = FORMAT_TYPE_INT;
1678 else
1679 spec->type = FORMAT_TYPE_UINT;
1680 }
1681
1682 return ++fmt - start;
1683}
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1734{
1735 unsigned long long num;
1736 char *str, *end;
1737 struct printf_spec spec = {0};
1738
1739
1740
1741 if (WARN_ON_ONCE((int) size < 0))
1742 return 0;
1743
1744 str = buf;
1745 end = buf + size;
1746
1747
1748 if (end < buf) {
1749 end = ((void *)-1);
1750 size = end - buf;
1751 }
1752
1753 while (*fmt) {
1754 const char *old_fmt = fmt;
1755 int read = format_decode(fmt, &spec);
1756
1757 fmt += read;
1758
1759 switch (spec.type) {
1760 case FORMAT_TYPE_NONE: {
1761 int copy = read;
1762 if (str < end) {
1763 if (copy > end - str)
1764 copy = end - str;
1765 memcpy(str, old_fmt, copy);
1766 }
1767 str += read;
1768 break;
1769 }
1770
1771 case FORMAT_TYPE_WIDTH:
1772 spec.field_width = va_arg(args, int);
1773 break;
1774
1775 case FORMAT_TYPE_PRECISION:
1776 spec.precision = va_arg(args, int);
1777 break;
1778
1779 case FORMAT_TYPE_CHAR: {
1780 char c;
1781
1782 if (!(spec.flags & LEFT)) {
1783 while (--spec.field_width > 0) {
1784 if (str < end)
1785 *str = ' ';
1786 ++str;
1787
1788 }
1789 }
1790 c = (unsigned char) va_arg(args, int);
1791 if (str < end)
1792 *str = c;
1793 ++str;
1794 while (--spec.field_width > 0) {
1795 if (str < end)
1796 *str = ' ';
1797 ++str;
1798 }
1799 break;
1800 }
1801
1802 case FORMAT_TYPE_STR:
1803 str = string(str, end, va_arg(args, char *), spec);
1804 break;
1805
1806 case FORMAT_TYPE_PTR:
1807 str = pointer(fmt+1, str, end, va_arg(args, void *),
1808 spec);
1809 while (isalnum(*fmt))
1810 fmt++;
1811 break;
1812
1813 case FORMAT_TYPE_PERCENT_CHAR:
1814 if (str < end)
1815 *str = '%';
1816 ++str;
1817 break;
1818
1819 case FORMAT_TYPE_INVALID:
1820 if (str < end)
1821 *str = '%';
1822 ++str;
1823 break;
1824
1825 case FORMAT_TYPE_NRCHARS: {
1826 u8 qualifier = spec.qualifier;
1827
1828 if (qualifier == 'l') {
1829 long *ip = va_arg(args, long *);
1830 *ip = (str - buf);
1831 } else if (_tolower(qualifier) == 'z') {
1832 size_t *ip = va_arg(args, size_t *);
1833 *ip = (str - buf);
1834 } else {
1835 int *ip = va_arg(args, int *);
1836 *ip = (str - buf);
1837 }
1838 break;
1839 }
1840
1841 default:
1842 switch (spec.type) {
1843 case FORMAT_TYPE_LONG_LONG:
1844 num = va_arg(args, long long);
1845 break;
1846 case FORMAT_TYPE_ULONG:
1847 num = va_arg(args, unsigned long);
1848 break;
1849 case FORMAT_TYPE_LONG:
1850 num = va_arg(args, long);
1851 break;
1852 case FORMAT_TYPE_SIZE_T:
1853 if (spec.flags & SIGN)
1854 num = va_arg(args, ssize_t);
1855 else
1856 num = va_arg(args, size_t);
1857 break;
1858 case FORMAT_TYPE_PTRDIFF:
1859 num = va_arg(args, ptrdiff_t);
1860 break;
1861 case FORMAT_TYPE_UBYTE:
1862 num = (unsigned char) va_arg(args, int);
1863 break;
1864 case FORMAT_TYPE_BYTE:
1865 num = (signed char) va_arg(args, int);
1866 break;
1867 case FORMAT_TYPE_USHORT:
1868 num = (unsigned short) va_arg(args, int);
1869 break;
1870 case FORMAT_TYPE_SHORT:
1871 num = (short) va_arg(args, int);
1872 break;
1873 case FORMAT_TYPE_INT:
1874 num = (int) va_arg(args, int);
1875 break;
1876 default:
1877 num = va_arg(args, unsigned int);
1878 }
1879
1880 str = number(str, end, num, spec);
1881 }
1882 }
1883
1884 if (size > 0) {
1885 if (str < end)
1886 *str = '\0';
1887 else
1888 end[-1] = '\0';
1889 }
1890
1891
1892 return str-buf;
1893
1894}
1895EXPORT_SYMBOL(vsnprintf);
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1913{
1914 int i;
1915
1916 i = vsnprintf(buf, size, fmt, args);
1917
1918 if (likely(i < size))
1919 return i;
1920 if (size != 0)
1921 return size - 1;
1922 return 0;
1923}
1924EXPORT_SYMBOL(vscnprintf);
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940int snprintf(char *buf, size_t size, const char *fmt, ...)
1941{
1942 va_list args;
1943 int i;
1944
1945 va_start(args, fmt);
1946 i = vsnprintf(buf, size, fmt, args);
1947 va_end(args);
1948
1949 return i;
1950}
1951EXPORT_SYMBOL(snprintf);
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964int scnprintf(char *buf, size_t size, const char *fmt, ...)
1965{
1966 va_list args;
1967 int i;
1968
1969 va_start(args, fmt);
1970 i = vscnprintf(buf, size, fmt, args);
1971 va_end(args);
1972
1973 return i;
1974}
1975EXPORT_SYMBOL(scnprintf);
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991int vsprintf(char *buf, const char *fmt, va_list args)
1992{
1993 return vsnprintf(buf, INT_MAX, fmt, args);
1994}
1995EXPORT_SYMBOL(vsprintf);
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009int sprintf(char *buf, const char *fmt, ...)
2010{
2011 va_list args;
2012 int i;
2013
2014 va_start(args, fmt);
2015 i = vsnprintf(buf, INT_MAX, fmt, args);
2016 va_end(args);
2017
2018 return i;
2019}
2020EXPORT_SYMBOL(sprintf);
2021
2022#ifdef CONFIG_BINARY_PRINTF
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2047{
2048 struct printf_spec spec = {0};
2049 char *str, *end;
2050
2051 str = (char *)bin_buf;
2052 end = (char *)(bin_buf + size);
2053
2054#define save_arg(type) \
2055do { \
2056 if (sizeof(type) == 8) { \
2057 unsigned long long value; \
2058 str = PTR_ALIGN(str, sizeof(u32)); \
2059 value = va_arg(args, unsigned long long); \
2060 if (str + sizeof(type) <= end) { \
2061 *(u32 *)str = *(u32 *)&value; \
2062 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2063 } \
2064 } else { \
2065 unsigned long value; \
2066 str = PTR_ALIGN(str, sizeof(type)); \
2067 value = va_arg(args, int); \
2068 if (str + sizeof(type) <= end) \
2069 *(typeof(type) *)str = (type)value; \
2070 } \
2071 str += sizeof(type); \
2072} while (0)
2073
2074 while (*fmt) {
2075 int read = format_decode(fmt, &spec);
2076
2077 fmt += read;
2078
2079 switch (spec.type) {
2080 case FORMAT_TYPE_NONE:
2081 case FORMAT_TYPE_INVALID:
2082 case FORMAT_TYPE_PERCENT_CHAR:
2083 break;
2084
2085 case FORMAT_TYPE_WIDTH:
2086 case FORMAT_TYPE_PRECISION:
2087 save_arg(int);
2088 break;
2089
2090 case FORMAT_TYPE_CHAR:
2091 save_arg(char);
2092 break;
2093
2094 case FORMAT_TYPE_STR: {
2095 const char *save_str = va_arg(args, char *);
2096 size_t len;
2097
2098 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2099 || (unsigned long)save_str < PAGE_SIZE)
2100 save_str = "(null)";
2101 len = strlen(save_str) + 1;
2102 if (str + len < end)
2103 memcpy(str, save_str, len);
2104 str += len;
2105 break;
2106 }
2107
2108 case FORMAT_TYPE_PTR:
2109 save_arg(void *);
2110
2111 while (isalnum(*fmt))
2112 fmt++;
2113 break;
2114
2115 case FORMAT_TYPE_NRCHARS: {
2116
2117 u8 qualifier = spec.qualifier;
2118 void *skip_arg;
2119 if (qualifier == 'l')
2120 skip_arg = va_arg(args, long *);
2121 else if (_tolower(qualifier) == 'z')
2122 skip_arg = va_arg(args, size_t *);
2123 else
2124 skip_arg = va_arg(args, int *);
2125 break;
2126 }
2127
2128 default:
2129 switch (spec.type) {
2130
2131 case FORMAT_TYPE_LONG_LONG:
2132 save_arg(long long);
2133 break;
2134 case FORMAT_TYPE_ULONG:
2135 case FORMAT_TYPE_LONG:
2136 save_arg(unsigned long);
2137 break;
2138 case FORMAT_TYPE_SIZE_T:
2139 save_arg(size_t);
2140 break;
2141 case FORMAT_TYPE_PTRDIFF:
2142 save_arg(ptrdiff_t);
2143 break;
2144 case FORMAT_TYPE_UBYTE:
2145 case FORMAT_TYPE_BYTE:
2146 save_arg(char);
2147 break;
2148 case FORMAT_TYPE_USHORT:
2149 case FORMAT_TYPE_SHORT:
2150 save_arg(short);
2151 break;
2152 default:
2153 save_arg(int);
2154 }
2155 }
2156 }
2157
2158 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2159#undef save_arg
2160}
2161EXPORT_SYMBOL_GPL(vbin_printf);
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2186{
2187 struct printf_spec spec = {0};
2188 char *str, *end;
2189 const char *args = (const char *)bin_buf;
2190
2191 if (WARN_ON_ONCE((int) size < 0))
2192 return 0;
2193
2194 str = buf;
2195 end = buf + size;
2196
2197#define get_arg(type) \
2198({ \
2199 typeof(type) value; \
2200 if (sizeof(type) == 8) { \
2201 args = PTR_ALIGN(args, sizeof(u32)); \
2202 *(u32 *)&value = *(u32 *)args; \
2203 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2204 } else { \
2205 args = PTR_ALIGN(args, sizeof(type)); \
2206 value = *(typeof(type) *)args; \
2207 } \
2208 args += sizeof(type); \
2209 value; \
2210})
2211
2212
2213 if (end < buf) {
2214 end = ((void *)-1);
2215 size = end - buf;
2216 }
2217
2218 while (*fmt) {
2219 const char *old_fmt = fmt;
2220 int read = format_decode(fmt, &spec);
2221
2222 fmt += read;
2223
2224 switch (spec.type) {
2225 case FORMAT_TYPE_NONE: {
2226 int copy = read;
2227 if (str < end) {
2228 if (copy > end - str)
2229 copy = end - str;
2230 memcpy(str, old_fmt, copy);
2231 }
2232 str += read;
2233 break;
2234 }
2235
2236 case FORMAT_TYPE_WIDTH:
2237 spec.field_width = get_arg(int);
2238 break;
2239
2240 case FORMAT_TYPE_PRECISION:
2241 spec.precision = get_arg(int);
2242 break;
2243
2244 case FORMAT_TYPE_CHAR: {
2245 char c;
2246
2247 if (!(spec.flags & LEFT)) {
2248 while (--spec.field_width > 0) {
2249 if (str < end)
2250 *str = ' ';
2251 ++str;
2252 }
2253 }
2254 c = (unsigned char) get_arg(char);
2255 if (str < end)
2256 *str = c;
2257 ++str;
2258 while (--spec.field_width > 0) {
2259 if (str < end)
2260 *str = ' ';
2261 ++str;
2262 }
2263 break;
2264 }
2265
2266 case FORMAT_TYPE_STR: {
2267 const char *str_arg = args;
2268 args += strlen(str_arg) + 1;
2269 str = string(str, end, (char *)str_arg, spec);
2270 break;
2271 }
2272
2273 case FORMAT_TYPE_PTR:
2274 str = pointer(fmt+1, str, end, get_arg(void *), spec);
2275 while (isalnum(*fmt))
2276 fmt++;
2277 break;
2278
2279 case FORMAT_TYPE_PERCENT_CHAR:
2280 case FORMAT_TYPE_INVALID:
2281 if (str < end)
2282 *str = '%';
2283 ++str;
2284 break;
2285
2286 case FORMAT_TYPE_NRCHARS:
2287
2288 break;
2289
2290 default: {
2291 unsigned long long num;
2292
2293 switch (spec.type) {
2294
2295 case FORMAT_TYPE_LONG_LONG:
2296 num = get_arg(long long);
2297 break;
2298 case FORMAT_TYPE_ULONG:
2299 case FORMAT_TYPE_LONG:
2300 num = get_arg(unsigned long);
2301 break;
2302 case FORMAT_TYPE_SIZE_T:
2303 num = get_arg(size_t);
2304 break;
2305 case FORMAT_TYPE_PTRDIFF:
2306 num = get_arg(ptrdiff_t);
2307 break;
2308 case FORMAT_TYPE_UBYTE:
2309 num = get_arg(unsigned char);
2310 break;
2311 case FORMAT_TYPE_BYTE:
2312 num = get_arg(signed char);
2313 break;
2314 case FORMAT_TYPE_USHORT:
2315 num = get_arg(unsigned short);
2316 break;
2317 case FORMAT_TYPE_SHORT:
2318 num = get_arg(short);
2319 break;
2320 case FORMAT_TYPE_UINT:
2321 num = get_arg(unsigned int);
2322 break;
2323 default:
2324 num = get_arg(int);
2325 }
2326
2327 str = number(str, end, num, spec);
2328 }
2329 }
2330 }
2331
2332 if (size > 0) {
2333 if (str < end)
2334 *str = '\0';
2335 else
2336 end[-1] = '\0';
2337 }
2338
2339#undef get_arg
2340
2341
2342 return str - buf;
2343}
2344EXPORT_SYMBOL_GPL(bstr_printf);
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2357{
2358 va_list args;
2359 int ret;
2360
2361 va_start(args, fmt);
2362 ret = vbin_printf(bin_buf, size, fmt, args);
2363 va_end(args);
2364
2365 return ret;
2366}
2367EXPORT_SYMBOL_GPL(bprintf);
2368
2369#endif
2370
2371
2372
2373
2374
2375
2376
2377int vsscanf(const char *buf, const char *fmt, va_list args)
2378{
2379 const char *str = buf;
2380 char *next;
2381 char digit;
2382 int num = 0;
2383 u8 qualifier;
2384 unsigned int base;
2385 union {
2386 long long s;
2387 unsigned long long u;
2388 } val;
2389 s16 field_width;
2390 bool is_sign;
2391
2392 while (*fmt) {
2393
2394
2395
2396
2397 if (isspace(*fmt)) {
2398 fmt = skip_spaces(++fmt);
2399 str = skip_spaces(str);
2400 }
2401
2402
2403 if (*fmt != '%' && *fmt) {
2404 if (*fmt++ != *str++)
2405 break;
2406 continue;
2407 }
2408
2409 if (!*fmt)
2410 break;
2411 ++fmt;
2412
2413
2414
2415
2416 if (*fmt == '*') {
2417 if (!*str)
2418 break;
2419 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2420
2421 if (*fmt == '[')
2422 return num;
2423 fmt++;
2424 }
2425 while (!isspace(*str) && *str)
2426 str++;
2427 continue;
2428 }
2429
2430
2431 field_width = -1;
2432 if (isdigit(*fmt)) {
2433 field_width = skip_atoi(&fmt);
2434 if (field_width <= 0)
2435 break;
2436 }
2437
2438
2439 qualifier = -1;
2440 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2441 _tolower(*fmt) == 'z') {
2442 qualifier = *fmt++;
2443 if (unlikely(qualifier == *fmt)) {
2444 if (qualifier == 'h') {
2445 qualifier = 'H';
2446 fmt++;
2447 } else if (qualifier == 'l') {
2448 qualifier = 'L';
2449 fmt++;
2450 }
2451 }
2452 }
2453
2454 if (!*fmt)
2455 break;
2456
2457 if (*fmt == 'n') {
2458
2459 *va_arg(args, int *) = str - buf;
2460 ++fmt;
2461 continue;
2462 }
2463
2464 if (!*str)
2465 break;
2466
2467 base = 10;
2468 is_sign = 0;
2469
2470 switch (*fmt++) {
2471 case 'c':
2472 {
2473 char *s = (char *)va_arg(args, char*);
2474 if (field_width == -1)
2475 field_width = 1;
2476 do {
2477 *s++ = *str++;
2478 } while (--field_width > 0 && *str);
2479 num++;
2480 }
2481 continue;
2482 case 's':
2483 {
2484 char *s = (char *)va_arg(args, char *);
2485 if (field_width == -1)
2486 field_width = SHRT_MAX;
2487
2488 str = skip_spaces(str);
2489
2490
2491 while (*str && !isspace(*str) && field_width--)
2492 *s++ = *str++;
2493 *s = '\0';
2494 num++;
2495 }
2496 continue;
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512 case '[':
2513 {
2514 char *s = (char *)va_arg(args, char *);
2515 DECLARE_BITMAP(set, 256) = {0};
2516 unsigned int len = 0;
2517 bool negate = (*fmt == '^');
2518
2519
2520 if (field_width == -1)
2521 return num;
2522
2523 if (negate)
2524 ++fmt;
2525
2526 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
2527 set_bit((u8)*fmt, set);
2528
2529
2530 if (!*fmt || !len)
2531 return num;
2532 ++fmt;
2533
2534 if (negate) {
2535 bitmap_complement(set, set, 256);
2536
2537 clear_bit(0, set);
2538 }
2539
2540
2541 if (!test_bit((u8)*str, set))
2542 return num;
2543
2544 while (test_bit((u8)*str, set) && field_width--)
2545 *s++ = *str++;
2546 *s = '\0';
2547 ++num;
2548 }
2549 continue;
2550 case 'o':
2551 base = 8;
2552 break;
2553 case 'x':
2554 case 'X':
2555 base = 16;
2556 break;
2557 case 'i':
2558 base = 0;
2559 case 'd':
2560 is_sign = 1;
2561 case 'u':
2562 break;
2563 case '%':
2564
2565 if (*str++ != '%')
2566 return num;
2567 continue;
2568 default:
2569
2570 return num;
2571 }
2572
2573
2574
2575
2576 str = skip_spaces(str);
2577
2578 digit = *str;
2579 if (is_sign && digit == '-')
2580 digit = *(str + 1);
2581
2582 if (!digit
2583 || (base == 16 && !isxdigit(digit))
2584 || (base == 10 && !isdigit(digit))
2585 || (base == 8 && (!isdigit(digit) || digit > '7'))
2586 || (base == 0 && !isdigit(digit)))
2587 break;
2588
2589 if (is_sign)
2590 val.s = qualifier != 'L' ?
2591 simple_strtol(str, &next, base) :
2592 simple_strtoll(str, &next, base);
2593 else
2594 val.u = qualifier != 'L' ?
2595 simple_strtoul(str, &next, base) :
2596 simple_strtoull(str, &next, base);
2597
2598 if (field_width > 0 && next - str > field_width) {
2599 if (base == 0)
2600 _parse_integer_fixup_radix(str, &base);
2601 while (next - str > field_width) {
2602 if (is_sign)
2603 val.s = div_s64(val.s, base);
2604 else
2605 val.u = div_u64(val.u, base);
2606 --next;
2607 }
2608 }
2609
2610 switch (qualifier) {
2611 case 'H':
2612 if (is_sign)
2613 *va_arg(args, signed char *) = val.s;
2614 else
2615 *va_arg(args, unsigned char *) = val.u;
2616 break;
2617 case 'h':
2618 if (is_sign)
2619 *va_arg(args, short *) = val.s;
2620 else
2621 *va_arg(args, unsigned short *) = val.u;
2622 break;
2623 case 'l':
2624 if (is_sign)
2625 *va_arg(args, long *) = val.s;
2626 else
2627 *va_arg(args, unsigned long *) = val.u;
2628 break;
2629 case 'L':
2630 if (is_sign)
2631 *va_arg(args, long long *) = val.s;
2632 else
2633 *va_arg(args, unsigned long long *) = val.u;
2634 break;
2635 case 'Z':
2636 case 'z':
2637 *va_arg(args, size_t *) = val.u;
2638 break;
2639 default:
2640 if (is_sign)
2641 *va_arg(args, int *) = val.s;
2642 else
2643 *va_arg(args, unsigned int *) = val.u;
2644 break;
2645 }
2646 num++;
2647
2648 if (!next)
2649 break;
2650 str = next;
2651 }
2652
2653 return num;
2654}
2655EXPORT_SYMBOL(vsscanf);
2656
2657
2658
2659
2660
2661
2662
2663int sscanf(const char *buf, const char *fmt, ...)
2664{
2665 va_list args;
2666 int i;
2667
2668 va_start(args, fmt);
2669 i = vsscanf(buf, fmt, args);
2670 va_end(args);
2671
2672 return i;
2673}
2674EXPORT_SYMBOL(sscanf);
2675