1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <stdarg.h>
21#include <linux/build_bug.h>
22#include <linux/clk.h>
23#include <linux/clk-provider.h>
24#include <linux/errname.h>
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/ctype.h>
29#include <linux/kernel.h>
30#include <linux/kallsyms.h>
31#include <linux/math64.h>
32#include <linux/uaccess.h>
33#include <linux/ioport.h>
34#include <linux/dcache.h>
35#include <linux/cred.h>
36#include <linux/rtc.h>
37#include <linux/uuid.h>
38#include <linux/of.h>
39#include <net/addrconf.h>
40#include <linux/siphash.h>
41#include <linux/compiler.h>
42#include <linux/property.h>
43#ifdef CONFIG_BLOCK
44#include <linux/blkdev.h>
45#endif
46
47#include "../mm/internal.h"
48
49#include <asm/page.h>
50#include <asm/byteorder.h>
51
52#include <linux/string_helpers.h>
53#include "kstrtox.h"
54
55
56
57
58
59
60
61
62
63unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
64{
65 unsigned long long result;
66 unsigned int rv;
67
68 cp = _parse_integer_fixup_radix(cp, &base);
69 rv = _parse_integer(cp, base, &result);
70
71 cp += (rv & ~KSTRTOX_OVERFLOW);
72
73 if (endp)
74 *endp = (char *)cp;
75
76 return result;
77}
78EXPORT_SYMBOL(simple_strtoull);
79
80
81
82
83
84
85
86
87
88unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
89{
90 return simple_strtoull(cp, endp, base);
91}
92EXPORT_SYMBOL(simple_strtoul);
93
94
95
96
97
98
99
100
101
102long simple_strtol(const char *cp, char **endp, unsigned int base)
103{
104 if (*cp == '-')
105 return -simple_strtoul(cp + 1, endp, base);
106
107 return simple_strtoul(cp, endp, base);
108}
109EXPORT_SYMBOL(simple_strtol);
110
111
112
113
114
115
116
117
118
119long long simple_strtoll(const char *cp, char **endp, unsigned int base)
120{
121 if (*cp == '-')
122 return -simple_strtoull(cp + 1, endp, base);
123
124 return simple_strtoull(cp, endp, base);
125}
126EXPORT_SYMBOL(simple_strtoll);
127
128static noinline_for_stack
129int skip_atoi(const char **s)
130{
131 int i = 0;
132
133 do {
134 i = i*10 + *((*s)++) - '0';
135 } while (isdigit(**s));
136
137 return i;
138}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165static const u16 decpair[100] = {
166#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
167 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
168 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
169 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
170 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
171 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
172 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
173 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
174 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
175 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
176 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
177#undef _
178};
179
180
181
182
183
184
185
186static noinline_for_stack
187char *put_dec_trunc8(char *buf, unsigned r)
188{
189 unsigned q;
190
191
192 if (r < 100)
193 goto out_r;
194
195
196 q = (r * (u64)0x28f5c29) >> 32;
197 *((u16 *)buf) = decpair[r - 100*q];
198 buf += 2;
199
200
201 if (q < 100)
202 goto out_q;
203
204
205 r = (q * (u64)0x28f5c29) >> 32;
206 *((u16 *)buf) = decpair[q - 100*r];
207 buf += 2;
208
209
210 if (r < 100)
211 goto out_r;
212
213
214 q = (r * 0x147b) >> 19;
215 *((u16 *)buf) = decpair[r - 100*q];
216 buf += 2;
217out_q:
218
219 r = q;
220out_r:
221
222 *((u16 *)buf) = decpair[r];
223 buf += r < 10 ? 1 : 2;
224 return buf;
225}
226
227#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
228static noinline_for_stack
229char *put_dec_full8(char *buf, unsigned r)
230{
231 unsigned q;
232
233
234 q = (r * (u64)0x28f5c29) >> 32;
235 *((u16 *)buf) = decpair[r - 100*q];
236 buf += 2;
237
238
239 r = (q * (u64)0x28f5c29) >> 32;
240 *((u16 *)buf) = decpair[q - 100*r];
241 buf += 2;
242
243
244 q = (r * 0x147b) >> 19;
245 *((u16 *)buf) = decpair[r - 100*q];
246 buf += 2;
247
248
249 *((u16 *)buf) = decpair[q];
250 buf += 2;
251 return buf;
252}
253
254static noinline_for_stack
255char *put_dec(char *buf, unsigned long long n)
256{
257 if (n >= 100*1000*1000)
258 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
259
260 if (n >= 100*1000*1000)
261 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
262
263 return put_dec_trunc8(buf, n);
264}
265
266#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
267
268static void
269put_dec_full4(char *buf, unsigned r)
270{
271 unsigned q;
272
273
274 q = (r * 0x147b) >> 19;
275 *((u16 *)buf) = decpair[r - 100*q];
276 buf += 2;
277
278 *((u16 *)buf) = decpair[q];
279}
280
281
282
283
284
285
286
287
288static noinline_for_stack
289unsigned put_dec_helper4(char *buf, unsigned x)
290{
291 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
292
293 put_dec_full4(buf, x - q * 10000);
294 return q;
295}
296
297
298
299
300
301
302static
303char *put_dec(char *buf, unsigned long long n)
304{
305 uint32_t d3, d2, d1, q, h;
306
307 if (n < 100*1000*1000)
308 return put_dec_trunc8(buf, n);
309
310 d1 = ((uint32_t)n >> 16);
311 h = (n >> 32);
312 d2 = (h ) & 0xffff;
313 d3 = (h >> 16);
314
315
316
317 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
318 q = put_dec_helper4(buf, q);
319
320 q += 7671 * d3 + 9496 * d2 + 6 * d1;
321 q = put_dec_helper4(buf+4, q);
322
323 q += 4749 * d3 + 42 * d2;
324 q = put_dec_helper4(buf+8, q);
325
326 q += 281 * d3;
327 buf += 12;
328 if (q)
329 buf = put_dec_trunc8(buf, q);
330 else while (buf[-1] == '0')
331 --buf;
332
333 return buf;
334}
335
336#endif
337
338
339
340
341
342
343
344int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
345{
346
347 char tmp[sizeof(num) * 3] __aligned(2);
348 int idx, len;
349
350
351 if (num <= 9) {
352 tmp[0] = '0' + num;
353 len = 1;
354 } else {
355 len = put_dec(tmp, num) - tmp;
356 }
357
358 if (len > size || width > size)
359 return 0;
360
361 if (width > len) {
362 width = width - len;
363 for (idx = 0; idx < width; idx++)
364 buf[idx] = ' ';
365 } else {
366 width = 0;
367 }
368
369 for (idx = 0; idx < len; ++idx)
370 buf[idx + width] = tmp[len - idx - 1];
371
372 return len + width;
373}
374
375#define SIGN 1
376#define LEFT 2
377#define PLUS 4
378#define SPACE 8
379#define ZEROPAD 16
380#define SMALL 32
381#define SPECIAL 64
382
383enum format_type {
384 FORMAT_TYPE_NONE,
385 FORMAT_TYPE_WIDTH,
386 FORMAT_TYPE_PRECISION,
387 FORMAT_TYPE_CHAR,
388 FORMAT_TYPE_STR,
389 FORMAT_TYPE_PTR,
390 FORMAT_TYPE_PERCENT_CHAR,
391 FORMAT_TYPE_INVALID,
392 FORMAT_TYPE_LONG_LONG,
393 FORMAT_TYPE_ULONG,
394 FORMAT_TYPE_LONG,
395 FORMAT_TYPE_UBYTE,
396 FORMAT_TYPE_BYTE,
397 FORMAT_TYPE_USHORT,
398 FORMAT_TYPE_SHORT,
399 FORMAT_TYPE_UINT,
400 FORMAT_TYPE_INT,
401 FORMAT_TYPE_SIZE_T,
402 FORMAT_TYPE_PTRDIFF
403};
404
405struct printf_spec {
406 unsigned int type:8;
407 signed int field_width:24;
408 unsigned int flags:8;
409 unsigned int base:8;
410 signed int precision:16;
411} __packed;
412static_assert(sizeof(struct printf_spec) == 8);
413
414#define FIELD_WIDTH_MAX ((1 << 23) - 1)
415#define PRECISION_MAX ((1 << 15) - 1)
416
417static noinline_for_stack
418char *number(char *buf, char *end, unsigned long long num,
419 struct printf_spec spec)
420{
421
422 char tmp[3 * sizeof(num)] __aligned(2);
423 char sign;
424 char locase;
425 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
426 int i;
427 bool is_zero = num == 0LL;
428 int field_width = spec.field_width;
429 int precision = spec.precision;
430
431
432
433 locase = (spec.flags & SMALL);
434 if (spec.flags & LEFT)
435 spec.flags &= ~ZEROPAD;
436 sign = 0;
437 if (spec.flags & SIGN) {
438 if ((signed long long)num < 0) {
439 sign = '-';
440 num = -(signed long long)num;
441 field_width--;
442 } else if (spec.flags & PLUS) {
443 sign = '+';
444 field_width--;
445 } else if (spec.flags & SPACE) {
446 sign = ' ';
447 field_width--;
448 }
449 }
450 if (need_pfx) {
451 if (spec.base == 16)
452 field_width -= 2;
453 else if (!is_zero)
454 field_width--;
455 }
456
457
458 i = 0;
459 if (num < spec.base)
460 tmp[i++] = hex_asc_upper[num] | locase;
461 else if (spec.base != 10) {
462 int mask = spec.base - 1;
463 int shift = 3;
464
465 if (spec.base == 16)
466 shift = 4;
467 do {
468 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
469 num >>= shift;
470 } while (num);
471 } else {
472 i = put_dec(tmp, num) - tmp;
473 }
474
475
476 if (i > precision)
477 precision = i;
478
479 field_width -= precision;
480 if (!(spec.flags & (ZEROPAD | LEFT))) {
481 while (--field_width >= 0) {
482 if (buf < end)
483 *buf = ' ';
484 ++buf;
485 }
486 }
487
488 if (sign) {
489 if (buf < end)
490 *buf = sign;
491 ++buf;
492 }
493
494 if (need_pfx) {
495 if (spec.base == 16 || !is_zero) {
496 if (buf < end)
497 *buf = '0';
498 ++buf;
499 }
500 if (spec.base == 16) {
501 if (buf < end)
502 *buf = ('X' | locase);
503 ++buf;
504 }
505 }
506
507 if (!(spec.flags & LEFT)) {
508 char c = ' ' + (spec.flags & ZEROPAD);
509 BUILD_BUG_ON(' ' + ZEROPAD != '0');
510 while (--field_width >= 0) {
511 if (buf < end)
512 *buf = c;
513 ++buf;
514 }
515 }
516
517 while (i <= --precision) {
518 if (buf < end)
519 *buf = '0';
520 ++buf;
521 }
522
523 while (--i >= 0) {
524 if (buf < end)
525 *buf = tmp[i];
526 ++buf;
527 }
528
529 while (--field_width >= 0) {
530 if (buf < end)
531 *buf = ' ';
532 ++buf;
533 }
534
535 return buf;
536}
537
538static noinline_for_stack
539char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
540{
541 struct printf_spec spec;
542
543 spec.type = FORMAT_TYPE_PTR;
544 spec.field_width = 2 + 2 * size;
545 spec.flags = SPECIAL | SMALL | ZEROPAD;
546 spec.base = 16;
547 spec.precision = -1;
548
549 return number(buf, end, num, spec);
550}
551
552static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
553{
554 size_t size;
555 if (buf >= end)
556 return;
557 size = end - buf;
558 if (size <= spaces) {
559 memset(buf, ' ', size);
560 return;
561 }
562 if (len) {
563 if (len > size - spaces)
564 len = size - spaces;
565 memmove(buf + spaces, buf, len);
566 }
567 memset(buf, ' ', spaces);
568}
569
570
571
572
573
574
575
576
577
578static noinline_for_stack
579char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
580{
581 unsigned spaces;
582
583 if (likely(n >= spec.field_width))
584 return buf;
585
586 spaces = spec.field_width - n;
587 if (!(spec.flags & LEFT)) {
588 move_right(buf - n, end, n, spaces);
589 return buf + spaces;
590 }
591 while (spaces--) {
592 if (buf < end)
593 *buf = ' ';
594 ++buf;
595 }
596 return buf;
597}
598
599
600static char *string_nocheck(char *buf, char *end, const char *s,
601 struct printf_spec spec)
602{
603 int len = 0;
604 int lim = spec.precision;
605
606 while (lim--) {
607 char c = *s++;
608 if (!c)
609 break;
610 if (buf < end)
611 *buf = c;
612 ++buf;
613 ++len;
614 }
615 return widen_string(buf, len, end, spec);
616}
617
618static char *err_ptr(char *buf, char *end, void *ptr,
619 struct printf_spec spec)
620{
621 int err = PTR_ERR(ptr);
622 const char *sym = errname(err);
623
624 if (sym)
625 return string_nocheck(buf, end, sym, spec);
626
627
628
629
630
631
632 spec.flags |= SIGN;
633 spec.base = 10;
634 return number(buf, end, err, spec);
635}
636
637
638static char *error_string(char *buf, char *end, const char *s,
639 struct printf_spec spec)
640{
641
642
643
644
645
646 if (spec.precision == -1)
647 spec.precision = 2 * sizeof(void *);
648
649 return string_nocheck(buf, end, s, spec);
650}
651
652
653
654
655
656
657static const char *check_pointer_msg(const void *ptr)
658{
659 if (!ptr)
660 return "(null)";
661
662 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
663 return "(efault)";
664
665 return NULL;
666}
667
668static int check_pointer(char **buf, char *end, const void *ptr,
669 struct printf_spec spec)
670{
671 const char *err_msg;
672
673 err_msg = check_pointer_msg(ptr);
674 if (err_msg) {
675 *buf = error_string(*buf, end, err_msg, spec);
676 return -EFAULT;
677 }
678
679 return 0;
680}
681
682static noinline_for_stack
683char *string(char *buf, char *end, const char *s,
684 struct printf_spec spec)
685{
686 if (check_pointer(&buf, end, s, spec))
687 return buf;
688
689 return string_nocheck(buf, end, s, spec);
690}
691
692static char *pointer_string(char *buf, char *end,
693 const void *ptr,
694 struct printf_spec spec)
695{
696 spec.base = 16;
697 spec.flags |= SMALL;
698 if (spec.field_width == -1) {
699 spec.field_width = 2 * sizeof(ptr);
700 spec.flags |= ZEROPAD;
701 }
702
703 return number(buf, end, (unsigned long int)ptr, spec);
704}
705
706
707static int debug_boot_weak_hash __ro_after_init;
708
709static int __init debug_boot_weak_hash_enable(char *str)
710{
711 debug_boot_weak_hash = 1;
712 pr_info("debug_boot_weak_hash enabled\n");
713 return 0;
714}
715early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
716
717static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
718static siphash_key_t ptr_key __read_mostly;
719
720static void enable_ptr_key_workfn(struct work_struct *work)
721{
722 get_random_bytes(&ptr_key, sizeof(ptr_key));
723
724 static_branch_disable(¬_filled_random_ptr_key);
725}
726
727static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
728
729static void fill_random_ptr_key(struct random_ready_callback *unused)
730{
731
732 queue_work(system_unbound_wq, &enable_ptr_key_work);
733}
734
735static struct random_ready_callback random_ready = {
736 .func = fill_random_ptr_key
737};
738
739static int __init initialize_ptr_random(void)
740{
741 int key_size = sizeof(ptr_key);
742 int ret;
743
744
745 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
746 static_branch_disable(¬_filled_random_ptr_key);
747 return 0;
748 }
749
750 ret = add_random_ready_callback(&random_ready);
751 if (!ret) {
752 return 0;
753 } else if (ret == -EALREADY) {
754
755 enable_ptr_key_workfn(&enable_ptr_key_work);
756 return 0;
757 }
758
759 return ret;
760}
761early_initcall(initialize_ptr_random);
762
763
764static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
765{
766 unsigned long hashval;
767
768 if (static_branch_unlikely(¬_filled_random_ptr_key))
769 return -EAGAIN;
770
771#ifdef CONFIG_64BIT
772 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
773
774
775
776
777 hashval = hashval & 0xffffffff;
778#else
779 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
780#endif
781 *hashval_out = hashval;
782 return 0;
783}
784
785int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
786{
787 return __ptr_to_hashval(ptr, hashval_out);
788}
789
790static char *ptr_to_id(char *buf, char *end, const void *ptr,
791 struct printf_spec spec)
792{
793 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
794 unsigned long hashval;
795 int ret;
796
797
798
799
800
801 if (IS_ERR_OR_NULL(ptr))
802 return pointer_string(buf, end, ptr, spec);
803
804
805 if (unlikely(debug_boot_weak_hash)) {
806 hashval = hash_long((unsigned long)ptr, 32);
807 return pointer_string(buf, end, (const void *)hashval, spec);
808 }
809
810 ret = __ptr_to_hashval(ptr, &hashval);
811 if (ret) {
812 spec.field_width = 2 * sizeof(ptr);
813
814 return error_string(buf, end, str, spec);
815 }
816
817 return pointer_string(buf, end, (const void *)hashval, spec);
818}
819
820int kptr_restrict __read_mostly;
821
822static noinline_for_stack
823char *restricted_pointer(char *buf, char *end, const void *ptr,
824 struct printf_spec spec)
825{
826 switch (kptr_restrict) {
827 case 0:
828
829 return ptr_to_id(buf, end, ptr, spec);
830 case 1: {
831 const struct cred *cred;
832
833
834
835
836
837 if (in_irq() || in_serving_softirq() || in_nmi()) {
838 if (spec.field_width == -1)
839 spec.field_width = 2 * sizeof(ptr);
840 return error_string(buf, end, "pK-error", spec);
841 }
842
843
844
845
846
847
848
849
850
851
852 cred = current_cred();
853 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
854 !uid_eq(cred->euid, cred->uid) ||
855 !gid_eq(cred->egid, cred->gid))
856 ptr = NULL;
857 break;
858 }
859 case 2:
860 default:
861
862 ptr = NULL;
863 break;
864 }
865
866 return pointer_string(buf, end, ptr, spec);
867}
868
869static noinline_for_stack
870char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
871 const char *fmt)
872{
873 const char *array[4], *s;
874 const struct dentry *p;
875 int depth;
876 int i, n;
877
878 switch (fmt[1]) {
879 case '2': case '3': case '4':
880 depth = fmt[1] - '0';
881 break;
882 default:
883 depth = 1;
884 }
885
886 rcu_read_lock();
887 for (i = 0; i < depth; i++, d = p) {
888 if (check_pointer(&buf, end, d, spec)) {
889 rcu_read_unlock();
890 return buf;
891 }
892
893 p = READ_ONCE(d->d_parent);
894 array[i] = READ_ONCE(d->d_name.name);
895 if (p == d) {
896 if (i)
897 array[i] = "";
898 i++;
899 break;
900 }
901 }
902 s = array[--i];
903 for (n = 0; n != spec.precision; n++, buf++) {
904 char c = *s++;
905 if (!c) {
906 if (!i)
907 break;
908 c = '/';
909 s = array[--i];
910 }
911 if (buf < end)
912 *buf = c;
913 }
914 rcu_read_unlock();
915 return widen_string(buf, n, end, spec);
916}
917
918static noinline_for_stack
919char *file_dentry_name(char *buf, char *end, const struct file *f,
920 struct printf_spec spec, const char *fmt)
921{
922 if (check_pointer(&buf, end, f, spec))
923 return buf;
924
925 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
926}
927#ifdef CONFIG_BLOCK
928static noinline_for_stack
929char *bdev_name(char *buf, char *end, struct block_device *bdev,
930 struct printf_spec spec, const char *fmt)
931{
932 struct gendisk *hd;
933
934 if (check_pointer(&buf, end, bdev, spec))
935 return buf;
936
937 hd = bdev->bd_disk;
938 buf = string(buf, end, hd->disk_name, spec);
939 if (bdev->bd_part->partno) {
940 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
941 if (buf < end)
942 *buf = 'p';
943 buf++;
944 }
945 buf = number(buf, end, bdev->bd_part->partno, spec);
946 }
947 return buf;
948}
949#endif
950
951static noinline_for_stack
952char *symbol_string(char *buf, char *end, void *ptr,
953 struct printf_spec spec, const char *fmt)
954{
955 unsigned long value;
956#ifdef CONFIG_KALLSYMS
957 char sym[KSYM_SYMBOL_LEN];
958#endif
959
960 if (fmt[1] == 'R')
961 ptr = __builtin_extract_return_addr(ptr);
962 value = (unsigned long)ptr;
963
964#ifdef CONFIG_KALLSYMS
965 if (*fmt == 'B')
966 sprint_backtrace(sym, value);
967 else if (*fmt != 's')
968 sprint_symbol(sym, value);
969 else
970 sprint_symbol_no_offset(sym, value);
971
972 return string_nocheck(buf, end, sym, spec);
973#else
974 return special_hex_number(buf, end, value, sizeof(void *));
975#endif
976}
977
978static const struct printf_spec default_str_spec = {
979 .field_width = -1,
980 .precision = -1,
981};
982
983static const struct printf_spec default_flag_spec = {
984 .base = 16,
985 .precision = -1,
986 .flags = SPECIAL | SMALL,
987};
988
989static const struct printf_spec default_dec_spec = {
990 .base = 10,
991 .precision = -1,
992};
993
994static const struct printf_spec default_dec02_spec = {
995 .base = 10,
996 .field_width = 2,
997 .precision = -1,
998 .flags = ZEROPAD,
999};
1000
1001static const struct printf_spec default_dec04_spec = {
1002 .base = 10,
1003 .field_width = 4,
1004 .precision = -1,
1005 .flags = ZEROPAD,
1006};
1007
1008static noinline_for_stack
1009char *resource_string(char *buf, char *end, struct resource *res,
1010 struct printf_spec spec, const char *fmt)
1011{
1012#ifndef IO_RSRC_PRINTK_SIZE
1013#define IO_RSRC_PRINTK_SIZE 6
1014#endif
1015
1016#ifndef MEM_RSRC_PRINTK_SIZE
1017#define MEM_RSRC_PRINTK_SIZE 10
1018#endif
1019 static const struct printf_spec io_spec = {
1020 .base = 16,
1021 .field_width = IO_RSRC_PRINTK_SIZE,
1022 .precision = -1,
1023 .flags = SPECIAL | SMALL | ZEROPAD,
1024 };
1025 static const struct printf_spec mem_spec = {
1026 .base = 16,
1027 .field_width = MEM_RSRC_PRINTK_SIZE,
1028 .precision = -1,
1029 .flags = SPECIAL | SMALL | ZEROPAD,
1030 };
1031 static const struct printf_spec bus_spec = {
1032 .base = 16,
1033 .field_width = 2,
1034 .precision = -1,
1035 .flags = SMALL | ZEROPAD,
1036 };
1037 static const struct printf_spec str_spec = {
1038 .field_width = -1,
1039 .precision = 10,
1040 .flags = LEFT,
1041 };
1042
1043
1044
1045#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1046#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
1047#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
1048#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1049 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1050 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1051
1052 char *p = sym, *pend = sym + sizeof(sym);
1053 int decode = (fmt[0] == 'R') ? 1 : 0;
1054 const struct printf_spec *specp;
1055
1056 if (check_pointer(&buf, end, res, spec))
1057 return buf;
1058
1059 *p++ = '[';
1060 if (res->flags & IORESOURCE_IO) {
1061 p = string_nocheck(p, pend, "io ", str_spec);
1062 specp = &io_spec;
1063 } else if (res->flags & IORESOURCE_MEM) {
1064 p = string_nocheck(p, pend, "mem ", str_spec);
1065 specp = &mem_spec;
1066 } else if (res->flags & IORESOURCE_IRQ) {
1067 p = string_nocheck(p, pend, "irq ", str_spec);
1068 specp = &default_dec_spec;
1069 } else if (res->flags & IORESOURCE_DMA) {
1070 p = string_nocheck(p, pend, "dma ", str_spec);
1071 specp = &default_dec_spec;
1072 } else if (res->flags & IORESOURCE_BUS) {
1073 p = string_nocheck(p, pend, "bus ", str_spec);
1074 specp = &bus_spec;
1075 } else {
1076 p = string_nocheck(p, pend, "??? ", str_spec);
1077 specp = &mem_spec;
1078 decode = 0;
1079 }
1080 if (decode && res->flags & IORESOURCE_UNSET) {
1081 p = string_nocheck(p, pend, "size ", str_spec);
1082 p = number(p, pend, resource_size(res), *specp);
1083 } else {
1084 p = number(p, pend, res->start, *specp);
1085 if (res->start != res->end) {
1086 *p++ = '-';
1087 p = number(p, pend, res->end, *specp);
1088 }
1089 }
1090 if (decode) {
1091 if (res->flags & IORESOURCE_MEM_64)
1092 p = string_nocheck(p, pend, " 64bit", str_spec);
1093 if (res->flags & IORESOURCE_PREFETCH)
1094 p = string_nocheck(p, pend, " pref", str_spec);
1095 if (res->flags & IORESOURCE_WINDOW)
1096 p = string_nocheck(p, pend, " window", str_spec);
1097 if (res->flags & IORESOURCE_DISABLED)
1098 p = string_nocheck(p, pend, " disabled", str_spec);
1099 } else {
1100 p = string_nocheck(p, pend, " flags ", str_spec);
1101 p = number(p, pend, res->flags, default_flag_spec);
1102 }
1103 *p++ = ']';
1104 *p = '\0';
1105
1106 return string_nocheck(buf, end, sym, spec);
1107}
1108
1109static noinline_for_stack
1110char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1111 const char *fmt)
1112{
1113 int i, len = 1;
1114
1115 char separator;
1116
1117 if (spec.field_width == 0)
1118
1119 return buf;
1120
1121 if (check_pointer(&buf, end, addr, spec))
1122 return buf;
1123
1124 switch (fmt[1]) {
1125 case 'C':
1126 separator = ':';
1127 break;
1128 case 'D':
1129 separator = '-';
1130 break;
1131 case 'N':
1132 separator = 0;
1133 break;
1134 default:
1135 separator = ' ';
1136 break;
1137 }
1138
1139 if (spec.field_width > 0)
1140 len = min_t(int, spec.field_width, 64);
1141
1142 for (i = 0; i < len; ++i) {
1143 if (buf < end)
1144 *buf = hex_asc_hi(addr[i]);
1145 ++buf;
1146 if (buf < end)
1147 *buf = hex_asc_lo(addr[i]);
1148 ++buf;
1149
1150 if (separator && i != len - 1) {
1151 if (buf < end)
1152 *buf = separator;
1153 ++buf;
1154 }
1155 }
1156
1157 return buf;
1158}
1159
1160static noinline_for_stack
1161char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1162 struct printf_spec spec, const char *fmt)
1163{
1164 const int CHUNKSZ = 32;
1165 int nr_bits = max_t(int, spec.field_width, 0);
1166 int i, chunksz;
1167 bool first = true;
1168
1169 if (check_pointer(&buf, end, bitmap, spec))
1170 return buf;
1171
1172
1173 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1174
1175 chunksz = nr_bits & (CHUNKSZ - 1);
1176 if (chunksz == 0)
1177 chunksz = CHUNKSZ;
1178
1179 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1180 for (; i >= 0; i -= CHUNKSZ) {
1181 u32 chunkmask, val;
1182 int word, bit;
1183
1184 chunkmask = ((1ULL << chunksz) - 1);
1185 word = i / BITS_PER_LONG;
1186 bit = i % BITS_PER_LONG;
1187 val = (bitmap[word] >> bit) & chunkmask;
1188
1189 if (!first) {
1190 if (buf < end)
1191 *buf = ',';
1192 buf++;
1193 }
1194 first = false;
1195
1196 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1197 buf = number(buf, end, val, spec);
1198
1199 chunksz = CHUNKSZ;
1200 }
1201 return buf;
1202}
1203
1204static noinline_for_stack
1205char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1206 struct printf_spec spec, const char *fmt)
1207{
1208 int nr_bits = max_t(int, spec.field_width, 0);
1209
1210 int cur, rbot, rtop;
1211 bool first = true;
1212
1213 if (check_pointer(&buf, end, bitmap, spec))
1214 return buf;
1215
1216 rbot = cur = find_first_bit(bitmap, nr_bits);
1217 while (cur < nr_bits) {
1218 rtop = cur;
1219 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1220 if (cur < nr_bits && cur <= rtop + 1)
1221 continue;
1222
1223 if (!first) {
1224 if (buf < end)
1225 *buf = ',';
1226 buf++;
1227 }
1228 first = false;
1229
1230 buf = number(buf, end, rbot, default_dec_spec);
1231 if (rbot < rtop) {
1232 if (buf < end)
1233 *buf = '-';
1234 buf++;
1235
1236 buf = number(buf, end, rtop, default_dec_spec);
1237 }
1238
1239 rbot = cur;
1240 }
1241 return buf;
1242}
1243
1244static noinline_for_stack
1245char *mac_address_string(char *buf, char *end, u8 *addr,
1246 struct printf_spec spec, const char *fmt)
1247{
1248 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1249 char *p = mac_addr;
1250 int i;
1251 char separator;
1252 bool reversed = false;
1253
1254 if (check_pointer(&buf, end, addr, spec))
1255 return buf;
1256
1257 switch (fmt[1]) {
1258 case 'F':
1259 separator = '-';
1260 break;
1261
1262 case 'R':
1263 reversed = true;
1264
1265
1266 default:
1267 separator = ':';
1268 break;
1269 }
1270
1271 for (i = 0; i < 6; i++) {
1272 if (reversed)
1273 p = hex_byte_pack(p, addr[5 - i]);
1274 else
1275 p = hex_byte_pack(p, addr[i]);
1276
1277 if (fmt[0] == 'M' && i != 5)
1278 *p++ = separator;
1279 }
1280 *p = '\0';
1281
1282 return string_nocheck(buf, end, mac_addr, spec);
1283}
1284
1285static noinline_for_stack
1286char *ip4_string(char *p, const u8 *addr, const char *fmt)
1287{
1288 int i;
1289 bool leading_zeros = (fmt[0] == 'i');
1290 int index;
1291 int step;
1292
1293 switch (fmt[2]) {
1294 case 'h':
1295#ifdef __BIG_ENDIAN
1296 index = 0;
1297 step = 1;
1298#else
1299 index = 3;
1300 step = -1;
1301#endif
1302 break;
1303 case 'l':
1304 index = 3;
1305 step = -1;
1306 break;
1307 case 'n':
1308 case 'b':
1309 default:
1310 index = 0;
1311 step = 1;
1312 break;
1313 }
1314 for (i = 0; i < 4; i++) {
1315 char temp[4] __aligned(2);
1316 int digits = put_dec_trunc8(temp, addr[index]) - temp;
1317 if (leading_zeros) {
1318 if (digits < 3)
1319 *p++ = '0';
1320 if (digits < 2)
1321 *p++ = '0';
1322 }
1323
1324 while (digits--)
1325 *p++ = temp[digits];
1326 if (i < 3)
1327 *p++ = '.';
1328 index += step;
1329 }
1330 *p = '\0';
1331
1332 return p;
1333}
1334
1335static noinline_for_stack
1336char *ip6_compressed_string(char *p, const char *addr)
1337{
1338 int i, j, range;
1339 unsigned char zerolength[8];
1340 int longest = 1;
1341 int colonpos = -1;
1342 u16 word;
1343 u8 hi, lo;
1344 bool needcolon = false;
1345 bool useIPv4;
1346 struct in6_addr in6;
1347
1348 memcpy(&in6, addr, sizeof(struct in6_addr));
1349
1350 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
1351
1352 memset(zerolength, 0, sizeof(zerolength));
1353
1354 if (useIPv4)
1355 range = 6;
1356 else
1357 range = 8;
1358
1359
1360 for (i = 0; i < range; i++) {
1361 for (j = i; j < range; j++) {
1362 if (in6.s6_addr16[j] != 0)
1363 break;
1364 zerolength[i]++;
1365 }
1366 }
1367 for (i = 0; i < range; i++) {
1368 if (zerolength[i] > longest) {
1369 longest = zerolength[i];
1370 colonpos = i;
1371 }
1372 }
1373 if (longest == 1)
1374 colonpos = -1;
1375
1376
1377 for (i = 0; i < range; i++) {
1378 if (i == colonpos) {
1379 if (needcolon || i == 0)
1380 *p++ = ':';
1381 *p++ = ':';
1382 needcolon = false;
1383 i += longest - 1;
1384 continue;
1385 }
1386 if (needcolon) {
1387 *p++ = ':';
1388 needcolon = false;
1389 }
1390
1391 word = ntohs(in6.s6_addr16[i]);
1392 hi = word >> 8;
1393 lo = word & 0xff;
1394 if (hi) {
1395 if (hi > 0x0f)
1396 p = hex_byte_pack(p, hi);
1397 else
1398 *p++ = hex_asc_lo(hi);
1399 p = hex_byte_pack(p, lo);
1400 }
1401 else if (lo > 0x0f)
1402 p = hex_byte_pack(p, lo);
1403 else
1404 *p++ = hex_asc_lo(lo);
1405 needcolon = true;
1406 }
1407
1408 if (useIPv4) {
1409 if (needcolon)
1410 *p++ = ':';
1411 p = ip4_string(p, &in6.s6_addr[12], "I4");
1412 }
1413 *p = '\0';
1414
1415 return p;
1416}
1417
1418static noinline_for_stack
1419char *ip6_string(char *p, const char *addr, const char *fmt)
1420{
1421 int i;
1422
1423 for (i = 0; i < 8; i++) {
1424 p = hex_byte_pack(p, *addr++);
1425 p = hex_byte_pack(p, *addr++);
1426 if (fmt[0] == 'I' && i != 7)
1427 *p++ = ':';
1428 }
1429 *p = '\0';
1430
1431 return p;
1432}
1433
1434static noinline_for_stack
1435char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1436 struct printf_spec spec, const char *fmt)
1437{
1438 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1439
1440 if (fmt[0] == 'I' && fmt[2] == 'c')
1441 ip6_compressed_string(ip6_addr, addr);
1442 else
1443 ip6_string(ip6_addr, addr, fmt);
1444
1445 return string_nocheck(buf, end, ip6_addr, spec);
1446}
1447
1448static noinline_for_stack
1449char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1450 struct printf_spec spec, const char *fmt)
1451{
1452 char ip4_addr[sizeof("255.255.255.255")];
1453
1454 ip4_string(ip4_addr, addr, fmt);
1455
1456 return string_nocheck(buf, end, ip4_addr, spec);
1457}
1458
1459static noinline_for_stack
1460char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1461 struct printf_spec spec, const char *fmt)
1462{
1463 bool have_p = false, have_s = false, have_f = false, have_c = false;
1464 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1465 sizeof(":12345") + sizeof("/123456789") +
1466 sizeof("%1234567890")];
1467 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1468 const u8 *addr = (const u8 *) &sa->sin6_addr;
1469 char fmt6[2] = { fmt[0], '6' };
1470 u8 off = 0;
1471
1472 fmt++;
1473 while (isalpha(*++fmt)) {
1474 switch (*fmt) {
1475 case 'p':
1476 have_p = true;
1477 break;
1478 case 'f':
1479 have_f = true;
1480 break;
1481 case 's':
1482 have_s = true;
1483 break;
1484 case 'c':
1485 have_c = true;
1486 break;
1487 }
1488 }
1489
1490 if (have_p || have_s || have_f) {
1491 *p = '[';
1492 off = 1;
1493 }
1494
1495 if (fmt6[0] == 'I' && have_c)
1496 p = ip6_compressed_string(ip6_addr + off, addr);
1497 else
1498 p = ip6_string(ip6_addr + off, addr, fmt6);
1499
1500 if (have_p || have_s || have_f)
1501 *p++ = ']';
1502
1503 if (have_p) {
1504 *p++ = ':';
1505 p = number(p, pend, ntohs(sa->sin6_port), spec);
1506 }
1507 if (have_f) {
1508 *p++ = '/';
1509 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1510 IPV6_FLOWINFO_MASK), spec);
1511 }
1512 if (have_s) {
1513 *p++ = '%';
1514 p = number(p, pend, sa->sin6_scope_id, spec);
1515 }
1516 *p = '\0';
1517
1518 return string_nocheck(buf, end, ip6_addr, spec);
1519}
1520
1521static noinline_for_stack
1522char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1523 struct printf_spec spec, const char *fmt)
1524{
1525 bool have_p = false;
1526 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1527 char *pend = ip4_addr + sizeof(ip4_addr);
1528 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1529 char fmt4[3] = { fmt[0], '4', 0 };
1530
1531 fmt++;
1532 while (isalpha(*++fmt)) {
1533 switch (*fmt) {
1534 case 'p':
1535 have_p = true;
1536 break;
1537 case 'h':
1538 case 'l':
1539 case 'n':
1540 case 'b':
1541 fmt4[2] = *fmt;
1542 break;
1543 }
1544 }
1545
1546 p = ip4_string(ip4_addr, addr, fmt4);
1547 if (have_p) {
1548 *p++ = ':';
1549 p = number(p, pend, ntohs(sa->sin_port), spec);
1550 }
1551 *p = '\0';
1552
1553 return string_nocheck(buf, end, ip4_addr, spec);
1554}
1555
1556static noinline_for_stack
1557char *ip_addr_string(char *buf, char *end, const void *ptr,
1558 struct printf_spec spec, const char *fmt)
1559{
1560 char *err_fmt_msg;
1561
1562 if (check_pointer(&buf, end, ptr, spec))
1563 return buf;
1564
1565 switch (fmt[1]) {
1566 case '6':
1567 return ip6_addr_string(buf, end, ptr, spec, fmt);
1568 case '4':
1569 return ip4_addr_string(buf, end, ptr, spec, fmt);
1570 case 'S': {
1571 const union {
1572 struct sockaddr raw;
1573 struct sockaddr_in v4;
1574 struct sockaddr_in6 v6;
1575 } *sa = ptr;
1576
1577 switch (sa->raw.sa_family) {
1578 case AF_INET:
1579 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1580 case AF_INET6:
1581 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1582 default:
1583 return error_string(buf, end, "(einval)", spec);
1584 }}
1585 }
1586
1587 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1588 return error_string(buf, end, err_fmt_msg, spec);
1589}
1590
1591static noinline_for_stack
1592char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1593 const char *fmt)
1594{
1595 bool found = true;
1596 int count = 1;
1597 unsigned int flags = 0;
1598 int len;
1599
1600 if (spec.field_width == 0)
1601 return buf;
1602
1603 if (check_pointer(&buf, end, addr, spec))
1604 return buf;
1605
1606 do {
1607 switch (fmt[count++]) {
1608 case 'a':
1609 flags |= ESCAPE_ANY;
1610 break;
1611 case 'c':
1612 flags |= ESCAPE_SPECIAL;
1613 break;
1614 case 'h':
1615 flags |= ESCAPE_HEX;
1616 break;
1617 case 'n':
1618 flags |= ESCAPE_NULL;
1619 break;
1620 case 'o':
1621 flags |= ESCAPE_OCTAL;
1622 break;
1623 case 'p':
1624 flags |= ESCAPE_NP;
1625 break;
1626 case 's':
1627 flags |= ESCAPE_SPACE;
1628 break;
1629 default:
1630 found = false;
1631 break;
1632 }
1633 } while (found);
1634
1635 if (!flags)
1636 flags = ESCAPE_ANY_NP;
1637
1638 len = spec.field_width < 0 ? 1 : spec.field_width;
1639
1640
1641
1642
1643
1644
1645 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
1646
1647 return buf;
1648}
1649
1650static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1651 struct printf_spec spec, const char *fmt)
1652{
1653 va_list va;
1654
1655 if (check_pointer(&buf, end, va_fmt, spec))
1656 return buf;
1657
1658 va_copy(va, *va_fmt->va);
1659 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1660 va_end(va);
1661
1662 return buf;
1663}
1664
1665static noinline_for_stack
1666char *uuid_string(char *buf, char *end, const u8 *addr,
1667 struct printf_spec spec, const char *fmt)
1668{
1669 char uuid[UUID_STRING_LEN + 1];
1670 char *p = uuid;
1671 int i;
1672 const u8 *index = uuid_index;
1673 bool uc = false;
1674
1675 if (check_pointer(&buf, end, addr, spec))
1676 return buf;
1677
1678 switch (*(++fmt)) {
1679 case 'L':
1680 uc = true;
1681 case 'l':
1682 index = guid_index;
1683 break;
1684 case 'B':
1685 uc = true;
1686 break;
1687 }
1688
1689 for (i = 0; i < 16; i++) {
1690 if (uc)
1691 p = hex_byte_pack_upper(p, addr[index[i]]);
1692 else
1693 p = hex_byte_pack(p, addr[index[i]]);
1694 switch (i) {
1695 case 3:
1696 case 5:
1697 case 7:
1698 case 9:
1699 *p++ = '-';
1700 break;
1701 }
1702 }
1703
1704 *p = 0;
1705
1706 return string_nocheck(buf, end, uuid, spec);
1707}
1708
1709static noinline_for_stack
1710char *netdev_bits(char *buf, char *end, const void *addr,
1711 struct printf_spec spec, const char *fmt)
1712{
1713 unsigned long long num;
1714 int size;
1715
1716 if (check_pointer(&buf, end, addr, spec))
1717 return buf;
1718
1719 switch (fmt[1]) {
1720 case 'F':
1721 num = *(const netdev_features_t *)addr;
1722 size = sizeof(netdev_features_t);
1723 break;
1724 default:
1725 return error_string(buf, end, "(%pN?)", spec);
1726 }
1727
1728 return special_hex_number(buf, end, num, size);
1729}
1730
1731static noinline_for_stack
1732char *address_val(char *buf, char *end, const void *addr,
1733 struct printf_spec spec, const char *fmt)
1734{
1735 unsigned long long num;
1736 int size;
1737
1738 if (check_pointer(&buf, end, addr, spec))
1739 return buf;
1740
1741 switch (fmt[1]) {
1742 case 'd':
1743 num = *(const dma_addr_t *)addr;
1744 size = sizeof(dma_addr_t);
1745 break;
1746 case 'p':
1747 default:
1748 num = *(const phys_addr_t *)addr;
1749 size = sizeof(phys_addr_t);
1750 break;
1751 }
1752
1753 return special_hex_number(buf, end, num, size);
1754}
1755
1756static noinline_for_stack
1757char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1758{
1759 int year = tm->tm_year + (r ? 0 : 1900);
1760 int mon = tm->tm_mon + (r ? 0 : 1);
1761
1762 buf = number(buf, end, year, default_dec04_spec);
1763 if (buf < end)
1764 *buf = '-';
1765 buf++;
1766
1767 buf = number(buf, end, mon, default_dec02_spec);
1768 if (buf < end)
1769 *buf = '-';
1770 buf++;
1771
1772 return number(buf, end, tm->tm_mday, default_dec02_spec);
1773}
1774
1775static noinline_for_stack
1776char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1777{
1778 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1779 if (buf < end)
1780 *buf = ':';
1781 buf++;
1782
1783 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1784 if (buf < end)
1785 *buf = ':';
1786 buf++;
1787
1788 return number(buf, end, tm->tm_sec, default_dec02_spec);
1789}
1790
1791static noinline_for_stack
1792char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1793 struct printf_spec spec, const char *fmt)
1794{
1795 bool have_t = true, have_d = true;
1796 bool raw = false;
1797 int count = 2;
1798
1799 if (check_pointer(&buf, end, tm, spec))
1800 return buf;
1801
1802 switch (fmt[count]) {
1803 case 'd':
1804 have_t = false;
1805 count++;
1806 break;
1807 case 't':
1808 have_d = false;
1809 count++;
1810 break;
1811 }
1812
1813 raw = fmt[count] == 'r';
1814
1815 if (have_d)
1816 buf = date_str(buf, end, tm, raw);
1817 if (have_d && have_t) {
1818
1819 if (buf < end)
1820 *buf = 'T';
1821 buf++;
1822 }
1823 if (have_t)
1824 buf = time_str(buf, end, tm, raw);
1825
1826 return buf;
1827}
1828
1829static noinline_for_stack
1830char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1831 const char *fmt)
1832{
1833 switch (fmt[1]) {
1834 case 'R':
1835 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
1836 default:
1837 return error_string(buf, end, "(%ptR?)", spec);
1838 }
1839}
1840
1841static noinline_for_stack
1842char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1843 const char *fmt)
1844{
1845 if (!IS_ENABLED(CONFIG_HAVE_CLK))
1846 return error_string(buf, end, "(%pC?)", spec);
1847
1848 if (check_pointer(&buf, end, clk, spec))
1849 return buf;
1850
1851 switch (fmt[1]) {
1852 case 'n':
1853 default:
1854#ifdef CONFIG_COMMON_CLK
1855 return string(buf, end, __clk_get_name(clk), spec);
1856#else
1857 return ptr_to_id(buf, end, clk, spec);
1858#endif
1859 }
1860}
1861
1862static
1863char *format_flags(char *buf, char *end, unsigned long flags,
1864 const struct trace_print_flags *names)
1865{
1866 unsigned long mask;
1867
1868 for ( ; flags && names->name; names++) {
1869 mask = names->mask;
1870 if ((flags & mask) != mask)
1871 continue;
1872
1873 buf = string(buf, end, names->name, default_str_spec);
1874
1875 flags &= ~mask;
1876 if (flags) {
1877 if (buf < end)
1878 *buf = '|';
1879 buf++;
1880 }
1881 }
1882
1883 if (flags)
1884 buf = number(buf, end, flags, default_flag_spec);
1885
1886 return buf;
1887}
1888
1889static noinline_for_stack
1890char *flags_string(char *buf, char *end, void *flags_ptr,
1891 struct printf_spec spec, const char *fmt)
1892{
1893 unsigned long flags;
1894 const struct trace_print_flags *names;
1895
1896 if (check_pointer(&buf, end, flags_ptr, spec))
1897 return buf;
1898
1899 switch (fmt[1]) {
1900 case 'p':
1901 flags = *(unsigned long *)flags_ptr;
1902
1903 flags &= (1UL << NR_PAGEFLAGS) - 1;
1904 names = pageflag_names;
1905 break;
1906 case 'v':
1907 flags = *(unsigned long *)flags_ptr;
1908 names = vmaflag_names;
1909 break;
1910 case 'g':
1911 flags = *(gfp_t *)flags_ptr;
1912 names = gfpflag_names;
1913 break;
1914 default:
1915 return error_string(buf, end, "(%pG?)", spec);
1916 }
1917
1918 return format_flags(buf, end, flags, names);
1919}
1920
1921static noinline_for_stack
1922char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1923 char *end)
1924{
1925 int depth;
1926
1927
1928 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1929 struct fwnode_handle *__fwnode =
1930 fwnode_get_nth_parent(fwnode, depth);
1931
1932 buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
1933 default_str_spec);
1934 buf = string(buf, end, fwnode_get_name(__fwnode),
1935 default_str_spec);
1936
1937 fwnode_handle_put(__fwnode);
1938 }
1939
1940 return buf;
1941}
1942
1943static noinline_for_stack
1944char *device_node_string(char *buf, char *end, struct device_node *dn,
1945 struct printf_spec spec, const char *fmt)
1946{
1947 char tbuf[sizeof("xxxx") + 1];
1948 const char *p;
1949 int ret;
1950 char *buf_start = buf;
1951 struct property *prop;
1952 bool has_mult, pass;
1953 static const struct printf_spec num_spec = {
1954 .flags = SMALL,
1955 .field_width = -1,
1956 .precision = -1,
1957 .base = 10,
1958 };
1959
1960 struct printf_spec str_spec = spec;
1961 str_spec.field_width = -1;
1962
1963 if (fmt[0] != 'F')
1964 return error_string(buf, end, "(%pO?)", spec);
1965
1966 if (!IS_ENABLED(CONFIG_OF))
1967 return error_string(buf, end, "(%pOF?)", spec);
1968
1969 if (check_pointer(&buf, end, dn, spec))
1970 return buf;
1971
1972
1973 fmt++;
1974 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1975 fmt = "f";
1976
1977 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1978 int precision;
1979 if (pass) {
1980 if (buf < end)
1981 *buf = ':';
1982 buf++;
1983 }
1984
1985 switch (*fmt) {
1986 case 'f':
1987 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
1988 end);
1989 break;
1990 case 'n':
1991 p = fwnode_get_name(of_fwnode_handle(dn));
1992 precision = str_spec.precision;
1993 str_spec.precision = strchrnul(p, '@') - p;
1994 buf = string(buf, end, p, str_spec);
1995 str_spec.precision = precision;
1996 break;
1997 case 'p':
1998 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1999 break;
2000 case 'P':
2001 p = fwnode_get_name(of_fwnode_handle(dn));
2002 if (!p[1])
2003 p = "/";
2004 buf = string(buf, end, p, str_spec);
2005 break;
2006 case 'F':
2007 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2008 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2009 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2010 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2011 tbuf[4] = 0;
2012 buf = string_nocheck(buf, end, tbuf, str_spec);
2013 break;
2014 case 'c':
2015 ret = of_property_read_string(dn, "compatible", &p);
2016 if (!ret)
2017 buf = string(buf, end, p, str_spec);
2018 break;
2019 case 'C':
2020 has_mult = false;
2021 of_property_for_each_string(dn, "compatible", prop, p) {
2022 if (has_mult)
2023 buf = string_nocheck(buf, end, ",", str_spec);
2024 buf = string_nocheck(buf, end, "\"", str_spec);
2025 buf = string(buf, end, p, str_spec);
2026 buf = string_nocheck(buf, end, "\"", str_spec);
2027
2028 has_mult = true;
2029 }
2030 break;
2031 default:
2032 break;
2033 }
2034 }
2035
2036 return widen_string(buf, buf - buf_start, end, spec);
2037}
2038
2039static noinline_for_stack
2040char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2041 struct printf_spec spec, const char *fmt)
2042{
2043 struct printf_spec str_spec = spec;
2044 char *buf_start = buf;
2045
2046 str_spec.field_width = -1;
2047
2048 if (*fmt != 'w')
2049 return error_string(buf, end, "(%pf?)", spec);
2050
2051 if (check_pointer(&buf, end, fwnode, spec))
2052 return buf;
2053
2054 fmt++;
2055
2056 switch (*fmt) {
2057 case 'P':
2058 buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
2059 break;
2060 case 'f':
2061 default:
2062 buf = fwnode_full_name_string(fwnode, buf, end);
2063 break;
2064 }
2065
2066 return widen_string(buf, buf - buf_start, end, spec);
2067}
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189static noinline_for_stack
2190char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2191 struct printf_spec spec)
2192{
2193 switch (*fmt) {
2194 case 'S':
2195 case 's':
2196 ptr = dereference_symbol_descriptor(ptr);
2197
2198 case 'B':
2199 return symbol_string(buf, end, ptr, spec, fmt);
2200 case 'R':
2201 case 'r':
2202 return resource_string(buf, end, ptr, spec, fmt);
2203 case 'h':
2204 return hex_string(buf, end, ptr, spec, fmt);
2205 case 'b':
2206 switch (fmt[1]) {
2207 case 'l':
2208 return bitmap_list_string(buf, end, ptr, spec, fmt);
2209 default:
2210 return bitmap_string(buf, end, ptr, spec, fmt);
2211 }
2212 case 'M':
2213 case 'm':
2214
2215
2216 return mac_address_string(buf, end, ptr, spec, fmt);
2217 case 'I':
2218
2219
2220
2221
2222 case 'i':
2223
2224
2225
2226 return ip_addr_string(buf, end, ptr, spec, fmt);
2227 case 'E':
2228 return escaped_string(buf, end, ptr, spec, fmt);
2229 case 'U':
2230 return uuid_string(buf, end, ptr, spec, fmt);
2231 case 'V':
2232 return va_format(buf, end, ptr, spec, fmt);
2233 case 'K':
2234 return restricted_pointer(buf, end, ptr, spec);
2235 case 'N':
2236 return netdev_bits(buf, end, ptr, spec, fmt);
2237 case 'a':
2238 return address_val(buf, end, ptr, spec, fmt);
2239 case 'd':
2240 return dentry_name(buf, end, ptr, spec, fmt);
2241 case 't':
2242 return time_and_date(buf, end, ptr, spec, fmt);
2243 case 'C':
2244 return clock(buf, end, ptr, spec, fmt);
2245 case 'D':
2246 return file_dentry_name(buf, end, ptr, spec, fmt);
2247#ifdef CONFIG_BLOCK
2248 case 'g':
2249 return bdev_name(buf, end, ptr, spec, fmt);
2250#endif
2251
2252 case 'G':
2253 return flags_string(buf, end, ptr, spec, fmt);
2254 case 'O':
2255 return device_node_string(buf, end, ptr, spec, fmt + 1);
2256 case 'f':
2257 return fwnode_string(buf, end, ptr, spec, fmt + 1);
2258 case 'x':
2259 return pointer_string(buf, end, ptr, spec);
2260 case 'e':
2261
2262 if (!IS_ERR(ptr))
2263 break;
2264 return err_ptr(buf, end, ptr, spec);
2265 case 'u':
2266 case 'k':
2267 switch (fmt[1]) {
2268 case 's':
2269 return string(buf, end, ptr, spec);
2270 default:
2271 return error_string(buf, end, "(einval)", spec);
2272 }
2273 }
2274
2275
2276 return ptr_to_id(buf, end, ptr, spec);
2277}
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300static noinline_for_stack
2301int format_decode(const char *fmt, struct printf_spec *spec)
2302{
2303 const char *start = fmt;
2304 char qualifier;
2305
2306
2307 if (spec->type == FORMAT_TYPE_WIDTH) {
2308 if (spec->field_width < 0) {
2309 spec->field_width = -spec->field_width;
2310 spec->flags |= LEFT;
2311 }
2312 spec->type = FORMAT_TYPE_NONE;
2313 goto precision;
2314 }
2315
2316
2317 if (spec->type == FORMAT_TYPE_PRECISION) {
2318 if (spec->precision < 0)
2319 spec->precision = 0;
2320
2321 spec->type = FORMAT_TYPE_NONE;
2322 goto qualifier;
2323 }
2324
2325
2326 spec->type = FORMAT_TYPE_NONE;
2327
2328 for (; *fmt ; ++fmt) {
2329 if (*fmt == '%')
2330 break;
2331 }
2332
2333
2334 if (fmt != start || !*fmt)
2335 return fmt - start;
2336
2337
2338 spec->flags = 0;
2339
2340 while (1) {
2341 bool found = true;
2342
2343 ++fmt;
2344
2345 switch (*fmt) {
2346 case '-': spec->flags |= LEFT; break;
2347 case '+': spec->flags |= PLUS; break;
2348 case ' ': spec->flags |= SPACE; break;
2349 case '#': spec->flags |= SPECIAL; break;
2350 case '0': spec->flags |= ZEROPAD; break;
2351 default: found = false;
2352 }
2353
2354 if (!found)
2355 break;
2356 }
2357
2358
2359 spec->field_width = -1;
2360
2361 if (isdigit(*fmt))
2362 spec->field_width = skip_atoi(&fmt);
2363 else if (*fmt == '*') {
2364
2365 spec->type = FORMAT_TYPE_WIDTH;
2366 return ++fmt - start;
2367 }
2368
2369precision:
2370
2371 spec->precision = -1;
2372 if (*fmt == '.') {
2373 ++fmt;
2374 if (isdigit(*fmt)) {
2375 spec->precision = skip_atoi(&fmt);
2376 if (spec->precision < 0)
2377 spec->precision = 0;
2378 } else if (*fmt == '*') {
2379
2380 spec->type = FORMAT_TYPE_PRECISION;
2381 return ++fmt - start;
2382 }
2383 }
2384
2385qualifier:
2386
2387 qualifier = 0;
2388 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2389 *fmt == 'z' || *fmt == 't') {
2390 qualifier = *fmt++;
2391 if (unlikely(qualifier == *fmt)) {
2392 if (qualifier == 'l') {
2393 qualifier = 'L';
2394 ++fmt;
2395 } else if (qualifier == 'h') {
2396 qualifier = 'H';
2397 ++fmt;
2398 }
2399 }
2400 }
2401
2402
2403 spec->base = 10;
2404 switch (*fmt) {
2405 case 'c':
2406 spec->type = FORMAT_TYPE_CHAR;
2407 return ++fmt - start;
2408
2409 case 's':
2410 spec->type = FORMAT_TYPE_STR;
2411 return ++fmt - start;
2412
2413 case 'p':
2414 spec->type = FORMAT_TYPE_PTR;
2415 return ++fmt - start;
2416
2417 case '%':
2418 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2419 return ++fmt - start;
2420
2421
2422 case 'o':
2423 spec->base = 8;
2424 break;
2425
2426 case 'x':
2427 spec->flags |= SMALL;
2428
2429
2430 case 'X':
2431 spec->base = 16;
2432 break;
2433
2434 case 'd':
2435 case 'i':
2436 spec->flags |= SIGN;
2437 case 'u':
2438 break;
2439
2440 case 'n':
2441
2442
2443
2444
2445
2446
2447
2448 default:
2449 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2450 spec->type = FORMAT_TYPE_INVALID;
2451 return fmt - start;
2452 }
2453
2454 if (qualifier == 'L')
2455 spec->type = FORMAT_TYPE_LONG_LONG;
2456 else if (qualifier == 'l') {
2457 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2458 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
2459 } else if (qualifier == 'z') {
2460 spec->type = FORMAT_TYPE_SIZE_T;
2461 } else if (qualifier == 't') {
2462 spec->type = FORMAT_TYPE_PTRDIFF;
2463 } else if (qualifier == 'H') {
2464 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2465 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2466 } else if (qualifier == 'h') {
2467 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2468 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2469 } else {
2470 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2471 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2472 }
2473
2474 return ++fmt - start;
2475}
2476
2477static void
2478set_field_width(struct printf_spec *spec, int width)
2479{
2480 spec->field_width = width;
2481 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2482 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2483 }
2484}
2485
2486static void
2487set_precision(struct printf_spec *spec, int prec)
2488{
2489 spec->precision = prec;
2490 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2491 spec->precision = clamp(prec, 0, PRECISION_MAX);
2492 }
2493}
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2524{
2525 unsigned long long num;
2526 char *str, *end;
2527 struct printf_spec spec = {0};
2528
2529
2530
2531 if (WARN_ON_ONCE(size > INT_MAX))
2532 return 0;
2533
2534 str = buf;
2535 end = buf + size;
2536
2537
2538 if (end < buf) {
2539 end = ((void *)-1);
2540 size = end - buf;
2541 }
2542
2543 while (*fmt) {
2544 const char *old_fmt = fmt;
2545 int read = format_decode(fmt, &spec);
2546
2547 fmt += read;
2548
2549 switch (spec.type) {
2550 case FORMAT_TYPE_NONE: {
2551 int copy = read;
2552 if (str < end) {
2553 if (copy > end - str)
2554 copy = end - str;
2555 memcpy(str, old_fmt, copy);
2556 }
2557 str += read;
2558 break;
2559 }
2560
2561 case FORMAT_TYPE_WIDTH:
2562 set_field_width(&spec, va_arg(args, int));
2563 break;
2564
2565 case FORMAT_TYPE_PRECISION:
2566 set_precision(&spec, va_arg(args, int));
2567 break;
2568
2569 case FORMAT_TYPE_CHAR: {
2570 char c;
2571
2572 if (!(spec.flags & LEFT)) {
2573 while (--spec.field_width > 0) {
2574 if (str < end)
2575 *str = ' ';
2576 ++str;
2577
2578 }
2579 }
2580 c = (unsigned char) va_arg(args, int);
2581 if (str < end)
2582 *str = c;
2583 ++str;
2584 while (--spec.field_width > 0) {
2585 if (str < end)
2586 *str = ' ';
2587 ++str;
2588 }
2589 break;
2590 }
2591
2592 case FORMAT_TYPE_STR:
2593 str = string(str, end, va_arg(args, char *), spec);
2594 break;
2595
2596 case FORMAT_TYPE_PTR:
2597 str = pointer(fmt, str, end, va_arg(args, void *),
2598 spec);
2599 while (isalnum(*fmt))
2600 fmt++;
2601 break;
2602
2603 case FORMAT_TYPE_PERCENT_CHAR:
2604 if (str < end)
2605 *str = '%';
2606 ++str;
2607 break;
2608
2609 case FORMAT_TYPE_INVALID:
2610
2611
2612
2613
2614
2615
2616
2617
2618 goto out;
2619
2620 default:
2621 switch (spec.type) {
2622 case FORMAT_TYPE_LONG_LONG:
2623 num = va_arg(args, long long);
2624 break;
2625 case FORMAT_TYPE_ULONG:
2626 num = va_arg(args, unsigned long);
2627 break;
2628 case FORMAT_TYPE_LONG:
2629 num = va_arg(args, long);
2630 break;
2631 case FORMAT_TYPE_SIZE_T:
2632 if (spec.flags & SIGN)
2633 num = va_arg(args, ssize_t);
2634 else
2635 num = va_arg(args, size_t);
2636 break;
2637 case FORMAT_TYPE_PTRDIFF:
2638 num = va_arg(args, ptrdiff_t);
2639 break;
2640 case FORMAT_TYPE_UBYTE:
2641 num = (unsigned char) va_arg(args, int);
2642 break;
2643 case FORMAT_TYPE_BYTE:
2644 num = (signed char) va_arg(args, int);
2645 break;
2646 case FORMAT_TYPE_USHORT:
2647 num = (unsigned short) va_arg(args, int);
2648 break;
2649 case FORMAT_TYPE_SHORT:
2650 num = (short) va_arg(args, int);
2651 break;
2652 case FORMAT_TYPE_INT:
2653 num = (int) va_arg(args, int);
2654 break;
2655 default:
2656 num = va_arg(args, unsigned int);
2657 }
2658
2659 str = number(str, end, num, spec);
2660 }
2661 }
2662
2663out:
2664 if (size > 0) {
2665 if (str < end)
2666 *str = '\0';
2667 else
2668 end[-1] = '\0';
2669 }
2670
2671
2672 return str-buf;
2673
2674}
2675EXPORT_SYMBOL(vsnprintf);
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2693{
2694 int i;
2695
2696 i = vsnprintf(buf, size, fmt, args);
2697
2698 if (likely(i < size))
2699 return i;
2700 if (size != 0)
2701 return size - 1;
2702 return 0;
2703}
2704EXPORT_SYMBOL(vscnprintf);
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720int snprintf(char *buf, size_t size, const char *fmt, ...)
2721{
2722 va_list args;
2723 int i;
2724
2725 va_start(args, fmt);
2726 i = vsnprintf(buf, size, fmt, args);
2727 va_end(args);
2728
2729 return i;
2730}
2731EXPORT_SYMBOL(snprintf);
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744int scnprintf(char *buf, size_t size, const char *fmt, ...)
2745{
2746 va_list args;
2747 int i;
2748
2749 va_start(args, fmt);
2750 i = vscnprintf(buf, size, fmt, args);
2751 va_end(args);
2752
2753 return i;
2754}
2755EXPORT_SYMBOL(scnprintf);
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771int vsprintf(char *buf, const char *fmt, va_list args)
2772{
2773 return vsnprintf(buf, INT_MAX, fmt, args);
2774}
2775EXPORT_SYMBOL(vsprintf);
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789int sprintf(char *buf, const char *fmt, ...)
2790{
2791 va_list args;
2792 int i;
2793
2794 va_start(args, fmt);
2795 i = vsnprintf(buf, INT_MAX, fmt, args);
2796 va_end(args);
2797
2798 return i;
2799}
2800EXPORT_SYMBOL(sprintf);
2801
2802#ifdef CONFIG_BINARY_PRINTF
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2827{
2828 struct printf_spec spec = {0};
2829 char *str, *end;
2830 int width;
2831
2832 str = (char *)bin_buf;
2833 end = (char *)(bin_buf + size);
2834
2835#define save_arg(type) \
2836({ \
2837 unsigned long long value; \
2838 if (sizeof(type) == 8) { \
2839 unsigned long long val8; \
2840 str = PTR_ALIGN(str, sizeof(u32)); \
2841 val8 = va_arg(args, unsigned long long); \
2842 if (str + sizeof(type) <= end) { \
2843 *(u32 *)str = *(u32 *)&val8; \
2844 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
2845 } \
2846 value = val8; \
2847 } else { \
2848 unsigned int val4; \
2849 str = PTR_ALIGN(str, sizeof(type)); \
2850 val4 = va_arg(args, int); \
2851 if (str + sizeof(type) <= end) \
2852 *(typeof(type) *)str = (type)(long)val4; \
2853 value = (unsigned long long)val4; \
2854 } \
2855 str += sizeof(type); \
2856 value; \
2857})
2858
2859 while (*fmt) {
2860 int read = format_decode(fmt, &spec);
2861
2862 fmt += read;
2863
2864 switch (spec.type) {
2865 case FORMAT_TYPE_NONE:
2866 case FORMAT_TYPE_PERCENT_CHAR:
2867 break;
2868 case FORMAT_TYPE_INVALID:
2869 goto out;
2870
2871 case FORMAT_TYPE_WIDTH:
2872 case FORMAT_TYPE_PRECISION:
2873 width = (int)save_arg(int);
2874
2875 if (*fmt == 'p')
2876 set_field_width(&spec, width);
2877 break;
2878
2879 case FORMAT_TYPE_CHAR:
2880 save_arg(char);
2881 break;
2882
2883 case FORMAT_TYPE_STR: {
2884 const char *save_str = va_arg(args, char *);
2885 const char *err_msg;
2886 size_t len;
2887
2888 err_msg = check_pointer_msg(save_str);
2889 if (err_msg)
2890 save_str = err_msg;
2891
2892 len = strlen(save_str) + 1;
2893 if (str + len < end)
2894 memcpy(str, save_str, len);
2895 str += len;
2896 break;
2897 }
2898
2899 case FORMAT_TYPE_PTR:
2900
2901 switch (*fmt) {
2902
2903 case 'S':
2904 case 's':
2905 case 'x':
2906 case 'K':
2907 case 'e':
2908 save_arg(void *);
2909 break;
2910 default:
2911 if (!isalnum(*fmt)) {
2912 save_arg(void *);
2913 break;
2914 }
2915 str = pointer(fmt, str, end, va_arg(args, void *),
2916 spec);
2917 if (str + 1 < end)
2918 *str++ = '\0';
2919 else
2920 end[-1] = '\0';
2921 }
2922
2923 while (isalnum(*fmt))
2924 fmt++;
2925 break;
2926
2927 default:
2928 switch (spec.type) {
2929
2930 case FORMAT_TYPE_LONG_LONG:
2931 save_arg(long long);
2932 break;
2933 case FORMAT_TYPE_ULONG:
2934 case FORMAT_TYPE_LONG:
2935 save_arg(unsigned long);
2936 break;
2937 case FORMAT_TYPE_SIZE_T:
2938 save_arg(size_t);
2939 break;
2940 case FORMAT_TYPE_PTRDIFF:
2941 save_arg(ptrdiff_t);
2942 break;
2943 case FORMAT_TYPE_UBYTE:
2944 case FORMAT_TYPE_BYTE:
2945 save_arg(char);
2946 break;
2947 case FORMAT_TYPE_USHORT:
2948 case FORMAT_TYPE_SHORT:
2949 save_arg(short);
2950 break;
2951 default:
2952 save_arg(int);
2953 }
2954 }
2955 }
2956
2957out:
2958 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2959#undef save_arg
2960}
2961EXPORT_SYMBOL_GPL(vbin_printf);
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2986{
2987 struct printf_spec spec = {0};
2988 char *str, *end;
2989 const char *args = (const char *)bin_buf;
2990
2991 if (WARN_ON_ONCE(size > INT_MAX))
2992 return 0;
2993
2994 str = buf;
2995 end = buf + size;
2996
2997#define get_arg(type) \
2998({ \
2999 typeof(type) value; \
3000 if (sizeof(type) == 8) { \
3001 args = PTR_ALIGN(args, sizeof(u32)); \
3002 *(u32 *)&value = *(u32 *)args; \
3003 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
3004 } else { \
3005 args = PTR_ALIGN(args, sizeof(type)); \
3006 value = *(typeof(type) *)args; \
3007 } \
3008 args += sizeof(type); \
3009 value; \
3010})
3011
3012
3013 if (end < buf) {
3014 end = ((void *)-1);
3015 size = end - buf;
3016 }
3017
3018 while (*fmt) {
3019 const char *old_fmt = fmt;
3020 int read = format_decode(fmt, &spec);
3021
3022 fmt += read;
3023
3024 switch (spec.type) {
3025 case FORMAT_TYPE_NONE: {
3026 int copy = read;
3027 if (str < end) {
3028 if (copy > end - str)
3029 copy = end - str;
3030 memcpy(str, old_fmt, copy);
3031 }
3032 str += read;
3033 break;
3034 }
3035
3036 case FORMAT_TYPE_WIDTH:
3037 set_field_width(&spec, get_arg(int));
3038 break;
3039
3040 case FORMAT_TYPE_PRECISION:
3041 set_precision(&spec, get_arg(int));
3042 break;
3043
3044 case FORMAT_TYPE_CHAR: {
3045 char c;
3046
3047 if (!(spec.flags & LEFT)) {
3048 while (--spec.field_width > 0) {
3049 if (str < end)
3050 *str = ' ';
3051 ++str;
3052 }
3053 }
3054 c = (unsigned char) get_arg(char);
3055 if (str < end)
3056 *str = c;
3057 ++str;
3058 while (--spec.field_width > 0) {
3059 if (str < end)
3060 *str = ' ';
3061 ++str;
3062 }
3063 break;
3064 }
3065
3066 case FORMAT_TYPE_STR: {
3067 const char *str_arg = args;
3068 args += strlen(str_arg) + 1;
3069 str = string(str, end, (char *)str_arg, spec);
3070 break;
3071 }
3072
3073 case FORMAT_TYPE_PTR: {
3074 bool process = false;
3075 int copy, len;
3076
3077 switch (*fmt) {
3078 case 'S':
3079 case 's':
3080 case 'F':
3081 case 'f':
3082 case 'x':
3083 case 'K':
3084 case 'e':
3085 process = true;
3086 break;
3087 default:
3088 if (!isalnum(*fmt)) {
3089 process = true;
3090 break;
3091 }
3092
3093 if (str < end) {
3094 len = copy = strlen(args);
3095 if (copy > end - str)
3096 copy = end - str;
3097 memcpy(str, args, copy);
3098 str += len;
3099 args += len + 1;
3100 }
3101 }
3102 if (process)
3103 str = pointer(fmt, str, end, get_arg(void *), spec);
3104
3105 while (isalnum(*fmt))
3106 fmt++;
3107 break;
3108 }
3109
3110 case FORMAT_TYPE_PERCENT_CHAR:
3111 if (str < end)
3112 *str = '%';
3113 ++str;
3114 break;
3115
3116 case FORMAT_TYPE_INVALID:
3117 goto out;
3118
3119 default: {
3120 unsigned long long num;
3121
3122 switch (spec.type) {
3123
3124 case FORMAT_TYPE_LONG_LONG:
3125 num = get_arg(long long);
3126 break;
3127 case FORMAT_TYPE_ULONG:
3128 case FORMAT_TYPE_LONG:
3129 num = get_arg(unsigned long);
3130 break;
3131 case FORMAT_TYPE_SIZE_T:
3132 num = get_arg(size_t);
3133 break;
3134 case FORMAT_TYPE_PTRDIFF:
3135 num = get_arg(ptrdiff_t);
3136 break;
3137 case FORMAT_TYPE_UBYTE:
3138 num = get_arg(unsigned char);
3139 break;
3140 case FORMAT_TYPE_BYTE:
3141 num = get_arg(signed char);
3142 break;
3143 case FORMAT_TYPE_USHORT:
3144 num = get_arg(unsigned short);
3145 break;
3146 case FORMAT_TYPE_SHORT:
3147 num = get_arg(short);
3148 break;
3149 case FORMAT_TYPE_UINT:
3150 num = get_arg(unsigned int);
3151 break;
3152 default:
3153 num = get_arg(int);
3154 }
3155
3156 str = number(str, end, num, spec);
3157 }
3158 }
3159 }
3160
3161out:
3162 if (size > 0) {
3163 if (str < end)
3164 *str = '\0';
3165 else
3166 end[-1] = '\0';
3167 }
3168
3169#undef get_arg
3170
3171
3172 return str - buf;
3173}
3174EXPORT_SYMBOL_GPL(bstr_printf);
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3187{
3188 va_list args;
3189 int ret;
3190
3191 va_start(args, fmt);
3192 ret = vbin_printf(bin_buf, size, fmt, args);
3193 va_end(args);
3194
3195 return ret;
3196}
3197EXPORT_SYMBOL_GPL(bprintf);
3198
3199#endif
3200
3201
3202
3203
3204
3205
3206
3207int vsscanf(const char *buf, const char *fmt, va_list args)
3208{
3209 const char *str = buf;
3210 char *next;
3211 char digit;
3212 int num = 0;
3213 u8 qualifier;
3214 unsigned int base;
3215 union {
3216 long long s;
3217 unsigned long long u;
3218 } val;
3219 s16 field_width;
3220 bool is_sign;
3221
3222 while (*fmt) {
3223
3224
3225
3226
3227 if (isspace(*fmt)) {
3228 fmt = skip_spaces(++fmt);
3229 str = skip_spaces(str);
3230 }
3231
3232
3233 if (*fmt != '%' && *fmt) {
3234 if (*fmt++ != *str++)
3235 break;
3236 continue;
3237 }
3238
3239 if (!*fmt)
3240 break;
3241 ++fmt;
3242
3243
3244
3245
3246 if (*fmt == '*') {
3247 if (!*str)
3248 break;
3249 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3250
3251 if (*fmt == '[')
3252 return num;
3253 fmt++;
3254 }
3255 while (!isspace(*str) && *str)
3256 str++;
3257 continue;
3258 }
3259
3260
3261 field_width = -1;
3262 if (isdigit(*fmt)) {
3263 field_width = skip_atoi(&fmt);
3264 if (field_width <= 0)
3265 break;
3266 }
3267
3268
3269 qualifier = -1;
3270 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
3271 *fmt == 'z') {
3272 qualifier = *fmt++;
3273 if (unlikely(qualifier == *fmt)) {
3274 if (qualifier == 'h') {
3275 qualifier = 'H';
3276 fmt++;
3277 } else if (qualifier == 'l') {
3278 qualifier = 'L';
3279 fmt++;
3280 }
3281 }
3282 }
3283
3284 if (!*fmt)
3285 break;
3286
3287 if (*fmt == 'n') {
3288
3289 *va_arg(args, int *) = str - buf;
3290 ++fmt;
3291 continue;
3292 }
3293
3294 if (!*str)
3295 break;
3296
3297 base = 10;
3298 is_sign = false;
3299
3300 switch (*fmt++) {
3301 case 'c':
3302 {
3303 char *s = (char *)va_arg(args, char*);
3304 if (field_width == -1)
3305 field_width = 1;
3306 do {
3307 *s++ = *str++;
3308 } while (--field_width > 0 && *str);
3309 num++;
3310 }
3311 continue;
3312 case 's':
3313 {
3314 char *s = (char *)va_arg(args, char *);
3315 if (field_width == -1)
3316 field_width = SHRT_MAX;
3317
3318 str = skip_spaces(str);
3319
3320
3321 while (*str && !isspace(*str) && field_width--)
3322 *s++ = *str++;
3323 *s = '\0';
3324 num++;
3325 }
3326 continue;
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342 case '[':
3343 {
3344 char *s = (char *)va_arg(args, char *);
3345 DECLARE_BITMAP(set, 256) = {0};
3346 unsigned int len = 0;
3347 bool negate = (*fmt == '^');
3348
3349
3350 if (field_width == -1)
3351 return num;
3352
3353 if (negate)
3354 ++fmt;
3355
3356 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3357 set_bit((u8)*fmt, set);
3358
3359
3360 if (!*fmt || !len)
3361 return num;
3362 ++fmt;
3363
3364 if (negate) {
3365 bitmap_complement(set, set, 256);
3366
3367 clear_bit(0, set);
3368 }
3369
3370
3371 if (!test_bit((u8)*str, set))
3372 return num;
3373
3374 while (test_bit((u8)*str, set) && field_width--)
3375 *s++ = *str++;
3376 *s = '\0';
3377 ++num;
3378 }
3379 continue;
3380 case 'o':
3381 base = 8;
3382 break;
3383 case 'x':
3384 case 'X':
3385 base = 16;
3386 break;
3387 case 'i':
3388 base = 0;
3389
3390 case 'd':
3391 is_sign = true;
3392
3393 case 'u':
3394 break;
3395 case '%':
3396
3397 if (*str++ != '%')
3398 return num;
3399 continue;
3400 default:
3401
3402 return num;
3403 }
3404
3405
3406
3407
3408 str = skip_spaces(str);
3409
3410 digit = *str;
3411 if (is_sign && digit == '-')
3412 digit = *(str + 1);
3413
3414 if (!digit
3415 || (base == 16 && !isxdigit(digit))
3416 || (base == 10 && !isdigit(digit))
3417 || (base == 8 && (!isdigit(digit) || digit > '7'))
3418 || (base == 0 && !isdigit(digit)))
3419 break;
3420
3421 if (is_sign)
3422 val.s = qualifier != 'L' ?
3423 simple_strtol(str, &next, base) :
3424 simple_strtoll(str, &next, base);
3425 else
3426 val.u = qualifier != 'L' ?
3427 simple_strtoul(str, &next, base) :
3428 simple_strtoull(str, &next, base);
3429
3430 if (field_width > 0 && next - str > field_width) {
3431 if (base == 0)
3432 _parse_integer_fixup_radix(str, &base);
3433 while (next - str > field_width) {
3434 if (is_sign)
3435 val.s = div_s64(val.s, base);
3436 else
3437 val.u = div_u64(val.u, base);
3438 --next;
3439 }
3440 }
3441
3442 switch (qualifier) {
3443 case 'H':
3444 if (is_sign)
3445 *va_arg(args, signed char *) = val.s;
3446 else
3447 *va_arg(args, unsigned char *) = val.u;
3448 break;
3449 case 'h':
3450 if (is_sign)
3451 *va_arg(args, short *) = val.s;
3452 else
3453 *va_arg(args, unsigned short *) = val.u;
3454 break;
3455 case 'l':
3456 if (is_sign)
3457 *va_arg(args, long *) = val.s;
3458 else
3459 *va_arg(args, unsigned long *) = val.u;
3460 break;
3461 case 'L':
3462 if (is_sign)
3463 *va_arg(args, long long *) = val.s;
3464 else
3465 *va_arg(args, unsigned long long *) = val.u;
3466 break;
3467 case 'z':
3468 *va_arg(args, size_t *) = val.u;
3469 break;
3470 default:
3471 if (is_sign)
3472 *va_arg(args, int *) = val.s;
3473 else
3474 *va_arg(args, unsigned int *) = val.u;
3475 break;
3476 }
3477 num++;
3478
3479 if (!next)
3480 break;
3481 str = next;
3482 }
3483
3484 return num;
3485}
3486EXPORT_SYMBOL(vsscanf);
3487
3488
3489
3490
3491
3492
3493
3494int sscanf(const char *buf, const char *fmt, ...)
3495{
3496 va_list args;
3497 int i;
3498
3499 va_start(args, fmt);
3500 i = vsscanf(buf, fmt, args);
3501 va_end(args);
3502
3503 return i;
3504}
3505EXPORT_SYMBOL(sscanf);
3506