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