]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/vsprintf.c
drivers/of/fdt.c: re-use kernel's kbasename()
[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:
ef124960
JG
1488 if (spec.flags & SIGN)
1489 num = va_arg(args, ssize_t);
1490 else
1491 num = va_arg(args, size_t);
fef20d9c
FW
1492 break;
1493 case FORMAT_TYPE_PTRDIFF:
1494 num = va_arg(args, ptrdiff_t);
1495 break;
a4e94ef0
Z
1496 case FORMAT_TYPE_UBYTE:
1497 num = (unsigned char) va_arg(args, int);
1498 break;
1499 case FORMAT_TYPE_BYTE:
1500 num = (signed char) va_arg(args, int);
1501 break;
fef20d9c
FW
1502 case FORMAT_TYPE_USHORT:
1503 num = (unsigned short) va_arg(args, int);
1504 break;
1505 case FORMAT_TYPE_SHORT:
1506 num = (short) va_arg(args, int);
1507 break;
39e874f8
FW
1508 case FORMAT_TYPE_INT:
1509 num = (int) va_arg(args, int);
fef20d9c
FW
1510 break;
1511 default:
1512 num = va_arg(args, unsigned int);
1513 }
1514
1515 str = number(str, end, num, spec);
1da177e4 1516 }
1da177e4 1517 }
fef20d9c 1518
f796937a
JF
1519 if (size > 0) {
1520 if (str < end)
1521 *str = '\0';
1522 else
0a6047ee 1523 end[-1] = '\0';
f796937a 1524 }
fef20d9c 1525
f796937a 1526 /* the trailing null byte doesn't count towards the total */
1da177e4 1527 return str-buf;
fef20d9c 1528
1da177e4 1529}
1da177e4
LT
1530EXPORT_SYMBOL(vsnprintf);
1531
1532/**
1533 * vscnprintf - Format a string and place it in a buffer
1534 * @buf: The buffer to place the result into
1535 * @size: The size of the buffer, including the trailing null space
1536 * @fmt: The format string to use
1537 * @args: Arguments for the format string
1538 *
1539 * The return value is the number of characters which have been written into
b921c69f 1540 * the @buf not including the trailing '\0'. If @size is == 0 the function
1da177e4
LT
1541 * returns 0.
1542 *
ba1835eb 1543 * If you're not already dealing with a va_list consider using scnprintf().
20036fdc
AK
1544 *
1545 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1546 */
1547int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1548{
1549 int i;
1550
7b9186f5
AGR
1551 i = vsnprintf(buf, size, fmt, args);
1552
b921c69f
AA
1553 if (likely(i < size))
1554 return i;
1555 if (size != 0)
1556 return size - 1;
1557 return 0;
1da177e4 1558}
1da177e4
LT
1559EXPORT_SYMBOL(vscnprintf);
1560
1561/**
1562 * snprintf - Format a string and place it in a buffer
1563 * @buf: The buffer to place the result into
1564 * @size: The size of the buffer, including the trailing null space
1565 * @fmt: The format string to use
1566 * @...: Arguments for the format string
1567 *
1568 * The return value is the number of characters which would be
1569 * generated for the given input, excluding the trailing null,
1570 * as per ISO C99. If the return is greater than or equal to
1571 * @size, the resulting string is truncated.
20036fdc
AK
1572 *
1573 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4 1574 */
7b9186f5 1575int snprintf(char *buf, size_t size, const char *fmt, ...)
1da177e4
LT
1576{
1577 va_list args;
1578 int i;
1579
1580 va_start(args, fmt);
7b9186f5 1581 i = vsnprintf(buf, size, fmt, args);
1da177e4 1582 va_end(args);
7b9186f5 1583
1da177e4
LT
1584 return i;
1585}
1da177e4
LT
1586EXPORT_SYMBOL(snprintf);
1587
1588/**
1589 * scnprintf - Format a string and place it in a buffer
1590 * @buf: The buffer to place the result into
1591 * @size: The size of the buffer, including the trailing null space
1592 * @fmt: The format string to use
1593 * @...: Arguments for the format string
1594 *
1595 * The return value is the number of characters written into @buf not including
b903c0b8 1596 * the trailing '\0'. If @size is == 0 the function returns 0.
1da177e4
LT
1597 */
1598
7b9186f5 1599int scnprintf(char *buf, size_t size, const char *fmt, ...)
1da177e4
LT
1600{
1601 va_list args;
1602 int i;
1603
1604 va_start(args, fmt);
b921c69f 1605 i = vscnprintf(buf, size, fmt, args);
1da177e4 1606 va_end(args);
7b9186f5 1607
b921c69f 1608 return i;
1da177e4
LT
1609}
1610EXPORT_SYMBOL(scnprintf);
1611
1612/**
1613 * vsprintf - Format a string and place it in a buffer
1614 * @buf: The buffer to place the result into
1615 * @fmt: The format string to use
1616 * @args: Arguments for the format string
1617 *
1618 * The function returns the number of characters written
72fd4a35 1619 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1da177e4
LT
1620 * buffer overflows.
1621 *
ba1835eb 1622 * If you're not already dealing with a va_list consider using sprintf().
20036fdc
AK
1623 *
1624 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1625 */
1626int vsprintf(char *buf, const char *fmt, va_list args)
1627{
1628 return vsnprintf(buf, INT_MAX, fmt, args);
1629}
1da177e4
LT
1630EXPORT_SYMBOL(vsprintf);
1631
1632/**
1633 * sprintf - Format a string and place it in a buffer
1634 * @buf: The buffer to place the result into
1635 * @fmt: The format string to use
1636 * @...: Arguments for the format string
1637 *
1638 * The function returns the number of characters written
72fd4a35 1639 * into @buf. Use snprintf() or scnprintf() in order to avoid
1da177e4 1640 * buffer overflows.
20036fdc
AK
1641 *
1642 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4 1643 */
7b9186f5 1644int sprintf(char *buf, const char *fmt, ...)
1da177e4
LT
1645{
1646 va_list args;
1647 int i;
1648
1649 va_start(args, fmt);
7b9186f5 1650 i = vsnprintf(buf, INT_MAX, fmt, args);
1da177e4 1651 va_end(args);
7b9186f5 1652
1da177e4
LT
1653 return i;
1654}
1da177e4
LT
1655EXPORT_SYMBOL(sprintf);
1656
4370aa4a
LJ
1657#ifdef CONFIG_BINARY_PRINTF
1658/*
1659 * bprintf service:
1660 * vbin_printf() - VA arguments to binary data
1661 * bstr_printf() - Binary data to text string
1662 */
1663
1664/**
1665 * vbin_printf - Parse a format string and place args' binary value in a buffer
1666 * @bin_buf: The buffer to place args' binary value
1667 * @size: The size of the buffer(by words(32bits), not characters)
1668 * @fmt: The format string to use
1669 * @args: Arguments for the format string
1670 *
1671 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1672 * is skiped.
1673 *
1674 * The return value is the number of words(32bits) which would be generated for
1675 * the given input.
1676 *
1677 * NOTE:
1678 * If the return value is greater than @size, the resulting bin_buf is NOT
1679 * valid for bstr_printf().
1680 */
1681int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1682{
fef20d9c 1683 struct printf_spec spec = {0};
4370aa4a 1684 char *str, *end;
4370aa4a
LJ
1685
1686 str = (char *)bin_buf;
1687 end = (char *)(bin_buf + size);
1688
1689#define save_arg(type) \
1690do { \
1691 if (sizeof(type) == 8) { \
1692 unsigned long long value; \
1693 str = PTR_ALIGN(str, sizeof(u32)); \
1694 value = va_arg(args, unsigned long long); \
1695 if (str + sizeof(type) <= end) { \
1696 *(u32 *)str = *(u32 *)&value; \
1697 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1698 } \
1699 } else { \
1700 unsigned long value; \
1701 str = PTR_ALIGN(str, sizeof(type)); \
1702 value = va_arg(args, int); \
1703 if (str + sizeof(type) <= end) \
1704 *(typeof(type) *)str = (type)value; \
1705 } \
1706 str += sizeof(type); \
1707} while (0)
1708
fef20d9c 1709 while (*fmt) {
d4be151b 1710 int read = format_decode(fmt, &spec);
4370aa4a 1711
fef20d9c 1712 fmt += read;
4370aa4a 1713
fef20d9c
FW
1714 switch (spec.type) {
1715 case FORMAT_TYPE_NONE:
d4be151b
AGR
1716 case FORMAT_TYPE_INVALID:
1717 case FORMAT_TYPE_PERCENT_CHAR:
fef20d9c
FW
1718 break;
1719
ed681a91 1720 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1721 case FORMAT_TYPE_PRECISION:
1722 save_arg(int);
1723 break;
1724
1725 case FORMAT_TYPE_CHAR:
4370aa4a 1726 save_arg(char);
fef20d9c
FW
1727 break;
1728
1729 case FORMAT_TYPE_STR: {
4370aa4a
LJ
1730 const char *save_str = va_arg(args, char *);
1731 size_t len;
6c356634 1732
4370aa4a
LJ
1733 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1734 || (unsigned long)save_str < PAGE_SIZE)
0f4f81dc 1735 save_str = "(null)";
6c356634
AGR
1736 len = strlen(save_str) + 1;
1737 if (str + len < end)
1738 memcpy(str, save_str, len);
1739 str += len;
fef20d9c 1740 break;
4370aa4a 1741 }
fef20d9c
FW
1742
1743 case FORMAT_TYPE_PTR:
4370aa4a
LJ
1744 save_arg(void *);
1745 /* skip all alphanumeric pointer suffixes */
fef20d9c 1746 while (isalnum(*fmt))
4370aa4a 1747 fmt++;
fef20d9c
FW
1748 break;
1749
fef20d9c 1750 case FORMAT_TYPE_NRCHARS: {
4370aa4a 1751 /* skip %n 's argument */
ef0658f3 1752 u8 qualifier = spec.qualifier;
4370aa4a
LJ
1753 void *skip_arg;
1754 if (qualifier == 'l')
1755 skip_arg = va_arg(args, long *);
75fb8f26 1756 else if (_tolower(qualifier) == 'z')
4370aa4a
LJ
1757 skip_arg = va_arg(args, size_t *);
1758 else
1759 skip_arg = va_arg(args, int *);
fef20d9c 1760 break;
4370aa4a 1761 }
fef20d9c
FW
1762
1763 default:
1764 switch (spec.type) {
1765
1766 case FORMAT_TYPE_LONG_LONG:
4370aa4a 1767 save_arg(long long);
fef20d9c
FW
1768 break;
1769 case FORMAT_TYPE_ULONG:
1770 case FORMAT_TYPE_LONG:
4370aa4a 1771 save_arg(unsigned long);
fef20d9c
FW
1772 break;
1773 case FORMAT_TYPE_SIZE_T:
4370aa4a 1774 save_arg(size_t);
fef20d9c
FW
1775 break;
1776 case FORMAT_TYPE_PTRDIFF:
4370aa4a 1777 save_arg(ptrdiff_t);
fef20d9c 1778 break;
a4e94ef0
Z
1779 case FORMAT_TYPE_UBYTE:
1780 case FORMAT_TYPE_BYTE:
1781 save_arg(char);
1782 break;
fef20d9c
FW
1783 case FORMAT_TYPE_USHORT:
1784 case FORMAT_TYPE_SHORT:
4370aa4a 1785 save_arg(short);
fef20d9c
FW
1786 break;
1787 default:
4370aa4a 1788 save_arg(int);
fef20d9c 1789 }
4370aa4a
LJ
1790 }
1791 }
fef20d9c 1792
7b9186f5 1793 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
fef20d9c 1794#undef save_arg
4370aa4a
LJ
1795}
1796EXPORT_SYMBOL_GPL(vbin_printf);
1797
1798/**
1799 * bstr_printf - Format a string from binary arguments and place it in a buffer
1800 * @buf: The buffer to place the result into
1801 * @size: The size of the buffer, including the trailing null space
1802 * @fmt: The format string to use
1803 * @bin_buf: Binary arguments for the format string
1804 *
1805 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1806 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1807 * a binary buffer that generated by vbin_printf.
1808 *
1809 * The format follows C99 vsnprintf, but has some extensions:
0efb4d20 1810 * see vsnprintf comment for details.
4370aa4a
LJ
1811 *
1812 * The return value is the number of characters which would
1813 * be generated for the given input, excluding the trailing
1814 * '\0', as per ISO C99. If you want to have the exact
1815 * number of characters written into @buf as return value
1816 * (not including the trailing '\0'), use vscnprintf(). If the
1817 * return is greater than or equal to @size, the resulting
1818 * string is truncated.
1819 */
1820int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1821{
fef20d9c 1822 struct printf_spec spec = {0};
d4be151b
AGR
1823 char *str, *end;
1824 const char *args = (const char *)bin_buf;
4370aa4a 1825
2f30b1f9 1826 if (WARN_ON_ONCE((int) size < 0))
4370aa4a 1827 return 0;
4370aa4a
LJ
1828
1829 str = buf;
1830 end = buf + size;
1831
1832#define get_arg(type) \
1833({ \
1834 typeof(type) value; \
1835 if (sizeof(type) == 8) { \
1836 args = PTR_ALIGN(args, sizeof(u32)); \
1837 *(u32 *)&value = *(u32 *)args; \
1838 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1839 } else { \
1840 args = PTR_ALIGN(args, sizeof(type)); \
1841 value = *(typeof(type) *)args; \
1842 } \
1843 args += sizeof(type); \
1844 value; \
1845})
1846
1847 /* Make sure end is always >= buf */
1848 if (end < buf) {
1849 end = ((void *)-1);
1850 size = end - buf;
1851 }
1852
fef20d9c 1853 while (*fmt) {
fef20d9c 1854 const char *old_fmt = fmt;
d4be151b 1855 int read = format_decode(fmt, &spec);
4370aa4a 1856
fef20d9c 1857 fmt += read;
4370aa4a 1858
fef20d9c
FW
1859 switch (spec.type) {
1860 case FORMAT_TYPE_NONE: {
1861 int copy = read;
1862 if (str < end) {
1863 if (copy > end - str)
1864 copy = end - str;
1865 memcpy(str, old_fmt, copy);
4370aa4a 1866 }
fef20d9c
FW
1867 str += read;
1868 break;
4370aa4a
LJ
1869 }
1870
ed681a91 1871 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1872 spec.field_width = get_arg(int);
1873 break;
4370aa4a 1874
fef20d9c
FW
1875 case FORMAT_TYPE_PRECISION:
1876 spec.precision = get_arg(int);
1877 break;
4370aa4a 1878
d4be151b
AGR
1879 case FORMAT_TYPE_CHAR: {
1880 char c;
1881
fef20d9c
FW
1882 if (!(spec.flags & LEFT)) {
1883 while (--spec.field_width > 0) {
4370aa4a
LJ
1884 if (str < end)
1885 *str = ' ';
1886 ++str;
1887 }
1888 }
1889 c = (unsigned char) get_arg(char);
1890 if (str < end)
1891 *str = c;
1892 ++str;
fef20d9c 1893 while (--spec.field_width > 0) {
4370aa4a
LJ
1894 if (str < end)
1895 *str = ' ';
1896 ++str;
1897 }
fef20d9c 1898 break;
d4be151b 1899 }
4370aa4a 1900
fef20d9c 1901 case FORMAT_TYPE_STR: {
4370aa4a 1902 const char *str_arg = args;
d4be151b 1903 args += strlen(str_arg) + 1;
fef20d9c
FW
1904 str = string(str, end, (char *)str_arg, spec);
1905 break;
4370aa4a
LJ
1906 }
1907
fef20d9c
FW
1908 case FORMAT_TYPE_PTR:
1909 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1910 while (isalnum(*fmt))
4370aa4a 1911 fmt++;
fef20d9c 1912 break;
4370aa4a 1913
fef20d9c 1914 case FORMAT_TYPE_PERCENT_CHAR:
fef20d9c 1915 case FORMAT_TYPE_INVALID:
4370aa4a
LJ
1916 if (str < end)
1917 *str = '%';
1918 ++str;
fef20d9c
FW
1919 break;
1920
1921 case FORMAT_TYPE_NRCHARS:
1922 /* skip */
1923 break;
1924
d4be151b
AGR
1925 default: {
1926 unsigned long long num;
1927
fef20d9c
FW
1928 switch (spec.type) {
1929
1930 case FORMAT_TYPE_LONG_LONG:
1931 num = get_arg(long long);
1932 break;
1933 case FORMAT_TYPE_ULONG:
fef20d9c
FW
1934 case FORMAT_TYPE_LONG:
1935 num = get_arg(unsigned long);
1936 break;
1937 case FORMAT_TYPE_SIZE_T:
1938 num = get_arg(size_t);
1939 break;
1940 case FORMAT_TYPE_PTRDIFF:
1941 num = get_arg(ptrdiff_t);
1942 break;
a4e94ef0
Z
1943 case FORMAT_TYPE_UBYTE:
1944 num = get_arg(unsigned char);
1945 break;
1946 case FORMAT_TYPE_BYTE:
1947 num = get_arg(signed char);
1948 break;
fef20d9c
FW
1949 case FORMAT_TYPE_USHORT:
1950 num = get_arg(unsigned short);
1951 break;
1952 case FORMAT_TYPE_SHORT:
1953 num = get_arg(short);
1954 break;
1955 case FORMAT_TYPE_UINT:
1956 num = get_arg(unsigned int);
1957 break;
1958 default:
1959 num = get_arg(int);
1960 }
1961
1962 str = number(str, end, num, spec);
d4be151b
AGR
1963 } /* default: */
1964 } /* switch(spec.type) */
1965 } /* while(*fmt) */
fef20d9c 1966
4370aa4a
LJ
1967 if (size > 0) {
1968 if (str < end)
1969 *str = '\0';
1970 else
1971 end[-1] = '\0';
1972 }
fef20d9c 1973
4370aa4a
LJ
1974#undef get_arg
1975
1976 /* the trailing null byte doesn't count towards the total */
1977 return str - buf;
1978}
1979EXPORT_SYMBOL_GPL(bstr_printf);
1980
1981/**
1982 * bprintf - Parse a format string and place args' binary value in a buffer
1983 * @bin_buf: The buffer to place args' binary value
1984 * @size: The size of the buffer(by words(32bits), not characters)
1985 * @fmt: The format string to use
1986 * @...: Arguments for the format string
1987 *
1988 * The function returns the number of words(u32) written
1989 * into @bin_buf.
1990 */
1991int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1992{
1993 va_list args;
1994 int ret;
1995
1996 va_start(args, fmt);
1997 ret = vbin_printf(bin_buf, size, fmt, args);
1998 va_end(args);
7b9186f5 1999
4370aa4a
LJ
2000 return ret;
2001}
2002EXPORT_SYMBOL_GPL(bprintf);
2003
2004#endif /* CONFIG_BINARY_PRINTF */
2005
1da177e4
LT
2006/**
2007 * vsscanf - Unformat a buffer into a list of arguments
2008 * @buf: input buffer
2009 * @fmt: format of buffer
2010 * @args: arguments
2011 */
7b9186f5 2012int vsscanf(const char *buf, const char *fmt, va_list args)
1da177e4
LT
2013{
2014 const char *str = buf;
2015 char *next;
2016 char digit;
2017 int num = 0;
ef0658f3
JP
2018 u8 qualifier;
2019 u8 base;
2020 s16 field_width;
d4be151b 2021 bool is_sign;
1da177e4 2022
da99075c 2023 while (*fmt) {
1da177e4
LT
2024 /* skip any white space in format */
2025 /* white space in format matchs any amount of
2026 * white space, including none, in the input.
2027 */
2028 if (isspace(*fmt)) {
e7d2860b
AGR
2029 fmt = skip_spaces(++fmt);
2030 str = skip_spaces(str);
1da177e4
LT
2031 }
2032
2033 /* anything that is not a conversion must match exactly */
2034 if (*fmt != '%' && *fmt) {
2035 if (*fmt++ != *str++)
2036 break;
2037 continue;
2038 }
2039
2040 if (!*fmt)
2041 break;
2042 ++fmt;
7b9186f5 2043
1da177e4
LT
2044 /* skip this conversion.
2045 * advance both strings to next white space
2046 */
2047 if (*fmt == '*') {
da99075c
JB
2048 if (!*str)
2049 break;
8fccae2c 2050 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1da177e4
LT
2051 fmt++;
2052 while (!isspace(*str) && *str)
2053 str++;
2054 continue;
2055 }
2056
2057 /* get field width */
2058 field_width = -1;
2059 if (isdigit(*fmt))
2060 field_width = skip_atoi(&fmt);
2061
2062 /* get conversion qualifier */
2063 qualifier = -1;
75fb8f26
AS
2064 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2065 _tolower(*fmt) == 'z') {
1da177e4
LT
2066 qualifier = *fmt++;
2067 if (unlikely(qualifier == *fmt)) {
2068 if (qualifier == 'h') {
2069 qualifier = 'H';
2070 fmt++;
2071 } else if (qualifier == 'l') {
2072 qualifier = 'L';
2073 fmt++;
2074 }
2075 }
2076 }
1da177e4 2077
da99075c
JB
2078 if (!*fmt)
2079 break;
2080
2081 if (*fmt == 'n') {
2082 /* return number of characters read so far */
2083 *va_arg(args, int *) = str - buf;
2084 ++fmt;
2085 continue;
2086 }
2087
2088 if (!*str)
1da177e4
LT
2089 break;
2090
d4be151b
AGR
2091 base = 10;
2092 is_sign = 0;
2093
7b9186f5 2094 switch (*fmt++) {
1da177e4
LT
2095 case 'c':
2096 {
7b9186f5 2097 char *s = (char *)va_arg(args, char*);
1da177e4
LT
2098 if (field_width == -1)
2099 field_width = 1;
2100 do {
2101 *s++ = *str++;
2102 } while (--field_width > 0 && *str);
2103 num++;
2104 }
2105 continue;
2106 case 's':
2107 {
7b9186f5
AGR
2108 char *s = (char *)va_arg(args, char *);
2109 if (field_width == -1)
4be929be 2110 field_width = SHRT_MAX;
1da177e4 2111 /* first, skip leading white space in buffer */
e7d2860b 2112 str = skip_spaces(str);
1da177e4
LT
2113
2114 /* now copy until next white space */
7b9186f5 2115 while (*str && !isspace(*str) && field_width--)
1da177e4 2116 *s++ = *str++;
1da177e4
LT
2117 *s = '\0';
2118 num++;
2119 }
2120 continue;
1da177e4
LT
2121 case 'o':
2122 base = 8;
2123 break;
2124 case 'x':
2125 case 'X':
2126 base = 16;
2127 break;
2128 case 'i':
7b9186f5 2129 base = 0;
1da177e4
LT
2130 case 'd':
2131 is_sign = 1;
2132 case 'u':
2133 break;
2134 case '%':
2135 /* looking for '%' in str */
7b9186f5 2136 if (*str++ != '%')
1da177e4
LT
2137 return num;
2138 continue;
2139 default:
2140 /* invalid format; stop here */
2141 return num;
2142 }
2143
2144 /* have some sort of integer conversion.
2145 * first, skip white space in buffer.
2146 */
e7d2860b 2147 str = skip_spaces(str);
1da177e4
LT
2148
2149 digit = *str;
2150 if (is_sign && digit == '-')
2151 digit = *(str + 1);
2152
2153 if (!digit
7b9186f5
AGR
2154 || (base == 16 && !isxdigit(digit))
2155 || (base == 10 && !isdigit(digit))
2156 || (base == 8 && (!isdigit(digit) || digit > '7'))
2157 || (base == 0 && !isdigit(digit)))
2158 break;
1da177e4 2159
7b9186f5 2160 switch (qualifier) {
1da177e4
LT
2161 case 'H': /* that's 'hh' in format */
2162 if (is_sign) {
7b9186f5
AGR
2163 signed char *s = (signed char *)va_arg(args, signed char *);
2164 *s = (signed char)simple_strtol(str, &next, base);
1da177e4 2165 } else {
7b9186f5
AGR
2166 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2167 *s = (unsigned char)simple_strtoul(str, &next, base);
1da177e4
LT
2168 }
2169 break;
2170 case 'h':
2171 if (is_sign) {
7b9186f5
AGR
2172 short *s = (short *)va_arg(args, short *);
2173 *s = (short)simple_strtol(str, &next, base);
1da177e4 2174 } else {
7b9186f5
AGR
2175 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2176 *s = (unsigned short)simple_strtoul(str, &next, base);
1da177e4
LT
2177 }
2178 break;
2179 case 'l':
2180 if (is_sign) {
7b9186f5
AGR
2181 long *l = (long *)va_arg(args, long *);
2182 *l = simple_strtol(str, &next, base);
1da177e4 2183 } else {
7b9186f5
AGR
2184 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2185 *l = simple_strtoul(str, &next, base);
1da177e4
LT
2186 }
2187 break;
2188 case 'L':
2189 if (is_sign) {
7b9186f5
AGR
2190 long long *l = (long long *)va_arg(args, long long *);
2191 *l = simple_strtoll(str, &next, base);
1da177e4 2192 } else {
7b9186f5
AGR
2193 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2194 *l = simple_strtoull(str, &next, base);
1da177e4
LT
2195 }
2196 break;
2197 case 'Z':
2198 case 'z':
2199 {
7b9186f5
AGR
2200 size_t *s = (size_t *)va_arg(args, size_t *);
2201 *s = (size_t)simple_strtoul(str, &next, base);
1da177e4
LT
2202 }
2203 break;
2204 default:
2205 if (is_sign) {
7b9186f5
AGR
2206 int *i = (int *)va_arg(args, int *);
2207 *i = (int)simple_strtol(str, &next, base);
1da177e4 2208 } else {
7b9186f5
AGR
2209 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2210 *i = (unsigned int)simple_strtoul(str, &next, base);
1da177e4
LT
2211 }
2212 break;
2213 }
2214 num++;
2215
2216 if (!next)
2217 break;
2218 str = next;
2219 }
c6b40d16 2220
1da177e4
LT
2221 return num;
2222}
1da177e4
LT
2223EXPORT_SYMBOL(vsscanf);
2224
2225/**
2226 * sscanf - Unformat a buffer into a list of arguments
2227 * @buf: input buffer
2228 * @fmt: formatting of buffer
2229 * @...: resulting arguments
2230 */
7b9186f5 2231int sscanf(const char *buf, const char *fmt, ...)
1da177e4
LT
2232{
2233 va_list args;
2234 int i;
2235
7b9186f5
AGR
2236 va_start(args, fmt);
2237 i = vsscanf(buf, fmt, args);
1da177e4 2238 va_end(args);
7b9186f5 2239
1da177e4
LT
2240 return i;
2241}
1da177e4 2242EXPORT_SYMBOL(sscanf);