blob: f9cee8e1233c0fe0f626fe2680ca4b9ff9d3153e [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>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070020#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070021#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050022#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ctype.h>
26#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070027#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080028#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070029#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110030#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040031#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080032#include <linux/cred.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000033#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Tim Schmielau4e57b682005-10-30 15:03:48 -080035#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050036#include <asm/sections.h> /* for dereference_function_descriptor() */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070037#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Andy Shevchenko71dca952014-10-13 15:55:18 -070039#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070040#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * simple_strtoull - convert a string to an unsigned long long
44 * @cp: The start of the string
45 * @endp: A pointer to the end of the parsed string will be placed here
46 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080047 *
48 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
Harvey Harrison22d27052008-10-16 13:40:35 -070050unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070052 unsigned long long result;
53 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070055 cp = _parse_integer_fixup_radix(cp, &base);
56 rv = _parse_integer(cp, base, &result);
57 /* FIXME */
58 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (endp)
61 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return result;
64}
Linus Torvalds1da177e2005-04-16 15:20:36 -070065EXPORT_SYMBOL(simple_strtoull);
66
67/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080068 * simple_strtoul - convert a string to an unsigned long
69 * @cp: The start of the string
70 * @endp: A pointer to the end of the parsed string will be placed here
71 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080072 *
73 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080074 */
75unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
76{
77 return simple_strtoull(cp, endp, base);
78}
79EXPORT_SYMBOL(simple_strtoul);
80
81/**
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080086 *
87 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080088 */
89long simple_strtol(const char *cp, char **endp, unsigned int base)
90{
91 if (*cp == '-')
92 return -simple_strtoul(cp + 1, endp, base);
93
94 return simple_strtoul(cp, endp, base);
95}
96EXPORT_SYMBOL(simple_strtol);
97
98/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * simple_strtoll - convert a string to a signed long long
100 * @cp: The start of the string
101 * @endp: A pointer to the end of the parsed string will be placed here
102 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800103 *
104 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700106long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800108 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700109 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800110
Harvey Harrison22d27052008-10-16 13:40:35 -0700111 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400113EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Joe Perchescf3b4292010-05-24 14:33:16 -0700115static noinline_for_stack
116int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800118 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800120 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800122 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return i;
125}
126
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700127/*
128 * Decimal conversion is by far the most typical, and is used for
129 * /proc and /sys data. This directly impacts e.g. top performance
130 * with many processes running. We optimize it for speed by emitting
131 * two characters at a time, using a 200 byte lookup table. This
132 * roughly halves the number of multiplications compared to computing
133 * the digits one at a time. Implementation strongly inspired by the
134 * previous version, which in turn used ideas described at
135 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
136 * from the author, Douglas W. Jones).
137 *
138 * It turns out there is precisely one 26 bit fixed-point
139 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
140 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
141 * range happens to be somewhat larger (x <= 1073741898), but that's
142 * irrelevant for our purpose.
143 *
144 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
145 * need a 32x32->64 bit multiply, so we simply use the same constant.
146 *
147 * For dividing a number in the range [100, 10^4-1] by 100, there are
148 * several options. The simplest is (x * 0x147b) >> 19, which is valid
149 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700150 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700151
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700152static const u16 decpair[100] = {
153#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
154 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
155 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
156 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
157 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
158 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
159 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
160 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
161 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
162 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
163 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
164#undef _
165};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700166
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700167/*
168 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700169 * immediately jump to out_r where two 0s would be written but only
170 * one of them accounted for in buf. This is needed by ip4_string
171 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700172*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700173static noinline_for_stack
174char *put_dec_trunc8(char *buf, unsigned r)
175{
176 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700177
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700178 /* 1 <= r < 10^8 */
179 if (r < 100)
180 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700181
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700182 /* 100 <= r < 10^8 */
183 q = (r * (u64)0x28f5c29) >> 32;
184 *((u16 *)buf) = decpair[r - 100*q];
185 buf += 2;
186
187 /* 1 <= q < 10^6 */
188 if (q < 100)
189 goto out_q;
190
191 /* 100 <= q < 10^6 */
192 r = (q * (u64)0x28f5c29) >> 32;
193 *((u16 *)buf) = decpair[q - 100*r];
194 buf += 2;
195
196 /* 1 <= r < 10^4 */
197 if (r < 100)
198 goto out_r;
199
200 /* 100 <= r < 10^4 */
201 q = (r * 0x147b) >> 19;
202 *((u16 *)buf) = decpair[r - 100*q];
203 buf += 2;
204out_q:
205 /* 1 <= q < 100 */
206 r = q;
207out_r:
208 /* 1 <= r < 100 */
209 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700210 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700211 return buf;
212}
213
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700214#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
215static noinline_for_stack
216char *put_dec_full8(char *buf, unsigned r)
217{
218 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700219
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700220 /* 0 <= r < 10^8 */
221 q = (r * (u64)0x28f5c29) >> 32;
222 *((u16 *)buf) = decpair[r - 100*q];
223 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700224
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700225 /* 0 <= q < 10^6 */
226 r = (q * (u64)0x28f5c29) >> 32;
227 *((u16 *)buf) = decpair[q - 100*r];
228 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700229
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700230 /* 0 <= r < 10^4 */
231 q = (r * 0x147b) >> 19;
232 *((u16 *)buf) = decpair[r - 100*q];
233 buf += 2;
234
235 /* 0 <= q < 100 */
236 *((u16 *)buf) = decpair[q];
237 buf += 2;
238 return buf;
239}
240
241static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700242char *put_dec(char *buf, unsigned long long n)
243{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700244 if (n >= 100*1000*1000)
245 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
246 /* 1 <= n <= 1.6e11 */
247 if (n >= 100*1000*1000)
248 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
249 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700250 return put_dec_trunc8(buf, n);
251}
252
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700253#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700254
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700255static void
256put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700257{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700258 unsigned q;
259
260 /* 0 <= r < 10^4 */
261 q = (r * 0x147b) >> 19;
262 *((u16 *)buf) = decpair[r - 100*q];
263 buf += 2;
264 /* 0 <= q < 100 */
265 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700266}
267
268/*
269 * Call put_dec_full4 on x % 10000, return x / 10000.
270 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
271 * holds for all x < 1,128,869,999. The largest value this
272 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700273 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700274 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700275static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700276unsigned put_dec_helper4(char *buf, unsigned x)
277{
278 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
279
280 put_dec_full4(buf, x - q * 10000);
281 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700282}
283
284/* Based on code by Douglas W. Jones found at
285 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
286 * (with permission from the author).
287 * Performs no 64-bit division and hence should be fast on 32-bit machines.
288 */
289static
290char *put_dec(char *buf, unsigned long long n)
291{
292 uint32_t d3, d2, d1, q, h;
293
294 if (n < 100*1000*1000)
295 return put_dec_trunc8(buf, n);
296
297 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
298 h = (n >> 32);
299 d2 = (h ) & 0xffff;
300 d3 = (h >> 16); /* implicit "& 0xffff" */
301
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700302 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
303 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700304 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700305 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700306
George Spelvin23591722012-10-04 17:12:29 -0700307 q += 7671 * d3 + 9496 * d2 + 6 * d1;
308 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700309
George Spelvin23591722012-10-04 17:12:29 -0700310 q += 4749 * d3 + 42 * d2;
311 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700312
George Spelvin23591722012-10-04 17:12:29 -0700313 q += 281 * d3;
314 buf += 12;
315 if (q)
316 buf = put_dec_trunc8(buf, q);
317 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700318 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800319
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700320 return buf;
321}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700322
323#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700324
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700325/*
326 * Convert passed number to decimal string.
327 * Returns the length of string. On buffer overflow, returns 0.
328 *
329 * If speed is not important, use snprintf(). It's easy to read the code.
330 */
331int num_to_str(char *buf, int size, unsigned long long num)
332{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700333 /* put_dec requires 2-byte alignment of the buffer. */
334 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700335 int idx, len;
336
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700337 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
338 if (num <= 9) {
339 tmp[0] = '0' + num;
340 len = 1;
341 } else {
342 len = put_dec(tmp, num) - tmp;
343 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700344
345 if (len > size)
346 return 0;
347 for (idx = 0; idx < len; ++idx)
348 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700349 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700350}
351
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700352#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700353#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#define PLUS 4 /* show plus */
355#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700356#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700357#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
358#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100360enum format_type {
361 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100362 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100363 FORMAT_TYPE_PRECISION,
364 FORMAT_TYPE_CHAR,
365 FORMAT_TYPE_STR,
366 FORMAT_TYPE_PTR,
367 FORMAT_TYPE_PERCENT_CHAR,
368 FORMAT_TYPE_INVALID,
369 FORMAT_TYPE_LONG_LONG,
370 FORMAT_TYPE_ULONG,
371 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800372 FORMAT_TYPE_UBYTE,
373 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100374 FORMAT_TYPE_USHORT,
375 FORMAT_TYPE_SHORT,
376 FORMAT_TYPE_UINT,
377 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100378 FORMAT_TYPE_SIZE_T,
379 FORMAT_TYPE_PTRDIFF
380};
381
382struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700383 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800384 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700385 u8 base; /* number base, 8, 10 or 16 only */
386 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
387 s16 field_width; /* width of output field */
388 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100389};
390
Joe Perchescf3b4292010-05-24 14:33:16 -0700391static noinline_for_stack
392char *number(char *buf, char *end, unsigned long long num,
393 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700395 /* put_dec requires 2-byte alignment of the buffer. */
396 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100397 char sign;
398 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100399 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700401 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100403 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
404 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100405 locase = (spec.flags & SMALL);
406 if (spec.flags & LEFT)
407 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100409 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800410 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800412 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100413 spec.field_width--;
414 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100416 spec.field_width--;
417 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100419 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700422 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100423 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700424 spec.field_width -= 2;
425 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100426 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700428
429 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700431 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700432 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100433 else if (spec.base != 10) { /* 8 or 16 */
434 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700435 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800436
437 if (spec.base == 16)
438 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700439 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700440 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700441 num >>= shift;
442 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700443 } else { /* base 10 */
444 i = put_dec(tmp, num) - tmp;
445 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700446
447 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100448 if (i > spec.precision)
449 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100451 spec.field_width -= spec.precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700452 if (!(spec.flags & (ZEROPAD | LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800453 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700454 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *buf = ' ';
456 ++buf;
457 }
458 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700459 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700461 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *buf = sign;
463 ++buf;
464 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700465 /* "0x" / "0" prefix */
466 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700467 if (spec.base == 16 || !is_zero) {
468 if (buf < end)
469 *buf = '0';
470 ++buf;
471 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100472 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700473 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100474 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 ++buf;
476 }
477 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700478 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100479 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700480 char c = ' ' + (spec.flags & ZEROPAD);
481 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100482 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700483 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *buf = c;
485 ++buf;
486 }
487 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700488 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100489 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700490 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 *buf = '0';
492 ++buf;
493 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700494 /* actual digits of result */
495 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700496 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *buf = tmp[i];
498 ++buf;
499 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700500 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100501 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700502 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *buf = ' ';
504 ++buf;
505 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return buf;
508}
509
Joe Perchescf3b4292010-05-24 14:33:16 -0700510static noinline_for_stack
511char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700512{
513 int len, i;
514
515 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800516 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700517
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100518 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700519
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100520 if (!(spec.flags & LEFT)) {
521 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700522 if (buf < end)
523 *buf = ' ';
524 ++buf;
525 }
526 }
527 for (i = 0; i < len; ++i) {
528 if (buf < end)
529 *buf = *s;
530 ++buf; ++s;
531 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100532 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700533 if (buf < end)
534 *buf = ' ';
535 ++buf;
536 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800537
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700538 return buf;
539}
540
Al Viro4b6ccca2013-09-03 12:00:44 -0400541static void widen(char *buf, char *end, unsigned len, unsigned spaces)
542{
543 size_t size;
544 if (buf >= end) /* nowhere to put anything */
545 return;
546 size = end - buf;
547 if (size <= spaces) {
548 memset(buf, ' ', size);
549 return;
550 }
551 if (len) {
552 if (len > size - spaces)
553 len = size - spaces;
554 memmove(buf + spaces, buf, len);
555 }
556 memset(buf, ' ', spaces);
557}
558
559static noinline_for_stack
560char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
561 const char *fmt)
562{
563 const char *array[4], *s;
564 const struct dentry *p;
565 int depth;
566 int i, n;
567
568 switch (fmt[1]) {
569 case '2': case '3': case '4':
570 depth = fmt[1] - '0';
571 break;
572 default:
573 depth = 1;
574 }
575
576 rcu_read_lock();
577 for (i = 0; i < depth; i++, d = p) {
578 p = ACCESS_ONCE(d->d_parent);
579 array[i] = ACCESS_ONCE(d->d_name.name);
580 if (p == d) {
581 if (i)
582 array[i] = "";
583 i++;
584 break;
585 }
586 }
587 s = array[--i];
588 for (n = 0; n != spec.precision; n++, buf++) {
589 char c = *s++;
590 if (!c) {
591 if (!i)
592 break;
593 c = '/';
594 s = array[--i];
595 }
596 if (buf < end)
597 *buf = c;
598 }
599 rcu_read_unlock();
600 if (n < spec.field_width) {
601 /* we want to pad the sucker */
602 unsigned spaces = spec.field_width - n;
603 if (!(spec.flags & LEFT)) {
604 widen(buf - n, end, n, spaces);
605 return buf + spaces;
606 }
607 while (spaces--) {
608 if (buf < end)
609 *buf = ' ';
610 ++buf;
611 }
612 }
613 return buf;
614}
615
Joe Perchescf3b4292010-05-24 14:33:16 -0700616static noinline_for_stack
617char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800618 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700619{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800620 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700621#ifdef CONFIG_KALLSYMS
622 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800623#endif
624
625 if (fmt[1] == 'R')
626 ptr = __builtin_extract_return_addr(ptr);
627 value = (unsigned long)ptr;
628
629#ifdef CONFIG_KALLSYMS
630 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900631 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800632 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200633 sprint_symbol(sym, value);
634 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700635 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800636
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100637 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700638#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800639 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100640 spec.flags |= SPECIAL | SMALL | ZEROPAD;
641 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800642
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100643 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700644#endif
645}
646
Joe Perchescf3b4292010-05-24 14:33:16 -0700647static noinline_for_stack
648char *resource_string(char *buf, char *end, struct resource *res,
649 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100650{
651#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600652#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100653#endif
654
655#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600656#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100657#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700658 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100659 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700660 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100661 .precision = -1,
662 .flags = SPECIAL | SMALL | ZEROPAD,
663 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700664 static const struct printf_spec mem_spec = {
665 .base = 16,
666 .field_width = MEM_RSRC_PRINTK_SIZE,
667 .precision = -1,
668 .flags = SPECIAL | SMALL | ZEROPAD,
669 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700670 static const struct printf_spec bus_spec = {
671 .base = 16,
672 .field_width = 2,
673 .precision = -1,
674 .flags = SMALL | ZEROPAD,
675 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700676 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600677 .base = 10,
678 .precision = -1,
679 .flags = 0,
680 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700681 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600682 .field_width = -1,
683 .precision = 10,
684 .flags = LEFT,
685 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700686 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600687 .base = 16,
688 .precision = -1,
689 .flags = SPECIAL | SMALL,
690 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600691
692 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
693 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
694#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
695#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700696#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600697#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
698 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
699 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
700
Linus Torvalds332d2e72008-10-20 15:07:34 +1100701 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600702 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700703 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100704
705 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700706 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600707 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700708 specp = &io_spec;
709 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600710 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700711 specp = &mem_spec;
712 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600713 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700714 specp = &dec_spec;
715 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600716 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700717 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700718 } else if (res->flags & IORESOURCE_BUS) {
719 p = string(p, pend, "bus ", str_spec);
720 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700721 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600722 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700723 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600724 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600725 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700726 if (decode && res->flags & IORESOURCE_UNSET) {
727 p = string(p, pend, "size ", str_spec);
728 p = number(p, pend, resource_size(res), *specp);
729 } else {
730 p = number(p, pend, res->start, *specp);
731 if (res->start != res->end) {
732 *p++ = '-';
733 p = number(p, pend, res->end, *specp);
734 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600735 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600736 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600737 if (res->flags & IORESOURCE_MEM_64)
738 p = string(p, pend, " 64bit", str_spec);
739 if (res->flags & IORESOURCE_PREFETCH)
740 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700741 if (res->flags & IORESOURCE_WINDOW)
742 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600743 if (res->flags & IORESOURCE_DISABLED)
744 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600745 } else {
746 p = string(p, pend, " flags ", str_spec);
747 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600748 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100749 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600750 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100751
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100752 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100753}
754
Joe Perchescf3b4292010-05-24 14:33:16 -0700755static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700756char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
757 const char *fmt)
758{
Steven Rostedt360603a2013-05-28 15:47:39 -0400759 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700760 negative value, fallback to the default */
761 char separator;
762
763 if (spec.field_width == 0)
764 /* nothing to print */
765 return buf;
766
767 if (ZERO_OR_NULL_PTR(addr))
768 /* NULL pointer */
769 return string(buf, end, NULL, spec);
770
771 switch (fmt[1]) {
772 case 'C':
773 separator = ':';
774 break;
775 case 'D':
776 separator = '-';
777 break;
778 case 'N':
779 separator = 0;
780 break;
781 default:
782 separator = ' ';
783 break;
784 }
785
786 if (spec.field_width > 0)
787 len = min_t(int, spec.field_width, 64);
788
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700789 for (i = 0; i < len; ++i) {
790 if (buf < end)
791 *buf = hex_asc_hi(addr[i]);
792 ++buf;
793 if (buf < end)
794 *buf = hex_asc_lo(addr[i]);
795 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700796
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700797 if (separator && i != len - 1) {
798 if (buf < end)
799 *buf = separator;
800 ++buf;
801 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700802 }
803
804 return buf;
805}
806
807static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800808char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
809 struct printf_spec spec, const char *fmt)
810{
811 const int CHUNKSZ = 32;
812 int nr_bits = max_t(int, spec.field_width, 0);
813 int i, chunksz;
814 bool first = true;
815
816 /* reused to print numbers */
817 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
818
819 chunksz = nr_bits & (CHUNKSZ - 1);
820 if (chunksz == 0)
821 chunksz = CHUNKSZ;
822
823 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
824 for (; i >= 0; i -= CHUNKSZ) {
825 u32 chunkmask, val;
826 int word, bit;
827
828 chunkmask = ((1ULL << chunksz) - 1);
829 word = i / BITS_PER_LONG;
830 bit = i % BITS_PER_LONG;
831 val = (bitmap[word] >> bit) & chunkmask;
832
833 if (!first) {
834 if (buf < end)
835 *buf = ',';
836 buf++;
837 }
838 first = false;
839
840 spec.field_width = DIV_ROUND_UP(chunksz, 4);
841 buf = number(buf, end, val, spec);
842
843 chunksz = CHUNKSZ;
844 }
845 return buf;
846}
847
848static noinline_for_stack
849char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
850 struct printf_spec spec, const char *fmt)
851{
852 int nr_bits = max_t(int, spec.field_width, 0);
853 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
854 int cur, rbot, rtop;
855 bool first = true;
856
857 /* reused to print numbers */
858 spec = (struct printf_spec){ .base = 10 };
859
860 rbot = cur = find_first_bit(bitmap, nr_bits);
861 while (cur < nr_bits) {
862 rtop = cur;
863 cur = find_next_bit(bitmap, nr_bits, cur + 1);
864 if (cur < nr_bits && cur <= rtop + 1)
865 continue;
866
867 if (!first) {
868 if (buf < end)
869 *buf = ',';
870 buf++;
871 }
872 first = false;
873
874 buf = number(buf, end, rbot, spec);
875 if (rbot < rtop) {
876 if (buf < end)
877 *buf = '-';
878 buf++;
879
880 buf = number(buf, end, rtop, spec);
881 }
882
883 rbot = cur;
884 }
885 return buf;
886}
887
888static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700889char *mac_address_string(char *buf, char *end, u8 *addr,
890 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700891{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000892 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700893 char *p = mac_addr;
894 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000895 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700896 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000897
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700898 switch (fmt[1]) {
899 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000900 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700901 break;
902
903 case 'R':
904 reversed = true;
905 /* fall through */
906
907 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000908 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700909 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000910 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700911
912 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700913 if (reversed)
914 p = hex_byte_pack(p, addr[5 - i]);
915 else
916 p = hex_byte_pack(p, addr[i]);
917
Joe Perches8a27f7c2009-08-17 12:29:44 +0000918 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000919 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700920 }
921 *p = '\0';
922
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100923 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700924}
925
Joe Perchescf3b4292010-05-24 14:33:16 -0700926static noinline_for_stack
927char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700928{
Harvey Harrison689afa72008-10-28 16:04:44 -0700929 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800930 bool leading_zeros = (fmt[0] == 'i');
931 int index;
932 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700933
Joe Perches0159f242010-01-13 20:23:30 -0800934 switch (fmt[2]) {
935 case 'h':
936#ifdef __BIG_ENDIAN
937 index = 0;
938 step = 1;
939#else
940 index = 3;
941 step = -1;
942#endif
943 break;
944 case 'l':
945 index = 3;
946 step = -1;
947 break;
948 case 'n':
949 case 'b':
950 default:
951 index = 0;
952 step = 1;
953 break;
954 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000955 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700956 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700957 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000958 if (leading_zeros) {
959 if (digits < 3)
960 *p++ = '0';
961 if (digits < 2)
962 *p++ = '0';
963 }
964 /* reverse the digits in the quad */
965 while (digits--)
966 *p++ = temp[digits];
967 if (i < 3)
968 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800969 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000970 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000971 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800972
Joe Perches8a27f7c2009-08-17 12:29:44 +0000973 return p;
974}
975
Joe Perchescf3b4292010-05-24 14:33:16 -0700976static noinline_for_stack
977char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000978{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800979 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000980 unsigned char zerolength[8];
981 int longest = 1;
982 int colonpos = -1;
983 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800984 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000985 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000986 bool useIPv4;
987 struct in6_addr in6;
988
989 memcpy(&in6, addr, sizeof(struct in6_addr));
990
991 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000992
993 memset(zerolength, 0, sizeof(zerolength));
994
995 if (useIPv4)
996 range = 6;
997 else
998 range = 8;
999
1000 /* find position of longest 0 run */
1001 for (i = 0; i < range; i++) {
1002 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001003 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001004 break;
1005 zerolength[i]++;
1006 }
1007 }
1008 for (i = 0; i < range; i++) {
1009 if (zerolength[i] > longest) {
1010 longest = zerolength[i];
1011 colonpos = i;
1012 }
1013 }
Joe Perches29cf5192011-06-09 11:23:37 -07001014 if (longest == 1) /* don't compress a single 0 */
1015 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001016
1017 /* emit address */
1018 for (i = 0; i < range; i++) {
1019 if (i == colonpos) {
1020 if (needcolon || i == 0)
1021 *p++ = ':';
1022 *p++ = ':';
1023 needcolon = false;
1024 i += longest - 1;
1025 continue;
1026 }
1027 if (needcolon) {
1028 *p++ = ':';
1029 needcolon = false;
1030 }
1031 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001032 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001033 hi = word >> 8;
1034 lo = word & 0xff;
1035 if (hi) {
1036 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001037 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001038 else
1039 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001040 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001041 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001042 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001043 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001044 else
1045 *p++ = hex_asc_lo(lo);
1046 needcolon = true;
1047 }
1048
1049 if (useIPv4) {
1050 if (needcolon)
1051 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001052 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001053 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001054 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001055
Joe Perches8a27f7c2009-08-17 12:29:44 +00001056 return p;
1057}
1058
Joe Perchescf3b4292010-05-24 14:33:16 -07001059static noinline_for_stack
1060char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001061{
1062 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001063
Harvey Harrison689afa72008-10-28 16:04:44 -07001064 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001065 p = hex_byte_pack(p, *addr++);
1066 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001067 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001068 *p++ = ':';
1069 }
1070 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001071
Joe Perches8a27f7c2009-08-17 12:29:44 +00001072 return p;
1073}
1074
Joe Perchescf3b4292010-05-24 14:33:16 -07001075static noinline_for_stack
1076char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1077 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001078{
1079 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1080
1081 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001082 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001083 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001084 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001085
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001086 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001087}
1088
Joe Perchescf3b4292010-05-24 14:33:16 -07001089static noinline_for_stack
1090char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1091 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001092{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001093 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001094
Joe Perches0159f242010-01-13 20:23:30 -08001095 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001096
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001097 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001098}
1099
Joe Perchescf3b4292010-05-24 14:33:16 -07001100static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001101char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1102 struct printf_spec spec, const char *fmt)
1103{
1104 bool have_p = false, have_s = false, have_f = false, have_c = false;
1105 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1106 sizeof(":12345") + sizeof("/123456789") +
1107 sizeof("%1234567890")];
1108 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1109 const u8 *addr = (const u8 *) &sa->sin6_addr;
1110 char fmt6[2] = { fmt[0], '6' };
1111 u8 off = 0;
1112
1113 fmt++;
1114 while (isalpha(*++fmt)) {
1115 switch (*fmt) {
1116 case 'p':
1117 have_p = true;
1118 break;
1119 case 'f':
1120 have_f = true;
1121 break;
1122 case 's':
1123 have_s = true;
1124 break;
1125 case 'c':
1126 have_c = true;
1127 break;
1128 }
1129 }
1130
1131 if (have_p || have_s || have_f) {
1132 *p = '[';
1133 off = 1;
1134 }
1135
1136 if (fmt6[0] == 'I' && have_c)
1137 p = ip6_compressed_string(ip6_addr + off, addr);
1138 else
1139 p = ip6_string(ip6_addr + off, addr, fmt6);
1140
1141 if (have_p || have_s || have_f)
1142 *p++ = ']';
1143
1144 if (have_p) {
1145 *p++ = ':';
1146 p = number(p, pend, ntohs(sa->sin6_port), spec);
1147 }
1148 if (have_f) {
1149 *p++ = '/';
1150 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1151 IPV6_FLOWINFO_MASK), spec);
1152 }
1153 if (have_s) {
1154 *p++ = '%';
1155 p = number(p, pend, sa->sin6_scope_id, spec);
1156 }
1157 *p = '\0';
1158
1159 return string(buf, end, ip6_addr, spec);
1160}
1161
1162static noinline_for_stack
1163char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1164 struct printf_spec spec, const char *fmt)
1165{
1166 bool have_p = false;
1167 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1168 char *pend = ip4_addr + sizeof(ip4_addr);
1169 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1170 char fmt4[3] = { fmt[0], '4', 0 };
1171
1172 fmt++;
1173 while (isalpha(*++fmt)) {
1174 switch (*fmt) {
1175 case 'p':
1176 have_p = true;
1177 break;
1178 case 'h':
1179 case 'l':
1180 case 'n':
1181 case 'b':
1182 fmt4[2] = *fmt;
1183 break;
1184 }
1185 }
1186
1187 p = ip4_string(ip4_addr, addr, fmt4);
1188 if (have_p) {
1189 *p++ = ':';
1190 p = number(p, pend, ntohs(sa->sin_port), spec);
1191 }
1192 *p = '\0';
1193
1194 return string(buf, end, ip4_addr, spec);
1195}
1196
1197static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001198char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1199 const char *fmt)
1200{
1201 bool found = true;
1202 int count = 1;
1203 unsigned int flags = 0;
1204 int len;
1205
1206 if (spec.field_width == 0)
1207 return buf; /* nothing to print */
1208
1209 if (ZERO_OR_NULL_PTR(addr))
1210 return string(buf, end, NULL, spec); /* NULL pointer */
1211
1212
1213 do {
1214 switch (fmt[count++]) {
1215 case 'a':
1216 flags |= ESCAPE_ANY;
1217 break;
1218 case 'c':
1219 flags |= ESCAPE_SPECIAL;
1220 break;
1221 case 'h':
1222 flags |= ESCAPE_HEX;
1223 break;
1224 case 'n':
1225 flags |= ESCAPE_NULL;
1226 break;
1227 case 'o':
1228 flags |= ESCAPE_OCTAL;
1229 break;
1230 case 'p':
1231 flags |= ESCAPE_NP;
1232 break;
1233 case 's':
1234 flags |= ESCAPE_SPACE;
1235 break;
1236 default:
1237 found = false;
1238 break;
1239 }
1240 } while (found);
1241
1242 if (!flags)
1243 flags = ESCAPE_ANY_NP;
1244
1245 len = spec.field_width < 0 ? 1 : spec.field_width;
1246
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001247 /*
1248 * string_escape_mem() writes as many characters as it can to
1249 * the given buffer, and returns the total size of the output
1250 * had the buffer been big enough.
1251 */
1252 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001253
1254 return buf;
1255}
1256
1257static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001258char *uuid_string(char *buf, char *end, const u8 *addr,
1259 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001260{
1261 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1262 char *p = uuid;
1263 int i;
1264 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1265 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1266 const u8 *index = be;
1267 bool uc = false;
1268
1269 switch (*(++fmt)) {
1270 case 'L':
1271 uc = true; /* fall-through */
1272 case 'l':
1273 index = le;
1274 break;
1275 case 'B':
1276 uc = true;
1277 break;
1278 }
1279
1280 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001281 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001282 switch (i) {
1283 case 3:
1284 case 5:
1285 case 7:
1286 case 9:
1287 *p++ = '-';
1288 break;
1289 }
1290 }
1291
1292 *p = 0;
1293
1294 if (uc) {
1295 p = uuid;
1296 do {
1297 *p = toupper(*p);
1298 } while (*(++p));
1299 }
1300
1301 return string(buf, end, uuid, spec);
1302}
1303
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001304static
1305char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1306 struct printf_spec spec)
1307{
1308 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1309 if (spec.field_width == -1)
1310 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1311 spec.base = 16;
1312
1313 return number(buf, end, *(const netdev_features_t *)addr, spec);
1314}
1315
Joe Perchesaaf07622014-01-23 15:54:17 -08001316static noinline_for_stack
1317char *address_val(char *buf, char *end, const void *addr,
1318 struct printf_spec spec, const char *fmt)
1319{
1320 unsigned long long num;
1321
1322 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1323 spec.base = 16;
1324
1325 switch (fmt[1]) {
1326 case 'd':
1327 num = *(const dma_addr_t *)addr;
1328 spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1329 break;
1330 case 'p':
1331 default:
1332 num = *(const phys_addr_t *)addr;
1333 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1334 break;
1335 }
1336
1337 return number(buf, end, num, spec);
1338}
1339
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001340static noinline_for_stack
1341char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1342 const char *fmt)
1343{
1344 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1345 return string(buf, end, NULL, spec);
1346
1347 switch (fmt[1]) {
1348 case 'r':
1349 return number(buf, end, clk_get_rate(clk), spec);
1350
1351 case 'n':
1352 default:
1353#ifdef CONFIG_COMMON_CLK
1354 return string(buf, end, __clk_get_name(clk), spec);
1355#else
1356 spec.base = 16;
1357 spec.field_width = sizeof(unsigned long) * 2 + 2;
1358 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1359 return number(buf, end, (unsigned long)clk, spec);
1360#endif
1361 }
1362}
1363
Ingo Molnar411f05f2011-05-12 23:00:28 +02001364int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001365
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001366/*
1367 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1368 * by an extra set of alphanumeric characters that are extended format
1369 * specifiers.
1370 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001371 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001372 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001373 * - 'F' For symbolic function descriptor pointers with offset
1374 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001375 * - 'S' For symbolic direct pointers with offset
1376 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001377 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001378 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001379 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1380 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001381 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1382 * width which must be explicitly specified either as part of the
1383 * format string '%32b[l]' or through '%*b[l]', [l] selects
1384 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001385 * - 'M' For a 6-byte MAC address, it prints the address in the
1386 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001387 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001388 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001389 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001390 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001391 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1392 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1393 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001394 * [S][pfs]
1395 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1396 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001397 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1398 * IPv6 omits the colons (01020304...0f)
1399 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001400 * [S][pfs]
1401 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1402 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1403 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1404 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001405 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001406 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1407 * of the following flags (see string_escape_mem() for the
1408 * details):
1409 * a - ESCAPE_ANY
1410 * c - ESCAPE_SPECIAL
1411 * h - ESCAPE_HEX
1412 * n - ESCAPE_NULL
1413 * o - ESCAPE_OCTAL
1414 * p - ESCAPE_NP
1415 * s - ESCAPE_SPACE
1416 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001417 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1418 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1419 * Options for %pU are:
1420 * b big endian lower case hex (default)
1421 * B big endian UPPER case hex
1422 * l little endian lower case hex
1423 * L little endian UPPER case hex
1424 * big endian output byte order is:
1425 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1426 * little endian output byte order is:
1427 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001428 * - 'V' For a struct va_format which contains a format string * and va_list *,
1429 * call vsnprintf(->format, *->va_list).
1430 * Implements a "recursive vsnprintf".
1431 * Do not use this feature without some mechanism to verify the
1432 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001433 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001434 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001435 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1436 * a certain separator (' ' by default):
1437 * C colon
1438 * D dash
1439 * N no separator
1440 * The maximum supported length is 64 bytes of the input. Consider
1441 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001442 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1443 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001444 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1445 * - 'D[234]' Same as 'd' but for a struct file
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001446 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1447 * (legacy clock framework) of the clock
1448 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1449 * (legacy clock framework) of the clock
1450 * - 'Cr' For a clock, it prints the current rate of the clock
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001451 *
1452 * ** Please update also Documentation/printk-formats.txt when making changes **
Joe Perches9ac6e442009-12-14 18:01:09 -08001453 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001454 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1455 * function pointers are really function descriptors, which contain a
1456 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001457 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001458static noinline_for_stack
1459char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1460 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001461{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001462 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001463
Kees Cook9f36e2c2011-03-22 16:34:22 -07001464 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001465 /*
1466 * Print (null) with the same width as a pointer so it makes
1467 * tabular output look nice.
1468 */
1469 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001470 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001471 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001472 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001473
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001474 switch (*fmt) {
1475 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001476 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001477 ptr = dereference_function_descriptor(ptr);
1478 /* Fallthrough */
1479 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001480 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001481 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001482 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001483 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001484 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001485 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001486 case 'h':
1487 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001488 case 'b':
1489 switch (fmt[1]) {
1490 case 'l':
1491 return bitmap_list_string(buf, end, ptr, spec, fmt);
1492 default:
1493 return bitmap_string(buf, end, ptr, spec, fmt);
1494 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001495 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1496 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001497 /* [mM]F (FDDI) */
1498 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001499 return mac_address_string(buf, end, ptr, spec, fmt);
1500 case 'I': /* Formatted IP supported
1501 * 4: 1.2.3.4
1502 * 6: 0001:0203:...:0708
1503 * 6c: 1::708 or 1::1.2.3.4
1504 */
1505 case 'i': /* Contiguous:
1506 * 4: 001.002.003.004
1507 * 6: 000102...0f
1508 */
1509 switch (fmt[1]) {
1510 case '6':
1511 return ip6_addr_string(buf, end, ptr, spec, fmt);
1512 case '4':
1513 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001514 case 'S': {
1515 const union {
1516 struct sockaddr raw;
1517 struct sockaddr_in v4;
1518 struct sockaddr_in6 v6;
1519 } *sa = ptr;
1520
1521 switch (sa->raw.sa_family) {
1522 case AF_INET:
1523 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1524 case AF_INET6:
1525 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1526 default:
1527 return string(buf, end, "(invalid address)", spec);
1528 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001529 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001530 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001531 case 'E':
1532 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001533 case 'U':
1534 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001535 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001536 {
1537 va_list va;
1538
1539 va_copy(va, *((struct va_format *)ptr)->va);
1540 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1541 ((struct va_format *)ptr)->fmt, va);
1542 va_end(va);
1543 return buf;
1544 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001545 case 'K':
1546 /*
1547 * %pK cannot be used in IRQ context because its test
1548 * for CAP_SYSLOG would be meaningless.
1549 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001550 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1551 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001552 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001553 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001554 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001555 }
Ryan Mallon312b4e22013-11-12 15:08:51 -08001556
1557 switch (kptr_restrict) {
1558 case 0:
1559 /* Always print %pK values */
1560 break;
1561 case 1: {
1562 /*
1563 * Only print the real pointer value if the current
1564 * process has CAP_SYSLOG and is running with the
1565 * same credentials it started with. This is because
1566 * access to files is checked at open() time, but %pK
1567 * checks permission at read() time. We don't want to
1568 * leak pointer values if a binary opens a file using
1569 * %pK and then elevates privileges before reading it.
1570 */
1571 const struct cred *cred = current_cred();
1572
1573 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1574 !uid_eq(cred->euid, cred->uid) ||
1575 !gid_eq(cred->egid, cred->gid))
1576 ptr = NULL;
1577 break;
1578 }
1579 case 2:
1580 default:
1581 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -07001582 ptr = NULL;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001583 break;
1584 }
Joe Perches26297602011-03-22 16:34:19 -07001585 break;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001586
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001587 case 'N':
1588 switch (fmt[1]) {
1589 case 'F':
1590 return netdev_feature_string(buf, end, ptr, spec);
1591 }
1592 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001593 case 'a':
Joe Perchesaaf07622014-01-23 15:54:17 -08001594 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001595 case 'd':
1596 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001597 case 'C':
1598 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001599 case 'D':
1600 return dentry_name(buf, end,
1601 ((const struct file *)ptr)->f_path.dentry,
1602 spec, fmt);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001603 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001604 spec.flags |= SMALL;
1605 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001606 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001607 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001608 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001609 spec.base = 16;
1610
1611 return number(buf, end, (unsigned long) ptr, spec);
1612}
1613
1614/*
1615 * Helper function to decode printf style format.
1616 * Each call decode a token from the format and return the
1617 * number of characters read (or likely the delta where it wants
1618 * to go on the next call).
1619 * The decoded token is returned through the parameters
1620 *
1621 * 'h', 'l', or 'L' for integer fields
1622 * 'z' support added 23/7/1999 S.H.
1623 * 'z' changed to 'Z' --davidm 1/25/99
1624 * 't' added for ptrdiff_t
1625 *
1626 * @fmt: the format string
1627 * @type of the token returned
1628 * @flags: various flags such as +, -, # tokens..
1629 * @field_width: overwritten width
1630 * @base: base of the number (octal, hex, ...)
1631 * @precision: precision of a number
1632 * @qualifier: qualifier of a number (long, size_t, ...)
1633 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001634static noinline_for_stack
1635int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001636{
1637 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001638
1639 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001640 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001641 if (spec->field_width < 0) {
1642 spec->field_width = -spec->field_width;
1643 spec->flags |= LEFT;
1644 }
1645 spec->type = FORMAT_TYPE_NONE;
1646 goto precision;
1647 }
1648
1649 /* we finished early by reading the precision */
1650 if (spec->type == FORMAT_TYPE_PRECISION) {
1651 if (spec->precision < 0)
1652 spec->precision = 0;
1653
1654 spec->type = FORMAT_TYPE_NONE;
1655 goto qualifier;
1656 }
1657
1658 /* By default */
1659 spec->type = FORMAT_TYPE_NONE;
1660
1661 for (; *fmt ; ++fmt) {
1662 if (*fmt == '%')
1663 break;
1664 }
1665
1666 /* Return the current non-format string */
1667 if (fmt != start || !*fmt)
1668 return fmt - start;
1669
1670 /* Process flags */
1671 spec->flags = 0;
1672
1673 while (1) { /* this also skips first '%' */
1674 bool found = true;
1675
1676 ++fmt;
1677
1678 switch (*fmt) {
1679 case '-': spec->flags |= LEFT; break;
1680 case '+': spec->flags |= PLUS; break;
1681 case ' ': spec->flags |= SPACE; break;
1682 case '#': spec->flags |= SPECIAL; break;
1683 case '0': spec->flags |= ZEROPAD; break;
1684 default: found = false;
1685 }
1686
1687 if (!found)
1688 break;
1689 }
1690
1691 /* get field width */
1692 spec->field_width = -1;
1693
1694 if (isdigit(*fmt))
1695 spec->field_width = skip_atoi(&fmt);
1696 else if (*fmt == '*') {
1697 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001698 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001699 return ++fmt - start;
1700 }
1701
1702precision:
1703 /* get the precision */
1704 spec->precision = -1;
1705 if (*fmt == '.') {
1706 ++fmt;
1707 if (isdigit(*fmt)) {
1708 spec->precision = skip_atoi(&fmt);
1709 if (spec->precision < 0)
1710 spec->precision = 0;
1711 } else if (*fmt == '*') {
1712 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001713 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001714 return ++fmt - start;
1715 }
1716 }
1717
1718qualifier:
1719 /* get the conversion qualifier */
1720 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001721 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1722 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001723 spec->qualifier = *fmt++;
1724 if (unlikely(spec->qualifier == *fmt)) {
1725 if (spec->qualifier == 'l') {
1726 spec->qualifier = 'L';
1727 ++fmt;
1728 } else if (spec->qualifier == 'h') {
1729 spec->qualifier = 'H';
1730 ++fmt;
1731 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001732 }
1733 }
1734
1735 /* default base */
1736 spec->base = 10;
1737 switch (*fmt) {
1738 case 'c':
1739 spec->type = FORMAT_TYPE_CHAR;
1740 return ++fmt - start;
1741
1742 case 's':
1743 spec->type = FORMAT_TYPE_STR;
1744 return ++fmt - start;
1745
1746 case 'p':
1747 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001748 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001749
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001750 case '%':
1751 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1752 return ++fmt - start;
1753
1754 /* integer number formats - set up the flags and "break" */
1755 case 'o':
1756 spec->base = 8;
1757 break;
1758
1759 case 'x':
1760 spec->flags |= SMALL;
1761
1762 case 'X':
1763 spec->base = 16;
1764 break;
1765
1766 case 'd':
1767 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001768 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001769 case 'u':
1770 break;
1771
Ryan Mallon708d96fd2014-04-03 14:48:37 -07001772 case 'n':
1773 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001774 * Since %n poses a greater security risk than
1775 * utility, treat it as any other invalid or
1776 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07001777 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07001778 /* Fall-through */
1779
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001780 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001781 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001782 spec->type = FORMAT_TYPE_INVALID;
1783 return fmt - start;
1784 }
1785
1786 if (spec->qualifier == 'L')
1787 spec->type = FORMAT_TYPE_LONG_LONG;
1788 else if (spec->qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001789 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1790 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001791 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001792 spec->type = FORMAT_TYPE_SIZE_T;
1793 } else if (spec->qualifier == 't') {
1794 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001795 } else if (spec->qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001796 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1797 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001798 } else if (spec->qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001799 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1800 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001801 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001802 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1803 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001804 }
1805
1806 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/**
1810 * vsnprintf - Format a string and place it in a buffer
1811 * @buf: The buffer to place the result into
1812 * @size: The size of the buffer, including the trailing null space
1813 * @fmt: The format string to use
1814 * @args: Arguments for the format string
1815 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08001816 * This function generally follows C99 vsnprintf, but has some
1817 * extensions and a few limitations:
1818 *
1819 * %n is unsupported
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001820 * %p* is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07001821 *
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001822 * See pointer() or Documentation/printk-formats.txt for more
1823 * extensive description.
1824 *
1825 * ** Please update the documentation in both places when making changes **
Andrew Morton80f548e2012-07-30 14:40:25 -07001826 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * The return value is the number of characters which would
1828 * be generated for the given input, excluding the trailing
1829 * '\0', as per ISO C99. If you want to have the exact
1830 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001831 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 * return is greater than or equal to @size, the resulting
1833 * string is truncated.
1834 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001835 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
1837int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1838{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001840 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001841 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001843 /* Reject out-of-range values early. Large positive sizes are
1844 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08001845 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001849 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001851 /* Make sure end is always >= buf */
1852 if (end < buf) {
1853 end = ((void *)-1);
1854 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001857 while (*fmt) {
1858 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001859 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001860
1861 fmt += read;
1862
1863 switch (spec.type) {
1864 case FORMAT_TYPE_NONE: {
1865 int copy = read;
1866 if (str < end) {
1867 if (copy > end - str)
1868 copy = end - str;
1869 memcpy(str, old_fmt, copy);
1870 }
1871 str += read;
1872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
Vegard Nossumed681a92009-03-14 12:08:50 +01001875 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001876 spec.field_width = va_arg(args, int);
1877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001879 case FORMAT_TYPE_PRECISION:
1880 spec.precision = va_arg(args, int);
1881 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
André Goddard Rosad4be1512009-12-14 18:00:59 -08001883 case FORMAT_TYPE_CHAR: {
1884 char c;
1885
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001886 if (!(spec.flags & LEFT)) {
1887 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001888 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 *str = ' ';
1890 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001893 }
1894 c = (unsigned char) va_arg(args, int);
1895 if (str < end)
1896 *str = c;
1897 ++str;
1898 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001899 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001900 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001902 }
1903 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001906 case FORMAT_TYPE_STR:
1907 str = string(str, end, va_arg(args, char *), spec);
1908 break;
1909
1910 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001911 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001912 spec);
1913 while (isalnum(*fmt))
1914 fmt++;
1915 break;
1916
1917 case FORMAT_TYPE_PERCENT_CHAR:
1918 if (str < end)
1919 *str = '%';
1920 ++str;
1921 break;
1922
1923 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001924 /*
1925 * Presumably the arguments passed gcc's type
1926 * checking, but there is no safe or sane way
1927 * for us to continue parsing the format and
1928 * fetching from the va_list; the remaining
1929 * specifiers and arguments would be out of
1930 * sync.
1931 */
1932 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001933
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001934 default:
1935 switch (spec.type) {
1936 case FORMAT_TYPE_LONG_LONG:
1937 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001939 case FORMAT_TYPE_ULONG:
1940 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001942 case FORMAT_TYPE_LONG:
1943 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001945 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001946 if (spec.flags & SIGN)
1947 num = va_arg(args, ssize_t);
1948 else
1949 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001950 break;
1951 case FORMAT_TYPE_PTRDIFF:
1952 num = va_arg(args, ptrdiff_t);
1953 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001954 case FORMAT_TYPE_UBYTE:
1955 num = (unsigned char) va_arg(args, int);
1956 break;
1957 case FORMAT_TYPE_BYTE:
1958 num = (signed char) va_arg(args, int);
1959 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001960 case FORMAT_TYPE_USHORT:
1961 num = (unsigned short) va_arg(args, int);
1962 break;
1963 case FORMAT_TYPE_SHORT:
1964 num = (short) va_arg(args, int);
1965 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001966 case FORMAT_TYPE_INT:
1967 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001970 num = va_arg(args, unsigned int);
1971 }
1972
1973 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001976
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001977out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001978 if (size > 0) {
1979 if (str < end)
1980 *str = '\0';
1981 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001982 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001983 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001984
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001985 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989EXPORT_SYMBOL(vsnprintf);
1990
1991/**
1992 * vscnprintf - Format a string and place it in a buffer
1993 * @buf: The buffer to place the result into
1994 * @size: The size of the buffer, including the trailing null space
1995 * @fmt: The format string to use
1996 * @args: Arguments for the format string
1997 *
1998 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001999 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * returns 0.
2001 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002002 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002003 *
2004 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 */
2006int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2007{
2008 int i;
2009
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002010 i = vsnprintf(buf, size, fmt, args);
2011
Anton Arapovb921c692011-01-12 16:59:49 -08002012 if (likely(i < size))
2013 return i;
2014 if (size != 0)
2015 return size - 1;
2016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018EXPORT_SYMBOL(vscnprintf);
2019
2020/**
2021 * snprintf - Format a string and place it in a buffer
2022 * @buf: The buffer to place the result into
2023 * @size: The size of the buffer, including the trailing null space
2024 * @fmt: The format string to use
2025 * @...: Arguments for the format string
2026 *
2027 * The return value is the number of characters which would be
2028 * generated for the given input, excluding the trailing null,
2029 * as per ISO C99. If the return is greater than or equal to
2030 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002031 *
2032 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002034int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 va_list args;
2037 int i;
2038
2039 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002040 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return i;
2044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045EXPORT_SYMBOL(snprintf);
2046
2047/**
2048 * scnprintf - Format a string and place it in a buffer
2049 * @buf: The buffer to place the result into
2050 * @size: The size of the buffer, including the trailing null space
2051 * @fmt: The format string to use
2052 * @...: Arguments for the format string
2053 *
2054 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002055 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
2057
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002058int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 va_list args;
2061 int i;
2062
2063 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002064 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002066
Anton Arapovb921c692011-01-12 16:59:49 -08002067 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069EXPORT_SYMBOL(scnprintf);
2070
2071/**
2072 * vsprintf - Format a string and place it in a buffer
2073 * @buf: The buffer to place the result into
2074 * @fmt: The format string to use
2075 * @args: Arguments for the format string
2076 *
2077 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002078 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 * buffer overflows.
2080 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002081 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002082 *
2083 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 */
2085int vsprintf(char *buf, const char *fmt, va_list args)
2086{
2087 return vsnprintf(buf, INT_MAX, fmt, args);
2088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089EXPORT_SYMBOL(vsprintf);
2090
2091/**
2092 * sprintf - Format a string and place it in a buffer
2093 * @buf: The buffer to place the result into
2094 * @fmt: The format string to use
2095 * @...: Arguments for the format string
2096 *
2097 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002098 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * buffer overflows.
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 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002103int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 va_list args;
2106 int i;
2107
2108 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002109 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return i;
2113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114EXPORT_SYMBOL(sprintf);
2115
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002116#ifdef CONFIG_BINARY_PRINTF
2117/*
2118 * bprintf service:
2119 * vbin_printf() - VA arguments to binary data
2120 * bstr_printf() - Binary data to text string
2121 */
2122
2123/**
2124 * vbin_printf - Parse a format string and place args' binary value in a buffer
2125 * @bin_buf: The buffer to place args' binary value
2126 * @size: The size of the buffer(by words(32bits), not characters)
2127 * @fmt: The format string to use
2128 * @args: Arguments for the format string
2129 *
2130 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002131 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002132 *
2133 * The return value is the number of words(32bits) which would be generated for
2134 * the given input.
2135 *
2136 * NOTE:
2137 * If the return value is greater than @size, the resulting bin_buf is NOT
2138 * valid for bstr_printf().
2139 */
2140int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2141{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002142 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002143 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002144
2145 str = (char *)bin_buf;
2146 end = (char *)(bin_buf + size);
2147
2148#define save_arg(type) \
2149do { \
2150 if (sizeof(type) == 8) { \
2151 unsigned long long value; \
2152 str = PTR_ALIGN(str, sizeof(u32)); \
2153 value = va_arg(args, unsigned long long); \
2154 if (str + sizeof(type) <= end) { \
2155 *(u32 *)str = *(u32 *)&value; \
2156 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2157 } \
2158 } else { \
2159 unsigned long value; \
2160 str = PTR_ALIGN(str, sizeof(type)); \
2161 value = va_arg(args, int); \
2162 if (str + sizeof(type) <= end) \
2163 *(typeof(type) *)str = (type)value; \
2164 } \
2165 str += sizeof(type); \
2166} while (0)
2167
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002168 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002169 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002170
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002171 fmt += read;
2172
2173 switch (spec.type) {
2174 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002175 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002176 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002177 case FORMAT_TYPE_INVALID:
2178 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002179
Vegard Nossumed681a92009-03-14 12:08:50 +01002180 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002181 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002182 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002183 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002184
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002185 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002186 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002187 break;
2188
2189 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002190 const char *save_str = va_arg(args, char *);
2191 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002192
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002193 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2194 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002195 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002196 len = strlen(save_str) + 1;
2197 if (str + len < end)
2198 memcpy(str, save_str, len);
2199 str += len;
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
2203 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002204 save_arg(void *);
2205 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002206 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002207 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002208 break;
2209
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002210 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002211 switch (spec.type) {
2212
2213 case FORMAT_TYPE_LONG_LONG:
2214 save_arg(long long);
2215 break;
2216 case FORMAT_TYPE_ULONG:
2217 case FORMAT_TYPE_LONG:
2218 save_arg(unsigned long);
2219 break;
2220 case FORMAT_TYPE_SIZE_T:
2221 save_arg(size_t);
2222 break;
2223 case FORMAT_TYPE_PTRDIFF:
2224 save_arg(ptrdiff_t);
2225 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002226 case FORMAT_TYPE_UBYTE:
2227 case FORMAT_TYPE_BYTE:
2228 save_arg(char);
2229 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002230 case FORMAT_TYPE_USHORT:
2231 case FORMAT_TYPE_SHORT:
2232 save_arg(short);
2233 break;
2234 default:
2235 save_arg(int);
2236 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002237 }
2238 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002239
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002240out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002241 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002242#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002243}
2244EXPORT_SYMBOL_GPL(vbin_printf);
2245
2246/**
2247 * bstr_printf - Format a string from binary arguments and place it in a buffer
2248 * @buf: The buffer to place the result into
2249 * @size: The size of the buffer, including the trailing null space
2250 * @fmt: The format string to use
2251 * @bin_buf: Binary arguments for the format string
2252 *
2253 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2254 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2255 * a binary buffer that generated by vbin_printf.
2256 *
2257 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002258 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002259 *
2260 * The return value is the number of characters which would
2261 * be generated for the given input, excluding the trailing
2262 * '\0', as per ISO C99. If you want to have the exact
2263 * number of characters written into @buf as return value
2264 * (not including the trailing '\0'), use vscnprintf(). If the
2265 * return is greater than or equal to @size, the resulting
2266 * string is truncated.
2267 */
2268int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2269{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002270 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002271 char *str, *end;
2272 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002273
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002274 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002275 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002276
2277 str = buf;
2278 end = buf + size;
2279
2280#define get_arg(type) \
2281({ \
2282 typeof(type) value; \
2283 if (sizeof(type) == 8) { \
2284 args = PTR_ALIGN(args, sizeof(u32)); \
2285 *(u32 *)&value = *(u32 *)args; \
2286 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2287 } else { \
2288 args = PTR_ALIGN(args, sizeof(type)); \
2289 value = *(typeof(type) *)args; \
2290 } \
2291 args += sizeof(type); \
2292 value; \
2293})
2294
2295 /* Make sure end is always >= buf */
2296 if (end < buf) {
2297 end = ((void *)-1);
2298 size = end - buf;
2299 }
2300
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002301 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002302 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002303 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002304
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002305 fmt += read;
2306
2307 switch (spec.type) {
2308 case FORMAT_TYPE_NONE: {
2309 int copy = read;
2310 if (str < end) {
2311 if (copy > end - str)
2312 copy = end - str;
2313 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002314 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002315 str += read;
2316 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002317 }
2318
Vegard Nossumed681a92009-03-14 12:08:50 +01002319 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002320 spec.field_width = get_arg(int);
2321 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002322
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002323 case FORMAT_TYPE_PRECISION:
2324 spec.precision = get_arg(int);
2325 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002326
André Goddard Rosad4be1512009-12-14 18:00:59 -08002327 case FORMAT_TYPE_CHAR: {
2328 char c;
2329
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002330 if (!(spec.flags & LEFT)) {
2331 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002332 if (str < end)
2333 *str = ' ';
2334 ++str;
2335 }
2336 }
2337 c = (unsigned char) get_arg(char);
2338 if (str < end)
2339 *str = c;
2340 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002341 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002342 if (str < end)
2343 *str = ' ';
2344 ++str;
2345 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002346 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002347 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002348
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002349 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002350 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002351 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002352 str = string(str, end, (char *)str_arg, spec);
2353 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002354 }
2355
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002356 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002357 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002358 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002359 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002360 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002361
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002362 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002363 if (str < end)
2364 *str = '%';
2365 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002366 break;
2367
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002368 case FORMAT_TYPE_INVALID:
2369 goto out;
2370
André Goddard Rosad4be1512009-12-14 18:00:59 -08002371 default: {
2372 unsigned long long num;
2373
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002374 switch (spec.type) {
2375
2376 case FORMAT_TYPE_LONG_LONG:
2377 num = get_arg(long long);
2378 break;
2379 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002380 case FORMAT_TYPE_LONG:
2381 num = get_arg(unsigned long);
2382 break;
2383 case FORMAT_TYPE_SIZE_T:
2384 num = get_arg(size_t);
2385 break;
2386 case FORMAT_TYPE_PTRDIFF:
2387 num = get_arg(ptrdiff_t);
2388 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002389 case FORMAT_TYPE_UBYTE:
2390 num = get_arg(unsigned char);
2391 break;
2392 case FORMAT_TYPE_BYTE:
2393 num = get_arg(signed char);
2394 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002395 case FORMAT_TYPE_USHORT:
2396 num = get_arg(unsigned short);
2397 break;
2398 case FORMAT_TYPE_SHORT:
2399 num = get_arg(short);
2400 break;
2401 case FORMAT_TYPE_UINT:
2402 num = get_arg(unsigned int);
2403 break;
2404 default:
2405 num = get_arg(int);
2406 }
2407
2408 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002409 } /* default: */
2410 } /* switch(spec.type) */
2411 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002412
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002413out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002414 if (size > 0) {
2415 if (str < end)
2416 *str = '\0';
2417 else
2418 end[-1] = '\0';
2419 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002420
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002421#undef get_arg
2422
2423 /* the trailing null byte doesn't count towards the total */
2424 return str - buf;
2425}
2426EXPORT_SYMBOL_GPL(bstr_printf);
2427
2428/**
2429 * bprintf - Parse a format string and place args' binary value in a buffer
2430 * @bin_buf: The buffer to place args' binary value
2431 * @size: The size of the buffer(by words(32bits), not characters)
2432 * @fmt: The format string to use
2433 * @...: Arguments for the format string
2434 *
2435 * The function returns the number of words(u32) written
2436 * into @bin_buf.
2437 */
2438int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2439{
2440 va_list args;
2441 int ret;
2442
2443 va_start(args, fmt);
2444 ret = vbin_printf(bin_buf, size, fmt, args);
2445 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002446
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002447 return ret;
2448}
2449EXPORT_SYMBOL_GPL(bprintf);
2450
2451#endif /* CONFIG_BINARY_PRINTF */
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453/**
2454 * vsscanf - Unformat a buffer into a list of arguments
2455 * @buf: input buffer
2456 * @fmt: format of buffer
2457 * @args: arguments
2458 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002459int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 const char *str = buf;
2462 char *next;
2463 char digit;
2464 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002465 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002466 unsigned int base;
2467 union {
2468 long long s;
2469 unsigned long long u;
2470 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002471 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002472 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Jan Beulichda990752012-10-04 17:13:24 -07002474 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /* skip any white space in format */
2476 /* white space in format matchs any amount of
2477 * white space, including none, in the input.
2478 */
2479 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002480 fmt = skip_spaces(++fmt);
2481 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
2484 /* anything that is not a conversion must match exactly */
2485 if (*fmt != '%' && *fmt) {
2486 if (*fmt++ != *str++)
2487 break;
2488 continue;
2489 }
2490
2491 if (!*fmt)
2492 break;
2493 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 /* skip this conversion.
2496 * advance both strings to next white space
2497 */
2498 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002499 if (!*str)
2500 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002501 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 fmt++;
2503 while (!isspace(*str) && *str)
2504 str++;
2505 continue;
2506 }
2507
2508 /* get field width */
2509 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002510 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002512 if (field_width <= 0)
2513 break;
2514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 /* get conversion qualifier */
2517 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002518 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2519 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 qualifier = *fmt++;
2521 if (unlikely(qualifier == *fmt)) {
2522 if (qualifier == 'h') {
2523 qualifier = 'H';
2524 fmt++;
2525 } else if (qualifier == 'l') {
2526 qualifier = 'L';
2527 fmt++;
2528 }
2529 }
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Jan Beulichda990752012-10-04 17:13:24 -07002532 if (!*fmt)
2533 break;
2534
2535 if (*fmt == 'n') {
2536 /* return number of characters read so far */
2537 *va_arg(args, int *) = str - buf;
2538 ++fmt;
2539 continue;
2540 }
2541
2542 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 break;
2544
André Goddard Rosad4be1512009-12-14 18:00:59 -08002545 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002546 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002547
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002548 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 case 'c':
2550 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002551 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (field_width == -1)
2553 field_width = 1;
2554 do {
2555 *s++ = *str++;
2556 } while (--field_width > 0 && *str);
2557 num++;
2558 }
2559 continue;
2560 case 's':
2561 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002562 char *s = (char *)va_arg(args, char *);
2563 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002564 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002566 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002569 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 *s = '\0';
2572 num++;
2573 }
2574 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 case 'o':
2576 base = 8;
2577 break;
2578 case 'x':
2579 case 'X':
2580 base = 16;
2581 break;
2582 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002583 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002585 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 case 'u':
2587 break;
2588 case '%':
2589 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002590 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return num;
2592 continue;
2593 default:
2594 /* invalid format; stop here */
2595 return num;
2596 }
2597
2598 /* have some sort of integer conversion.
2599 * first, skip white space in buffer.
2600 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002601 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 digit = *str;
2604 if (is_sign && digit == '-')
2605 digit = *(str + 1);
2606
2607 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002608 || (base == 16 && !isxdigit(digit))
2609 || (base == 10 && !isdigit(digit))
2610 || (base == 8 && (!isdigit(digit) || digit > '7'))
2611 || (base == 0 && !isdigit(digit)))
2612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Jan Beulich53809752012-12-17 16:01:31 -08002614 if (is_sign)
2615 val.s = qualifier != 'L' ?
2616 simple_strtol(str, &next, base) :
2617 simple_strtoll(str, &next, base);
2618 else
2619 val.u = qualifier != 'L' ?
2620 simple_strtoul(str, &next, base) :
2621 simple_strtoull(str, &next, base);
2622
2623 if (field_width > 0 && next - str > field_width) {
2624 if (base == 0)
2625 _parse_integer_fixup_radix(str, &base);
2626 while (next - str > field_width) {
2627 if (is_sign)
2628 val.s = div_s64(val.s, base);
2629 else
2630 val.u = div_u64(val.u, base);
2631 --next;
2632 }
2633 }
2634
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002635 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002637 if (is_sign)
2638 *va_arg(args, signed char *) = val.s;
2639 else
2640 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 break;
2642 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002643 if (is_sign)
2644 *va_arg(args, short *) = val.s;
2645 else
2646 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 break;
2648 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002649 if (is_sign)
2650 *va_arg(args, long *) = val.s;
2651 else
2652 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 break;
2654 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002655 if (is_sign)
2656 *va_arg(args, long long *) = val.s;
2657 else
2658 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 break;
2660 case 'Z':
2661 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002662 *va_arg(args, size_t *) = val.u;
2663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 default:
Jan Beulich53809752012-12-17 16:01:31 -08002665 if (is_sign)
2666 *va_arg(args, int *) = val.s;
2667 else
2668 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 break;
2670 }
2671 num++;
2672
2673 if (!next)
2674 break;
2675 str = next;
2676 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return num;
2679}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680EXPORT_SYMBOL(vsscanf);
2681
2682/**
2683 * sscanf - Unformat a buffer into a list of arguments
2684 * @buf: input buffer
2685 * @fmt: formatting of buffer
2686 * @...: resulting arguments
2687 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002688int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 va_list args;
2691 int i;
2692
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002693 va_start(args, fmt);
2694 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return i;
2698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699EXPORT_SYMBOL(sscanf);