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