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