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