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