blob: 3a1e0843f9a2b31e599a5ef9d33ebd0e81c5a208 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070020#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050021#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/types.h>
23#include <linux/string.h>
24#include <linux/ctype.h>
25#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070026#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080027#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070028#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110029#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040030#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080031#include <linux/cred.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000032#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Tim Schmielau4e57b682005-10-30 15:03:48 -080034#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050035#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Andy Shevchenko71dca952014-10-13 15:55:18 -070037#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070038#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * simple_strtoull - convert a string to an unsigned long long
42 * @cp: The start of the string
43 * @endp: A pointer to the end of the parsed string will be placed here
44 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080045 *
46 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Harvey Harrison22d27052008-10-16 13:40:35 -070048unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070050 unsigned long long result;
51 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070053 cp = _parse_integer_fixup_radix(cp, &base);
54 rv = _parse_integer(cp, base, &result);
55 /* FIXME */
56 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (endp)
59 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return result;
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063EXPORT_SYMBOL(simple_strtoull);
64
65/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080066 * simple_strtoul - convert a string to an unsigned long
67 * @cp: The start of the string
68 * @endp: A pointer to the end of the parsed string will be placed here
69 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080070 *
71 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080072 */
73unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
74{
75 return simple_strtoull(cp, endp, base);
76}
77EXPORT_SYMBOL(simple_strtoul);
78
79/**
80 * simple_strtol - convert a string to a signed long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080084 *
85 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080086 */
87long simple_strtol(const char *cp, char **endp, unsigned int base)
88{
89 if (*cp == '-')
90 return -simple_strtoul(cp + 1, endp, base);
91
92 return simple_strtoul(cp, endp, base);
93}
94EXPORT_SYMBOL(simple_strtol);
95
96/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * simple_strtoll - convert a string to a signed long long
98 * @cp: The start of the string
99 * @endp: A pointer to the end of the parsed string will be placed here
100 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800101 *
102 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700104long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800106 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700107 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800108
Harvey Harrison22d27052008-10-16 13:40:35 -0700109 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400111EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Joe Perchescf3b4292010-05-24 14:33:16 -0700113static noinline_for_stack
114int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800116 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800118 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800120 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return i;
123}
124
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700125/* Decimal conversion is by far the most typical, and is used
126 * for /proc and /sys data. This directly impacts e.g. top performance
127 * with many processes running. We optimize it for speed
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700128 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
129 * (with permission from the author, Douglas W. Jones).
130 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700131
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700132#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
133/* Formats correctly any integer in [0, 999999999] */
Joe Perchescf3b4292010-05-24 14:33:16 -0700134static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700135char *put_dec_full9(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700136{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700137 unsigned r;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700138
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800139 /*
140 * Possible ways to approx. divide by 10
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700141 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
142 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
143 * (x * 0x6667) >> 18 x < 43699
144 * (x * 0x3334) >> 17 x < 16389
145 * (x * 0x199a) >> 16 x < 16389
146 * (x * 0x0ccd) >> 15 x < 16389
147 * (x * 0x0667) >> 14 x < 2739
148 * (x * 0x0334) >> 13 x < 1029
149 * (x * 0x019a) >> 12 x < 1029
150 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
151 * (x * 0x0067) >> 10 x < 179
152 * (x * 0x0034) >> 9 x < 69 same
153 * (x * 0x001a) >> 8 x < 69 same
154 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
155 * (x * 0x0007) >> 6 x < 19
156 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800157 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700158 r = (q * (uint64_t)0x1999999a) >> 32;
159 *buf++ = (q - 10 * r) + '0'; /* 1 */
160 q = (r * (uint64_t)0x1999999a) >> 32;
161 *buf++ = (r - 10 * q) + '0'; /* 2 */
162 r = (q * (uint64_t)0x1999999a) >> 32;
163 *buf++ = (q - 10 * r) + '0'; /* 3 */
164 q = (r * (uint64_t)0x1999999a) >> 32;
165 *buf++ = (r - 10 * q) + '0'; /* 4 */
166 r = (q * (uint64_t)0x1999999a) >> 32;
167 *buf++ = (q - 10 * r) + '0'; /* 5 */
168 /* Now value is under 10000, can avoid 64-bit multiply */
169 q = (r * 0x199a) >> 16;
170 *buf++ = (r - 10 * q) + '0'; /* 6 */
171 r = (q * 0xcd) >> 11;
172 *buf++ = (q - 10 * r) + '0'; /* 7 */
173 q = (r * 0xcd) >> 11;
174 *buf++ = (r - 10 * q) + '0'; /* 8 */
175 *buf++ = q + '0'; /* 9 */
176 return buf;
177}
178#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700179
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700180/* Similar to above but do not pad with zeros.
181 * Code can be easily arranged to print 9 digits too, but our callers
182 * always call put_dec_full9() instead when the number has 9 decimal digits.
183 */
184static noinline_for_stack
185char *put_dec_trunc8(char *buf, unsigned r)
186{
187 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700188
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700189 /* Copy of previous function's body with added early returns */
George Spelvincb239d02012-10-04 17:12:30 -0700190 while (r >= 10000) {
191 q = r + '0';
192 r = (r * (uint64_t)0x1999999a) >> 32;
193 *buf++ = q - 10*r;
194 }
195
George Spelvinf4000512012-10-04 17:12:32 -0700196 q = (r * 0x199a) >> 16; /* r <= 9999 */
197 *buf++ = (r - 10 * q) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700198 if (q == 0)
199 return buf;
George Spelvinf4000512012-10-04 17:12:32 -0700200 r = (q * 0xcd) >> 11; /* q <= 999 */
201 *buf++ = (q - 10 * r) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700202 if (r == 0)
203 return buf;
George Spelvinf4000512012-10-04 17:12:32 -0700204 q = (r * 0xcd) >> 11; /* r <= 99 */
205 *buf++ = (r - 10 * q) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700206 if (q == 0)
207 return buf;
George Spelvinf4000512012-10-04 17:12:32 -0700208 *buf++ = q + '0'; /* q <= 9 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700209 return buf;
210}
211
212/* There are two algorithms to print larger numbers.
213 * One is generic: divide by 1000000000 and repeatedly print
214 * groups of (up to) 9 digits. It's conceptually simple,
215 * but requires a (unsigned long long) / 1000000000 division.
216 *
217 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
218 * manipulates them cleverly and generates groups of 4 decimal digits.
219 * It so happens that it does NOT require long long division.
220 *
221 * If long is > 32 bits, division of 64-bit values is relatively easy,
222 * and we will use the first algorithm.
223 * If long long is > 64 bits (strange architecture with VERY large long long),
224 * second algorithm can't be used, and we again use the first one.
225 *
226 * Else (if long is 32 bits and long long is 64 bits) we use second one.
227 */
228
229#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
230
231/* First algorithm: generic */
232
233static
234char *put_dec(char *buf, unsigned long long n)
235{
236 if (n >= 100*1000*1000) {
237 while (n >= 1000*1000*1000)
238 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
239 if (n >= 100*1000*1000)
240 return put_dec_full9(buf, n);
241 }
242 return put_dec_trunc8(buf, n);
243}
244
245#else
246
247/* Second algorithm: valid only for 64-bit long longs */
248
George Spelvine49317d2012-10-04 17:12:27 -0700249/* See comment in put_dec_full9 for choice of constants */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700250static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700251void put_dec_full4(char *buf, unsigned q)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700252{
253 unsigned r;
George Spelvine49317d2012-10-04 17:12:27 -0700254 r = (q * 0xccd) >> 15;
George Spelvin23591722012-10-04 17:12:29 -0700255 buf[0] = (q - 10 * r) + '0';
George Spelvine49317d2012-10-04 17:12:27 -0700256 q = (r * 0xcd) >> 11;
George Spelvin23591722012-10-04 17:12:29 -0700257 buf[1] = (r - 10 * q) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700258 r = (q * 0xcd) >> 11;
George Spelvin23591722012-10-04 17:12:29 -0700259 buf[2] = (q - 10 * r) + '0';
260 buf[3] = r + '0';
261}
262
263/*
264 * Call put_dec_full4 on x % 10000, return x / 10000.
265 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
266 * holds for all x < 1,128,869,999. The largest value this
267 * helper will ever be asked to convert is 1,125,520,955.
268 * (d1 in the put_dec code, assuming n is all-ones).
269 */
270static
271unsigned put_dec_helper4(char *buf, unsigned x)
272{
273 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
274
275 put_dec_full4(buf, x - q * 10000);
276 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700277}
278
279/* Based on code by Douglas W. Jones found at
280 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
281 * (with permission from the author).
282 * Performs no 64-bit division and hence should be fast on 32-bit machines.
283 */
284static
285char *put_dec(char *buf, unsigned long long n)
286{
287 uint32_t d3, d2, d1, q, h;
288
289 if (n < 100*1000*1000)
290 return put_dec_trunc8(buf, n);
291
292 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
293 h = (n >> 32);
294 d2 = (h ) & 0xffff;
295 d3 = (h >> 16); /* implicit "& 0xffff" */
296
297 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700298 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700299
George Spelvin23591722012-10-04 17:12:29 -0700300 q += 7671 * d3 + 9496 * d2 + 6 * d1;
301 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700302
George Spelvin23591722012-10-04 17:12:29 -0700303 q += 4749 * d3 + 42 * d2;
304 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700305
George Spelvin23591722012-10-04 17:12:29 -0700306 q += 281 * d3;
307 buf += 12;
308 if (q)
309 buf = put_dec_trunc8(buf, q);
310 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700311 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800312
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700313 return buf;
314}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700315
316#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700317
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700318/*
319 * Convert passed number to decimal string.
320 * Returns the length of string. On buffer overflow, returns 0.
321 *
322 * If speed is not important, use snprintf(). It's easy to read the code.
323 */
324int num_to_str(char *buf, int size, unsigned long long num)
325{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700326 char tmp[sizeof(num) * 3];
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700327 int idx, len;
328
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700329 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
330 if (num <= 9) {
331 tmp[0] = '0' + num;
332 len = 1;
333 } else {
334 len = put_dec(tmp, num) - tmp;
335 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700336
337 if (len > size)
338 return 0;
339 for (idx = 0; idx < len; ++idx)
340 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700341 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700342}
343
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700344#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700345#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#define PLUS 4 /* show plus */
347#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700348#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700349#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
350#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100352enum format_type {
353 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100354 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100355 FORMAT_TYPE_PRECISION,
356 FORMAT_TYPE_CHAR,
357 FORMAT_TYPE_STR,
358 FORMAT_TYPE_PTR,
359 FORMAT_TYPE_PERCENT_CHAR,
360 FORMAT_TYPE_INVALID,
361 FORMAT_TYPE_LONG_LONG,
362 FORMAT_TYPE_ULONG,
363 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800364 FORMAT_TYPE_UBYTE,
365 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100366 FORMAT_TYPE_USHORT,
367 FORMAT_TYPE_SHORT,
368 FORMAT_TYPE_UINT,
369 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100370 FORMAT_TYPE_SIZE_T,
371 FORMAT_TYPE_PTRDIFF
372};
373
374struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700375 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800376 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700377 u8 base; /* number base, 8, 10 or 16 only */
378 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
379 s16 field_width; /* width of output field */
380 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100381};
382
Joe Perchescf3b4292010-05-24 14:33:16 -0700383static noinline_for_stack
384char *number(char *buf, char *end, unsigned long long num,
385 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Rasmus Villemoese26c12c2015-04-15 16:17:05 -0700387 char tmp[3 * sizeof(num)];
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100388 char sign;
389 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100390 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700392 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100394 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
395 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100396 locase = (spec.flags & SMALL);
397 if (spec.flags & LEFT)
398 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100400 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800401 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800403 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100404 spec.field_width--;
405 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100407 spec.field_width--;
408 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100410 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700413 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100414 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700415 spec.field_width -= 2;
416 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100417 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700419
420 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700422 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700423 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100424 else if (spec.base != 10) { /* 8 or 16 */
425 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700426 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800427
428 if (spec.base == 16)
429 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700430 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700431 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700432 num >>= shift;
433 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700434 } else { /* base 10 */
435 i = put_dec(tmp, num) - tmp;
436 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700437
438 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100439 if (i > spec.precision)
440 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700441 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100442 spec.field_width -= spec.precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700443 if (!(spec.flags & (ZEROPAD | LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800444 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700445 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *buf = ' ';
447 ++buf;
448 }
449 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700452 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *buf = sign;
454 ++buf;
455 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700456 /* "0x" / "0" prefix */
457 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700458 if (spec.base == 16 || !is_zero) {
459 if (buf < end)
460 *buf = '0';
461 ++buf;
462 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100463 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700464 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100465 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ++buf;
467 }
468 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700469 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100470 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700471 char c = ' ' + (spec.flags & ZEROPAD);
472 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100473 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700474 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *buf = c;
476 ++buf;
477 }
478 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100480 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700481 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *buf = '0';
483 ++buf;
484 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700485 /* actual digits of result */
486 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700487 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *buf = tmp[i];
489 ++buf;
490 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700491 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100492 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700493 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *buf = ' ';
495 ++buf;
496 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return buf;
499}
500
Joe Perchescf3b4292010-05-24 14:33:16 -0700501static noinline_for_stack
502char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700503{
504 int len, i;
505
506 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800507 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700508
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100509 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700510
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100511 if (!(spec.flags & LEFT)) {
512 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700513 if (buf < end)
514 *buf = ' ';
515 ++buf;
516 }
517 }
518 for (i = 0; i < len; ++i) {
519 if (buf < end)
520 *buf = *s;
521 ++buf; ++s;
522 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100523 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700524 if (buf < end)
525 *buf = ' ';
526 ++buf;
527 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800528
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700529 return buf;
530}
531
Al Viro4b6ccca2013-09-03 12:00:44 -0400532static void widen(char *buf, char *end, unsigned len, unsigned spaces)
533{
534 size_t size;
535 if (buf >= end) /* nowhere to put anything */
536 return;
537 size = end - buf;
538 if (size <= spaces) {
539 memset(buf, ' ', size);
540 return;
541 }
542 if (len) {
543 if (len > size - spaces)
544 len = size - spaces;
545 memmove(buf + spaces, buf, len);
546 }
547 memset(buf, ' ', spaces);
548}
549
550static noinline_for_stack
551char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
552 const char *fmt)
553{
554 const char *array[4], *s;
555 const struct dentry *p;
556 int depth;
557 int i, n;
558
559 switch (fmt[1]) {
560 case '2': case '3': case '4':
561 depth = fmt[1] - '0';
562 break;
563 default:
564 depth = 1;
565 }
566
567 rcu_read_lock();
568 for (i = 0; i < depth; i++, d = p) {
569 p = ACCESS_ONCE(d->d_parent);
570 array[i] = ACCESS_ONCE(d->d_name.name);
571 if (p == d) {
572 if (i)
573 array[i] = "";
574 i++;
575 break;
576 }
577 }
578 s = array[--i];
579 for (n = 0; n != spec.precision; n++, buf++) {
580 char c = *s++;
581 if (!c) {
582 if (!i)
583 break;
584 c = '/';
585 s = array[--i];
586 }
587 if (buf < end)
588 *buf = c;
589 }
590 rcu_read_unlock();
591 if (n < spec.field_width) {
592 /* we want to pad the sucker */
593 unsigned spaces = spec.field_width - n;
594 if (!(spec.flags & LEFT)) {
595 widen(buf - n, end, n, spaces);
596 return buf + spaces;
597 }
598 while (spaces--) {
599 if (buf < end)
600 *buf = ' ';
601 ++buf;
602 }
603 }
604 return buf;
605}
606
Joe Perchescf3b4292010-05-24 14:33:16 -0700607static noinline_for_stack
608char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800609 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700610{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800611 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700612#ifdef CONFIG_KALLSYMS
613 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800614#endif
615
616 if (fmt[1] == 'R')
617 ptr = __builtin_extract_return_addr(ptr);
618 value = (unsigned long)ptr;
619
620#ifdef CONFIG_KALLSYMS
621 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900622 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800623 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200624 sprint_symbol(sym, value);
625 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700626 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800627
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100628 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700629#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800630 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100631 spec.flags |= SPECIAL | SMALL | ZEROPAD;
632 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800633
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100634 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700635#endif
636}
637
Joe Perchescf3b4292010-05-24 14:33:16 -0700638static noinline_for_stack
639char *resource_string(char *buf, char *end, struct resource *res,
640 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100641{
642#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600643#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100644#endif
645
646#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600647#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100648#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700649 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100650 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700651 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100652 .precision = -1,
653 .flags = SPECIAL | SMALL | ZEROPAD,
654 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700655 static const struct printf_spec mem_spec = {
656 .base = 16,
657 .field_width = MEM_RSRC_PRINTK_SIZE,
658 .precision = -1,
659 .flags = SPECIAL | SMALL | ZEROPAD,
660 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700661 static const struct printf_spec bus_spec = {
662 .base = 16,
663 .field_width = 2,
664 .precision = -1,
665 .flags = SMALL | ZEROPAD,
666 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700667 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600668 .base = 10,
669 .precision = -1,
670 .flags = 0,
671 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700672 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600673 .field_width = -1,
674 .precision = 10,
675 .flags = LEFT,
676 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700677 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600678 .base = 16,
679 .precision = -1,
680 .flags = SPECIAL | SMALL,
681 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600682
683 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
684 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
685#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
686#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700687#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600688#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
689 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
690 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
691
Linus Torvalds332d2e72008-10-20 15:07:34 +1100692 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600693 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700694 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100695
696 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700697 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600698 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700699 specp = &io_spec;
700 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600701 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700702 specp = &mem_spec;
703 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600704 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700705 specp = &dec_spec;
706 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600707 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700708 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700709 } else if (res->flags & IORESOURCE_BUS) {
710 p = string(p, pend, "bus ", str_spec);
711 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700712 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600713 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700714 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600715 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600716 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700717 if (decode && res->flags & IORESOURCE_UNSET) {
718 p = string(p, pend, "size ", str_spec);
719 p = number(p, pend, resource_size(res), *specp);
720 } else {
721 p = number(p, pend, res->start, *specp);
722 if (res->start != res->end) {
723 *p++ = '-';
724 p = number(p, pend, res->end, *specp);
725 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600726 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600727 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600728 if (res->flags & IORESOURCE_MEM_64)
729 p = string(p, pend, " 64bit", str_spec);
730 if (res->flags & IORESOURCE_PREFETCH)
731 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700732 if (res->flags & IORESOURCE_WINDOW)
733 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600734 if (res->flags & IORESOURCE_DISABLED)
735 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600736 } else {
737 p = string(p, pend, " flags ", str_spec);
738 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600739 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100740 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600741 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100742
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100743 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100744}
745
Joe Perchescf3b4292010-05-24 14:33:16 -0700746static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700747char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
748 const char *fmt)
749{
Steven Rostedt360603a2013-05-28 15:47:39 -0400750 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700751 negative value, fallback to the default */
752 char separator;
753
754 if (spec.field_width == 0)
755 /* nothing to print */
756 return buf;
757
758 if (ZERO_OR_NULL_PTR(addr))
759 /* NULL pointer */
760 return string(buf, end, NULL, spec);
761
762 switch (fmt[1]) {
763 case 'C':
764 separator = ':';
765 break;
766 case 'D':
767 separator = '-';
768 break;
769 case 'N':
770 separator = 0;
771 break;
772 default:
773 separator = ' ';
774 break;
775 }
776
777 if (spec.field_width > 0)
778 len = min_t(int, spec.field_width, 64);
779
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700780 for (i = 0; i < len; ++i) {
781 if (buf < end)
782 *buf = hex_asc_hi(addr[i]);
783 ++buf;
784 if (buf < end)
785 *buf = hex_asc_lo(addr[i]);
786 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700787
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700788 if (separator && i != len - 1) {
789 if (buf < end)
790 *buf = separator;
791 ++buf;
792 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700793 }
794
795 return buf;
796}
797
798static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800799char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
800 struct printf_spec spec, const char *fmt)
801{
802 const int CHUNKSZ = 32;
803 int nr_bits = max_t(int, spec.field_width, 0);
804 int i, chunksz;
805 bool first = true;
806
807 /* reused to print numbers */
808 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
809
810 chunksz = nr_bits & (CHUNKSZ - 1);
811 if (chunksz == 0)
812 chunksz = CHUNKSZ;
813
814 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
815 for (; i >= 0; i -= CHUNKSZ) {
816 u32 chunkmask, val;
817 int word, bit;
818
819 chunkmask = ((1ULL << chunksz) - 1);
820 word = i / BITS_PER_LONG;
821 bit = i % BITS_PER_LONG;
822 val = (bitmap[word] >> bit) & chunkmask;
823
824 if (!first) {
825 if (buf < end)
826 *buf = ',';
827 buf++;
828 }
829 first = false;
830
831 spec.field_width = DIV_ROUND_UP(chunksz, 4);
832 buf = number(buf, end, val, spec);
833
834 chunksz = CHUNKSZ;
835 }
836 return buf;
837}
838
839static noinline_for_stack
840char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
841 struct printf_spec spec, const char *fmt)
842{
843 int nr_bits = max_t(int, spec.field_width, 0);
844 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
845 int cur, rbot, rtop;
846 bool first = true;
847
848 /* reused to print numbers */
849 spec = (struct printf_spec){ .base = 10 };
850
851 rbot = cur = find_first_bit(bitmap, nr_bits);
852 while (cur < nr_bits) {
853 rtop = cur;
854 cur = find_next_bit(bitmap, nr_bits, cur + 1);
855 if (cur < nr_bits && cur <= rtop + 1)
856 continue;
857
858 if (!first) {
859 if (buf < end)
860 *buf = ',';
861 buf++;
862 }
863 first = false;
864
865 buf = number(buf, end, rbot, spec);
866 if (rbot < rtop) {
867 if (buf < end)
868 *buf = '-';
869 buf++;
870
871 buf = number(buf, end, rtop, spec);
872 }
873
874 rbot = cur;
875 }
876 return buf;
877}
878
879static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700880char *mac_address_string(char *buf, char *end, u8 *addr,
881 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700882{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000883 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700884 char *p = mac_addr;
885 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000886 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700887 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000888
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700889 switch (fmt[1]) {
890 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000891 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700892 break;
893
894 case 'R':
895 reversed = true;
896 /* fall through */
897
898 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000899 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700900 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000901 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700902
903 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700904 if (reversed)
905 p = hex_byte_pack(p, addr[5 - i]);
906 else
907 p = hex_byte_pack(p, addr[i]);
908
Joe Perches8a27f7c2009-08-17 12:29:44 +0000909 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000910 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700911 }
912 *p = '\0';
913
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100914 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700915}
916
Joe Perchescf3b4292010-05-24 14:33:16 -0700917static noinline_for_stack
918char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700919{
Harvey Harrison689afa72008-10-28 16:04:44 -0700920 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800921 bool leading_zeros = (fmt[0] == 'i');
922 int index;
923 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700924
Joe Perches0159f242010-01-13 20:23:30 -0800925 switch (fmt[2]) {
926 case 'h':
927#ifdef __BIG_ENDIAN
928 index = 0;
929 step = 1;
930#else
931 index = 3;
932 step = -1;
933#endif
934 break;
935 case 'l':
936 index = 3;
937 step = -1;
938 break;
939 case 'n':
940 case 'b':
941 default:
942 index = 0;
943 step = 1;
944 break;
945 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000946 for (i = 0; i < 4; i++) {
947 char temp[3]; /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700948 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000949 if (leading_zeros) {
950 if (digits < 3)
951 *p++ = '0';
952 if (digits < 2)
953 *p++ = '0';
954 }
955 /* reverse the digits in the quad */
956 while (digits--)
957 *p++ = temp[digits];
958 if (i < 3)
959 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800960 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000961 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000962 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800963
Joe Perches8a27f7c2009-08-17 12:29:44 +0000964 return p;
965}
966
Joe Perchescf3b4292010-05-24 14:33:16 -0700967static noinline_for_stack
968char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000969{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800970 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000971 unsigned char zerolength[8];
972 int longest = 1;
973 int colonpos = -1;
974 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800975 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000976 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000977 bool useIPv4;
978 struct in6_addr in6;
979
980 memcpy(&in6, addr, sizeof(struct in6_addr));
981
982 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000983
984 memset(zerolength, 0, sizeof(zerolength));
985
986 if (useIPv4)
987 range = 6;
988 else
989 range = 8;
990
991 /* find position of longest 0 run */
992 for (i = 0; i < range; i++) {
993 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000994 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000995 break;
996 zerolength[i]++;
997 }
998 }
999 for (i = 0; i < range; i++) {
1000 if (zerolength[i] > longest) {
1001 longest = zerolength[i];
1002 colonpos = i;
1003 }
1004 }
Joe Perches29cf5192011-06-09 11:23:37 -07001005 if (longest == 1) /* don't compress a single 0 */
1006 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001007
1008 /* emit address */
1009 for (i = 0; i < range; i++) {
1010 if (i == colonpos) {
1011 if (needcolon || i == 0)
1012 *p++ = ':';
1013 *p++ = ':';
1014 needcolon = false;
1015 i += longest - 1;
1016 continue;
1017 }
1018 if (needcolon) {
1019 *p++ = ':';
1020 needcolon = false;
1021 }
1022 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001023 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001024 hi = word >> 8;
1025 lo = word & 0xff;
1026 if (hi) {
1027 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001028 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001029 else
1030 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001031 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001032 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001033 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001034 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001035 else
1036 *p++ = hex_asc_lo(lo);
1037 needcolon = true;
1038 }
1039
1040 if (useIPv4) {
1041 if (needcolon)
1042 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001043 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001044 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001045 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001046
Joe Perches8a27f7c2009-08-17 12:29:44 +00001047 return p;
1048}
1049
Joe Perchescf3b4292010-05-24 14:33:16 -07001050static noinline_for_stack
1051char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001052{
1053 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001054
Harvey Harrison689afa72008-10-28 16:04:44 -07001055 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001056 p = hex_byte_pack(p, *addr++);
1057 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001058 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001059 *p++ = ':';
1060 }
1061 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001062
Joe Perches8a27f7c2009-08-17 12:29:44 +00001063 return p;
1064}
1065
Joe Perchescf3b4292010-05-24 14:33:16 -07001066static noinline_for_stack
1067char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1068 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001069{
1070 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1071
1072 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001073 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001074 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001075 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001076
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001077 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001078}
1079
Joe Perchescf3b4292010-05-24 14:33:16 -07001080static noinline_for_stack
1081char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1082 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001083{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001084 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001085
Joe Perches0159f242010-01-13 20:23:30 -08001086 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001087
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001088 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001089}
1090
Joe Perchescf3b4292010-05-24 14:33:16 -07001091static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001092char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1093 struct printf_spec spec, const char *fmt)
1094{
1095 bool have_p = false, have_s = false, have_f = false, have_c = false;
1096 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1097 sizeof(":12345") + sizeof("/123456789") +
1098 sizeof("%1234567890")];
1099 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1100 const u8 *addr = (const u8 *) &sa->sin6_addr;
1101 char fmt6[2] = { fmt[0], '6' };
1102 u8 off = 0;
1103
1104 fmt++;
1105 while (isalpha(*++fmt)) {
1106 switch (*fmt) {
1107 case 'p':
1108 have_p = true;
1109 break;
1110 case 'f':
1111 have_f = true;
1112 break;
1113 case 's':
1114 have_s = true;
1115 break;
1116 case 'c':
1117 have_c = true;
1118 break;
1119 }
1120 }
1121
1122 if (have_p || have_s || have_f) {
1123 *p = '[';
1124 off = 1;
1125 }
1126
1127 if (fmt6[0] == 'I' && have_c)
1128 p = ip6_compressed_string(ip6_addr + off, addr);
1129 else
1130 p = ip6_string(ip6_addr + off, addr, fmt6);
1131
1132 if (have_p || have_s || have_f)
1133 *p++ = ']';
1134
1135 if (have_p) {
1136 *p++ = ':';
1137 p = number(p, pend, ntohs(sa->sin6_port), spec);
1138 }
1139 if (have_f) {
1140 *p++ = '/';
1141 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1142 IPV6_FLOWINFO_MASK), spec);
1143 }
1144 if (have_s) {
1145 *p++ = '%';
1146 p = number(p, pend, sa->sin6_scope_id, spec);
1147 }
1148 *p = '\0';
1149
1150 return string(buf, end, ip6_addr, spec);
1151}
1152
1153static noinline_for_stack
1154char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1155 struct printf_spec spec, const char *fmt)
1156{
1157 bool have_p = false;
1158 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1159 char *pend = ip4_addr + sizeof(ip4_addr);
1160 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1161 char fmt4[3] = { fmt[0], '4', 0 };
1162
1163 fmt++;
1164 while (isalpha(*++fmt)) {
1165 switch (*fmt) {
1166 case 'p':
1167 have_p = true;
1168 break;
1169 case 'h':
1170 case 'l':
1171 case 'n':
1172 case 'b':
1173 fmt4[2] = *fmt;
1174 break;
1175 }
1176 }
1177
1178 p = ip4_string(ip4_addr, addr, fmt4);
1179 if (have_p) {
1180 *p++ = ':';
1181 p = number(p, pend, ntohs(sa->sin_port), spec);
1182 }
1183 *p = '\0';
1184
1185 return string(buf, end, ip4_addr, spec);
1186}
1187
1188static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001189char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1190 const char *fmt)
1191{
1192 bool found = true;
1193 int count = 1;
1194 unsigned int flags = 0;
1195 int len;
1196
1197 if (spec.field_width == 0)
1198 return buf; /* nothing to print */
1199
1200 if (ZERO_OR_NULL_PTR(addr))
1201 return string(buf, end, NULL, spec); /* NULL pointer */
1202
1203
1204 do {
1205 switch (fmt[count++]) {
1206 case 'a':
1207 flags |= ESCAPE_ANY;
1208 break;
1209 case 'c':
1210 flags |= ESCAPE_SPECIAL;
1211 break;
1212 case 'h':
1213 flags |= ESCAPE_HEX;
1214 break;
1215 case 'n':
1216 flags |= ESCAPE_NULL;
1217 break;
1218 case 'o':
1219 flags |= ESCAPE_OCTAL;
1220 break;
1221 case 'p':
1222 flags |= ESCAPE_NP;
1223 break;
1224 case 's':
1225 flags |= ESCAPE_SPACE;
1226 break;
1227 default:
1228 found = false;
1229 break;
1230 }
1231 } while (found);
1232
1233 if (!flags)
1234 flags = ESCAPE_ANY_NP;
1235
1236 len = spec.field_width < 0 ? 1 : spec.field_width;
1237
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001238 /*
1239 * string_escape_mem() writes as many characters as it can to
1240 * the given buffer, and returns the total size of the output
1241 * had the buffer been big enough.
1242 */
1243 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001244
1245 return buf;
1246}
1247
1248static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001249char *uuid_string(char *buf, char *end, const u8 *addr,
1250 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001251{
1252 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1253 char *p = uuid;
1254 int i;
1255 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1256 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1257 const u8 *index = be;
1258 bool uc = false;
1259
1260 switch (*(++fmt)) {
1261 case 'L':
1262 uc = true; /* fall-through */
1263 case 'l':
1264 index = le;
1265 break;
1266 case 'B':
1267 uc = true;
1268 break;
1269 }
1270
1271 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001272 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001273 switch (i) {
1274 case 3:
1275 case 5:
1276 case 7:
1277 case 9:
1278 *p++ = '-';
1279 break;
1280 }
1281 }
1282
1283 *p = 0;
1284
1285 if (uc) {
1286 p = uuid;
1287 do {
1288 *p = toupper(*p);
1289 } while (*(++p));
1290 }
1291
1292 return string(buf, end, uuid, spec);
1293}
1294
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001295static
1296char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1297 struct printf_spec spec)
1298{
1299 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1300 if (spec.field_width == -1)
1301 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1302 spec.base = 16;
1303
1304 return number(buf, end, *(const netdev_features_t *)addr, spec);
1305}
1306
Joe Perchesaaf07622014-01-23 15:54:17 -08001307static noinline_for_stack
1308char *address_val(char *buf, char *end, const void *addr,
1309 struct printf_spec spec, const char *fmt)
1310{
1311 unsigned long long num;
1312
1313 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1314 spec.base = 16;
1315
1316 switch (fmt[1]) {
1317 case 'd':
1318 num = *(const dma_addr_t *)addr;
1319 spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1320 break;
1321 case 'p':
1322 default:
1323 num = *(const phys_addr_t *)addr;
1324 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1325 break;
1326 }
1327
1328 return number(buf, end, num, spec);
1329}
1330
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001331static noinline_for_stack
1332char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1333 const char *fmt)
1334{
1335 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1336 return string(buf, end, NULL, spec);
1337
1338 switch (fmt[1]) {
1339 case 'r':
1340 return number(buf, end, clk_get_rate(clk), spec);
1341
1342 case 'n':
1343 default:
1344#ifdef CONFIG_COMMON_CLK
1345 return string(buf, end, __clk_get_name(clk), spec);
1346#else
1347 spec.base = 16;
1348 spec.field_width = sizeof(unsigned long) * 2 + 2;
1349 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1350 return number(buf, end, (unsigned long)clk, spec);
1351#endif
1352 }
1353}
1354
Ingo Molnar411f05f2011-05-12 23:00:28 +02001355int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001356
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001357/*
1358 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1359 * by an extra set of alphanumeric characters that are extended format
1360 * specifiers.
1361 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001362 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001363 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001364 * - 'F' For symbolic function descriptor pointers with offset
1365 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001366 * - 'S' For symbolic direct pointers with offset
1367 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001368 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001369 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001370 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1371 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001372 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1373 * width which must be explicitly specified either as part of the
1374 * format string '%32b[l]' or through '%*b[l]', [l] selects
1375 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001376 * - 'M' For a 6-byte MAC address, it prints the address in the
1377 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001378 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001379 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001380 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001381 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001382 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1383 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1384 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001385 * [S][pfs]
1386 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1387 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001388 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1389 * IPv6 omits the colons (01020304...0f)
1390 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001391 * [S][pfs]
1392 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1393 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1394 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1395 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001396 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001397 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1398 * of the following flags (see string_escape_mem() for the
1399 * details):
1400 * a - ESCAPE_ANY
1401 * c - ESCAPE_SPECIAL
1402 * h - ESCAPE_HEX
1403 * n - ESCAPE_NULL
1404 * o - ESCAPE_OCTAL
1405 * p - ESCAPE_NP
1406 * s - ESCAPE_SPACE
1407 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001408 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1409 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1410 * Options for %pU are:
1411 * b big endian lower case hex (default)
1412 * B big endian UPPER case hex
1413 * l little endian lower case hex
1414 * L little endian UPPER case hex
1415 * big endian output byte order is:
1416 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1417 * little endian output byte order is:
1418 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001419 * - 'V' For a struct va_format which contains a format string * and va_list *,
1420 * call vsnprintf(->format, *->va_list).
1421 * Implements a "recursive vsnprintf".
1422 * Do not use this feature without some mechanism to verify the
1423 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001424 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001425 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001426 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1427 * a certain separator (' ' by default):
1428 * C colon
1429 * D dash
1430 * N no separator
1431 * The maximum supported length is 64 bytes of the input. Consider
1432 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001433 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1434 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001435 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1436 * - 'D[234]' Same as 'd' but for a struct file
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001437 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1438 * (legacy clock framework) of the clock
1439 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1440 * (legacy clock framework) of the clock
1441 * - 'Cr' For a clock, it prints the current rate of the clock
Joe Perches9ac6e442009-12-14 18:01:09 -08001442 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001443 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1444 * function pointers are really function descriptors, which contain a
1445 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001446 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001447static noinline_for_stack
1448char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1449 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001450{
Grant Likely725fe002012-05-31 16:26:08 -07001451 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1452
Kees Cook9f36e2c2011-03-22 16:34:22 -07001453 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001454 /*
1455 * Print (null) with the same width as a pointer so it makes
1456 * tabular output look nice.
1457 */
1458 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001459 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001460 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001461 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001462
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001463 switch (*fmt) {
1464 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001465 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001466 ptr = dereference_function_descriptor(ptr);
1467 /* Fallthrough */
1468 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001469 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001470 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001471 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001472 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001473 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001474 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001475 case 'h':
1476 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001477 case 'b':
1478 switch (fmt[1]) {
1479 case 'l':
1480 return bitmap_list_string(buf, end, ptr, spec, fmt);
1481 default:
1482 return bitmap_string(buf, end, ptr, spec, fmt);
1483 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001484 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1485 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001486 /* [mM]F (FDDI) */
1487 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001488 return mac_address_string(buf, end, ptr, spec, fmt);
1489 case 'I': /* Formatted IP supported
1490 * 4: 1.2.3.4
1491 * 6: 0001:0203:...:0708
1492 * 6c: 1::708 or 1::1.2.3.4
1493 */
1494 case 'i': /* Contiguous:
1495 * 4: 001.002.003.004
1496 * 6: 000102...0f
1497 */
1498 switch (fmt[1]) {
1499 case '6':
1500 return ip6_addr_string(buf, end, ptr, spec, fmt);
1501 case '4':
1502 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001503 case 'S': {
1504 const union {
1505 struct sockaddr raw;
1506 struct sockaddr_in v4;
1507 struct sockaddr_in6 v6;
1508 } *sa = ptr;
1509
1510 switch (sa->raw.sa_family) {
1511 case AF_INET:
1512 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1513 case AF_INET6:
1514 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1515 default:
1516 return string(buf, end, "(invalid address)", spec);
1517 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001518 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001519 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001520 case 'E':
1521 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001522 case 'U':
1523 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001524 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001525 {
1526 va_list va;
1527
1528 va_copy(va, *((struct va_format *)ptr)->va);
1529 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1530 ((struct va_format *)ptr)->fmt, va);
1531 va_end(va);
1532 return buf;
1533 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001534 case 'K':
1535 /*
1536 * %pK cannot be used in IRQ context because its test
1537 * for CAP_SYSLOG would be meaningless.
1538 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001539 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1540 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001541 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001542 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001543 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001544 }
Ryan Mallon312b4e22013-11-12 15:08:51 -08001545
1546 switch (kptr_restrict) {
1547 case 0:
1548 /* Always print %pK values */
1549 break;
1550 case 1: {
1551 /*
1552 * Only print the real pointer value if the current
1553 * process has CAP_SYSLOG and is running with the
1554 * same credentials it started with. This is because
1555 * access to files is checked at open() time, but %pK
1556 * checks permission at read() time. We don't want to
1557 * leak pointer values if a binary opens a file using
1558 * %pK and then elevates privileges before reading it.
1559 */
1560 const struct cred *cred = current_cred();
1561
1562 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1563 !uid_eq(cred->euid, cred->uid) ||
1564 !gid_eq(cred->egid, cred->gid))
1565 ptr = NULL;
1566 break;
1567 }
1568 case 2:
1569 default:
1570 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -07001571 ptr = NULL;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001572 break;
1573 }
Joe Perches26297602011-03-22 16:34:19 -07001574 break;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001575
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001576 case 'N':
1577 switch (fmt[1]) {
1578 case 'F':
1579 return netdev_feature_string(buf, end, ptr, spec);
1580 }
1581 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001582 case 'a':
Joe Perchesaaf07622014-01-23 15:54:17 -08001583 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001584 case 'd':
1585 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001586 case 'C':
1587 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001588 case 'D':
1589 return dentry_name(buf, end,
1590 ((const struct file *)ptr)->f_path.dentry,
1591 spec, fmt);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001592 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001593 spec.flags |= SMALL;
1594 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001595 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001596 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001597 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001598 spec.base = 16;
1599
1600 return number(buf, end, (unsigned long) ptr, spec);
1601}
1602
1603/*
1604 * Helper function to decode printf style format.
1605 * Each call decode a token from the format and return the
1606 * number of characters read (or likely the delta where it wants
1607 * to go on the next call).
1608 * The decoded token is returned through the parameters
1609 *
1610 * 'h', 'l', or 'L' for integer fields
1611 * 'z' support added 23/7/1999 S.H.
1612 * 'z' changed to 'Z' --davidm 1/25/99
1613 * 't' added for ptrdiff_t
1614 *
1615 * @fmt: the format string
1616 * @type of the token returned
1617 * @flags: various flags such as +, -, # tokens..
1618 * @field_width: overwritten width
1619 * @base: base of the number (octal, hex, ...)
1620 * @precision: precision of a number
1621 * @qualifier: qualifier of a number (long, size_t, ...)
1622 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001623static noinline_for_stack
1624int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001625{
1626 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001627
1628 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001629 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001630 if (spec->field_width < 0) {
1631 spec->field_width = -spec->field_width;
1632 spec->flags |= LEFT;
1633 }
1634 spec->type = FORMAT_TYPE_NONE;
1635 goto precision;
1636 }
1637
1638 /* we finished early by reading the precision */
1639 if (spec->type == FORMAT_TYPE_PRECISION) {
1640 if (spec->precision < 0)
1641 spec->precision = 0;
1642
1643 spec->type = FORMAT_TYPE_NONE;
1644 goto qualifier;
1645 }
1646
1647 /* By default */
1648 spec->type = FORMAT_TYPE_NONE;
1649
1650 for (; *fmt ; ++fmt) {
1651 if (*fmt == '%')
1652 break;
1653 }
1654
1655 /* Return the current non-format string */
1656 if (fmt != start || !*fmt)
1657 return fmt - start;
1658
1659 /* Process flags */
1660 spec->flags = 0;
1661
1662 while (1) { /* this also skips first '%' */
1663 bool found = true;
1664
1665 ++fmt;
1666
1667 switch (*fmt) {
1668 case '-': spec->flags |= LEFT; break;
1669 case '+': spec->flags |= PLUS; break;
1670 case ' ': spec->flags |= SPACE; break;
1671 case '#': spec->flags |= SPECIAL; break;
1672 case '0': spec->flags |= ZEROPAD; break;
1673 default: found = false;
1674 }
1675
1676 if (!found)
1677 break;
1678 }
1679
1680 /* get field width */
1681 spec->field_width = -1;
1682
1683 if (isdigit(*fmt))
1684 spec->field_width = skip_atoi(&fmt);
1685 else if (*fmt == '*') {
1686 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001687 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001688 return ++fmt - start;
1689 }
1690
1691precision:
1692 /* get the precision */
1693 spec->precision = -1;
1694 if (*fmt == '.') {
1695 ++fmt;
1696 if (isdigit(*fmt)) {
1697 spec->precision = skip_atoi(&fmt);
1698 if (spec->precision < 0)
1699 spec->precision = 0;
1700 } else if (*fmt == '*') {
1701 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001702 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001703 return ++fmt - start;
1704 }
1705 }
1706
1707qualifier:
1708 /* get the conversion qualifier */
1709 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001710 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1711 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001712 spec->qualifier = *fmt++;
1713 if (unlikely(spec->qualifier == *fmt)) {
1714 if (spec->qualifier == 'l') {
1715 spec->qualifier = 'L';
1716 ++fmt;
1717 } else if (spec->qualifier == 'h') {
1718 spec->qualifier = 'H';
1719 ++fmt;
1720 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001721 }
1722 }
1723
1724 /* default base */
1725 spec->base = 10;
1726 switch (*fmt) {
1727 case 'c':
1728 spec->type = FORMAT_TYPE_CHAR;
1729 return ++fmt - start;
1730
1731 case 's':
1732 spec->type = FORMAT_TYPE_STR;
1733 return ++fmt - start;
1734
1735 case 'p':
1736 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001737 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001738
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001739 case '%':
1740 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1741 return ++fmt - start;
1742
1743 /* integer number formats - set up the flags and "break" */
1744 case 'o':
1745 spec->base = 8;
1746 break;
1747
1748 case 'x':
1749 spec->flags |= SMALL;
1750
1751 case 'X':
1752 spec->base = 16;
1753 break;
1754
1755 case 'd':
1756 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001757 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001758 case 'u':
1759 break;
1760
Ryan Mallon708d96f2014-04-03 14:48:37 -07001761 case 'n':
1762 /*
1763 * Since %n poses a greater security risk than utility, treat
1764 * it as an invalid format specifier. Warn about its use so
1765 * that new instances don't get added.
1766 */
1767 WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1768 /* Fall-through */
1769
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001770 default:
1771 spec->type = FORMAT_TYPE_INVALID;
1772 return fmt - start;
1773 }
1774
1775 if (spec->qualifier == 'L')
1776 spec->type = FORMAT_TYPE_LONG_LONG;
1777 else if (spec->qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001778 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1779 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001780 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001781 spec->type = FORMAT_TYPE_SIZE_T;
1782 } else if (spec->qualifier == 't') {
1783 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001784 } else if (spec->qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001785 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1786 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001787 } else if (spec->qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001788 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1789 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001790 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001791 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1792 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001793 }
1794
1795 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001796}
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/**
1799 * vsnprintf - Format a string 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 * @args: Arguments for the format string
1804 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001805 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001806 * %pS output the name of a text symbol with offset
1807 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001808 * %pF output the name of a function pointer with its offset
1809 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001810 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001811 * %pR output the address range in a struct resource with decoded flags
1812 * %pr output the address range in a struct resource with raw flags
Tejun Heodbc760b2015-02-13 14:36:53 -08001813 * %pb output the bitmap with field width as the number of bits
1814 * %pbl output the bitmap as range list with field width as the number of bits
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001815 * %pM output a 6-byte MAC address with colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001816 * %pMR output a 6-byte MAC address with colons in reversed order
1817 * %pMF output a 6-byte MAC address with dashes
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001818 * %pm output a 6-byte MAC address without colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001819 * %pmR output a 6-byte MAC address without colons in reversed order
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001820 * %pI4 print an IPv4 address without leading zeros
1821 * %pi4 print an IPv4 address with leading zeros
1822 * %pI6 print an IPv6 address with colons
1823 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001824 * %pI6c print an IPv6 address as specified by RFC 5952
Daniel Borkmann10679642013-06-28 19:49:39 +02001825 * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1826 * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001827 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1828 * case.
Andy Shevchenko71dca952014-10-13 15:55:18 -07001829 * %*pE[achnops] print an escaped buffer
Andy Shevchenko31550a12012-07-30 14:40:27 -07001830 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1831 * bytes of the input)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001832 * %pC output the name (Common Clock Framework) or address (legacy clock
1833 * framework) of a clock
1834 * %pCn output the name (Common Clock Framework) or address (legacy clock
1835 * framework) of a clock
1836 * %pCr output the current rate of a clock
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001837 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001838 *
Andrew Morton80f548e2012-07-30 14:40:25 -07001839 * ** Please update Documentation/printk-formats.txt when making changes **
1840 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 * The return value is the number of characters which would
1842 * be generated for the given input, excluding the trailing
1843 * '\0', as per ISO C99. If you want to have the exact
1844 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001845 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 * return is greater than or equal to @size, the resulting
1847 * string is truncated.
1848 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001849 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 */
1851int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001854 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001855 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001857 /* Reject out-of-range values early. Large positive sizes are
1858 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08001859 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001863 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001865 /* Make sure end is always >= buf */
1866 if (end < buf) {
1867 end = ((void *)-1);
1868 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001871 while (*fmt) {
1872 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001873 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001874
1875 fmt += read;
1876
1877 switch (spec.type) {
1878 case FORMAT_TYPE_NONE: {
1879 int copy = read;
1880 if (str < end) {
1881 if (copy > end - str)
1882 copy = end - str;
1883 memcpy(str, old_fmt, copy);
1884 }
1885 str += read;
1886 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888
Vegard Nossumed681a92009-03-14 12:08:50 +01001889 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001890 spec.field_width = va_arg(args, int);
1891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001893 case FORMAT_TYPE_PRECISION:
1894 spec.precision = va_arg(args, int);
1895 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
André Goddard Rosad4be1512009-12-14 18:00:59 -08001897 case FORMAT_TYPE_CHAR: {
1898 char c;
1899
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001900 if (!(spec.flags & LEFT)) {
1901 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001902 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 *str = ' ';
1904 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001907 }
1908 c = (unsigned char) va_arg(args, int);
1909 if (str < end)
1910 *str = c;
1911 ++str;
1912 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001913 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001914 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001916 }
1917 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001920 case FORMAT_TYPE_STR:
1921 str = string(str, end, va_arg(args, char *), spec);
1922 break;
1923
1924 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001925 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001926 spec);
1927 while (isalnum(*fmt))
1928 fmt++;
1929 break;
1930
1931 case FORMAT_TYPE_PERCENT_CHAR:
1932 if (str < end)
1933 *str = '%';
1934 ++str;
1935 break;
1936
1937 case FORMAT_TYPE_INVALID:
1938 if (str < end)
1939 *str = '%';
1940 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001941 break;
1942
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001943 default:
1944 switch (spec.type) {
1945 case FORMAT_TYPE_LONG_LONG:
1946 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001948 case FORMAT_TYPE_ULONG:
1949 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001951 case FORMAT_TYPE_LONG:
1952 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001954 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001955 if (spec.flags & SIGN)
1956 num = va_arg(args, ssize_t);
1957 else
1958 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001959 break;
1960 case FORMAT_TYPE_PTRDIFF:
1961 num = va_arg(args, ptrdiff_t);
1962 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001963 case FORMAT_TYPE_UBYTE:
1964 num = (unsigned char) va_arg(args, int);
1965 break;
1966 case FORMAT_TYPE_BYTE:
1967 num = (signed char) va_arg(args, int);
1968 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001969 case FORMAT_TYPE_USHORT:
1970 num = (unsigned short) va_arg(args, int);
1971 break;
1972 case FORMAT_TYPE_SHORT:
1973 num = (short) va_arg(args, int);
1974 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001975 case FORMAT_TYPE_INT:
1976 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001979 num = va_arg(args, unsigned int);
1980 }
1981
1982 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001985
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001986 if (size > 0) {
1987 if (str < end)
1988 *str = '\0';
1989 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001990 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001991 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001992
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001993 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997EXPORT_SYMBOL(vsnprintf);
1998
1999/**
2000 * vscnprintf - Format a string and place it in a buffer
2001 * @buf: The buffer to place the result into
2002 * @size: The size of the buffer, including the trailing null space
2003 * @fmt: The format string to use
2004 * @args: Arguments for the format string
2005 *
2006 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002007 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 * returns 0.
2009 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002010 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002011 *
2012 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 */
2014int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2015{
2016 int i;
2017
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002018 i = vsnprintf(buf, size, fmt, args);
2019
Anton Arapovb921c692011-01-12 16:59:49 -08002020 if (likely(i < size))
2021 return i;
2022 if (size != 0)
2023 return size - 1;
2024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026EXPORT_SYMBOL(vscnprintf);
2027
2028/**
2029 * snprintf - Format a string and place it in a buffer
2030 * @buf: The buffer to place the result into
2031 * @size: The size of the buffer, including the trailing null space
2032 * @fmt: The format string to use
2033 * @...: Arguments for the format string
2034 *
2035 * The return value is the number of characters which would be
2036 * generated for the given input, excluding the trailing null,
2037 * as per ISO C99. If the return is greater than or equal to
2038 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002039 *
2040 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002042int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 va_list args;
2045 int i;
2046
2047 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002048 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return i;
2052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053EXPORT_SYMBOL(snprintf);
2054
2055/**
2056 * scnprintf - Format a string and place it in a buffer
2057 * @buf: The buffer to place the result into
2058 * @size: The size of the buffer, including the trailing null space
2059 * @fmt: The format string to use
2060 * @...: Arguments for the format string
2061 *
2062 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002063 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
2065
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002066int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 va_list args;
2069 int i;
2070
2071 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002072 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002074
Anton Arapovb921c692011-01-12 16:59:49 -08002075 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077EXPORT_SYMBOL(scnprintf);
2078
2079/**
2080 * vsprintf - Format a string and place it in a buffer
2081 * @buf: The buffer to place the result into
2082 * @fmt: The format string to use
2083 * @args: Arguments for the format string
2084 *
2085 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002086 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 * buffer overflows.
2088 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002089 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002090 *
2091 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 */
2093int vsprintf(char *buf, const char *fmt, va_list args)
2094{
2095 return vsnprintf(buf, INT_MAX, fmt, args);
2096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097EXPORT_SYMBOL(vsprintf);
2098
2099/**
2100 * sprintf - Format a string and place it in a buffer
2101 * @buf: The buffer to place the result into
2102 * @fmt: The format string to use
2103 * @...: Arguments for the format string
2104 *
2105 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002106 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002108 *
2109 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002111int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 va_list args;
2114 int i;
2115
2116 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002117 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return i;
2121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122EXPORT_SYMBOL(sprintf);
2123
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002124#ifdef CONFIG_BINARY_PRINTF
2125/*
2126 * bprintf service:
2127 * vbin_printf() - VA arguments to binary data
2128 * bstr_printf() - Binary data to text string
2129 */
2130
2131/**
2132 * vbin_printf - Parse a format string and place args' binary value in a buffer
2133 * @bin_buf: The buffer to place args' binary value
2134 * @size: The size of the buffer(by words(32bits), not characters)
2135 * @fmt: The format string to use
2136 * @args: Arguments for the format string
2137 *
2138 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002139 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002140 *
2141 * The return value is the number of words(32bits) which would be generated for
2142 * the given input.
2143 *
2144 * NOTE:
2145 * If the return value is greater than @size, the resulting bin_buf is NOT
2146 * valid for bstr_printf().
2147 */
2148int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2149{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002150 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002151 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002152
2153 str = (char *)bin_buf;
2154 end = (char *)(bin_buf + size);
2155
2156#define save_arg(type) \
2157do { \
2158 if (sizeof(type) == 8) { \
2159 unsigned long long value; \
2160 str = PTR_ALIGN(str, sizeof(u32)); \
2161 value = va_arg(args, unsigned long long); \
2162 if (str + sizeof(type) <= end) { \
2163 *(u32 *)str = *(u32 *)&value; \
2164 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2165 } \
2166 } else { \
2167 unsigned long value; \
2168 str = PTR_ALIGN(str, sizeof(type)); \
2169 value = va_arg(args, int); \
2170 if (str + sizeof(type) <= end) \
2171 *(typeof(type) *)str = (type)value; \
2172 } \
2173 str += sizeof(type); \
2174} while (0)
2175
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002176 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002177 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002178
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002179 fmt += read;
2180
2181 switch (spec.type) {
2182 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002183 case FORMAT_TYPE_INVALID:
2184 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002185 break;
2186
Vegard Nossumed681a92009-03-14 12:08:50 +01002187 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002188 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002189 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002190 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002191
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002192 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002193 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002194 break;
2195
2196 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002197 const char *save_str = va_arg(args, char *);
2198 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002199
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002200 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2201 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002202 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002203 len = strlen(save_str) + 1;
2204 if (str + len < end)
2205 memcpy(str, save_str, len);
2206 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002207 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002208 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002209
2210 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002211 save_arg(void *);
2212 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002213 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002214 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002215 break;
2216
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002217 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002218 switch (spec.type) {
2219
2220 case FORMAT_TYPE_LONG_LONG:
2221 save_arg(long long);
2222 break;
2223 case FORMAT_TYPE_ULONG:
2224 case FORMAT_TYPE_LONG:
2225 save_arg(unsigned long);
2226 break;
2227 case FORMAT_TYPE_SIZE_T:
2228 save_arg(size_t);
2229 break;
2230 case FORMAT_TYPE_PTRDIFF:
2231 save_arg(ptrdiff_t);
2232 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002233 case FORMAT_TYPE_UBYTE:
2234 case FORMAT_TYPE_BYTE:
2235 save_arg(char);
2236 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002237 case FORMAT_TYPE_USHORT:
2238 case FORMAT_TYPE_SHORT:
2239 save_arg(short);
2240 break;
2241 default:
2242 save_arg(int);
2243 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002244 }
2245 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002246
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002247 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002248#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002249}
2250EXPORT_SYMBOL_GPL(vbin_printf);
2251
2252/**
2253 * bstr_printf - Format a string from binary arguments and place it in a buffer
2254 * @buf: The buffer to place the result into
2255 * @size: The size of the buffer, including the trailing null space
2256 * @fmt: The format string to use
2257 * @bin_buf: Binary arguments for the format string
2258 *
2259 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2260 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2261 * a binary buffer that generated by vbin_printf.
2262 *
2263 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002264 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002265 *
2266 * The return value is the number of characters which would
2267 * be generated for the given input, excluding the trailing
2268 * '\0', as per ISO C99. If you want to have the exact
2269 * number of characters written into @buf as return value
2270 * (not including the trailing '\0'), use vscnprintf(). If the
2271 * return is greater than or equal to @size, the resulting
2272 * string is truncated.
2273 */
2274int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2275{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002276 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002277 char *str, *end;
2278 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002279
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07002280 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002281 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002282
2283 str = buf;
2284 end = buf + size;
2285
2286#define get_arg(type) \
2287({ \
2288 typeof(type) value; \
2289 if (sizeof(type) == 8) { \
2290 args = PTR_ALIGN(args, sizeof(u32)); \
2291 *(u32 *)&value = *(u32 *)args; \
2292 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2293 } else { \
2294 args = PTR_ALIGN(args, sizeof(type)); \
2295 value = *(typeof(type) *)args; \
2296 } \
2297 args += sizeof(type); \
2298 value; \
2299})
2300
2301 /* Make sure end is always >= buf */
2302 if (end < buf) {
2303 end = ((void *)-1);
2304 size = end - buf;
2305 }
2306
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002307 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002308 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002309 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002310
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002311 fmt += read;
2312
2313 switch (spec.type) {
2314 case FORMAT_TYPE_NONE: {
2315 int copy = read;
2316 if (str < end) {
2317 if (copy > end - str)
2318 copy = end - str;
2319 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002320 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002321 str += read;
2322 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002323 }
2324
Vegard Nossumed681a92009-03-14 12:08:50 +01002325 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002326 spec.field_width = get_arg(int);
2327 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002328
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002329 case FORMAT_TYPE_PRECISION:
2330 spec.precision = get_arg(int);
2331 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002332
André Goddard Rosad4be1512009-12-14 18:00:59 -08002333 case FORMAT_TYPE_CHAR: {
2334 char c;
2335
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002336 if (!(spec.flags & LEFT)) {
2337 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002338 if (str < end)
2339 *str = ' ';
2340 ++str;
2341 }
2342 }
2343 c = (unsigned char) get_arg(char);
2344 if (str < end)
2345 *str = c;
2346 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002347 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002348 if (str < end)
2349 *str = ' ';
2350 ++str;
2351 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002352 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002353 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002354
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002355 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002356 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002357 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002358 str = string(str, end, (char *)str_arg, spec);
2359 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002360 }
2361
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002362 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002363 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002364 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002365 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002366 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002367
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002368 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002369 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002370 if (str < end)
2371 *str = '%';
2372 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002373 break;
2374
André Goddard Rosad4be1512009-12-14 18:00:59 -08002375 default: {
2376 unsigned long long num;
2377
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002378 switch (spec.type) {
2379
2380 case FORMAT_TYPE_LONG_LONG:
2381 num = get_arg(long long);
2382 break;
2383 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002384 case FORMAT_TYPE_LONG:
2385 num = get_arg(unsigned long);
2386 break;
2387 case FORMAT_TYPE_SIZE_T:
2388 num = get_arg(size_t);
2389 break;
2390 case FORMAT_TYPE_PTRDIFF:
2391 num = get_arg(ptrdiff_t);
2392 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002393 case FORMAT_TYPE_UBYTE:
2394 num = get_arg(unsigned char);
2395 break;
2396 case FORMAT_TYPE_BYTE:
2397 num = get_arg(signed char);
2398 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002399 case FORMAT_TYPE_USHORT:
2400 num = get_arg(unsigned short);
2401 break;
2402 case FORMAT_TYPE_SHORT:
2403 num = get_arg(short);
2404 break;
2405 case FORMAT_TYPE_UINT:
2406 num = get_arg(unsigned int);
2407 break;
2408 default:
2409 num = get_arg(int);
2410 }
2411
2412 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002413 } /* default: */
2414 } /* switch(spec.type) */
2415 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002416
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002417 if (size > 0) {
2418 if (str < end)
2419 *str = '\0';
2420 else
2421 end[-1] = '\0';
2422 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002423
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002424#undef get_arg
2425
2426 /* the trailing null byte doesn't count towards the total */
2427 return str - buf;
2428}
2429EXPORT_SYMBOL_GPL(bstr_printf);
2430
2431/**
2432 * bprintf - Parse a format string and place args' binary value in a buffer
2433 * @bin_buf: The buffer to place args' binary value
2434 * @size: The size of the buffer(by words(32bits), not characters)
2435 * @fmt: The format string to use
2436 * @...: Arguments for the format string
2437 *
2438 * The function returns the number of words(u32) written
2439 * into @bin_buf.
2440 */
2441int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2442{
2443 va_list args;
2444 int ret;
2445
2446 va_start(args, fmt);
2447 ret = vbin_printf(bin_buf, size, fmt, args);
2448 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002449
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002450 return ret;
2451}
2452EXPORT_SYMBOL_GPL(bprintf);
2453
2454#endif /* CONFIG_BINARY_PRINTF */
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456/**
2457 * vsscanf - Unformat a buffer into a list of arguments
2458 * @buf: input buffer
2459 * @fmt: format of buffer
2460 * @args: arguments
2461 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002462int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 const char *str = buf;
2465 char *next;
2466 char digit;
2467 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002468 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002469 unsigned int base;
2470 union {
2471 long long s;
2472 unsigned long long u;
2473 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002474 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002475 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Jan Beulichda990752012-10-04 17:13:24 -07002477 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /* skip any white space in format */
2479 /* white space in format matchs any amount of
2480 * white space, including none, in the input.
2481 */
2482 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002483 fmt = skip_spaces(++fmt);
2484 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
2486
2487 /* anything that is not a conversion must match exactly */
2488 if (*fmt != '%' && *fmt) {
2489 if (*fmt++ != *str++)
2490 break;
2491 continue;
2492 }
2493
2494 if (!*fmt)
2495 break;
2496 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* skip this conversion.
2499 * advance both strings to next white space
2500 */
2501 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002502 if (!*str)
2503 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002504 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 fmt++;
2506 while (!isspace(*str) && *str)
2507 str++;
2508 continue;
2509 }
2510
2511 /* get field width */
2512 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002513 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002515 if (field_width <= 0)
2516 break;
2517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /* get conversion qualifier */
2520 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002521 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2522 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 qualifier = *fmt++;
2524 if (unlikely(qualifier == *fmt)) {
2525 if (qualifier == 'h') {
2526 qualifier = 'H';
2527 fmt++;
2528 } else if (qualifier == 'l') {
2529 qualifier = 'L';
2530 fmt++;
2531 }
2532 }
2533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Jan Beulichda990752012-10-04 17:13:24 -07002535 if (!*fmt)
2536 break;
2537
2538 if (*fmt == 'n') {
2539 /* return number of characters read so far */
2540 *va_arg(args, int *) = str - buf;
2541 ++fmt;
2542 continue;
2543 }
2544
2545 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 break;
2547
André Goddard Rosad4be1512009-12-14 18:00:59 -08002548 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002549 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002550
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002551 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 case 'c':
2553 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002554 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (field_width == -1)
2556 field_width = 1;
2557 do {
2558 *s++ = *str++;
2559 } while (--field_width > 0 && *str);
2560 num++;
2561 }
2562 continue;
2563 case 's':
2564 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002565 char *s = (char *)va_arg(args, char *);
2566 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002567 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002569 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002572 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 *s = '\0';
2575 num++;
2576 }
2577 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 case 'o':
2579 base = 8;
2580 break;
2581 case 'x':
2582 case 'X':
2583 base = 16;
2584 break;
2585 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002586 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002588 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 case 'u':
2590 break;
2591 case '%':
2592 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002593 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return num;
2595 continue;
2596 default:
2597 /* invalid format; stop here */
2598 return num;
2599 }
2600
2601 /* have some sort of integer conversion.
2602 * first, skip white space in buffer.
2603 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002604 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 digit = *str;
2607 if (is_sign && digit == '-')
2608 digit = *(str + 1);
2609
2610 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002611 || (base == 16 && !isxdigit(digit))
2612 || (base == 10 && !isdigit(digit))
2613 || (base == 8 && (!isdigit(digit) || digit > '7'))
2614 || (base == 0 && !isdigit(digit)))
2615 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Jan Beulich53809752012-12-17 16:01:31 -08002617 if (is_sign)
2618 val.s = qualifier != 'L' ?
2619 simple_strtol(str, &next, base) :
2620 simple_strtoll(str, &next, base);
2621 else
2622 val.u = qualifier != 'L' ?
2623 simple_strtoul(str, &next, base) :
2624 simple_strtoull(str, &next, base);
2625
2626 if (field_width > 0 && next - str > field_width) {
2627 if (base == 0)
2628 _parse_integer_fixup_radix(str, &base);
2629 while (next - str > field_width) {
2630 if (is_sign)
2631 val.s = div_s64(val.s, base);
2632 else
2633 val.u = div_u64(val.u, base);
2634 --next;
2635 }
2636 }
2637
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002638 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002640 if (is_sign)
2641 *va_arg(args, signed char *) = val.s;
2642 else
2643 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 break;
2645 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002646 if (is_sign)
2647 *va_arg(args, short *) = val.s;
2648 else
2649 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 break;
2651 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002652 if (is_sign)
2653 *va_arg(args, long *) = val.s;
2654 else
2655 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 break;
2657 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002658 if (is_sign)
2659 *va_arg(args, long long *) = val.s;
2660 else
2661 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 break;
2663 case 'Z':
2664 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002665 *va_arg(args, size_t *) = val.u;
2666 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 default:
Jan Beulich53809752012-12-17 16:01:31 -08002668 if (is_sign)
2669 *va_arg(args, int *) = val.s;
2670 else
2671 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 break;
2673 }
2674 num++;
2675
2676 if (!next)
2677 break;
2678 str = next;
2679 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return num;
2682}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683EXPORT_SYMBOL(vsscanf);
2684
2685/**
2686 * sscanf - Unformat a buffer into a list of arguments
2687 * @buf: input buffer
2688 * @fmt: formatting of buffer
2689 * @...: resulting arguments
2690 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002691int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 va_list args;
2694 int i;
2695
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002696 va_start(args, fmt);
2697 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return i;
2701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702EXPORT_SYMBOL(sscanf);