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