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