blob: c93ec8a035b32e3f064d214bcabb74f942a0bd2d [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() */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070036#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Andy Shevchenko71dca952014-10-13 15:55:18 -070038#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070039#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * simple_strtoull - convert a string to an unsigned long long
43 * @cp: The start of the string
44 * @endp: A pointer to the end of the parsed string will be placed here
45 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080046 *
47 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
Harvey Harrison22d27052008-10-16 13:40:35 -070049unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070051 unsigned long long result;
52 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070054 cp = _parse_integer_fixup_radix(cp, &base);
55 rv = _parse_integer(cp, base, &result);
56 /* FIXME */
57 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (endp)
60 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return result;
63}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064EXPORT_SYMBOL(simple_strtoull);
65
66/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080067 * simple_strtoul - convert a string to an unsigned long
68 * @cp: The start of the string
69 * @endp: A pointer to the end of the parsed string will be placed here
70 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080071 *
72 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080073 */
74unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
75{
76 return simple_strtoull(cp, endp, base);
77}
78EXPORT_SYMBOL(simple_strtoul);
79
80/**
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080085 *
86 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080087 */
88long simple_strtol(const char *cp, char **endp, unsigned int base)
89{
90 if (*cp == '-')
91 return -simple_strtoul(cp + 1, endp, base);
92
93 return simple_strtoul(cp, endp, base);
94}
95EXPORT_SYMBOL(simple_strtol);
96
97/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * simple_strtoll - convert a string to a signed long long
99 * @cp: The start of the string
100 * @endp: A pointer to the end of the parsed string will be placed here
101 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800102 *
103 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700105long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800107 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700108 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800109
Harvey Harrison22d27052008-10-16 13:40:35 -0700110 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400112EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Joe Perchescf3b4292010-05-24 14:33:16 -0700114static noinline_for_stack
115int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800117 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800119 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800121 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return i;
124}
125
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700126/*
127 * Decimal conversion is by far the most typical, and is used for
128 * /proc and /sys data. This directly impacts e.g. top performance
129 * with many processes running. We optimize it for speed by emitting
130 * two characters at a time, using a 200 byte lookup table. This
131 * roughly halves the number of multiplications compared to computing
132 * the digits one at a time. Implementation strongly inspired by the
133 * previous version, which in turn used ideas described at
134 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
135 * from the author, Douglas W. Jones).
136 *
137 * It turns out there is precisely one 26 bit fixed-point
138 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
139 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
140 * range happens to be somewhat larger (x <= 1073741898), but that's
141 * irrelevant for our purpose.
142 *
143 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
144 * need a 32x32->64 bit multiply, so we simply use the same constant.
145 *
146 * For dividing a number in the range [100, 10^4-1] by 100, there are
147 * several options. The simplest is (x * 0x147b) >> 19, which is valid
148 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700149 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700150
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700151static const u16 decpair[100] = {
152#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
153 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
154 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
155 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
156 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
157 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
158 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
159 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
160 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
161 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
162 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
163#undef _
164};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700165
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700166/*
167 * This will print a single '0' even if r == 0, since we would
168 * immediately jump to out_r where two 0s would be written and one of
169 * them then discarded. This is needed by ip4_string below. All other
170 * callers pass a non-zero value of r.
171*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700172static noinline_for_stack
173char *put_dec_trunc8(char *buf, unsigned r)
174{
175 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700176
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700177 /* 1 <= r < 10^8 */
178 if (r < 100)
179 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700180
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700181 /* 100 <= r < 10^8 */
182 q = (r * (u64)0x28f5c29) >> 32;
183 *((u16 *)buf) = decpair[r - 100*q];
184 buf += 2;
185
186 /* 1 <= q < 10^6 */
187 if (q < 100)
188 goto out_q;
189
190 /* 100 <= q < 10^6 */
191 r = (q * (u64)0x28f5c29) >> 32;
192 *((u16 *)buf) = decpair[q - 100*r];
193 buf += 2;
194
195 /* 1 <= r < 10^4 */
196 if (r < 100)
197 goto out_r;
198
199 /* 100 <= r < 10^4 */
200 q = (r * 0x147b) >> 19;
201 *((u16 *)buf) = decpair[r - 100*q];
202 buf += 2;
203out_q:
204 /* 1 <= q < 100 */
205 r = q;
206out_r:
207 /* 1 <= r < 100 */
208 *((u16 *)buf) = decpair[r];
209 buf += 2;
210 if (buf[-1] == '0')
211 buf--;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700212 return buf;
213}
214
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700215#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
216static noinline_for_stack
217char *put_dec_full8(char *buf, unsigned r)
218{
219 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700220
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700221 /* 0 <= r < 10^8 */
222 q = (r * (u64)0x28f5c29) >> 32;
223 *((u16 *)buf) = decpair[r - 100*q];
224 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700225
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700226 /* 0 <= q < 10^6 */
227 r = (q * (u64)0x28f5c29) >> 32;
228 *((u16 *)buf) = decpair[q - 100*r];
229 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700230
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700231 /* 0 <= r < 10^4 */
232 q = (r * 0x147b) >> 19;
233 *((u16 *)buf) = decpair[r - 100*q];
234 buf += 2;
235
236 /* 0 <= q < 100 */
237 *((u16 *)buf) = decpair[q];
238 buf += 2;
239 return buf;
240}
241
242static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700243char *put_dec(char *buf, unsigned long long n)
244{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700245 if (n >= 100*1000*1000)
246 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
247 /* 1 <= n <= 1.6e11 */
248 if (n >= 100*1000*1000)
249 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
250 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700251 return put_dec_trunc8(buf, n);
252}
253
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700254#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700255
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700256static void
257put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700258{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700259 unsigned q;
260
261 /* 0 <= r < 10^4 */
262 q = (r * 0x147b) >> 19;
263 *((u16 *)buf) = decpair[r - 100*q];
264 buf += 2;
265 /* 0 <= q < 100 */
266 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700267}
268
269/*
270 * Call put_dec_full4 on x % 10000, return x / 10000.
271 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
272 * holds for all x < 1,128,869,999. The largest value this
273 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700274 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700275 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700276static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700277unsigned put_dec_helper4(char *buf, unsigned x)
278{
279 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
280
281 put_dec_full4(buf, x - q * 10000);
282 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700283}
284
285/* Based on code by Douglas W. Jones found at
286 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
287 * (with permission from the author).
288 * Performs no 64-bit division and hence should be fast on 32-bit machines.
289 */
290static
291char *put_dec(char *buf, unsigned long long n)
292{
293 uint32_t d3, d2, d1, q, h;
294
295 if (n < 100*1000*1000)
296 return put_dec_trunc8(buf, n);
297
298 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
299 h = (n >> 32);
300 d2 = (h ) & 0xffff;
301 d3 = (h >> 16); /* implicit "& 0xffff" */
302
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700303 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
304 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700305 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700306 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700307
George Spelvin23591722012-10-04 17:12:29 -0700308 q += 7671 * d3 + 9496 * d2 + 6 * d1;
309 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700310
George Spelvin23591722012-10-04 17:12:29 -0700311 q += 4749 * d3 + 42 * d2;
312 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700313
George Spelvin23591722012-10-04 17:12:29 -0700314 q += 281 * d3;
315 buf += 12;
316 if (q)
317 buf = put_dec_trunc8(buf, q);
318 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700319 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800320
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700321 return buf;
322}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700323
324#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700325
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700326/*
327 * Convert passed number to decimal string.
328 * Returns the length of string. On buffer overflow, returns 0.
329 *
330 * If speed is not important, use snprintf(). It's easy to read the code.
331 */
332int num_to_str(char *buf, int size, unsigned long long num)
333{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700334 /* put_dec requires 2-byte alignment of the buffer. */
335 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700336 int idx, len;
337
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700338 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
339 if (num <= 9) {
340 tmp[0] = '0' + num;
341 len = 1;
342 } else {
343 len = put_dec(tmp, num) - tmp;
344 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700345
346 if (len > size)
347 return 0;
348 for (idx = 0; idx < len; ++idx)
349 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700350 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700351}
352
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700353#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700354#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355#define PLUS 4 /* show plus */
356#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700357#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700358#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
359#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100361enum format_type {
362 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100363 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100364 FORMAT_TYPE_PRECISION,
365 FORMAT_TYPE_CHAR,
366 FORMAT_TYPE_STR,
367 FORMAT_TYPE_PTR,
368 FORMAT_TYPE_PERCENT_CHAR,
369 FORMAT_TYPE_INVALID,
370 FORMAT_TYPE_LONG_LONG,
371 FORMAT_TYPE_ULONG,
372 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800373 FORMAT_TYPE_UBYTE,
374 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100375 FORMAT_TYPE_USHORT,
376 FORMAT_TYPE_SHORT,
377 FORMAT_TYPE_UINT,
378 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100379 FORMAT_TYPE_SIZE_T,
380 FORMAT_TYPE_PTRDIFF
381};
382
383struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700384 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800385 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700386 u8 base; /* number base, 8, 10 or 16 only */
387 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
388 s16 field_width; /* width of output field */
389 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100390};
391
Joe Perchescf3b4292010-05-24 14:33:16 -0700392static noinline_for_stack
393char *number(char *buf, char *end, unsigned long long num,
394 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700396 /* put_dec requires 2-byte alignment of the buffer. */
397 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100398 char sign;
399 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100400 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700402 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100404 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
405 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100406 locase = (spec.flags & SMALL);
407 if (spec.flags & LEFT)
408 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100410 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800411 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800413 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100414 spec.field_width--;
415 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100417 spec.field_width--;
418 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100420 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700423 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100424 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700425 spec.field_width -= 2;
426 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100427 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700429
430 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700432 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700433 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100434 else if (spec.base != 10) { /* 8 or 16 */
435 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700436 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800437
438 if (spec.base == 16)
439 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700440 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700441 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700442 num >>= shift;
443 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700444 } else { /* base 10 */
445 i = put_dec(tmp, num) - tmp;
446 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447
448 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100449 if (i > spec.precision)
450 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700451 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100452 spec.field_width -= spec.precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700453 if (!(spec.flags & (ZEROPAD | LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800454 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700455 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *buf = ' ';
457 ++buf;
458 }
459 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700462 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *buf = sign;
464 ++buf;
465 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700466 /* "0x" / "0" prefix */
467 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700468 if (spec.base == 16 || !is_zero) {
469 if (buf < end)
470 *buf = '0';
471 ++buf;
472 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100473 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700474 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100475 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 ++buf;
477 }
478 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100480 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700481 char c = ' ' + (spec.flags & ZEROPAD);
482 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100483 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700484 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *buf = c;
486 ++buf;
487 }
488 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700489 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100490 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700491 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 *buf = '0';
493 ++buf;
494 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700495 /* actual digits of result */
496 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700497 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 *buf = tmp[i];
499 ++buf;
500 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700501 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100502 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700503 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *buf = ' ';
505 ++buf;
506 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return buf;
509}
510
Joe Perchescf3b4292010-05-24 14:33:16 -0700511static noinline_for_stack
512char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700513{
514 int len, i;
515
516 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800517 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700518
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100519 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700520
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100521 if (!(spec.flags & LEFT)) {
522 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700523 if (buf < end)
524 *buf = ' ';
525 ++buf;
526 }
527 }
528 for (i = 0; i < len; ++i) {
529 if (buf < end)
530 *buf = *s;
531 ++buf; ++s;
532 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100533 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700534 if (buf < end)
535 *buf = ' ';
536 ++buf;
537 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800538
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700539 return buf;
540}
541
Al Viro4b6ccca2013-09-03 12:00:44 -0400542static void widen(char *buf, char *end, unsigned len, unsigned spaces)
543{
544 size_t size;
545 if (buf >= end) /* nowhere to put anything */
546 return;
547 size = end - buf;
548 if (size <= spaces) {
549 memset(buf, ' ', size);
550 return;
551 }
552 if (len) {
553 if (len > size - spaces)
554 len = size - spaces;
555 memmove(buf + spaces, buf, len);
556 }
557 memset(buf, ' ', spaces);
558}
559
560static noinline_for_stack
561char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
562 const char *fmt)
563{
564 const char *array[4], *s;
565 const struct dentry *p;
566 int depth;
567 int i, n;
568
569 switch (fmt[1]) {
570 case '2': case '3': case '4':
571 depth = fmt[1] - '0';
572 break;
573 default:
574 depth = 1;
575 }
576
577 rcu_read_lock();
578 for (i = 0; i < depth; i++, d = p) {
579 p = ACCESS_ONCE(d->d_parent);
580 array[i] = ACCESS_ONCE(d->d_name.name);
581 if (p == d) {
582 if (i)
583 array[i] = "";
584 i++;
585 break;
586 }
587 }
588 s = array[--i];
589 for (n = 0; n != spec.precision; n++, buf++) {
590 char c = *s++;
591 if (!c) {
592 if (!i)
593 break;
594 c = '/';
595 s = array[--i];
596 }
597 if (buf < end)
598 *buf = c;
599 }
600 rcu_read_unlock();
601 if (n < spec.field_width) {
602 /* we want to pad the sucker */
603 unsigned spaces = spec.field_width - n;
604 if (!(spec.flags & LEFT)) {
605 widen(buf - n, end, n, spaces);
606 return buf + spaces;
607 }
608 while (spaces--) {
609 if (buf < end)
610 *buf = ' ';
611 ++buf;
612 }
613 }
614 return buf;
615}
616
Joe Perchescf3b4292010-05-24 14:33:16 -0700617static noinline_for_stack
618char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800619 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700620{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800621 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700622#ifdef CONFIG_KALLSYMS
623 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800624#endif
625
626 if (fmt[1] == 'R')
627 ptr = __builtin_extract_return_addr(ptr);
628 value = (unsigned long)ptr;
629
630#ifdef CONFIG_KALLSYMS
631 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900632 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800633 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200634 sprint_symbol(sym, value);
635 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700636 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800637
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100638 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700639#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800640 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100641 spec.flags |= SPECIAL | SMALL | ZEROPAD;
642 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800643
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100644 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700645#endif
646}
647
Joe Perchescf3b4292010-05-24 14:33:16 -0700648static noinline_for_stack
649char *resource_string(char *buf, char *end, struct resource *res,
650 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100651{
652#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600653#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100654#endif
655
656#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600657#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100658#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700659 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100660 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700661 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100662 .precision = -1,
663 .flags = SPECIAL | SMALL | ZEROPAD,
664 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700665 static const struct printf_spec mem_spec = {
666 .base = 16,
667 .field_width = MEM_RSRC_PRINTK_SIZE,
668 .precision = -1,
669 .flags = SPECIAL | SMALL | ZEROPAD,
670 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700671 static const struct printf_spec bus_spec = {
672 .base = 16,
673 .field_width = 2,
674 .precision = -1,
675 .flags = SMALL | ZEROPAD,
676 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700677 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600678 .base = 10,
679 .precision = -1,
680 .flags = 0,
681 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700682 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600683 .field_width = -1,
684 .precision = 10,
685 .flags = LEFT,
686 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700687 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600688 .base = 16,
689 .precision = -1,
690 .flags = SPECIAL | SMALL,
691 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600692
693 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
694 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
695#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
696#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700697#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600698#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
699 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
700 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
701
Linus Torvalds332d2e72008-10-20 15:07:34 +1100702 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600703 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700704 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100705
706 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700707 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600708 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700709 specp = &io_spec;
710 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600711 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700712 specp = &mem_spec;
713 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600714 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700715 specp = &dec_spec;
716 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600717 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700718 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700719 } else if (res->flags & IORESOURCE_BUS) {
720 p = string(p, pend, "bus ", str_spec);
721 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700722 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600723 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700724 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600725 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600726 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700727 if (decode && res->flags & IORESOURCE_UNSET) {
728 p = string(p, pend, "size ", str_spec);
729 p = number(p, pend, resource_size(res), *specp);
730 } else {
731 p = number(p, pend, res->start, *specp);
732 if (res->start != res->end) {
733 *p++ = '-';
734 p = number(p, pend, res->end, *specp);
735 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600736 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600737 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600738 if (res->flags & IORESOURCE_MEM_64)
739 p = string(p, pend, " 64bit", str_spec);
740 if (res->flags & IORESOURCE_PREFETCH)
741 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700742 if (res->flags & IORESOURCE_WINDOW)
743 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600744 if (res->flags & IORESOURCE_DISABLED)
745 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600746 } else {
747 p = string(p, pend, " flags ", str_spec);
748 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600749 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100750 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600751 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100752
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100753 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100754}
755
Joe Perchescf3b4292010-05-24 14:33:16 -0700756static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700757char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
758 const char *fmt)
759{
Steven Rostedt360603a2013-05-28 15:47:39 -0400760 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700761 negative value, fallback to the default */
762 char separator;
763
764 if (spec.field_width == 0)
765 /* nothing to print */
766 return buf;
767
768 if (ZERO_OR_NULL_PTR(addr))
769 /* NULL pointer */
770 return string(buf, end, NULL, spec);
771
772 switch (fmt[1]) {
773 case 'C':
774 separator = ':';
775 break;
776 case 'D':
777 separator = '-';
778 break;
779 case 'N':
780 separator = 0;
781 break;
782 default:
783 separator = ' ';
784 break;
785 }
786
787 if (spec.field_width > 0)
788 len = min_t(int, spec.field_width, 64);
789
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700790 for (i = 0; i < len; ++i) {
791 if (buf < end)
792 *buf = hex_asc_hi(addr[i]);
793 ++buf;
794 if (buf < end)
795 *buf = hex_asc_lo(addr[i]);
796 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700797
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700798 if (separator && i != len - 1) {
799 if (buf < end)
800 *buf = separator;
801 ++buf;
802 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700803 }
804
805 return buf;
806}
807
808static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800809char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
810 struct printf_spec spec, const char *fmt)
811{
812 const int CHUNKSZ = 32;
813 int nr_bits = max_t(int, spec.field_width, 0);
814 int i, chunksz;
815 bool first = true;
816
817 /* reused to print numbers */
818 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
819
820 chunksz = nr_bits & (CHUNKSZ - 1);
821 if (chunksz == 0)
822 chunksz = CHUNKSZ;
823
824 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
825 for (; i >= 0; i -= CHUNKSZ) {
826 u32 chunkmask, val;
827 int word, bit;
828
829 chunkmask = ((1ULL << chunksz) - 1);
830 word = i / BITS_PER_LONG;
831 bit = i % BITS_PER_LONG;
832 val = (bitmap[word] >> bit) & chunkmask;
833
834 if (!first) {
835 if (buf < end)
836 *buf = ',';
837 buf++;
838 }
839 first = false;
840
841 spec.field_width = DIV_ROUND_UP(chunksz, 4);
842 buf = number(buf, end, val, spec);
843
844 chunksz = CHUNKSZ;
845 }
846 return buf;
847}
848
849static noinline_for_stack
850char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
851 struct printf_spec spec, const char *fmt)
852{
853 int nr_bits = max_t(int, spec.field_width, 0);
854 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
855 int cur, rbot, rtop;
856 bool first = true;
857
858 /* reused to print numbers */
859 spec = (struct printf_spec){ .base = 10 };
860
861 rbot = cur = find_first_bit(bitmap, nr_bits);
862 while (cur < nr_bits) {
863 rtop = cur;
864 cur = find_next_bit(bitmap, nr_bits, cur + 1);
865 if (cur < nr_bits && cur <= rtop + 1)
866 continue;
867
868 if (!first) {
869 if (buf < end)
870 *buf = ',';
871 buf++;
872 }
873 first = false;
874
875 buf = number(buf, end, rbot, spec);
876 if (rbot < rtop) {
877 if (buf < end)
878 *buf = '-';
879 buf++;
880
881 buf = number(buf, end, rtop, spec);
882 }
883
884 rbot = cur;
885 }
886 return buf;
887}
888
889static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700890char *mac_address_string(char *buf, char *end, u8 *addr,
891 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700892{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000893 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700894 char *p = mac_addr;
895 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000896 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700897 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000898
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700899 switch (fmt[1]) {
900 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000901 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700902 break;
903
904 case 'R':
905 reversed = true;
906 /* fall through */
907
908 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000909 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700910 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000911 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700912
913 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700914 if (reversed)
915 p = hex_byte_pack(p, addr[5 - i]);
916 else
917 p = hex_byte_pack(p, addr[i]);
918
Joe Perches8a27f7c2009-08-17 12:29:44 +0000919 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000920 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700921 }
922 *p = '\0';
923
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100924 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700925}
926
Joe Perchescf3b4292010-05-24 14:33:16 -0700927static noinline_for_stack
928char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700929{
Harvey Harrison689afa72008-10-28 16:04:44 -0700930 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800931 bool leading_zeros = (fmt[0] == 'i');
932 int index;
933 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700934
Joe Perches0159f242010-01-13 20:23:30 -0800935 switch (fmt[2]) {
936 case 'h':
937#ifdef __BIG_ENDIAN
938 index = 0;
939 step = 1;
940#else
941 index = 3;
942 step = -1;
943#endif
944 break;
945 case 'l':
946 index = 3;
947 step = -1;
948 break;
949 case 'n':
950 case 'b':
951 default:
952 index = 0;
953 step = 1;
954 break;
955 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000956 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700957 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700958 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000959 if (leading_zeros) {
960 if (digits < 3)
961 *p++ = '0';
962 if (digits < 2)
963 *p++ = '0';
964 }
965 /* reverse the digits in the quad */
966 while (digits--)
967 *p++ = temp[digits];
968 if (i < 3)
969 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800970 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000971 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000972 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800973
Joe Perches8a27f7c2009-08-17 12:29:44 +0000974 return p;
975}
976
Joe Perchescf3b4292010-05-24 14:33:16 -0700977static noinline_for_stack
978char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000979{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800980 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000981 unsigned char zerolength[8];
982 int longest = 1;
983 int colonpos = -1;
984 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800985 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000986 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000987 bool useIPv4;
988 struct in6_addr in6;
989
990 memcpy(&in6, addr, sizeof(struct in6_addr));
991
992 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000993
994 memset(zerolength, 0, sizeof(zerolength));
995
996 if (useIPv4)
997 range = 6;
998 else
999 range = 8;
1000
1001 /* find position of longest 0 run */
1002 for (i = 0; i < range; i++) {
1003 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001004 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001005 break;
1006 zerolength[i]++;
1007 }
1008 }
1009 for (i = 0; i < range; i++) {
1010 if (zerolength[i] > longest) {
1011 longest = zerolength[i];
1012 colonpos = i;
1013 }
1014 }
Joe Perches29cf5192011-06-09 11:23:37 -07001015 if (longest == 1) /* don't compress a single 0 */
1016 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001017
1018 /* emit address */
1019 for (i = 0; i < range; i++) {
1020 if (i == colonpos) {
1021 if (needcolon || i == 0)
1022 *p++ = ':';
1023 *p++ = ':';
1024 needcolon = false;
1025 i += longest - 1;
1026 continue;
1027 }
1028 if (needcolon) {
1029 *p++ = ':';
1030 needcolon = false;
1031 }
1032 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001033 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001034 hi = word >> 8;
1035 lo = word & 0xff;
1036 if (hi) {
1037 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001038 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001039 else
1040 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001041 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001042 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001043 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001044 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001045 else
1046 *p++ = hex_asc_lo(lo);
1047 needcolon = true;
1048 }
1049
1050 if (useIPv4) {
1051 if (needcolon)
1052 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001053 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001054 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001055 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001056
Joe Perches8a27f7c2009-08-17 12:29:44 +00001057 return p;
1058}
1059
Joe Perchescf3b4292010-05-24 14:33:16 -07001060static noinline_for_stack
1061char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001062{
1063 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001064
Harvey Harrison689afa72008-10-28 16:04:44 -07001065 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001066 p = hex_byte_pack(p, *addr++);
1067 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001068 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001069 *p++ = ':';
1070 }
1071 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001072
Joe Perches8a27f7c2009-08-17 12:29:44 +00001073 return p;
1074}
1075
Joe Perchescf3b4292010-05-24 14:33:16 -07001076static noinline_for_stack
1077char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1078 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001079{
1080 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1081
1082 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001083 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001084 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001085 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001086
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001087 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001088}
1089
Joe Perchescf3b4292010-05-24 14:33:16 -07001090static noinline_for_stack
1091char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1092 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001093{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001094 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001095
Joe Perches0159f242010-01-13 20:23:30 -08001096 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001097
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001098 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001099}
1100
Joe Perchescf3b4292010-05-24 14:33:16 -07001101static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001102char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1103 struct printf_spec spec, const char *fmt)
1104{
1105 bool have_p = false, have_s = false, have_f = false, have_c = false;
1106 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1107 sizeof(":12345") + sizeof("/123456789") +
1108 sizeof("%1234567890")];
1109 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1110 const u8 *addr = (const u8 *) &sa->sin6_addr;
1111 char fmt6[2] = { fmt[0], '6' };
1112 u8 off = 0;
1113
1114 fmt++;
1115 while (isalpha(*++fmt)) {
1116 switch (*fmt) {
1117 case 'p':
1118 have_p = true;
1119 break;
1120 case 'f':
1121 have_f = true;
1122 break;
1123 case 's':
1124 have_s = true;
1125 break;
1126 case 'c':
1127 have_c = true;
1128 break;
1129 }
1130 }
1131
1132 if (have_p || have_s || have_f) {
1133 *p = '[';
1134 off = 1;
1135 }
1136
1137 if (fmt6[0] == 'I' && have_c)
1138 p = ip6_compressed_string(ip6_addr + off, addr);
1139 else
1140 p = ip6_string(ip6_addr + off, addr, fmt6);
1141
1142 if (have_p || have_s || have_f)
1143 *p++ = ']';
1144
1145 if (have_p) {
1146 *p++ = ':';
1147 p = number(p, pend, ntohs(sa->sin6_port), spec);
1148 }
1149 if (have_f) {
1150 *p++ = '/';
1151 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1152 IPV6_FLOWINFO_MASK), spec);
1153 }
1154 if (have_s) {
1155 *p++ = '%';
1156 p = number(p, pend, sa->sin6_scope_id, spec);
1157 }
1158 *p = '\0';
1159
1160 return string(buf, end, ip6_addr, spec);
1161}
1162
1163static noinline_for_stack
1164char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1165 struct printf_spec spec, const char *fmt)
1166{
1167 bool have_p = false;
1168 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1169 char *pend = ip4_addr + sizeof(ip4_addr);
1170 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1171 char fmt4[3] = { fmt[0], '4', 0 };
1172
1173 fmt++;
1174 while (isalpha(*++fmt)) {
1175 switch (*fmt) {
1176 case 'p':
1177 have_p = true;
1178 break;
1179 case 'h':
1180 case 'l':
1181 case 'n':
1182 case 'b':
1183 fmt4[2] = *fmt;
1184 break;
1185 }
1186 }
1187
1188 p = ip4_string(ip4_addr, addr, fmt4);
1189 if (have_p) {
1190 *p++ = ':';
1191 p = number(p, pend, ntohs(sa->sin_port), spec);
1192 }
1193 *p = '\0';
1194
1195 return string(buf, end, ip4_addr, spec);
1196}
1197
1198static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001199char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1200 const char *fmt)
1201{
1202 bool found = true;
1203 int count = 1;
1204 unsigned int flags = 0;
1205 int len;
1206
1207 if (spec.field_width == 0)
1208 return buf; /* nothing to print */
1209
1210 if (ZERO_OR_NULL_PTR(addr))
1211 return string(buf, end, NULL, spec); /* NULL pointer */
1212
1213
1214 do {
1215 switch (fmt[count++]) {
1216 case 'a':
1217 flags |= ESCAPE_ANY;
1218 break;
1219 case 'c':
1220 flags |= ESCAPE_SPECIAL;
1221 break;
1222 case 'h':
1223 flags |= ESCAPE_HEX;
1224 break;
1225 case 'n':
1226 flags |= ESCAPE_NULL;
1227 break;
1228 case 'o':
1229 flags |= ESCAPE_OCTAL;
1230 break;
1231 case 'p':
1232 flags |= ESCAPE_NP;
1233 break;
1234 case 's':
1235 flags |= ESCAPE_SPACE;
1236 break;
1237 default:
1238 found = false;
1239 break;
1240 }
1241 } while (found);
1242
1243 if (!flags)
1244 flags = ESCAPE_ANY_NP;
1245
1246 len = spec.field_width < 0 ? 1 : spec.field_width;
1247
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001248 /*
1249 * string_escape_mem() writes as many characters as it can to
1250 * the given buffer, and returns the total size of the output
1251 * had the buffer been big enough.
1252 */
1253 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001254
1255 return buf;
1256}
1257
1258static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001259char *uuid_string(char *buf, char *end, const u8 *addr,
1260 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001261{
1262 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1263 char *p = uuid;
1264 int i;
1265 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1266 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1267 const u8 *index = be;
1268 bool uc = false;
1269
1270 switch (*(++fmt)) {
1271 case 'L':
1272 uc = true; /* fall-through */
1273 case 'l':
1274 index = le;
1275 break;
1276 case 'B':
1277 uc = true;
1278 break;
1279 }
1280
1281 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001282 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001283 switch (i) {
1284 case 3:
1285 case 5:
1286 case 7:
1287 case 9:
1288 *p++ = '-';
1289 break;
1290 }
1291 }
1292
1293 *p = 0;
1294
1295 if (uc) {
1296 p = uuid;
1297 do {
1298 *p = toupper(*p);
1299 } while (*(++p));
1300 }
1301
1302 return string(buf, end, uuid, spec);
1303}
1304
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001305static
1306char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1307 struct printf_spec spec)
1308{
1309 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1310 if (spec.field_width == -1)
1311 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1312 spec.base = 16;
1313
1314 return number(buf, end, *(const netdev_features_t *)addr, spec);
1315}
1316
Joe Perchesaaf07622014-01-23 15:54:17 -08001317static noinline_for_stack
1318char *address_val(char *buf, char *end, const void *addr,
1319 struct printf_spec spec, const char *fmt)
1320{
1321 unsigned long long num;
1322
1323 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1324 spec.base = 16;
1325
1326 switch (fmt[1]) {
1327 case 'd':
1328 num = *(const dma_addr_t *)addr;
1329 spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1330 break;
1331 case 'p':
1332 default:
1333 num = *(const phys_addr_t *)addr;
1334 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1335 break;
1336 }
1337
1338 return number(buf, end, num, spec);
1339}
1340
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001341static noinline_for_stack
1342char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1343 const char *fmt)
1344{
1345 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1346 return string(buf, end, NULL, spec);
1347
1348 switch (fmt[1]) {
1349 case 'r':
1350 return number(buf, end, clk_get_rate(clk), spec);
1351
1352 case 'n':
1353 default:
1354#ifdef CONFIG_COMMON_CLK
1355 return string(buf, end, __clk_get_name(clk), spec);
1356#else
1357 spec.base = 16;
1358 spec.field_width = sizeof(unsigned long) * 2 + 2;
1359 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1360 return number(buf, end, (unsigned long)clk, spec);
1361#endif
1362 }
1363}
1364
Ingo Molnar411f05f2011-05-12 23:00:28 +02001365int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001366
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001367/*
1368 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1369 * by an extra set of alphanumeric characters that are extended format
1370 * specifiers.
1371 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001372 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001373 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001374 * - 'F' For symbolic function descriptor pointers with offset
1375 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001376 * - 'S' For symbolic direct pointers with offset
1377 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001378 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001379 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001380 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1381 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001382 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1383 * width which must be explicitly specified either as part of the
1384 * format string '%32b[l]' or through '%*b[l]', [l] selects
1385 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001386 * - 'M' For a 6-byte MAC address, it prints the address in the
1387 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001388 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001389 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001390 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001391 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001392 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1393 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1394 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001395 * [S][pfs]
1396 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1397 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001398 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1399 * IPv6 omits the colons (01020304...0f)
1400 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001401 * [S][pfs]
1402 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1403 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1404 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1405 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001406 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001407 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1408 * of the following flags (see string_escape_mem() for the
1409 * details):
1410 * a - ESCAPE_ANY
1411 * c - ESCAPE_SPECIAL
1412 * h - ESCAPE_HEX
1413 * n - ESCAPE_NULL
1414 * o - ESCAPE_OCTAL
1415 * p - ESCAPE_NP
1416 * s - ESCAPE_SPACE
1417 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001418 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1419 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1420 * Options for %pU are:
1421 * b big endian lower case hex (default)
1422 * B big endian UPPER case hex
1423 * l little endian lower case hex
1424 * L little endian UPPER case hex
1425 * big endian output byte order is:
1426 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1427 * little endian output byte order is:
1428 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001429 * - 'V' For a struct va_format which contains a format string * and va_list *,
1430 * call vsnprintf(->format, *->va_list).
1431 * Implements a "recursive vsnprintf".
1432 * Do not use this feature without some mechanism to verify the
1433 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001434 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001435 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001436 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1437 * a certain separator (' ' by default):
1438 * C colon
1439 * D dash
1440 * N no separator
1441 * The maximum supported length is 64 bytes of the input. Consider
1442 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001443 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1444 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001445 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1446 * - 'D[234]' Same as 'd' but for a struct file
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001447 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1448 * (legacy clock framework) of the clock
1449 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1450 * (legacy clock framework) of the clock
1451 * - 'Cr' For a clock, it prints the current rate of the clock
Joe Perches9ac6e442009-12-14 18:01:09 -08001452 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001453 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1454 * function pointers are really function descriptors, which contain a
1455 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001456 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001457static noinline_for_stack
1458char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1459 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001460{
Grant Likely725fe002012-05-31 16:26:08 -07001461 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1462
Kees Cook9f36e2c2011-03-22 16:34:22 -07001463 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001464 /*
1465 * Print (null) with the same width as a pointer so it makes
1466 * tabular output look nice.
1467 */
1468 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001469 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001470 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001471 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001472
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001473 switch (*fmt) {
1474 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001475 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001476 ptr = dereference_function_descriptor(ptr);
1477 /* Fallthrough */
1478 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001479 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001480 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001481 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001482 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001483 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001484 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001485 case 'h':
1486 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001487 case 'b':
1488 switch (fmt[1]) {
1489 case 'l':
1490 return bitmap_list_string(buf, end, ptr, spec, fmt);
1491 default:
1492 return bitmap_string(buf, end, ptr, spec, fmt);
1493 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001494 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1495 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001496 /* [mM]F (FDDI) */
1497 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001498 return mac_address_string(buf, end, ptr, spec, fmt);
1499 case 'I': /* Formatted IP supported
1500 * 4: 1.2.3.4
1501 * 6: 0001:0203:...:0708
1502 * 6c: 1::708 or 1::1.2.3.4
1503 */
1504 case 'i': /* Contiguous:
1505 * 4: 001.002.003.004
1506 * 6: 000102...0f
1507 */
1508 switch (fmt[1]) {
1509 case '6':
1510 return ip6_addr_string(buf, end, ptr, spec, fmt);
1511 case '4':
1512 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001513 case 'S': {
1514 const union {
1515 struct sockaddr raw;
1516 struct sockaddr_in v4;
1517 struct sockaddr_in6 v6;
1518 } *sa = ptr;
1519
1520 switch (sa->raw.sa_family) {
1521 case AF_INET:
1522 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1523 case AF_INET6:
1524 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1525 default:
1526 return string(buf, end, "(invalid address)", spec);
1527 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001528 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001529 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001530 case 'E':
1531 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001532 case 'U':
1533 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001534 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001535 {
1536 va_list va;
1537
1538 va_copy(va, *((struct va_format *)ptr)->va);
1539 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1540 ((struct va_format *)ptr)->fmt, va);
1541 va_end(va);
1542 return buf;
1543 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001544 case 'K':
1545 /*
1546 * %pK cannot be used in IRQ context because its test
1547 * for CAP_SYSLOG would be meaningless.
1548 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001549 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1550 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001551 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001552 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001553 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001554 }
Ryan Mallon312b4e22013-11-12 15:08:51 -08001555
1556 switch (kptr_restrict) {
1557 case 0:
1558 /* Always print %pK values */
1559 break;
1560 case 1: {
1561 /*
1562 * Only print the real pointer value if the current
1563 * process has CAP_SYSLOG and is running with the
1564 * same credentials it started with. This is because
1565 * access to files is checked at open() time, but %pK
1566 * checks permission at read() time. We don't want to
1567 * leak pointer values if a binary opens a file using
1568 * %pK and then elevates privileges before reading it.
1569 */
1570 const struct cred *cred = current_cred();
1571
1572 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1573 !uid_eq(cred->euid, cred->uid) ||
1574 !gid_eq(cred->egid, cred->gid))
1575 ptr = NULL;
1576 break;
1577 }
1578 case 2:
1579 default:
1580 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -07001581 ptr = NULL;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001582 break;
1583 }
Joe Perches26297602011-03-22 16:34:19 -07001584 break;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001585
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001586 case 'N':
1587 switch (fmt[1]) {
1588 case 'F':
1589 return netdev_feature_string(buf, end, ptr, spec);
1590 }
1591 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001592 case 'a':
Joe Perchesaaf07622014-01-23 15:54:17 -08001593 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001594 case 'd':
1595 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001596 case 'C':
1597 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001598 case 'D':
1599 return dentry_name(buf, end,
1600 ((const struct file *)ptr)->f_path.dentry,
1601 spec, fmt);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001602 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001603 spec.flags |= SMALL;
1604 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001605 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001606 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001607 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001608 spec.base = 16;
1609
1610 return number(buf, end, (unsigned long) ptr, spec);
1611}
1612
1613/*
1614 * Helper function to decode printf style format.
1615 * Each call decode a token from the format and return the
1616 * number of characters read (or likely the delta where it wants
1617 * to go on the next call).
1618 * The decoded token is returned through the parameters
1619 *
1620 * 'h', 'l', or 'L' for integer fields
1621 * 'z' support added 23/7/1999 S.H.
1622 * 'z' changed to 'Z' --davidm 1/25/99
1623 * 't' added for ptrdiff_t
1624 *
1625 * @fmt: the format string
1626 * @type of the token returned
1627 * @flags: various flags such as +, -, # tokens..
1628 * @field_width: overwritten width
1629 * @base: base of the number (octal, hex, ...)
1630 * @precision: precision of a number
1631 * @qualifier: qualifier of a number (long, size_t, ...)
1632 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001633static noinline_for_stack
1634int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001635{
1636 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001637
1638 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001639 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001640 if (spec->field_width < 0) {
1641 spec->field_width = -spec->field_width;
1642 spec->flags |= LEFT;
1643 }
1644 spec->type = FORMAT_TYPE_NONE;
1645 goto precision;
1646 }
1647
1648 /* we finished early by reading the precision */
1649 if (spec->type == FORMAT_TYPE_PRECISION) {
1650 if (spec->precision < 0)
1651 spec->precision = 0;
1652
1653 spec->type = FORMAT_TYPE_NONE;
1654 goto qualifier;
1655 }
1656
1657 /* By default */
1658 spec->type = FORMAT_TYPE_NONE;
1659
1660 for (; *fmt ; ++fmt) {
1661 if (*fmt == '%')
1662 break;
1663 }
1664
1665 /* Return the current non-format string */
1666 if (fmt != start || !*fmt)
1667 return fmt - start;
1668
1669 /* Process flags */
1670 spec->flags = 0;
1671
1672 while (1) { /* this also skips first '%' */
1673 bool found = true;
1674
1675 ++fmt;
1676
1677 switch (*fmt) {
1678 case '-': spec->flags |= LEFT; break;
1679 case '+': spec->flags |= PLUS; break;
1680 case ' ': spec->flags |= SPACE; break;
1681 case '#': spec->flags |= SPECIAL; break;
1682 case '0': spec->flags |= ZEROPAD; break;
1683 default: found = false;
1684 }
1685
1686 if (!found)
1687 break;
1688 }
1689
1690 /* get field width */
1691 spec->field_width = -1;
1692
1693 if (isdigit(*fmt))
1694 spec->field_width = skip_atoi(&fmt);
1695 else if (*fmt == '*') {
1696 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001697 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001698 return ++fmt - start;
1699 }
1700
1701precision:
1702 /* get the precision */
1703 spec->precision = -1;
1704 if (*fmt == '.') {
1705 ++fmt;
1706 if (isdigit(*fmt)) {
1707 spec->precision = skip_atoi(&fmt);
1708 if (spec->precision < 0)
1709 spec->precision = 0;
1710 } else if (*fmt == '*') {
1711 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001712 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001713 return ++fmt - start;
1714 }
1715 }
1716
1717qualifier:
1718 /* get the conversion qualifier */
1719 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001720 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1721 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001722 spec->qualifier = *fmt++;
1723 if (unlikely(spec->qualifier == *fmt)) {
1724 if (spec->qualifier == 'l') {
1725 spec->qualifier = 'L';
1726 ++fmt;
1727 } else if (spec->qualifier == 'h') {
1728 spec->qualifier = 'H';
1729 ++fmt;
1730 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001731 }
1732 }
1733
1734 /* default base */
1735 spec->base = 10;
1736 switch (*fmt) {
1737 case 'c':
1738 spec->type = FORMAT_TYPE_CHAR;
1739 return ++fmt - start;
1740
1741 case 's':
1742 spec->type = FORMAT_TYPE_STR;
1743 return ++fmt - start;
1744
1745 case 'p':
1746 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001747 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001748
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001749 case '%':
1750 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1751 return ++fmt - start;
1752
1753 /* integer number formats - set up the flags and "break" */
1754 case 'o':
1755 spec->base = 8;
1756 break;
1757
1758 case 'x':
1759 spec->flags |= SMALL;
1760
1761 case 'X':
1762 spec->base = 16;
1763 break;
1764
1765 case 'd':
1766 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001767 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001768 case 'u':
1769 break;
1770
Ryan Mallon708d96f2014-04-03 14:48:37 -07001771 case 'n':
1772 /*
1773 * Since %n poses a greater security risk than utility, treat
1774 * it as an invalid format specifier. Warn about its use so
1775 * that new instances don't get added.
1776 */
1777 WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1778 /* Fall-through */
1779
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001780 default:
1781 spec->type = FORMAT_TYPE_INVALID;
1782 return fmt - start;
1783 }
1784
1785 if (spec->qualifier == 'L')
1786 spec->type = FORMAT_TYPE_LONG_LONG;
1787 else if (spec->qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001788 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1789 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001790 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001791 spec->type = FORMAT_TYPE_SIZE_T;
1792 } else if (spec->qualifier == 't') {
1793 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001794 } else if (spec->qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001795 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1796 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001797 } else if (spec->qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001798 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1799 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001800 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001801 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1802 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001803 }
1804
1805 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001806}
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808/**
1809 * vsnprintf - Format a string and place it in a buffer
1810 * @buf: The buffer to place the result into
1811 * @size: The size of the buffer, including the trailing null space
1812 * @fmt: The format string to use
1813 * @args: Arguments for the format string
1814 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001815 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001816 * %pS output the name of a text symbol with offset
1817 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001818 * %pF output the name of a function pointer with its offset
1819 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001820 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001821 * %pR output the address range in a struct resource with decoded flags
1822 * %pr output the address range in a struct resource with raw flags
Tejun Heodbc760b2015-02-13 14:36:53 -08001823 * %pb output the bitmap with field width as the number of bits
1824 * %pbl output the bitmap as range list with field width as the number of bits
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001825 * %pM output a 6-byte MAC address with colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001826 * %pMR output a 6-byte MAC address with colons in reversed order
1827 * %pMF output a 6-byte MAC address with dashes
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001828 * %pm output a 6-byte MAC address without colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001829 * %pmR output a 6-byte MAC address without colons in reversed order
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001830 * %pI4 print an IPv4 address without leading zeros
1831 * %pi4 print an IPv4 address with leading zeros
1832 * %pI6 print an IPv6 address with colons
1833 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001834 * %pI6c print an IPv6 address as specified by RFC 5952
Daniel Borkmann10679642013-06-28 19:49:39 +02001835 * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1836 * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001837 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1838 * case.
Andy Shevchenko71dca952014-10-13 15:55:18 -07001839 * %*pE[achnops] print an escaped buffer
Andy Shevchenko31550a12012-07-30 14:40:27 -07001840 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1841 * bytes of the input)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001842 * %pC output the name (Common Clock Framework) or address (legacy clock
1843 * framework) of a clock
1844 * %pCn output the name (Common Clock Framework) or address (legacy clock
1845 * framework) of a clock
1846 * %pCr output the current rate of a clock
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001847 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001848 *
Andrew Morton80f548e2012-07-30 14:40:25 -07001849 * ** Please update Documentation/printk-formats.txt when making changes **
1850 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 * The return value is the number of characters which would
1852 * be generated for the given input, excluding the trailing
1853 * '\0', as per ISO C99. If you want to have the exact
1854 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001855 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 * return is greater than or equal to @size, the resulting
1857 * string is truncated.
1858 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001859 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
1861int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001864 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001865 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001867 /* Reject out-of-range values early. Large positive sizes are
1868 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08001869 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001873 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001875 /* Make sure end is always >= buf */
1876 if (end < buf) {
1877 end = ((void *)-1);
1878 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001881 while (*fmt) {
1882 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001883 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001884
1885 fmt += read;
1886
1887 switch (spec.type) {
1888 case FORMAT_TYPE_NONE: {
1889 int copy = read;
1890 if (str < end) {
1891 if (copy > end - str)
1892 copy = end - str;
1893 memcpy(str, old_fmt, copy);
1894 }
1895 str += read;
1896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
Vegard Nossumed681a92009-03-14 12:08:50 +01001899 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001900 spec.field_width = va_arg(args, int);
1901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001903 case FORMAT_TYPE_PRECISION:
1904 spec.precision = va_arg(args, int);
1905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
André Goddard Rosad4be1512009-12-14 18:00:59 -08001907 case FORMAT_TYPE_CHAR: {
1908 char c;
1909
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001910 if (!(spec.flags & LEFT)) {
1911 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001912 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 *str = ' ';
1914 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001917 }
1918 c = (unsigned char) va_arg(args, int);
1919 if (str < end)
1920 *str = c;
1921 ++str;
1922 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001923 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001924 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001926 }
1927 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001930 case FORMAT_TYPE_STR:
1931 str = string(str, end, va_arg(args, char *), spec);
1932 break;
1933
1934 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001935 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001936 spec);
1937 while (isalnum(*fmt))
1938 fmt++;
1939 break;
1940
1941 case FORMAT_TYPE_PERCENT_CHAR:
1942 if (str < end)
1943 *str = '%';
1944 ++str;
1945 break;
1946
1947 case FORMAT_TYPE_INVALID:
1948 if (str < end)
1949 *str = '%';
1950 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001951 break;
1952
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001953 default:
1954 switch (spec.type) {
1955 case FORMAT_TYPE_LONG_LONG:
1956 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001958 case FORMAT_TYPE_ULONG:
1959 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001961 case FORMAT_TYPE_LONG:
1962 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001964 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001965 if (spec.flags & SIGN)
1966 num = va_arg(args, ssize_t);
1967 else
1968 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001969 break;
1970 case FORMAT_TYPE_PTRDIFF:
1971 num = va_arg(args, ptrdiff_t);
1972 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001973 case FORMAT_TYPE_UBYTE:
1974 num = (unsigned char) va_arg(args, int);
1975 break;
1976 case FORMAT_TYPE_BYTE:
1977 num = (signed char) va_arg(args, int);
1978 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001979 case FORMAT_TYPE_USHORT:
1980 num = (unsigned short) va_arg(args, int);
1981 break;
1982 case FORMAT_TYPE_SHORT:
1983 num = (short) va_arg(args, int);
1984 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001985 case FORMAT_TYPE_INT:
1986 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001989 num = va_arg(args, unsigned int);
1990 }
1991
1992 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001995
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001996 if (size > 0) {
1997 if (str < end)
1998 *str = '\0';
1999 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002000 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002001 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002002
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002003 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007EXPORT_SYMBOL(vsnprintf);
2008
2009/**
2010 * vscnprintf - Format a string and place it in a buffer
2011 * @buf: The buffer to place the result into
2012 * @size: The size of the buffer, including the trailing null space
2013 * @fmt: The format string to use
2014 * @args: Arguments for the format string
2015 *
2016 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002017 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 * returns 0.
2019 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002020 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002021 *
2022 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 */
2024int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2025{
2026 int i;
2027
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002028 i = vsnprintf(buf, size, fmt, args);
2029
Anton Arapovb921c692011-01-12 16:59:49 -08002030 if (likely(i < size))
2031 return i;
2032 if (size != 0)
2033 return size - 1;
2034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036EXPORT_SYMBOL(vscnprintf);
2037
2038/**
2039 * snprintf - Format a string and place it in a buffer
2040 * @buf: The buffer to place the result into
2041 * @size: The size of the buffer, including the trailing null space
2042 * @fmt: The format string to use
2043 * @...: Arguments for the format string
2044 *
2045 * The return value is the number of characters which would be
2046 * generated for the given input, excluding the trailing null,
2047 * as per ISO C99. If the return is greater than or equal to
2048 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002049 *
2050 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002052int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 va_list args;
2055 int i;
2056
2057 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002058 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return i;
2062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063EXPORT_SYMBOL(snprintf);
2064
2065/**
2066 * scnprintf - Format a string and place it in a buffer
2067 * @buf: The buffer to place the result into
2068 * @size: The size of the buffer, including the trailing null space
2069 * @fmt: The format string to use
2070 * @...: Arguments for the format string
2071 *
2072 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002073 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
2075
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002076int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 va_list args;
2079 int i;
2080
2081 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002082 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002084
Anton Arapovb921c692011-01-12 16:59:49 -08002085 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087EXPORT_SYMBOL(scnprintf);
2088
2089/**
2090 * vsprintf - Format a string and place it in a buffer
2091 * @buf: The buffer to place the result into
2092 * @fmt: The format string to use
2093 * @args: Arguments for the format string
2094 *
2095 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002096 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 * buffer overflows.
2098 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002099 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002100 *
2101 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 */
2103int vsprintf(char *buf, const char *fmt, va_list args)
2104{
2105 return vsnprintf(buf, INT_MAX, fmt, args);
2106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107EXPORT_SYMBOL(vsprintf);
2108
2109/**
2110 * sprintf - Format a string and place it in a buffer
2111 * @buf: The buffer to place the result into
2112 * @fmt: The format string to use
2113 * @...: Arguments for the format string
2114 *
2115 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002116 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002118 *
2119 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002121int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 va_list args;
2124 int i;
2125
2126 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002127 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return i;
2131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132EXPORT_SYMBOL(sprintf);
2133
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002134#ifdef CONFIG_BINARY_PRINTF
2135/*
2136 * bprintf service:
2137 * vbin_printf() - VA arguments to binary data
2138 * bstr_printf() - Binary data to text string
2139 */
2140
2141/**
2142 * vbin_printf - Parse a format string and place args' binary value in a buffer
2143 * @bin_buf: The buffer to place args' binary value
2144 * @size: The size of the buffer(by words(32bits), not characters)
2145 * @fmt: The format string to use
2146 * @args: Arguments for the format string
2147 *
2148 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002149 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002150 *
2151 * The return value is the number of words(32bits) which would be generated for
2152 * the given input.
2153 *
2154 * NOTE:
2155 * If the return value is greater than @size, the resulting bin_buf is NOT
2156 * valid for bstr_printf().
2157 */
2158int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2159{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002160 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002161 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002162
2163 str = (char *)bin_buf;
2164 end = (char *)(bin_buf + size);
2165
2166#define save_arg(type) \
2167do { \
2168 if (sizeof(type) == 8) { \
2169 unsigned long long value; \
2170 str = PTR_ALIGN(str, sizeof(u32)); \
2171 value = va_arg(args, unsigned long long); \
2172 if (str + sizeof(type) <= end) { \
2173 *(u32 *)str = *(u32 *)&value; \
2174 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2175 } \
2176 } else { \
2177 unsigned long value; \
2178 str = PTR_ALIGN(str, sizeof(type)); \
2179 value = va_arg(args, int); \
2180 if (str + sizeof(type) <= end) \
2181 *(typeof(type) *)str = (type)value; \
2182 } \
2183 str += sizeof(type); \
2184} while (0)
2185
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002186 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002187 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002188
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002189 fmt += read;
2190
2191 switch (spec.type) {
2192 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002193 case FORMAT_TYPE_INVALID:
2194 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002195 break;
2196
Vegard Nossumed681a92009-03-14 12:08:50 +01002197 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002198 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002199 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002200 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002201
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002202 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002203 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002204 break;
2205
2206 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002207 const char *save_str = va_arg(args, char *);
2208 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002209
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002210 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2211 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002212 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002213 len = strlen(save_str) + 1;
2214 if (str + len < end)
2215 memcpy(str, save_str, len);
2216 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002217 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002218 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002219
2220 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002221 save_arg(void *);
2222 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002223 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002224 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002225 break;
2226
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002227 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002228 switch (spec.type) {
2229
2230 case FORMAT_TYPE_LONG_LONG:
2231 save_arg(long long);
2232 break;
2233 case FORMAT_TYPE_ULONG:
2234 case FORMAT_TYPE_LONG:
2235 save_arg(unsigned long);
2236 break;
2237 case FORMAT_TYPE_SIZE_T:
2238 save_arg(size_t);
2239 break;
2240 case FORMAT_TYPE_PTRDIFF:
2241 save_arg(ptrdiff_t);
2242 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002243 case FORMAT_TYPE_UBYTE:
2244 case FORMAT_TYPE_BYTE:
2245 save_arg(char);
2246 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002247 case FORMAT_TYPE_USHORT:
2248 case FORMAT_TYPE_SHORT:
2249 save_arg(short);
2250 break;
2251 default:
2252 save_arg(int);
2253 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002254 }
2255 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002256
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002257 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002258#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002259}
2260EXPORT_SYMBOL_GPL(vbin_printf);
2261
2262/**
2263 * bstr_printf - Format a string from binary arguments and place it in a buffer
2264 * @buf: The buffer to place the result into
2265 * @size: The size of the buffer, including the trailing null space
2266 * @fmt: The format string to use
2267 * @bin_buf: Binary arguments for the format string
2268 *
2269 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2270 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2271 * a binary buffer that generated by vbin_printf.
2272 *
2273 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002274 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002275 *
2276 * The return value is the number of characters which would
2277 * be generated for the given input, excluding the trailing
2278 * '\0', as per ISO C99. If you want to have the exact
2279 * number of characters written into @buf as return value
2280 * (not including the trailing '\0'), use vscnprintf(). If the
2281 * return is greater than or equal to @size, the resulting
2282 * string is truncated.
2283 */
2284int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2285{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002286 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002287 char *str, *end;
2288 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002289
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07002290 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002291 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002292
2293 str = buf;
2294 end = buf + size;
2295
2296#define get_arg(type) \
2297({ \
2298 typeof(type) value; \
2299 if (sizeof(type) == 8) { \
2300 args = PTR_ALIGN(args, sizeof(u32)); \
2301 *(u32 *)&value = *(u32 *)args; \
2302 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2303 } else { \
2304 args = PTR_ALIGN(args, sizeof(type)); \
2305 value = *(typeof(type) *)args; \
2306 } \
2307 args += sizeof(type); \
2308 value; \
2309})
2310
2311 /* Make sure end is always >= buf */
2312 if (end < buf) {
2313 end = ((void *)-1);
2314 size = end - buf;
2315 }
2316
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002317 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002318 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002319 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002320
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002321 fmt += read;
2322
2323 switch (spec.type) {
2324 case FORMAT_TYPE_NONE: {
2325 int copy = read;
2326 if (str < end) {
2327 if (copy > end - str)
2328 copy = end - str;
2329 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002330 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002331 str += read;
2332 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002333 }
2334
Vegard Nossumed681a92009-03-14 12:08:50 +01002335 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002336 spec.field_width = get_arg(int);
2337 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002338
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002339 case FORMAT_TYPE_PRECISION:
2340 spec.precision = get_arg(int);
2341 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002342
André Goddard Rosad4be1512009-12-14 18:00:59 -08002343 case FORMAT_TYPE_CHAR: {
2344 char c;
2345
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002346 if (!(spec.flags & LEFT)) {
2347 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002348 if (str < end)
2349 *str = ' ';
2350 ++str;
2351 }
2352 }
2353 c = (unsigned char) get_arg(char);
2354 if (str < end)
2355 *str = c;
2356 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002357 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002358 if (str < end)
2359 *str = ' ';
2360 ++str;
2361 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002362 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002363 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002364
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002365 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002366 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002367 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002368 str = string(str, end, (char *)str_arg, spec);
2369 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002370 }
2371
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002372 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002373 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002374 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002375 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002376 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002377
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002378 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002379 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002380 if (str < end)
2381 *str = '%';
2382 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002383 break;
2384
André Goddard Rosad4be1512009-12-14 18:00:59 -08002385 default: {
2386 unsigned long long num;
2387
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002388 switch (spec.type) {
2389
2390 case FORMAT_TYPE_LONG_LONG:
2391 num = get_arg(long long);
2392 break;
2393 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002394 case FORMAT_TYPE_LONG:
2395 num = get_arg(unsigned long);
2396 break;
2397 case FORMAT_TYPE_SIZE_T:
2398 num = get_arg(size_t);
2399 break;
2400 case FORMAT_TYPE_PTRDIFF:
2401 num = get_arg(ptrdiff_t);
2402 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002403 case FORMAT_TYPE_UBYTE:
2404 num = get_arg(unsigned char);
2405 break;
2406 case FORMAT_TYPE_BYTE:
2407 num = get_arg(signed char);
2408 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002409 case FORMAT_TYPE_USHORT:
2410 num = get_arg(unsigned short);
2411 break;
2412 case FORMAT_TYPE_SHORT:
2413 num = get_arg(short);
2414 break;
2415 case FORMAT_TYPE_UINT:
2416 num = get_arg(unsigned int);
2417 break;
2418 default:
2419 num = get_arg(int);
2420 }
2421
2422 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002423 } /* default: */
2424 } /* switch(spec.type) */
2425 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002426
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002427 if (size > 0) {
2428 if (str < end)
2429 *str = '\0';
2430 else
2431 end[-1] = '\0';
2432 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002433
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002434#undef get_arg
2435
2436 /* the trailing null byte doesn't count towards the total */
2437 return str - buf;
2438}
2439EXPORT_SYMBOL_GPL(bstr_printf);
2440
2441/**
2442 * bprintf - Parse a format string and place args' binary value in a buffer
2443 * @bin_buf: The buffer to place args' binary value
2444 * @size: The size of the buffer(by words(32bits), not characters)
2445 * @fmt: The format string to use
2446 * @...: Arguments for the format string
2447 *
2448 * The function returns the number of words(u32) written
2449 * into @bin_buf.
2450 */
2451int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2452{
2453 va_list args;
2454 int ret;
2455
2456 va_start(args, fmt);
2457 ret = vbin_printf(bin_buf, size, fmt, args);
2458 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002459
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002460 return ret;
2461}
2462EXPORT_SYMBOL_GPL(bprintf);
2463
2464#endif /* CONFIG_BINARY_PRINTF */
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466/**
2467 * vsscanf - Unformat a buffer into a list of arguments
2468 * @buf: input buffer
2469 * @fmt: format of buffer
2470 * @args: arguments
2471 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002472int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 const char *str = buf;
2475 char *next;
2476 char digit;
2477 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002478 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002479 unsigned int base;
2480 union {
2481 long long s;
2482 unsigned long long u;
2483 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002484 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002485 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
Jan Beulichda990752012-10-04 17:13:24 -07002487 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /* skip any white space in format */
2489 /* white space in format matchs any amount of
2490 * white space, including none, in the input.
2491 */
2492 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002493 fmt = skip_spaces(++fmt);
2494 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496
2497 /* anything that is not a conversion must match exactly */
2498 if (*fmt != '%' && *fmt) {
2499 if (*fmt++ != *str++)
2500 break;
2501 continue;
2502 }
2503
2504 if (!*fmt)
2505 break;
2506 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* skip this conversion.
2509 * advance both strings to next white space
2510 */
2511 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002512 if (!*str)
2513 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002514 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 fmt++;
2516 while (!isspace(*str) && *str)
2517 str++;
2518 continue;
2519 }
2520
2521 /* get field width */
2522 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002523 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002525 if (field_width <= 0)
2526 break;
2527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 /* get conversion qualifier */
2530 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002531 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2532 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 qualifier = *fmt++;
2534 if (unlikely(qualifier == *fmt)) {
2535 if (qualifier == 'h') {
2536 qualifier = 'H';
2537 fmt++;
2538 } else if (qualifier == 'l') {
2539 qualifier = 'L';
2540 fmt++;
2541 }
2542 }
2543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Jan Beulichda990752012-10-04 17:13:24 -07002545 if (!*fmt)
2546 break;
2547
2548 if (*fmt == 'n') {
2549 /* return number of characters read so far */
2550 *va_arg(args, int *) = str - buf;
2551 ++fmt;
2552 continue;
2553 }
2554
2555 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 break;
2557
André Goddard Rosad4be1512009-12-14 18:00:59 -08002558 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002559 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002560
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002561 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 case 'c':
2563 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002564 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (field_width == -1)
2566 field_width = 1;
2567 do {
2568 *s++ = *str++;
2569 } while (--field_width > 0 && *str);
2570 num++;
2571 }
2572 continue;
2573 case 's':
2574 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002575 char *s = (char *)va_arg(args, char *);
2576 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002577 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002579 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002582 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 *s = '\0';
2585 num++;
2586 }
2587 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 case 'o':
2589 base = 8;
2590 break;
2591 case 'x':
2592 case 'X':
2593 base = 16;
2594 break;
2595 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002596 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002598 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 case 'u':
2600 break;
2601 case '%':
2602 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002603 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return num;
2605 continue;
2606 default:
2607 /* invalid format; stop here */
2608 return num;
2609 }
2610
2611 /* have some sort of integer conversion.
2612 * first, skip white space in buffer.
2613 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002614 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 digit = *str;
2617 if (is_sign && digit == '-')
2618 digit = *(str + 1);
2619
2620 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002621 || (base == 16 && !isxdigit(digit))
2622 || (base == 10 && !isdigit(digit))
2623 || (base == 8 && (!isdigit(digit) || digit > '7'))
2624 || (base == 0 && !isdigit(digit)))
2625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Jan Beulich53809752012-12-17 16:01:31 -08002627 if (is_sign)
2628 val.s = qualifier != 'L' ?
2629 simple_strtol(str, &next, base) :
2630 simple_strtoll(str, &next, base);
2631 else
2632 val.u = qualifier != 'L' ?
2633 simple_strtoul(str, &next, base) :
2634 simple_strtoull(str, &next, base);
2635
2636 if (field_width > 0 && next - str > field_width) {
2637 if (base == 0)
2638 _parse_integer_fixup_radix(str, &base);
2639 while (next - str > field_width) {
2640 if (is_sign)
2641 val.s = div_s64(val.s, base);
2642 else
2643 val.u = div_u64(val.u, base);
2644 --next;
2645 }
2646 }
2647
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002648 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002650 if (is_sign)
2651 *va_arg(args, signed char *) = val.s;
2652 else
2653 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 break;
2655 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002656 if (is_sign)
2657 *va_arg(args, short *) = val.s;
2658 else
2659 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 break;
2661 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002662 if (is_sign)
2663 *va_arg(args, long *) = val.s;
2664 else
2665 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 break;
2667 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002668 if (is_sign)
2669 *va_arg(args, long long *) = val.s;
2670 else
2671 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 break;
2673 case 'Z':
2674 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002675 *va_arg(args, size_t *) = val.u;
2676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 default:
Jan Beulich53809752012-12-17 16:01:31 -08002678 if (is_sign)
2679 *va_arg(args, int *) = val.s;
2680 else
2681 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 break;
2683 }
2684 num++;
2685
2686 if (!next)
2687 break;
2688 str = next;
2689 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return num;
2692}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693EXPORT_SYMBOL(vsscanf);
2694
2695/**
2696 * sscanf - Unformat a buffer into a list of arguments
2697 * @buf: input buffer
2698 * @fmt: formatting of buffer
2699 * @...: resulting arguments
2700 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002701int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
2703 va_list args;
2704 int i;
2705
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002706 va_start(args, fmt);
2707 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return i;
2711}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712EXPORT_SYMBOL(sscanf);