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