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