]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/vsprintf.c
lib/spinlock_debug: avoid livelock in do_raw_spin_lock()
[mirror_ubuntu-artful-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>
8bc3bcc9 20#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
1da177e4
LT
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
0fe1ef24
LT
25#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
332d2e78 27#include <linux/ioport.h>
8a27f7c9 28#include <net/addrconf.h>
1da177e4 29
4e57b681 30#include <asm/page.h> /* for PAGE_SIZE */
1da177e4 31#include <asm/div64.h>
deac93df 32#include <asm/sections.h> /* for dereference_function_descriptor() */
1da177e4 33
1dff46d6 34#include "kstrtox.h"
aa46a63e 35
1da177e4 36/**
922ac25c 37 * simple_strtoull - convert a string to an unsigned long long
1da177e4
LT
38 * @cp: The start of the string
39 * @endp: A pointer to the end of the parsed string will be placed here
40 * @base: The number base to use
41 */
922ac25c 42unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
1da177e4 43{
1dff46d6
AD
44 unsigned long long result;
45 unsigned int rv;
aa46a63e 46
1dff46d6
AD
47 cp = _parse_integer_fixup_radix(cp, &base);
48 rv = _parse_integer(cp, base, &result);
49 /* FIXME */
50 cp += (rv & ~KSTRTOX_OVERFLOW);
aa46a63e 51
1da177e4
LT
52 if (endp)
53 *endp = (char *)cp;
7b9186f5 54
1da177e4
LT
55 return result;
56}
922ac25c 57EXPORT_SYMBOL(simple_strtoull);
1da177e4
LT
58
59/**
922ac25c 60 * simple_strtoul - convert a string to an unsigned long
1da177e4
LT
61 * @cp: The start of the string
62 * @endp: A pointer to the end of the parsed string will be placed here
63 * @base: The number base to use
64 */
922ac25c 65unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
1da177e4 66{
922ac25c 67 return simple_strtoull(cp, endp, base);
1da177e4 68}
922ac25c 69EXPORT_SYMBOL(simple_strtoul);
1da177e4
LT
70
71/**
922ac25c 72 * simple_strtol - convert a string to a signed long
1da177e4
LT
73 * @cp: The start of the string
74 * @endp: A pointer to the end of the parsed string will be placed here
75 * @base: The number base to use
76 */
922ac25c 77long simple_strtol(const char *cp, char **endp, unsigned int base)
1da177e4 78{
922ac25c
AGR
79 if (*cp == '-')
80 return -simple_strtoul(cp + 1, endp, base);
7b9186f5 81
922ac25c 82 return simple_strtoul(cp, endp, base);
1da177e4 83}
922ac25c 84EXPORT_SYMBOL(simple_strtol);
1da177e4
LT
85
86/**
87 * simple_strtoll - convert a string to a signed long long
88 * @cp: The start of the string
89 * @endp: A pointer to the end of the parsed string will be placed here
90 * @base: The number base to use
91 */
22d27051 92long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1da177e4 93{
7b9186f5 94 if (*cp == '-')
22d27051 95 return -simple_strtoull(cp + 1, endp, base);
7b9186f5 96
22d27051 97 return simple_strtoull(cp, endp, base);
1da177e4 98}
98d5ce0d 99EXPORT_SYMBOL(simple_strtoll);
1da177e4 100
cf3b429b
JP
101static noinline_for_stack
102int skip_atoi(const char **s)
1da177e4 103{
7b9186f5 104 int i = 0;
1da177e4
LT
105
106 while (isdigit(**s))
107 i = i*10 + *((*s)++) - '0';
7b9186f5 108
1da177e4
LT
109 return i;
110}
111
4277eedd
DV
112/* Decimal conversion is by far the most typical, and is used
113 * for /proc and /sys data. This directly impacts e.g. top performance
114 * with many processes running. We optimize it for speed
133fd9f5
DV
115 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
116 * (with permission from the author, Douglas W. Jones).
117 */
4277eedd 118
133fd9f5
DV
119#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
120/* Formats correctly any integer in [0, 999999999] */
cf3b429b 121static noinline_for_stack
133fd9f5 122char *put_dec_full9(char *buf, unsigned q)
4277eedd 123{
133fd9f5 124 unsigned r;
7b9186f5 125
133fd9f5
DV
126 /*
127 * Possible ways to approx. divide by 10
128 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
129 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
130 * (x * 0x6667) >> 18 x < 43699
131 * (x * 0x3334) >> 17 x < 16389
132 * (x * 0x199a) >> 16 x < 16389
133 * (x * 0x0ccd) >> 15 x < 16389
134 * (x * 0x0667) >> 14 x < 2739
135 * (x * 0x0334) >> 13 x < 1029
136 * (x * 0x019a) >> 12 x < 1029
137 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
138 * (x * 0x0067) >> 10 x < 179
139 * (x * 0x0034) >> 9 x < 69 same
140 * (x * 0x001a) >> 8 x < 69 same
141 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
142 * (x * 0x0007) >> 6 x < 19
143 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
144 */
145 r = (q * (uint64_t)0x1999999a) >> 32;
146 *buf++ = (q - 10 * r) + '0'; /* 1 */
147 q = (r * (uint64_t)0x1999999a) >> 32;
148 *buf++ = (r - 10 * q) + '0'; /* 2 */
149 r = (q * (uint64_t)0x1999999a) >> 32;
150 *buf++ = (q - 10 * r) + '0'; /* 3 */
151 q = (r * (uint64_t)0x1999999a) >> 32;
152 *buf++ = (r - 10 * q) + '0'; /* 4 */
153 r = (q * (uint64_t)0x1999999a) >> 32;
154 *buf++ = (q - 10 * r) + '0'; /* 5 */
155 /* Now value is under 10000, can avoid 64-bit multiply */
156 q = (r * 0x199a) >> 16;
157 *buf++ = (r - 10 * q) + '0'; /* 6 */
158 r = (q * 0xcd) >> 11;
159 *buf++ = (q - 10 * r) + '0'; /* 7 */
160 q = (r * 0xcd) >> 11;
161 *buf++ = (r - 10 * q) + '0'; /* 8 */
162 *buf++ = q + '0'; /* 9 */
4277eedd
DV
163 return buf;
164}
133fd9f5
DV
165#endif
166
167/* Similar to above but do not pad with zeros.
168 * Code can be easily arranged to print 9 digits too, but our callers
169 * always call put_dec_full9() instead when the number has 9 decimal digits.
170 */
cf3b429b 171static noinline_for_stack
133fd9f5 172char *put_dec_trunc8(char *buf, unsigned r)
4277eedd 173{
133fd9f5
DV
174 unsigned q;
175
176 /* Copy of previous function's body with added early returns */
cb239d0a
GS
177 while (r >= 10000) {
178 q = r + '0';
179 r = (r * (uint64_t)0x1999999a) >> 32;
180 *buf++ = q - 10*r;
181 }
182
f4000516
GS
183 q = (r * 0x199a) >> 16; /* r <= 9999 */
184 *buf++ = (r - 10 * q) + '0';
133fd9f5
DV
185 if (q == 0)
186 return buf;
f4000516
GS
187 r = (q * 0xcd) >> 11; /* q <= 999 */
188 *buf++ = (q - 10 * r) + '0';
133fd9f5
DV
189 if (r == 0)
190 return buf;
f4000516
GS
191 q = (r * 0xcd) >> 11; /* r <= 99 */
192 *buf++ = (r - 10 * q) + '0';
133fd9f5
DV
193 if (q == 0)
194 return buf;
f4000516 195 *buf++ = q + '0'; /* q <= 9 */
133fd9f5
DV
196 return buf;
197}
4277eedd 198
133fd9f5
DV
199/* There are two algorithms to print larger numbers.
200 * One is generic: divide by 1000000000 and repeatedly print
201 * groups of (up to) 9 digits. It's conceptually simple,
202 * but requires a (unsigned long long) / 1000000000 division.
203 *
204 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
205 * manipulates them cleverly and generates groups of 4 decimal digits.
206 * It so happens that it does NOT require long long division.
207 *
208 * If long is > 32 bits, division of 64-bit values is relatively easy,
209 * and we will use the first algorithm.
210 * If long long is > 64 bits (strange architecture with VERY large long long),
211 * second algorithm can't be used, and we again use the first one.
212 *
213 * Else (if long is 32 bits and long long is 64 bits) we use second one.
214 */
7b9186f5 215
133fd9f5
DV
216#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
217
218/* First algorithm: generic */
219
220static
221char *put_dec(char *buf, unsigned long long n)
222{
223 if (n >= 100*1000*1000) {
224 while (n >= 1000*1000*1000)
225 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
226 if (n >= 100*1000*1000)
227 return put_dec_full9(buf, n);
228 }
229 return put_dec_trunc8(buf, n);
4277eedd 230}
133fd9f5
DV
231
232#else
233
234/* Second algorithm: valid only for 64-bit long longs */
235
e49317d4 236/* See comment in put_dec_full9 for choice of constants */
cf3b429b 237static noinline_for_stack
2359172a 238void put_dec_full4(char *buf, unsigned q)
4277eedd 239{
133fd9f5 240 unsigned r;
e49317d4 241 r = (q * 0xccd) >> 15;
2359172a 242 buf[0] = (q - 10 * r) + '0';
e49317d4 243 q = (r * 0xcd) >> 11;
2359172a 244 buf[1] = (r - 10 * q) + '0';
133fd9f5 245 r = (q * 0xcd) >> 11;
2359172a
GS
246 buf[2] = (q - 10 * r) + '0';
247 buf[3] = r + '0';
248}
249
250/*
251 * Call put_dec_full4 on x % 10000, return x / 10000.
252 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
253 * holds for all x < 1,128,869,999. The largest value this
254 * helper will ever be asked to convert is 1,125,520,955.
255 * (d1 in the put_dec code, assuming n is all-ones).
256 */
257static
258unsigned put_dec_helper4(char *buf, unsigned x)
259{
260 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
261
262 put_dec_full4(buf, x - q * 10000);
263 return q;
4277eedd
DV
264}
265
133fd9f5
DV
266/* Based on code by Douglas W. Jones found at
267 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
268 * (with permission from the author).
269 * Performs no 64-bit division and hence should be fast on 32-bit machines.
270 */
271static
272char *put_dec(char *buf, unsigned long long n)
273{
274 uint32_t d3, d2, d1, q, h;
275
276 if (n < 100*1000*1000)
277 return put_dec_trunc8(buf, n);
278
279 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
280 h = (n >> 32);
281 d2 = (h ) & 0xffff;
282 d3 = (h >> 16); /* implicit "& 0xffff" */
283
284 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
2359172a
GS
285 q = put_dec_helper4(buf, q);
286
287 q += 7671 * d3 + 9496 * d2 + 6 * d1;
288 q = put_dec_helper4(buf+4, q);
289
290 q += 4749 * d3 + 42 * d2;
291 q = put_dec_helper4(buf+8, q);
133fd9f5 292
2359172a
GS
293 q += 281 * d3;
294 buf += 12;
295 if (q)
296 buf = put_dec_trunc8(buf, q);
297 else while (buf[-1] == '0')
133fd9f5
DV
298 --buf;
299
300 return buf;
301}
302
303#endif
304
1ac101a5
KH
305/*
306 * Convert passed number to decimal string.
307 * Returns the length of string. On buffer overflow, returns 0.
308 *
309 * If speed is not important, use snprintf(). It's easy to read the code.
310 */
311int num_to_str(char *buf, int size, unsigned long long num)
312{
133fd9f5 313 char tmp[sizeof(num) * 3];
1ac101a5
KH
314 int idx, len;
315
133fd9f5
DV
316 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
317 if (num <= 9) {
318 tmp[0] = '0' + num;
319 len = 1;
320 } else {
321 len = put_dec(tmp, num) - tmp;
322 }
1ac101a5
KH
323
324 if (len > size)
325 return 0;
326 for (idx = 0; idx < len; ++idx)
327 buf[idx] = tmp[len - idx - 1];
133fd9f5 328 return len;
1ac101a5
KH
329}
330
1da177e4
LT
331#define ZEROPAD 1 /* pad with zero */
332#define SIGN 2 /* unsigned/signed long */
333#define PLUS 4 /* show plus */
334#define SPACE 8 /* space if plus */
335#define LEFT 16 /* left justified */
b89dc5d6
BH
336#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
337#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
1da177e4 338
fef20d9c
FW
339enum format_type {
340 FORMAT_TYPE_NONE, /* Just a string part */
ed681a91 341 FORMAT_TYPE_WIDTH,
fef20d9c
FW
342 FORMAT_TYPE_PRECISION,
343 FORMAT_TYPE_CHAR,
344 FORMAT_TYPE_STR,
345 FORMAT_TYPE_PTR,
346 FORMAT_TYPE_PERCENT_CHAR,
347 FORMAT_TYPE_INVALID,
348 FORMAT_TYPE_LONG_LONG,
349 FORMAT_TYPE_ULONG,
350 FORMAT_TYPE_LONG,
a4e94ef0
Z
351 FORMAT_TYPE_UBYTE,
352 FORMAT_TYPE_BYTE,
fef20d9c
FW
353 FORMAT_TYPE_USHORT,
354 FORMAT_TYPE_SHORT,
355 FORMAT_TYPE_UINT,
356 FORMAT_TYPE_INT,
357 FORMAT_TYPE_NRCHARS,
358 FORMAT_TYPE_SIZE_T,
359 FORMAT_TYPE_PTRDIFF
360};
361
362struct printf_spec {
4e310fda 363 u8 type; /* format_type enum */
ef0658f3 364 u8 flags; /* flags to number() */
4e310fda
JP
365 u8 base; /* number base, 8, 10 or 16 only */
366 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
367 s16 field_width; /* width of output field */
368 s16 precision; /* # of digits/chars */
fef20d9c
FW
369};
370
cf3b429b
JP
371static noinline_for_stack
372char *number(char *buf, char *end, unsigned long long num,
373 struct printf_spec spec)
1da177e4 374{
9b706aee
DV
375 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
376 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
377
378 char tmp[66];
379 char sign;
380 char locase;
fef20d9c 381 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
1da177e4 382 int i;
7c203422 383 bool is_zero = num == 0LL;
1da177e4 384
9b706aee
DV
385 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
386 * produces same digits or (maybe lowercased) letters */
fef20d9c
FW
387 locase = (spec.flags & SMALL);
388 if (spec.flags & LEFT)
389 spec.flags &= ~ZEROPAD;
1da177e4 390 sign = 0;
fef20d9c 391 if (spec.flags & SIGN) {
7b9186f5 392 if ((signed long long)num < 0) {
1da177e4 393 sign = '-';
7b9186f5 394 num = -(signed long long)num;
fef20d9c
FW
395 spec.field_width--;
396 } else if (spec.flags & PLUS) {
1da177e4 397 sign = '+';
fef20d9c
FW
398 spec.field_width--;
399 } else if (spec.flags & SPACE) {
1da177e4 400 sign = ' ';
fef20d9c 401 spec.field_width--;
1da177e4
LT
402 }
403 }
b39a7340 404 if (need_pfx) {
fef20d9c 405 if (spec.base == 16)
7c203422
PC
406 spec.field_width -= 2;
407 else if (!is_zero)
fef20d9c 408 spec.field_width--;
1da177e4 409 }
b39a7340
DV
410
411 /* generate full string in tmp[], in reverse order */
1da177e4 412 i = 0;
133fd9f5
DV
413 if (num < spec.base)
414 tmp[i++] = digits[num] | locase;
4277eedd
DV
415 /* Generic code, for any base:
416 else do {
9b706aee 417 tmp[i++] = (digits[do_div(num,base)] | locase);
4277eedd
DV
418 } while (num != 0);
419 */
fef20d9c
FW
420 else if (spec.base != 10) { /* 8 or 16 */
421 int mask = spec.base - 1;
b39a7340 422 int shift = 3;
7b9186f5
AGR
423
424 if (spec.base == 16)
425 shift = 4;
b39a7340 426 do {
9b706aee 427 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
b39a7340
DV
428 num >>= shift;
429 } while (num);
4277eedd
DV
430 } else { /* base 10 */
431 i = put_dec(tmp, num) - tmp;
432 }
b39a7340
DV
433
434 /* printing 100 using %2d gives "100", not "00" */
fef20d9c
FW
435 if (i > spec.precision)
436 spec.precision = i;
b39a7340 437 /* leading space padding */
fef20d9c
FW
438 spec.field_width -= spec.precision;
439 if (!(spec.flags & (ZEROPAD+LEFT))) {
7b9186f5 440 while (--spec.field_width >= 0) {
f796937a 441 if (buf < end)
1da177e4
LT
442 *buf = ' ';
443 ++buf;
444 }
445 }
b39a7340 446 /* sign */
1da177e4 447 if (sign) {
f796937a 448 if (buf < end)
1da177e4
LT
449 *buf = sign;
450 ++buf;
451 }
b39a7340
DV
452 /* "0x" / "0" prefix */
453 if (need_pfx) {
7c203422
PC
454 if (spec.base == 16 || !is_zero) {
455 if (buf < end)
456 *buf = '0';
457 ++buf;
458 }
fef20d9c 459 if (spec.base == 16) {
f796937a 460 if (buf < end)
9b706aee 461 *buf = ('X' | locase);
1da177e4
LT
462 ++buf;
463 }
464 }
b39a7340 465 /* zero or space padding */
fef20d9c
FW
466 if (!(spec.flags & LEFT)) {
467 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
468 while (--spec.field_width >= 0) {
f796937a 469 if (buf < end)
1da177e4
LT
470 *buf = c;
471 ++buf;
472 }
473 }
b39a7340 474 /* hmm even more zero padding? */
fef20d9c 475 while (i <= --spec.precision) {
f796937a 476 if (buf < end)
1da177e4
LT
477 *buf = '0';
478 ++buf;
479 }
b39a7340
DV
480 /* actual digits of result */
481 while (--i >= 0) {
f796937a 482 if (buf < end)
1da177e4
LT
483 *buf = tmp[i];
484 ++buf;
485 }
b39a7340 486 /* trailing space padding */
fef20d9c 487 while (--spec.field_width >= 0) {
f796937a 488 if (buf < end)
1da177e4
LT
489 *buf = ' ';
490 ++buf;
491 }
7b9186f5 492
1da177e4
LT
493 return buf;
494}
495
cf3b429b
JP
496static noinline_for_stack
497char *string(char *buf, char *end, const char *s, struct printf_spec spec)
0f9bfa56
LT
498{
499 int len, i;
500
501 if ((unsigned long)s < PAGE_SIZE)
0f4f81dc 502 s = "(null)";
0f9bfa56 503
fef20d9c 504 len = strnlen(s, spec.precision);
0f9bfa56 505
fef20d9c
FW
506 if (!(spec.flags & LEFT)) {
507 while (len < spec.field_width--) {
0f9bfa56
LT
508 if (buf < end)
509 *buf = ' ';
510 ++buf;
511 }
512 }
513 for (i = 0; i < len; ++i) {
514 if (buf < end)
515 *buf = *s;
516 ++buf; ++s;
517 }
fef20d9c 518 while (len < spec.field_width--) {
0f9bfa56
LT
519 if (buf < end)
520 *buf = ' ';
521 ++buf;
522 }
7b9186f5 523
0f9bfa56
LT
524 return buf;
525}
526
cf3b429b
JP
527static noinline_for_stack
528char *symbol_string(char *buf, char *end, void *ptr,
529 struct printf_spec spec, char ext)
0fe1ef24
LT
530{
531 unsigned long value = (unsigned long) ptr;
532#ifdef CONFIG_KALLSYMS
533 char sym[KSYM_SYMBOL_LEN];
0f77a8d3
NK
534 if (ext == 'B')
535 sprint_backtrace(sym, value);
536 else if (ext != 'f' && ext != 's')
0c8b946e
FW
537 sprint_symbol(sym, value);
538 else
4796dd20 539 sprint_symbol_no_offset(sym, value);
7b9186f5 540
fef20d9c 541 return string(buf, end, sym, spec);
0fe1ef24 542#else
7b9186f5 543 spec.field_width = 2 * sizeof(void *);
fef20d9c
FW
544 spec.flags |= SPECIAL | SMALL | ZEROPAD;
545 spec.base = 16;
7b9186f5 546
fef20d9c 547 return number(buf, end, value, spec);
0fe1ef24
LT
548#endif
549}
550
cf3b429b
JP
551static noinline_for_stack
552char *resource_string(char *buf, char *end, struct resource *res,
553 struct printf_spec spec, const char *fmt)
332d2e78
LT
554{
555#ifndef IO_RSRC_PRINTK_SIZE
28405372 556#define IO_RSRC_PRINTK_SIZE 6
332d2e78
LT
557#endif
558
559#ifndef MEM_RSRC_PRINTK_SIZE
28405372 560#define MEM_RSRC_PRINTK_SIZE 10
332d2e78 561#endif
4da0b66c 562 static const struct printf_spec io_spec = {
fef20d9c 563 .base = 16,
4da0b66c 564 .field_width = IO_RSRC_PRINTK_SIZE,
fef20d9c
FW
565 .precision = -1,
566 .flags = SPECIAL | SMALL | ZEROPAD,
567 };
4da0b66c
BH
568 static const struct printf_spec mem_spec = {
569 .base = 16,
570 .field_width = MEM_RSRC_PRINTK_SIZE,
571 .precision = -1,
572 .flags = SPECIAL | SMALL | ZEROPAD,
573 };
0f4050c7
BH
574 static const struct printf_spec bus_spec = {
575 .base = 16,
576 .field_width = 2,
577 .precision = -1,
578 .flags = SMALL | ZEROPAD,
579 };
4da0b66c 580 static const struct printf_spec dec_spec = {
c91d3376
BH
581 .base = 10,
582 .precision = -1,
583 .flags = 0,
584 };
4da0b66c 585 static const struct printf_spec str_spec = {
fd95541e
BH
586 .field_width = -1,
587 .precision = 10,
588 .flags = LEFT,
589 };
4da0b66c 590 static const struct printf_spec flag_spec = {
fd95541e
BH
591 .base = 16,
592 .precision = -1,
593 .flags = SPECIAL | SMALL,
594 };
c7dabef8
BH
595
596 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
597 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
598#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
599#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
9d7cca04 600#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
c7dabef8
BH
601#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
602 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
603 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
604
332d2e78 605 char *p = sym, *pend = sym + sizeof(sym);
c7dabef8 606 int decode = (fmt[0] == 'R') ? 1 : 0;
4da0b66c 607 const struct printf_spec *specp;
332d2e78
LT
608
609 *p++ = '[';
4da0b66c 610 if (res->flags & IORESOURCE_IO) {
c7dabef8 611 p = string(p, pend, "io ", str_spec);
4da0b66c
BH
612 specp = &io_spec;
613 } else if (res->flags & IORESOURCE_MEM) {
c7dabef8 614 p = string(p, pend, "mem ", str_spec);
4da0b66c
BH
615 specp = &mem_spec;
616 } else if (res->flags & IORESOURCE_IRQ) {
c7dabef8 617 p = string(p, pend, "irq ", str_spec);
4da0b66c
BH
618 specp = &dec_spec;
619 } else if (res->flags & IORESOURCE_DMA) {
c7dabef8 620 p = string(p, pend, "dma ", str_spec);
4da0b66c 621 specp = &dec_spec;
0f4050c7
BH
622 } else if (res->flags & IORESOURCE_BUS) {
623 p = string(p, pend, "bus ", str_spec);
624 specp = &bus_spec;
4da0b66c 625 } else {
c7dabef8 626 p = string(p, pend, "??? ", str_spec);
4da0b66c 627 specp = &mem_spec;
c7dabef8 628 decode = 0;
fd95541e 629 }
4da0b66c 630 p = number(p, pend, res->start, *specp);
c91d3376
BH
631 if (res->start != res->end) {
632 *p++ = '-';
4da0b66c 633 p = number(p, pend, res->end, *specp);
c91d3376 634 }
c7dabef8 635 if (decode) {
fd95541e
BH
636 if (res->flags & IORESOURCE_MEM_64)
637 p = string(p, pend, " 64bit", str_spec);
638 if (res->flags & IORESOURCE_PREFETCH)
639 p = string(p, pend, " pref", str_spec);
9d7cca04
BH
640 if (res->flags & IORESOURCE_WINDOW)
641 p = string(p, pend, " window", str_spec);
fd95541e
BH
642 if (res->flags & IORESOURCE_DISABLED)
643 p = string(p, pend, " disabled", str_spec);
c7dabef8
BH
644 } else {
645 p = string(p, pend, " flags ", str_spec);
646 p = number(p, pend, res->flags, flag_spec);
fd95541e 647 }
332d2e78 648 *p++ = ']';
c7dabef8 649 *p = '\0';
332d2e78 650
fef20d9c 651 return string(buf, end, sym, spec);
332d2e78
LT
652}
653
31550a16
AS
654static noinline_for_stack
655char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
656 const char *fmt)
657{
658 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
659 negative value, fallback to the default */
660 char separator;
661
662 if (spec.field_width == 0)
663 /* nothing to print */
664 return buf;
665
666 if (ZERO_OR_NULL_PTR(addr))
667 /* NULL pointer */
668 return string(buf, end, NULL, spec);
669
670 switch (fmt[1]) {
671 case 'C':
672 separator = ':';
673 break;
674 case 'D':
675 separator = '-';
676 break;
677 case 'N':
678 separator = 0;
679 break;
680 default:
681 separator = ' ';
682 break;
683 }
684
685 if (spec.field_width > 0)
686 len = min_t(int, spec.field_width, 64);
687
688 for (i = 0; i < len && buf < end - 1; i++) {
689 buf = hex_byte_pack(buf, addr[i]);
690
691 if (buf < end && separator && i != len - 1)
692 *buf++ = separator;
693 }
694
695 return buf;
696}
697
cf3b429b
JP
698static noinline_for_stack
699char *mac_address_string(char *buf, char *end, u8 *addr,
700 struct printf_spec spec, const char *fmt)
dd45c9cf 701{
8a27f7c9 702 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
dd45c9cf
HH
703 char *p = mac_addr;
704 int i;
bc7259a2 705 char separator;
76597ff9 706 bool reversed = false;
bc7259a2 707
76597ff9
AE
708 switch (fmt[1]) {
709 case 'F':
bc7259a2 710 separator = '-';
76597ff9
AE
711 break;
712
713 case 'R':
714 reversed = true;
715 /* fall through */
716
717 default:
bc7259a2 718 separator = ':';
76597ff9 719 break;
bc7259a2 720 }
dd45c9cf
HH
721
722 for (i = 0; i < 6; i++) {
76597ff9
AE
723 if (reversed)
724 p = hex_byte_pack(p, addr[5 - i]);
725 else
726 p = hex_byte_pack(p, addr[i]);
727
8a27f7c9 728 if (fmt[0] == 'M' && i != 5)
bc7259a2 729 *p++ = separator;
dd45c9cf
HH
730 }
731 *p = '\0';
732
fef20d9c 733 return string(buf, end, mac_addr, spec);
dd45c9cf
HH
734}
735
cf3b429b
JP
736static noinline_for_stack
737char *ip4_string(char *p, const u8 *addr, const char *fmt)
8a27f7c9
JP
738{
739 int i;
0159f24e
JP
740 bool leading_zeros = (fmt[0] == 'i');
741 int index;
742 int step;
743
744 switch (fmt[2]) {
745 case 'h':
746#ifdef __BIG_ENDIAN
747 index = 0;
748 step = 1;
749#else
750 index = 3;
751 step = -1;
752#endif
753 break;
754 case 'l':
755 index = 3;
756 step = -1;
757 break;
758 case 'n':
759 case 'b':
760 default:
761 index = 0;
762 step = 1;
763 break;
764 }
8a27f7c9
JP
765 for (i = 0; i < 4; i++) {
766 char temp[3]; /* hold each IP quad in reverse order */
133fd9f5 767 int digits = put_dec_trunc8(temp, addr[index]) - temp;
8a27f7c9
JP
768 if (leading_zeros) {
769 if (digits < 3)
770 *p++ = '0';
771 if (digits < 2)
772 *p++ = '0';
773 }
774 /* reverse the digits in the quad */
775 while (digits--)
776 *p++ = temp[digits];
777 if (i < 3)
778 *p++ = '.';
0159f24e 779 index += step;
8a27f7c9 780 }
8a27f7c9 781 *p = '\0';
7b9186f5 782
8a27f7c9
JP
783 return p;
784}
785
cf3b429b
JP
786static noinline_for_stack
787char *ip6_compressed_string(char *p, const char *addr)
689afa7d 788{
7b9186f5 789 int i, j, range;
8a27f7c9
JP
790 unsigned char zerolength[8];
791 int longest = 1;
792 int colonpos = -1;
793 u16 word;
7b9186f5 794 u8 hi, lo;
8a27f7c9 795 bool needcolon = false;
eb78cd26
JP
796 bool useIPv4;
797 struct in6_addr in6;
798
799 memcpy(&in6, addr, sizeof(struct in6_addr));
800
801 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
8a27f7c9
JP
802
803 memset(zerolength, 0, sizeof(zerolength));
804
805 if (useIPv4)
806 range = 6;
807 else
808 range = 8;
809
810 /* find position of longest 0 run */
811 for (i = 0; i < range; i++) {
812 for (j = i; j < range; j++) {
eb78cd26 813 if (in6.s6_addr16[j] != 0)
8a27f7c9
JP
814 break;
815 zerolength[i]++;
816 }
817 }
818 for (i = 0; i < range; i++) {
819 if (zerolength[i] > longest) {
820 longest = zerolength[i];
821 colonpos = i;
822 }
823 }
29cf519e
JP
824 if (longest == 1) /* don't compress a single 0 */
825 colonpos = -1;
689afa7d 826
8a27f7c9
JP
827 /* emit address */
828 for (i = 0; i < range; i++) {
829 if (i == colonpos) {
830 if (needcolon || i == 0)
831 *p++ = ':';
832 *p++ = ':';
833 needcolon = false;
834 i += longest - 1;
835 continue;
836 }
837 if (needcolon) {
838 *p++ = ':';
839 needcolon = false;
840 }
841 /* hex u16 without leading 0s */
eb78cd26 842 word = ntohs(in6.s6_addr16[i]);
8a27f7c9
JP
843 hi = word >> 8;
844 lo = word & 0xff;
845 if (hi) {
846 if (hi > 0x0f)
55036ba7 847 p = hex_byte_pack(p, hi);
8a27f7c9
JP
848 else
849 *p++ = hex_asc_lo(hi);
55036ba7 850 p = hex_byte_pack(p, lo);
8a27f7c9 851 }
b5ff992b 852 else if (lo > 0x0f)
55036ba7 853 p = hex_byte_pack(p, lo);
8a27f7c9
JP
854 else
855 *p++ = hex_asc_lo(lo);
856 needcolon = true;
857 }
858
859 if (useIPv4) {
860 if (needcolon)
861 *p++ = ':';
0159f24e 862 p = ip4_string(p, &in6.s6_addr[12], "I4");
8a27f7c9 863 }
8a27f7c9 864 *p = '\0';
7b9186f5 865
8a27f7c9
JP
866 return p;
867}
868
cf3b429b
JP
869static noinline_for_stack
870char *ip6_string(char *p, const char *addr, const char *fmt)
8a27f7c9
JP
871{
872 int i;
7b9186f5 873
689afa7d 874 for (i = 0; i < 8; i++) {
55036ba7
AS
875 p = hex_byte_pack(p, *addr++);
876 p = hex_byte_pack(p, *addr++);
8a27f7c9 877 if (fmt[0] == 'I' && i != 7)
689afa7d
HH
878 *p++ = ':';
879 }
880 *p = '\0';
7b9186f5 881
8a27f7c9
JP
882 return p;
883}
884
cf3b429b
JP
885static noinline_for_stack
886char *ip6_addr_string(char *buf, char *end, const u8 *addr,
887 struct printf_spec spec, const char *fmt)
8a27f7c9
JP
888{
889 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
890
891 if (fmt[0] == 'I' && fmt[2] == 'c')
eb78cd26 892 ip6_compressed_string(ip6_addr, addr);
8a27f7c9 893 else
eb78cd26 894 ip6_string(ip6_addr, addr, fmt);
689afa7d 895
fef20d9c 896 return string(buf, end, ip6_addr, spec);
689afa7d
HH
897}
898
cf3b429b
JP
899static noinline_for_stack
900char *ip4_addr_string(char *buf, char *end, const u8 *addr,
901 struct printf_spec spec, const char *fmt)
4aa99606 902{
8a27f7c9 903 char ip4_addr[sizeof("255.255.255.255")];
4aa99606 904
0159f24e 905 ip4_string(ip4_addr, addr, fmt);
4aa99606 906
fef20d9c 907 return string(buf, end, ip4_addr, spec);
4aa99606
HH
908}
909
cf3b429b
JP
910static noinline_for_stack
911char *uuid_string(char *buf, char *end, const u8 *addr,
912 struct printf_spec spec, const char *fmt)
9ac6e44e
JP
913{
914 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
915 char *p = uuid;
916 int i;
917 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
918 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
919 const u8 *index = be;
920 bool uc = false;
921
922 switch (*(++fmt)) {
923 case 'L':
924 uc = true; /* fall-through */
925 case 'l':
926 index = le;
927 break;
928 case 'B':
929 uc = true;
930 break;
931 }
932
933 for (i = 0; i < 16; i++) {
55036ba7 934 p = hex_byte_pack(p, addr[index[i]]);
9ac6e44e
JP
935 switch (i) {
936 case 3:
937 case 5:
938 case 7:
939 case 9:
940 *p++ = '-';
941 break;
942 }
943 }
944
945 *p = 0;
946
947 if (uc) {
948 p = uuid;
949 do {
950 *p = toupper(*p);
951 } while (*(++p));
952 }
953
954 return string(buf, end, uuid, spec);
955}
956
c8f44aff
MM
957static
958char *netdev_feature_string(char *buf, char *end, const u8 *addr,
959 struct printf_spec spec)
960{
961 spec.flags |= SPECIAL | SMALL | ZEROPAD;
962 if (spec.field_width == -1)
963 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
964 spec.base = 16;
965
966 return number(buf, end, *(const netdev_features_t *)addr, spec);
967}
968
411f05f1 969int kptr_restrict __read_mostly;
455cd5ab 970
4d8a743c
LT
971/*
972 * Show a '%p' thing. A kernel extension is that the '%p' is followed
973 * by an extra set of alphanumeric characters that are extended format
974 * specifiers.
975 *
332d2e78
LT
976 * Right now we handle:
977 *
0c8b946e
FW
978 * - 'F' For symbolic function descriptor pointers with offset
979 * - 'f' For simple symbolic function names without offset
0efb4d20
SR
980 * - 'S' For symbolic direct pointers with offset
981 * - 's' For symbolic direct pointers without offset
0f77a8d3 982 * - 'B' For backtraced symbolic direct pointers with offset
c7dabef8
BH
983 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
984 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
dd45c9cf
HH
985 * - 'M' For a 6-byte MAC address, it prints the address in the
986 * usual colon-separated hex notation
8a27f7c9 987 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
bc7259a2 988 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
c8e00060 989 * with a dash-separated hex notation
7c59154e 990 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
8a27f7c9
JP
991 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
992 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
993 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
994 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
995 * IPv6 omits the colons (01020304...0f)
996 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
0159f24e 997 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
8a27f7c9 998 * - 'I6c' for IPv6 addresses printed as specified by
29cf519e 999 * http://tools.ietf.org/html/rfc5952
9ac6e44e
JP
1000 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1001 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1002 * Options for %pU are:
1003 * b big endian lower case hex (default)
1004 * B big endian UPPER case hex
1005 * l little endian lower case hex
1006 * L little endian UPPER case hex
1007 * big endian output byte order is:
1008 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1009 * little endian output byte order is:
1010 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
7db6f5fb
JP
1011 * - 'V' For a struct va_format which contains a format string * and va_list *,
1012 * call vsnprintf(->format, *->va_list).
1013 * Implements a "recursive vsnprintf".
1014 * Do not use this feature without some mechanism to verify the
1015 * correctness of the format string and va_list arguments.
455cd5ab 1016 * - 'K' For a kernel pointer that should be hidden from unprivileged users
c8f44aff 1017 * - 'NF' For a netdev_features_t
31550a16
AS
1018 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1019 * a certain separator (' ' by default):
1020 * C colon
1021 * D dash
1022 * N no separator
1023 * The maximum supported length is 64 bytes of the input. Consider
1024 * to use print_hex_dump() for the larger input.
9ac6e44e 1025 *
332d2e78
LT
1026 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1027 * function pointers are really function descriptors, which contain a
1028 * pointer to the real address.
4d8a743c 1029 */
cf3b429b
JP
1030static noinline_for_stack
1031char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1032 struct printf_spec spec)
78a8bf69 1033{
725fe002
GL
1034 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1035
9f36e2c4 1036 if (!ptr && *fmt != 'K') {
5e057981
JP
1037 /*
1038 * Print (null) with the same width as a pointer so it makes
1039 * tabular output look nice.
1040 */
1041 if (spec.field_width == -1)
725fe002 1042 spec.field_width = default_width;
fef20d9c 1043 return string(buf, end, "(null)", spec);
5e057981 1044 }
d97106ab 1045
0fe1ef24
LT
1046 switch (*fmt) {
1047 case 'F':
0c8b946e 1048 case 'f':
0fe1ef24
LT
1049 ptr = dereference_function_descriptor(ptr);
1050 /* Fallthrough */
1051 case 'S':
9ac6e44e 1052 case 's':
0f77a8d3 1053 case 'B':
0c8b946e 1054 return symbol_string(buf, end, ptr, spec, *fmt);
332d2e78 1055 case 'R':
c7dabef8 1056 case 'r':
fd95541e 1057 return resource_string(buf, end, ptr, spec, fmt);
31550a16
AS
1058 case 'h':
1059 return hex_string(buf, end, ptr, spec, fmt);
8a27f7c9
JP
1060 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1061 case 'm': /* Contiguous: 000102030405 */
76597ff9
AE
1062 /* [mM]F (FDDI) */
1063 /* [mM]R (Reverse order; Bluetooth) */
8a27f7c9
JP
1064 return mac_address_string(buf, end, ptr, spec, fmt);
1065 case 'I': /* Formatted IP supported
1066 * 4: 1.2.3.4
1067 * 6: 0001:0203:...:0708
1068 * 6c: 1::708 or 1::1.2.3.4
1069 */
1070 case 'i': /* Contiguous:
1071 * 4: 001.002.003.004
1072 * 6: 000102...0f
1073 */
1074 switch (fmt[1]) {
1075 case '6':
1076 return ip6_addr_string(buf, end, ptr, spec, fmt);
1077 case '4':
1078 return ip4_addr_string(buf, end, ptr, spec, fmt);
1079 }
fef20d9c 1080 break;
9ac6e44e
JP
1081 case 'U':
1082 return uuid_string(buf, end, ptr, spec, fmt);
7db6f5fb 1083 case 'V':
5756b76e
JB
1084 {
1085 va_list va;
1086
1087 va_copy(va, *((struct va_format *)ptr)->va);
1088 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1089 ((struct va_format *)ptr)->fmt, va);
1090 va_end(va);
1091 return buf;
1092 }
455cd5ab
DR
1093 case 'K':
1094 /*
1095 * %pK cannot be used in IRQ context because its test
1096 * for CAP_SYSLOG would be meaningless.
1097 */
3715c530
DR
1098 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1099 in_nmi())) {
455cd5ab 1100 if (spec.field_width == -1)
725fe002 1101 spec.field_width = default_width;
455cd5ab 1102 return string(buf, end, "pK-error", spec);
455cd5ab 1103 }
26297607
JP
1104 if (!((kptr_restrict == 0) ||
1105 (kptr_restrict == 1 &&
1106 has_capability_noaudit(current, CAP_SYSLOG))))
1107 ptr = NULL;
1108 break;
c8f44aff
MM
1109 case 'N':
1110 switch (fmt[1]) {
1111 case 'F':
1112 return netdev_feature_string(buf, end, ptr, spec);
1113 }
1114 break;
fef20d9c
FW
1115 }
1116 spec.flags |= SMALL;
1117 if (spec.field_width == -1) {
725fe002 1118 spec.field_width = default_width;
fef20d9c
FW
1119 spec.flags |= ZEROPAD;
1120 }
1121 spec.base = 16;
1122
1123 return number(buf, end, (unsigned long) ptr, spec);
1124}
1125
1126/*
1127 * Helper function to decode printf style format.
1128 * Each call decode a token from the format and return the
1129 * number of characters read (or likely the delta where it wants
1130 * to go on the next call).
1131 * The decoded token is returned through the parameters
1132 *
1133 * 'h', 'l', or 'L' for integer fields
1134 * 'z' support added 23/7/1999 S.H.
1135 * 'z' changed to 'Z' --davidm 1/25/99
1136 * 't' added for ptrdiff_t
1137 *
1138 * @fmt: the format string
1139 * @type of the token returned
1140 * @flags: various flags such as +, -, # tokens..
1141 * @field_width: overwritten width
1142 * @base: base of the number (octal, hex, ...)
1143 * @precision: precision of a number
1144 * @qualifier: qualifier of a number (long, size_t, ...)
1145 */
cf3b429b
JP
1146static noinline_for_stack
1147int format_decode(const char *fmt, struct printf_spec *spec)
fef20d9c
FW
1148{
1149 const char *start = fmt;
fef20d9c
FW
1150
1151 /* we finished early by reading the field width */
ed681a91 1152 if (spec->type == FORMAT_TYPE_WIDTH) {
fef20d9c
FW
1153 if (spec->field_width < 0) {
1154 spec->field_width = -spec->field_width;
1155 spec->flags |= LEFT;
1156 }
1157 spec->type = FORMAT_TYPE_NONE;
1158 goto precision;
1159 }
1160
1161 /* we finished early by reading the precision */
1162 if (spec->type == FORMAT_TYPE_PRECISION) {
1163 if (spec->precision < 0)
1164 spec->precision = 0;
1165
1166 spec->type = FORMAT_TYPE_NONE;
1167 goto qualifier;
1168 }
1169
1170 /* By default */
1171 spec->type = FORMAT_TYPE_NONE;
1172
1173 for (; *fmt ; ++fmt) {
1174 if (*fmt == '%')
1175 break;
1176 }
1177
1178 /* Return the current non-format string */
1179 if (fmt != start || !*fmt)
1180 return fmt - start;
1181
1182 /* Process flags */
1183 spec->flags = 0;
1184
1185 while (1) { /* this also skips first '%' */
1186 bool found = true;
1187
1188 ++fmt;
1189
1190 switch (*fmt) {
1191 case '-': spec->flags |= LEFT; break;
1192 case '+': spec->flags |= PLUS; break;
1193 case ' ': spec->flags |= SPACE; break;
1194 case '#': spec->flags |= SPECIAL; break;
1195 case '0': spec->flags |= ZEROPAD; break;
1196 default: found = false;
1197 }
1198
1199 if (!found)
1200 break;
1201 }
1202
1203 /* get field width */
1204 spec->field_width = -1;
1205
1206 if (isdigit(*fmt))
1207 spec->field_width = skip_atoi(&fmt);
1208 else if (*fmt == '*') {
1209 /* it's the next argument */
ed681a91 1210 spec->type = FORMAT_TYPE_WIDTH;
fef20d9c
FW
1211 return ++fmt - start;
1212 }
1213
1214precision:
1215 /* get the precision */
1216 spec->precision = -1;
1217 if (*fmt == '.') {
1218 ++fmt;
1219 if (isdigit(*fmt)) {
1220 spec->precision = skip_atoi(&fmt);
1221 if (spec->precision < 0)
1222 spec->precision = 0;
1223 } else if (*fmt == '*') {
1224 /* it's the next argument */
adf26f84 1225 spec->type = FORMAT_TYPE_PRECISION;
fef20d9c
FW
1226 return ++fmt - start;
1227 }
1228 }
1229
1230qualifier:
1231 /* get the conversion qualifier */
1232 spec->qualifier = -1;
75fb8f26
AS
1233 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1234 _tolower(*fmt) == 'z' || *fmt == 't') {
a4e94ef0
Z
1235 spec->qualifier = *fmt++;
1236 if (unlikely(spec->qualifier == *fmt)) {
1237 if (spec->qualifier == 'l') {
1238 spec->qualifier = 'L';
1239 ++fmt;
1240 } else if (spec->qualifier == 'h') {
1241 spec->qualifier = 'H';
1242 ++fmt;
1243 }
fef20d9c
FW
1244 }
1245 }
1246
1247 /* default base */
1248 spec->base = 10;
1249 switch (*fmt) {
1250 case 'c':
1251 spec->type = FORMAT_TYPE_CHAR;
1252 return ++fmt - start;
1253
1254 case 's':
1255 spec->type = FORMAT_TYPE_STR;
1256 return ++fmt - start;
1257
1258 case 'p':
1259 spec->type = FORMAT_TYPE_PTR;
1260 return fmt - start;
1261 /* skip alnum */
1262
1263 case 'n':
1264 spec->type = FORMAT_TYPE_NRCHARS;
1265 return ++fmt - start;
1266
1267 case '%':
1268 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1269 return ++fmt - start;
1270
1271 /* integer number formats - set up the flags and "break" */
1272 case 'o':
1273 spec->base = 8;
1274 break;
1275
1276 case 'x':
1277 spec->flags |= SMALL;
1278
1279 case 'X':
1280 spec->base = 16;
1281 break;
1282
1283 case 'd':
1284 case 'i':
39e874f8 1285 spec->flags |= SIGN;
fef20d9c 1286 case 'u':
4aa99606 1287 break;
fef20d9c
FW
1288
1289 default:
1290 spec->type = FORMAT_TYPE_INVALID;
1291 return fmt - start;
0fe1ef24 1292 }
fef20d9c
FW
1293
1294 if (spec->qualifier == 'L')
1295 spec->type = FORMAT_TYPE_LONG_LONG;
1296 else if (spec->qualifier == 'l') {
39e874f8 1297 if (spec->flags & SIGN)
fef20d9c
FW
1298 spec->type = FORMAT_TYPE_LONG;
1299 else
1300 spec->type = FORMAT_TYPE_ULONG;
75fb8f26 1301 } else if (_tolower(spec->qualifier) == 'z') {
fef20d9c
FW
1302 spec->type = FORMAT_TYPE_SIZE_T;
1303 } else if (spec->qualifier == 't') {
1304 spec->type = FORMAT_TYPE_PTRDIFF;
a4e94ef0
Z
1305 } else if (spec->qualifier == 'H') {
1306 if (spec->flags & SIGN)
1307 spec->type = FORMAT_TYPE_BYTE;
1308 else
1309 spec->type = FORMAT_TYPE_UBYTE;
fef20d9c 1310 } else if (spec->qualifier == 'h') {
39e874f8 1311 if (spec->flags & SIGN)
fef20d9c
FW
1312 spec->type = FORMAT_TYPE_SHORT;
1313 else
1314 spec->type = FORMAT_TYPE_USHORT;
1315 } else {
39e874f8 1316 if (spec->flags & SIGN)
fef20d9c
FW
1317 spec->type = FORMAT_TYPE_INT;
1318 else
1319 spec->type = FORMAT_TYPE_UINT;
78a8bf69 1320 }
fef20d9c
FW
1321
1322 return ++fmt - start;
78a8bf69
LT
1323}
1324
1da177e4
LT
1325/**
1326 * vsnprintf - Format a string and place it in a buffer
1327 * @buf: The buffer to place the result into
1328 * @size: The size of the buffer, including the trailing null space
1329 * @fmt: The format string to use
1330 * @args: Arguments for the format string
1331 *
20036fdc 1332 * This function follows C99 vsnprintf, but has some extensions:
91adcd2c
SR
1333 * %pS output the name of a text symbol with offset
1334 * %ps output the name of a text symbol without offset
0c8b946e
FW
1335 * %pF output the name of a function pointer with its offset
1336 * %pf output the name of a function pointer without its offset
0f77a8d3 1337 * %pB output the name of a backtrace symbol with its offset
8a79503a
UKK
1338 * %pR output the address range in a struct resource with decoded flags
1339 * %pr output the address range in a struct resource with raw flags
1340 * %pM output a 6-byte MAC address with colons
7c59154e
AS
1341 * %pMR output a 6-byte MAC address with colons in reversed order
1342 * %pMF output a 6-byte MAC address with dashes
8a79503a 1343 * %pm output a 6-byte MAC address without colons
7c59154e 1344 * %pmR output a 6-byte MAC address without colons in reversed order
8a79503a
UKK
1345 * %pI4 print an IPv4 address without leading zeros
1346 * %pi4 print an IPv4 address with leading zeros
1347 * %pI6 print an IPv6 address with colons
1348 * %pi6 print an IPv6 address without colons
f996f208 1349 * %pI6c print an IPv6 address as specified by RFC 5952
8a79503a
UKK
1350 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1351 * case.
31550a16
AS
1352 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1353 * bytes of the input)
0efb4d20 1354 * %n is ignored
20036fdc 1355 *
80f548e0
AM
1356 * ** Please update Documentation/printk-formats.txt when making changes **
1357 *
1da177e4
LT
1358 * The return value is the number of characters which would
1359 * be generated for the given input, excluding the trailing
1360 * '\0', as per ISO C99. If you want to have the exact
1361 * number of characters written into @buf as return value
72fd4a35 1362 * (not including the trailing '\0'), use vscnprintf(). If the
1da177e4
LT
1363 * return is greater than or equal to @size, the resulting
1364 * string is truncated.
1365 *
ba1835eb 1366 * If you're not already dealing with a va_list consider using snprintf().
1da177e4
LT
1367 */
1368int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1369{
1da177e4 1370 unsigned long long num;
d4be151b 1371 char *str, *end;
fef20d9c 1372 struct printf_spec spec = {0};
1da177e4 1373
f796937a
JF
1374 /* Reject out-of-range values early. Large positive sizes are
1375 used for unknown buffer sizes. */
2f30b1f9 1376 if (WARN_ON_ONCE((int) size < 0))
1da177e4 1377 return 0;
1da177e4
LT
1378
1379 str = buf;
f796937a 1380 end = buf + size;
1da177e4 1381
f796937a
JF
1382 /* Make sure end is always >= buf */
1383 if (end < buf) {
1384 end = ((void *)-1);
1385 size = end - buf;
1da177e4
LT
1386 }
1387
fef20d9c
FW
1388 while (*fmt) {
1389 const char *old_fmt = fmt;
d4be151b 1390 int read = format_decode(fmt, &spec);
1da177e4 1391
fef20d9c 1392 fmt += read;
1da177e4 1393
fef20d9c
FW
1394 switch (spec.type) {
1395 case FORMAT_TYPE_NONE: {
1396 int copy = read;
1397 if (str < end) {
1398 if (copy > end - str)
1399 copy = end - str;
1400 memcpy(str, old_fmt, copy);
1da177e4 1401 }
fef20d9c
FW
1402 str += read;
1403 break;
1da177e4
LT
1404 }
1405
ed681a91 1406 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1407 spec.field_width = va_arg(args, int);
1408 break;
1da177e4 1409
fef20d9c
FW
1410 case FORMAT_TYPE_PRECISION:
1411 spec.precision = va_arg(args, int);
1412 break;
1da177e4 1413
d4be151b
AGR
1414 case FORMAT_TYPE_CHAR: {
1415 char c;
1416
fef20d9c
FW
1417 if (!(spec.flags & LEFT)) {
1418 while (--spec.field_width > 0) {
f796937a 1419 if (str < end)
1da177e4
LT
1420 *str = ' ';
1421 ++str;
1da177e4 1422
fef20d9c
FW
1423 }
1424 }
1425 c = (unsigned char) va_arg(args, int);
1426 if (str < end)
1427 *str = c;
1428 ++str;
1429 while (--spec.field_width > 0) {
f796937a 1430 if (str < end)
fef20d9c 1431 *str = ' ';
1da177e4 1432 ++str;
fef20d9c
FW
1433 }
1434 break;
d4be151b 1435 }
1da177e4 1436
fef20d9c
FW
1437 case FORMAT_TYPE_STR:
1438 str = string(str, end, va_arg(args, char *), spec);
1439 break;
1da177e4 1440
fef20d9c
FW
1441 case FORMAT_TYPE_PTR:
1442 str = pointer(fmt+1, str, end, va_arg(args, void *),
1443 spec);
1444 while (isalnum(*fmt))
1445 fmt++;
1446 break;
1da177e4 1447
fef20d9c
FW
1448 case FORMAT_TYPE_PERCENT_CHAR:
1449 if (str < end)
1450 *str = '%';
1451 ++str;
1452 break;
1da177e4 1453
fef20d9c
FW
1454 case FORMAT_TYPE_INVALID:
1455 if (str < end)
1456 *str = '%';
1457 ++str;
fef20d9c
FW
1458 break;
1459
1460 case FORMAT_TYPE_NRCHARS: {
ef0658f3 1461 u8 qualifier = spec.qualifier;
fef20d9c
FW
1462
1463 if (qualifier == 'l') {
1464 long *ip = va_arg(args, long *);
1465 *ip = (str - buf);
75fb8f26 1466 } else if (_tolower(qualifier) == 'z') {
fef20d9c
FW
1467 size_t *ip = va_arg(args, size_t *);
1468 *ip = (str - buf);
1469 } else {
1470 int *ip = va_arg(args, int *);
1471 *ip = (str - buf);
1472 }
1473 break;
1da177e4 1474 }
fef20d9c
FW
1475
1476 default:
1477 switch (spec.type) {
1478 case FORMAT_TYPE_LONG_LONG:
1479 num = va_arg(args, long long);
1480 break;
1481 case FORMAT_TYPE_ULONG:
1482 num = va_arg(args, unsigned long);
1483 break;
1484 case FORMAT_TYPE_LONG:
1485 num = va_arg(args, long);
1486 break;
1487 case FORMAT_TYPE_SIZE_T:
1488 num = va_arg(args, size_t);
1489 break;
1490 case FORMAT_TYPE_PTRDIFF:
1491 num = va_arg(args, ptrdiff_t);
1492 break;
a4e94ef0
Z
1493 case FORMAT_TYPE_UBYTE:
1494 num = (unsigned char) va_arg(args, int);
1495 break;
1496 case FORMAT_TYPE_BYTE:
1497 num = (signed char) va_arg(args, int);
1498 break;
fef20d9c
FW
1499 case FORMAT_TYPE_USHORT:
1500 num = (unsigned short) va_arg(args, int);
1501 break;
1502 case FORMAT_TYPE_SHORT:
1503 num = (short) va_arg(args, int);
1504 break;
39e874f8
FW
1505 case FORMAT_TYPE_INT:
1506 num = (int) va_arg(args, int);
fef20d9c
FW
1507 break;
1508 default:
1509 num = va_arg(args, unsigned int);
1510 }
1511
1512 str = number(str, end, num, spec);
1da177e4 1513 }
1da177e4 1514 }
fef20d9c 1515
f796937a
JF
1516 if (size > 0) {
1517 if (str < end)
1518 *str = '\0';
1519 else
0a6047ee 1520 end[-1] = '\0';
f796937a 1521 }
fef20d9c 1522
f796937a 1523 /* the trailing null byte doesn't count towards the total */
1da177e4 1524 return str-buf;
fef20d9c 1525
1da177e4 1526}
1da177e4
LT
1527EXPORT_SYMBOL(vsnprintf);
1528
1529/**
1530 * vscnprintf - Format a string and place it in a buffer
1531 * @buf: The buffer to place the result into
1532 * @size: The size of the buffer, including the trailing null space
1533 * @fmt: The format string to use
1534 * @args: Arguments for the format string
1535 *
1536 * The return value is the number of characters which have been written into
b921c69f 1537 * the @buf not including the trailing '\0'. If @size is == 0 the function
1da177e4
LT
1538 * returns 0.
1539 *
ba1835eb 1540 * If you're not already dealing with a va_list consider using scnprintf().
20036fdc
AK
1541 *
1542 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1543 */
1544int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1545{
1546 int i;
1547
7b9186f5
AGR
1548 i = vsnprintf(buf, size, fmt, args);
1549
b921c69f
AA
1550 if (likely(i < size))
1551 return i;
1552 if (size != 0)
1553 return size - 1;
1554 return 0;
1da177e4 1555}
1da177e4
LT
1556EXPORT_SYMBOL(vscnprintf);
1557
1558/**
1559 * snprintf - Format a string and place it in a buffer
1560 * @buf: The buffer to place the result into
1561 * @size: The size of the buffer, including the trailing null space
1562 * @fmt: The format string to use
1563 * @...: Arguments for the format string
1564 *
1565 * The return value is the number of characters which would be
1566 * generated for the given input, excluding the trailing null,
1567 * as per ISO C99. If the return is greater than or equal to
1568 * @size, the resulting string is truncated.
20036fdc
AK
1569 *
1570 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4 1571 */
7b9186f5 1572int snprintf(char *buf, size_t size, const char *fmt, ...)
1da177e4
LT
1573{
1574 va_list args;
1575 int i;
1576
1577 va_start(args, fmt);
7b9186f5 1578 i = vsnprintf(buf, size, fmt, args);
1da177e4 1579 va_end(args);
7b9186f5 1580
1da177e4
LT
1581 return i;
1582}
1da177e4
LT
1583EXPORT_SYMBOL(snprintf);
1584
1585/**
1586 * scnprintf - Format a string and place it in a buffer
1587 * @buf: The buffer to place the result into
1588 * @size: The size of the buffer, including the trailing null space
1589 * @fmt: The format string to use
1590 * @...: Arguments for the format string
1591 *
1592 * The return value is the number of characters written into @buf not including
b903c0b8 1593 * the trailing '\0'. If @size is == 0 the function returns 0.
1da177e4
LT
1594 */
1595
7b9186f5 1596int scnprintf(char *buf, size_t size, const char *fmt, ...)
1da177e4
LT
1597{
1598 va_list args;
1599 int i;
1600
1601 va_start(args, fmt);
b921c69f 1602 i = vscnprintf(buf, size, fmt, args);
1da177e4 1603 va_end(args);
7b9186f5 1604
b921c69f 1605 return i;
1da177e4
LT
1606}
1607EXPORT_SYMBOL(scnprintf);
1608
1609/**
1610 * vsprintf - Format a string and place it in a buffer
1611 * @buf: The buffer to place the result into
1612 * @fmt: The format string to use
1613 * @args: Arguments for the format string
1614 *
1615 * The function returns the number of characters written
72fd4a35 1616 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1da177e4
LT
1617 * buffer overflows.
1618 *
ba1835eb 1619 * If you're not already dealing with a va_list consider using sprintf().
20036fdc
AK
1620 *
1621 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1622 */
1623int vsprintf(char *buf, const char *fmt, va_list args)
1624{
1625 return vsnprintf(buf, INT_MAX, fmt, args);
1626}
1da177e4
LT
1627EXPORT_SYMBOL(vsprintf);
1628
1629/**
1630 * sprintf - Format a string and place it in a buffer
1631 * @buf: The buffer to place the result into
1632 * @fmt: The format string to use
1633 * @...: Arguments for the format string
1634 *
1635 * The function returns the number of characters written
72fd4a35 1636 * into @buf. Use snprintf() or scnprintf() in order to avoid
1da177e4 1637 * buffer overflows.
20036fdc
AK
1638 *
1639 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4 1640 */
7b9186f5 1641int sprintf(char *buf, const char *fmt, ...)
1da177e4
LT
1642{
1643 va_list args;
1644 int i;
1645
1646 va_start(args, fmt);
7b9186f5 1647 i = vsnprintf(buf, INT_MAX, fmt, args);
1da177e4 1648 va_end(args);
7b9186f5 1649
1da177e4
LT
1650 return i;
1651}
1da177e4
LT
1652EXPORT_SYMBOL(sprintf);
1653
4370aa4a
LJ
1654#ifdef CONFIG_BINARY_PRINTF
1655/*
1656 * bprintf service:
1657 * vbin_printf() - VA arguments to binary data
1658 * bstr_printf() - Binary data to text string
1659 */
1660
1661/**
1662 * vbin_printf - Parse a format string and place args' binary value in a buffer
1663 * @bin_buf: The buffer to place args' binary value
1664 * @size: The size of the buffer(by words(32bits), not characters)
1665 * @fmt: The format string to use
1666 * @args: Arguments for the format string
1667 *
1668 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1669 * is skiped.
1670 *
1671 * The return value is the number of words(32bits) which would be generated for
1672 * the given input.
1673 *
1674 * NOTE:
1675 * If the return value is greater than @size, the resulting bin_buf is NOT
1676 * valid for bstr_printf().
1677 */
1678int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1679{
fef20d9c 1680 struct printf_spec spec = {0};
4370aa4a 1681 char *str, *end;
4370aa4a
LJ
1682
1683 str = (char *)bin_buf;
1684 end = (char *)(bin_buf + size);
1685
1686#define save_arg(type) \
1687do { \
1688 if (sizeof(type) == 8) { \
1689 unsigned long long value; \
1690 str = PTR_ALIGN(str, sizeof(u32)); \
1691 value = va_arg(args, unsigned long long); \
1692 if (str + sizeof(type) <= end) { \
1693 *(u32 *)str = *(u32 *)&value; \
1694 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1695 } \
1696 } else { \
1697 unsigned long value; \
1698 str = PTR_ALIGN(str, sizeof(type)); \
1699 value = va_arg(args, int); \
1700 if (str + sizeof(type) <= end) \
1701 *(typeof(type) *)str = (type)value; \
1702 } \
1703 str += sizeof(type); \
1704} while (0)
1705
fef20d9c 1706 while (*fmt) {
d4be151b 1707 int read = format_decode(fmt, &spec);
4370aa4a 1708
fef20d9c 1709 fmt += read;
4370aa4a 1710
fef20d9c
FW
1711 switch (spec.type) {
1712 case FORMAT_TYPE_NONE:
d4be151b
AGR
1713 case FORMAT_TYPE_INVALID:
1714 case FORMAT_TYPE_PERCENT_CHAR:
fef20d9c
FW
1715 break;
1716
ed681a91 1717 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1718 case FORMAT_TYPE_PRECISION:
1719 save_arg(int);
1720 break;
1721
1722 case FORMAT_TYPE_CHAR:
4370aa4a 1723 save_arg(char);
fef20d9c
FW
1724 break;
1725
1726 case FORMAT_TYPE_STR: {
4370aa4a
LJ
1727 const char *save_str = va_arg(args, char *);
1728 size_t len;
6c356634 1729
4370aa4a
LJ
1730 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1731 || (unsigned long)save_str < PAGE_SIZE)
0f4f81dc 1732 save_str = "(null)";
6c356634
AGR
1733 len = strlen(save_str) + 1;
1734 if (str + len < end)
1735 memcpy(str, save_str, len);
1736 str += len;
fef20d9c 1737 break;
4370aa4a 1738 }
fef20d9c
FW
1739
1740 case FORMAT_TYPE_PTR:
4370aa4a
LJ
1741 save_arg(void *);
1742 /* skip all alphanumeric pointer suffixes */
fef20d9c 1743 while (isalnum(*fmt))
4370aa4a 1744 fmt++;
fef20d9c
FW
1745 break;
1746
fef20d9c 1747 case FORMAT_TYPE_NRCHARS: {
4370aa4a 1748 /* skip %n 's argument */
ef0658f3 1749 u8 qualifier = spec.qualifier;
4370aa4a
LJ
1750 void *skip_arg;
1751 if (qualifier == 'l')
1752 skip_arg = va_arg(args, long *);
75fb8f26 1753 else if (_tolower(qualifier) == 'z')
4370aa4a
LJ
1754 skip_arg = va_arg(args, size_t *);
1755 else
1756 skip_arg = va_arg(args, int *);
fef20d9c 1757 break;
4370aa4a 1758 }
fef20d9c
FW
1759
1760 default:
1761 switch (spec.type) {
1762
1763 case FORMAT_TYPE_LONG_LONG:
4370aa4a 1764 save_arg(long long);
fef20d9c
FW
1765 break;
1766 case FORMAT_TYPE_ULONG:
1767 case FORMAT_TYPE_LONG:
4370aa4a 1768 save_arg(unsigned long);
fef20d9c
FW
1769 break;
1770 case FORMAT_TYPE_SIZE_T:
4370aa4a 1771 save_arg(size_t);
fef20d9c
FW
1772 break;
1773 case FORMAT_TYPE_PTRDIFF:
4370aa4a 1774 save_arg(ptrdiff_t);
fef20d9c 1775 break;
a4e94ef0
Z
1776 case FORMAT_TYPE_UBYTE:
1777 case FORMAT_TYPE_BYTE:
1778 save_arg(char);
1779 break;
fef20d9c
FW
1780 case FORMAT_TYPE_USHORT:
1781 case FORMAT_TYPE_SHORT:
4370aa4a 1782 save_arg(short);
fef20d9c
FW
1783 break;
1784 default:
4370aa4a 1785 save_arg(int);
fef20d9c 1786 }
4370aa4a
LJ
1787 }
1788 }
fef20d9c 1789
7b9186f5 1790 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
fef20d9c 1791#undef save_arg
4370aa4a
LJ
1792}
1793EXPORT_SYMBOL_GPL(vbin_printf);
1794
1795/**
1796 * bstr_printf - Format a string from binary arguments and place it in a buffer
1797 * @buf: The buffer to place the result into
1798 * @size: The size of the buffer, including the trailing null space
1799 * @fmt: The format string to use
1800 * @bin_buf: Binary arguments for the format string
1801 *
1802 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1803 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1804 * a binary buffer that generated by vbin_printf.
1805 *
1806 * The format follows C99 vsnprintf, but has some extensions:
0efb4d20 1807 * see vsnprintf comment for details.
4370aa4a
LJ
1808 *
1809 * The return value is the number of characters which would
1810 * be generated for the given input, excluding the trailing
1811 * '\0', as per ISO C99. If you want to have the exact
1812 * number of characters written into @buf as return value
1813 * (not including the trailing '\0'), use vscnprintf(). If the
1814 * return is greater than or equal to @size, the resulting
1815 * string is truncated.
1816 */
1817int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1818{
fef20d9c 1819 struct printf_spec spec = {0};
d4be151b
AGR
1820 char *str, *end;
1821 const char *args = (const char *)bin_buf;
4370aa4a 1822
2f30b1f9 1823 if (WARN_ON_ONCE((int) size < 0))
4370aa4a 1824 return 0;
4370aa4a
LJ
1825
1826 str = buf;
1827 end = buf + size;
1828
1829#define get_arg(type) \
1830({ \
1831 typeof(type) value; \
1832 if (sizeof(type) == 8) { \
1833 args = PTR_ALIGN(args, sizeof(u32)); \
1834 *(u32 *)&value = *(u32 *)args; \
1835 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1836 } else { \
1837 args = PTR_ALIGN(args, sizeof(type)); \
1838 value = *(typeof(type) *)args; \
1839 } \
1840 args += sizeof(type); \
1841 value; \
1842})
1843
1844 /* Make sure end is always >= buf */
1845 if (end < buf) {
1846 end = ((void *)-1);
1847 size = end - buf;
1848 }
1849
fef20d9c 1850 while (*fmt) {
fef20d9c 1851 const char *old_fmt = fmt;
d4be151b 1852 int read = format_decode(fmt, &spec);
4370aa4a 1853
fef20d9c 1854 fmt += read;
4370aa4a 1855
fef20d9c
FW
1856 switch (spec.type) {
1857 case FORMAT_TYPE_NONE: {
1858 int copy = read;
1859 if (str < end) {
1860 if (copy > end - str)
1861 copy = end - str;
1862 memcpy(str, old_fmt, copy);
4370aa4a 1863 }
fef20d9c
FW
1864 str += read;
1865 break;
4370aa4a
LJ
1866 }
1867
ed681a91 1868 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1869 spec.field_width = get_arg(int);
1870 break;
4370aa4a 1871
fef20d9c
FW
1872 case FORMAT_TYPE_PRECISION:
1873 spec.precision = get_arg(int);
1874 break;
4370aa4a 1875
d4be151b
AGR
1876 case FORMAT_TYPE_CHAR: {
1877 char c;
1878
fef20d9c
FW
1879 if (!(spec.flags & LEFT)) {
1880 while (--spec.field_width > 0) {
4370aa4a
LJ
1881 if (str < end)
1882 *str = ' ';
1883 ++str;
1884 }
1885 }
1886 c = (unsigned char) get_arg(char);
1887 if (str < end)
1888 *str = c;
1889 ++str;
fef20d9c 1890 while (--spec.field_width > 0) {
4370aa4a
LJ
1891 if (str < end)
1892 *str = ' ';
1893 ++str;
1894 }
fef20d9c 1895 break;
d4be151b 1896 }
4370aa4a 1897
fef20d9c 1898 case FORMAT_TYPE_STR: {
4370aa4a 1899 const char *str_arg = args;
d4be151b 1900 args += strlen(str_arg) + 1;
fef20d9c
FW
1901 str = string(str, end, (char *)str_arg, spec);
1902 break;
4370aa4a
LJ
1903 }
1904
fef20d9c
FW
1905 case FORMAT_TYPE_PTR:
1906 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1907 while (isalnum(*fmt))
4370aa4a 1908 fmt++;
fef20d9c 1909 break;
4370aa4a 1910
fef20d9c 1911 case FORMAT_TYPE_PERCENT_CHAR:
fef20d9c 1912 case FORMAT_TYPE_INVALID:
4370aa4a
LJ
1913 if (str < end)
1914 *str = '%';
1915 ++str;
fef20d9c
FW
1916 break;
1917
1918 case FORMAT_TYPE_NRCHARS:
1919 /* skip */
1920 break;
1921
d4be151b
AGR
1922 default: {
1923 unsigned long long num;
1924
fef20d9c
FW
1925 switch (spec.type) {
1926
1927 case FORMAT_TYPE_LONG_LONG:
1928 num = get_arg(long long);
1929 break;
1930 case FORMAT_TYPE_ULONG:
fef20d9c
FW
1931 case FORMAT_TYPE_LONG:
1932 num = get_arg(unsigned long);
1933 break;
1934 case FORMAT_TYPE_SIZE_T:
1935 num = get_arg(size_t);
1936 break;
1937 case FORMAT_TYPE_PTRDIFF:
1938 num = get_arg(ptrdiff_t);
1939 break;
a4e94ef0
Z
1940 case FORMAT_TYPE_UBYTE:
1941 num = get_arg(unsigned char);
1942 break;
1943 case FORMAT_TYPE_BYTE:
1944 num = get_arg(signed char);
1945 break;
fef20d9c
FW
1946 case FORMAT_TYPE_USHORT:
1947 num = get_arg(unsigned short);
1948 break;
1949 case FORMAT_TYPE_SHORT:
1950 num = get_arg(short);
1951 break;
1952 case FORMAT_TYPE_UINT:
1953 num = get_arg(unsigned int);
1954 break;
1955 default:
1956 num = get_arg(int);
1957 }
1958
1959 str = number(str, end, num, spec);
d4be151b
AGR
1960 } /* default: */
1961 } /* switch(spec.type) */
1962 } /* while(*fmt) */
fef20d9c 1963
4370aa4a
LJ
1964 if (size > 0) {
1965 if (str < end)
1966 *str = '\0';
1967 else
1968 end[-1] = '\0';
1969 }
fef20d9c 1970
4370aa4a
LJ
1971#undef get_arg
1972
1973 /* the trailing null byte doesn't count towards the total */
1974 return str - buf;
1975}
1976EXPORT_SYMBOL_GPL(bstr_printf);
1977
1978/**
1979 * bprintf - Parse a format string and place args' binary value in a buffer
1980 * @bin_buf: The buffer to place args' binary value
1981 * @size: The size of the buffer(by words(32bits), not characters)
1982 * @fmt: The format string to use
1983 * @...: Arguments for the format string
1984 *
1985 * The function returns the number of words(u32) written
1986 * into @bin_buf.
1987 */
1988int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1989{
1990 va_list args;
1991 int ret;
1992
1993 va_start(args, fmt);
1994 ret = vbin_printf(bin_buf, size, fmt, args);
1995 va_end(args);
7b9186f5 1996
4370aa4a
LJ
1997 return ret;
1998}
1999EXPORT_SYMBOL_GPL(bprintf);
2000
2001#endif /* CONFIG_BINARY_PRINTF */
2002
1da177e4
LT
2003/**
2004 * vsscanf - Unformat a buffer into a list of arguments
2005 * @buf: input buffer
2006 * @fmt: format of buffer
2007 * @args: arguments
2008 */
7b9186f5 2009int vsscanf(const char *buf, const char *fmt, va_list args)
1da177e4
LT
2010{
2011 const char *str = buf;
2012 char *next;
2013 char digit;
2014 int num = 0;
ef0658f3
JP
2015 u8 qualifier;
2016 u8 base;
2017 s16 field_width;
d4be151b 2018 bool is_sign;
1da177e4 2019
7b9186f5 2020 while (*fmt && *str) {
1da177e4
LT
2021 /* skip any white space in format */
2022 /* white space in format matchs any amount of
2023 * white space, including none, in the input.
2024 */
2025 if (isspace(*fmt)) {
e7d2860b
AGR
2026 fmt = skip_spaces(++fmt);
2027 str = skip_spaces(str);
1da177e4
LT
2028 }
2029
2030 /* anything that is not a conversion must match exactly */
2031 if (*fmt != '%' && *fmt) {
2032 if (*fmt++ != *str++)
2033 break;
2034 continue;
2035 }
2036
2037 if (!*fmt)
2038 break;
2039 ++fmt;
7b9186f5 2040
1da177e4
LT
2041 /* skip this conversion.
2042 * advance both strings to next white space
2043 */
2044 if (*fmt == '*') {
8fccae2c 2045 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1da177e4
LT
2046 fmt++;
2047 while (!isspace(*str) && *str)
2048 str++;
2049 continue;
2050 }
2051
2052 /* get field width */
2053 field_width = -1;
2054 if (isdigit(*fmt))
2055 field_width = skip_atoi(&fmt);
2056
2057 /* get conversion qualifier */
2058 qualifier = -1;
75fb8f26
AS
2059 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2060 _tolower(*fmt) == 'z') {
1da177e4
LT
2061 qualifier = *fmt++;
2062 if (unlikely(qualifier == *fmt)) {
2063 if (qualifier == 'h') {
2064 qualifier = 'H';
2065 fmt++;
2066 } else if (qualifier == 'l') {
2067 qualifier = 'L';
2068 fmt++;
2069 }
2070 }
2071 }
1da177e4
LT
2072
2073 if (!*fmt || !*str)
2074 break;
2075
d4be151b
AGR
2076 base = 10;
2077 is_sign = 0;
2078
7b9186f5 2079 switch (*fmt++) {
1da177e4
LT
2080 case 'c':
2081 {
7b9186f5 2082 char *s = (char *)va_arg(args, char*);
1da177e4
LT
2083 if (field_width == -1)
2084 field_width = 1;
2085 do {
2086 *s++ = *str++;
2087 } while (--field_width > 0 && *str);
2088 num++;
2089 }
2090 continue;
2091 case 's':
2092 {
7b9186f5
AGR
2093 char *s = (char *)va_arg(args, char *);
2094 if (field_width == -1)
4be929be 2095 field_width = SHRT_MAX;
1da177e4 2096 /* first, skip leading white space in buffer */
e7d2860b 2097 str = skip_spaces(str);
1da177e4
LT
2098
2099 /* now copy until next white space */
7b9186f5 2100 while (*str && !isspace(*str) && field_width--)
1da177e4 2101 *s++ = *str++;
1da177e4
LT
2102 *s = '\0';
2103 num++;
2104 }
2105 continue;
2106 case 'n':
2107 /* return number of characters read so far */
2108 {
7b9186f5 2109 int *i = (int *)va_arg(args, int*);
1da177e4
LT
2110 *i = str - buf;
2111 }
2112 continue;
2113 case 'o':
2114 base = 8;
2115 break;
2116 case 'x':
2117 case 'X':
2118 base = 16;
2119 break;
2120 case 'i':
7b9186f5 2121 base = 0;
1da177e4
LT
2122 case 'd':
2123 is_sign = 1;
2124 case 'u':
2125 break;
2126 case '%':
2127 /* looking for '%' in str */
7b9186f5 2128 if (*str++ != '%')
1da177e4
LT
2129 return num;
2130 continue;
2131 default:
2132 /* invalid format; stop here */
2133 return num;
2134 }
2135
2136 /* have some sort of integer conversion.
2137 * first, skip white space in buffer.
2138 */
e7d2860b 2139 str = skip_spaces(str);
1da177e4
LT
2140
2141 digit = *str;
2142 if (is_sign && digit == '-')
2143 digit = *(str + 1);
2144
2145 if (!digit
7b9186f5
AGR
2146 || (base == 16 && !isxdigit(digit))
2147 || (base == 10 && !isdigit(digit))
2148 || (base == 8 && (!isdigit(digit) || digit > '7'))
2149 || (base == 0 && !isdigit(digit)))
2150 break;
1da177e4 2151
7b9186f5 2152 switch (qualifier) {
1da177e4
LT
2153 case 'H': /* that's 'hh' in format */
2154 if (is_sign) {
7b9186f5
AGR
2155 signed char *s = (signed char *)va_arg(args, signed char *);
2156 *s = (signed char)simple_strtol(str, &next, base);
1da177e4 2157 } else {
7b9186f5
AGR
2158 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2159 *s = (unsigned char)simple_strtoul(str, &next, base);
1da177e4
LT
2160 }
2161 break;
2162 case 'h':
2163 if (is_sign) {
7b9186f5
AGR
2164 short *s = (short *)va_arg(args, short *);
2165 *s = (short)simple_strtol(str, &next, base);
1da177e4 2166 } else {
7b9186f5
AGR
2167 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2168 *s = (unsigned short)simple_strtoul(str, &next, base);
1da177e4
LT
2169 }
2170 break;
2171 case 'l':
2172 if (is_sign) {
7b9186f5
AGR
2173 long *l = (long *)va_arg(args, long *);
2174 *l = simple_strtol(str, &next, base);
1da177e4 2175 } else {
7b9186f5
AGR
2176 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2177 *l = simple_strtoul(str, &next, base);
1da177e4
LT
2178 }
2179 break;
2180 case 'L':
2181 if (is_sign) {
7b9186f5
AGR
2182 long long *l = (long long *)va_arg(args, long long *);
2183 *l = simple_strtoll(str, &next, base);
1da177e4 2184 } else {
7b9186f5
AGR
2185 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2186 *l = simple_strtoull(str, &next, base);
1da177e4
LT
2187 }
2188 break;
2189 case 'Z':
2190 case 'z':
2191 {
7b9186f5
AGR
2192 size_t *s = (size_t *)va_arg(args, size_t *);
2193 *s = (size_t)simple_strtoul(str, &next, base);
1da177e4
LT
2194 }
2195 break;
2196 default:
2197 if (is_sign) {
7b9186f5
AGR
2198 int *i = (int *)va_arg(args, int *);
2199 *i = (int)simple_strtol(str, &next, base);
1da177e4 2200 } else {
7b9186f5
AGR
2201 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2202 *i = (unsigned int)simple_strtoul(str, &next, base);
1da177e4
LT
2203 }
2204 break;
2205 }
2206 num++;
2207
2208 if (!next)
2209 break;
2210 str = next;
2211 }
c6b40d16
JB
2212
2213 /*
2214 * Now we've come all the way through so either the input string or the
2215 * format ended. In the former case, there can be a %n at the current
2216 * position in the format that needs to be filled.
2217 */
2218 if (*fmt == '%' && *(fmt + 1) == 'n') {
2219 int *p = (int *)va_arg(args, int *);
2220 *p = str - buf;
2221 }
2222
1da177e4
LT
2223 return num;
2224}
1da177e4
LT
2225EXPORT_SYMBOL(vsscanf);
2226
2227/**
2228 * sscanf - Unformat a buffer into a list of arguments
2229 * @buf: input buffer
2230 * @fmt: formatting of buffer
2231 * @...: resulting arguments
2232 */
7b9186f5 2233int sscanf(const char *buf, const char *fmt, ...)
1da177e4
LT
2234{
2235 va_list args;
2236 int i;
2237
7b9186f5
AGR
2238 va_start(args, fmt);
2239 i = vsscanf(buf, fmt, args);
1da177e4 2240 va_end(args);
7b9186f5 2241
1da177e4
LT
2242 return i;
2243}
1da177e4 2244EXPORT_SYMBOL(sscanf);