blob: b30203b00adfba162e0017e8d77401d9c5f57af6 [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>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040034#ifdef CONFIG_BLOCK
35#include <linux/blkdev.h>
36#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Tim Schmielau4e57b682005-10-30 15:03:48 -080038#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050039#include <asm/sections.h> /* for dereference_function_descriptor() */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070040#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Andy Shevchenko71dca952014-10-13 15:55:18 -070042#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070043#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * simple_strtoull - convert a string to an unsigned long long
47 * @cp: The start of the string
48 * @endp: A pointer to the end of the parsed string will be placed here
49 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080050 *
51 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Harvey Harrison22d27052008-10-16 13:40:35 -070053unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070055 unsigned long long result;
56 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070058 cp = _parse_integer_fixup_radix(cp, &base);
59 rv = _parse_integer(cp, base, &result);
60 /* FIXME */
61 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (endp)
64 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return result;
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068EXPORT_SYMBOL(simple_strtoull);
69
70/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080071 * simple_strtoul - convert a string to an unsigned long
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080075 *
76 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080077 */
78unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
79{
80 return simple_strtoull(cp, endp, base);
81}
82EXPORT_SYMBOL(simple_strtoul);
83
84/**
85 * simple_strtol - convert a string to a signed long
86 * @cp: The start of the string
87 * @endp: A pointer to the end of the parsed string will be placed here
88 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080089 *
90 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080091 */
92long simple_strtol(const char *cp, char **endp, unsigned int base)
93{
94 if (*cp == '-')
95 return -simple_strtoul(cp + 1, endp, base);
96
97 return simple_strtoul(cp, endp, base);
98}
99EXPORT_SYMBOL(simple_strtol);
100
101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * simple_strtoll - convert a string to a signed long long
103 * @cp: The start of the string
104 * @endp: A pointer to the end of the parsed string will be placed here
105 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800106 *
107 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700109long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800111 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700112 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800113
Harvey Harrison22d27052008-10-16 13:40:35 -0700114 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400116EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Joe Perchescf3b4292010-05-24 14:33:16 -0700118static noinline_for_stack
119int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800121 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800123 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800125 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return i;
128}
129
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700130/*
131 * Decimal conversion is by far the most typical, and is used for
132 * /proc and /sys data. This directly impacts e.g. top performance
133 * with many processes running. We optimize it for speed by emitting
134 * two characters at a time, using a 200 byte lookup table. This
135 * roughly halves the number of multiplications compared to computing
136 * the digits one at a time. Implementation strongly inspired by the
137 * previous version, which in turn used ideas described at
138 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
139 * from the author, Douglas W. Jones).
140 *
141 * It turns out there is precisely one 26 bit fixed-point
142 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
143 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
144 * range happens to be somewhat larger (x <= 1073741898), but that's
145 * irrelevant for our purpose.
146 *
147 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
148 * need a 32x32->64 bit multiply, so we simply use the same constant.
149 *
150 * For dividing a number in the range [100, 10^4-1] by 100, there are
151 * several options. The simplest is (x * 0x147b) >> 19, which is valid
152 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700153 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700154
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700155static const u16 decpair[100] = {
156#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
157 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
158 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
159 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
160 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
161 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
162 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
163 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
164 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
165 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
166 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
167#undef _
168};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700169
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700170/*
171 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700172 * immediately jump to out_r where two 0s would be written but only
173 * one of them accounted for in buf. This is needed by ip4_string
174 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700175*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700176static noinline_for_stack
177char *put_dec_trunc8(char *buf, unsigned r)
178{
179 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700180
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700181 /* 1 <= r < 10^8 */
182 if (r < 100)
183 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700184
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700185 /* 100 <= r < 10^8 */
186 q = (r * (u64)0x28f5c29) >> 32;
187 *((u16 *)buf) = decpair[r - 100*q];
188 buf += 2;
189
190 /* 1 <= q < 10^6 */
191 if (q < 100)
192 goto out_q;
193
194 /* 100 <= q < 10^6 */
195 r = (q * (u64)0x28f5c29) >> 32;
196 *((u16 *)buf) = decpair[q - 100*r];
197 buf += 2;
198
199 /* 1 <= r < 10^4 */
200 if (r < 100)
201 goto out_r;
202
203 /* 100 <= r < 10^4 */
204 q = (r * 0x147b) >> 19;
205 *((u16 *)buf) = decpair[r - 100*q];
206 buf += 2;
207out_q:
208 /* 1 <= q < 100 */
209 r = q;
210out_r:
211 /* 1 <= r < 100 */
212 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700213 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700214 return buf;
215}
216
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700217#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
218static noinline_for_stack
219char *put_dec_full8(char *buf, unsigned r)
220{
221 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700222
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700223 /* 0 <= r < 10^8 */
224 q = (r * (u64)0x28f5c29) >> 32;
225 *((u16 *)buf) = decpair[r - 100*q];
226 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700227
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700228 /* 0 <= q < 10^6 */
229 r = (q * (u64)0x28f5c29) >> 32;
230 *((u16 *)buf) = decpair[q - 100*r];
231 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700232
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700233 /* 0 <= r < 10^4 */
234 q = (r * 0x147b) >> 19;
235 *((u16 *)buf) = decpair[r - 100*q];
236 buf += 2;
237
238 /* 0 <= q < 100 */
239 *((u16 *)buf) = decpair[q];
240 buf += 2;
241 return buf;
242}
243
244static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700245char *put_dec(char *buf, unsigned long long n)
246{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700247 if (n >= 100*1000*1000)
248 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
249 /* 1 <= n <= 1.6e11 */
250 if (n >= 100*1000*1000)
251 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
252 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700253 return put_dec_trunc8(buf, n);
254}
255
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700256#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700257
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700258static void
259put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700260{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700261 unsigned q;
262
263 /* 0 <= r < 10^4 */
264 q = (r * 0x147b) >> 19;
265 *((u16 *)buf) = decpair[r - 100*q];
266 buf += 2;
267 /* 0 <= q < 100 */
268 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700269}
270
271/*
272 * Call put_dec_full4 on x % 10000, return x / 10000.
273 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
274 * holds for all x < 1,128,869,999. The largest value this
275 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700276 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700277 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700278static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700279unsigned put_dec_helper4(char *buf, unsigned x)
280{
281 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
282
283 put_dec_full4(buf, x - q * 10000);
284 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700285}
286
287/* Based on code by Douglas W. Jones found at
288 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
289 * (with permission from the author).
290 * Performs no 64-bit division and hence should be fast on 32-bit machines.
291 */
292static
293char *put_dec(char *buf, unsigned long long n)
294{
295 uint32_t d3, d2, d1, q, h;
296
297 if (n < 100*1000*1000)
298 return put_dec_trunc8(buf, n);
299
300 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
301 h = (n >> 32);
302 d2 = (h ) & 0xffff;
303 d3 = (h >> 16); /* implicit "& 0xffff" */
304
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700305 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
306 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700307 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700308 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700309
George Spelvin23591722012-10-04 17:12:29 -0700310 q += 7671 * d3 + 9496 * d2 + 6 * d1;
311 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700312
George Spelvin23591722012-10-04 17:12:29 -0700313 q += 4749 * d3 + 42 * d2;
314 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700315
George Spelvin23591722012-10-04 17:12:29 -0700316 q += 281 * d3;
317 buf += 12;
318 if (q)
319 buf = put_dec_trunc8(buf, q);
320 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700321 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800322
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700323 return buf;
324}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700325
326#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700327
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700328/*
329 * Convert passed number to decimal string.
330 * Returns the length of string. On buffer overflow, returns 0.
331 *
332 * If speed is not important, use snprintf(). It's easy to read the code.
333 */
334int num_to_str(char *buf, int size, unsigned long long num)
335{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700336 /* put_dec requires 2-byte alignment of the buffer. */
337 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700338 int idx, len;
339
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700340 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
341 if (num <= 9) {
342 tmp[0] = '0' + num;
343 len = 1;
344 } else {
345 len = put_dec(tmp, num) - tmp;
346 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700347
348 if (len > size)
349 return 0;
350 for (idx = 0; idx < len; ++idx)
351 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700352 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700353}
354
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700355#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700356#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#define PLUS 4 /* show plus */
358#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700359#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700360#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
361#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100363enum format_type {
364 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100365 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100366 FORMAT_TYPE_PRECISION,
367 FORMAT_TYPE_CHAR,
368 FORMAT_TYPE_STR,
369 FORMAT_TYPE_PTR,
370 FORMAT_TYPE_PERCENT_CHAR,
371 FORMAT_TYPE_INVALID,
372 FORMAT_TYPE_LONG_LONG,
373 FORMAT_TYPE_ULONG,
374 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800375 FORMAT_TYPE_UBYTE,
376 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100377 FORMAT_TYPE_USHORT,
378 FORMAT_TYPE_SHORT,
379 FORMAT_TYPE_UINT,
380 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100381 FORMAT_TYPE_SIZE_T,
382 FORMAT_TYPE_PTRDIFF
383};
384
385struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800386 unsigned int type:8; /* format_type enum */
387 signed int field_width:24; /* width of output field */
388 unsigned int flags:8; /* flags to number() */
389 unsigned int base:8; /* number base, 8, 10 or 16 only */
390 signed int precision:16; /* # of digits/chars */
391} __packed;
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800392#define FIELD_WIDTH_MAX ((1 << 23) - 1)
393#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100394
Joe Perchescf3b4292010-05-24 14:33:16 -0700395static noinline_for_stack
396char *number(char *buf, char *end, unsigned long long num,
397 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700399 /* put_dec requires 2-byte alignment of the buffer. */
400 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100401 char sign;
402 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100403 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700405 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800406 int field_width = spec.field_width;
407 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800409 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
410
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100411 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
412 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100413 locase = (spec.flags & SMALL);
414 if (spec.flags & LEFT)
415 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100417 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800418 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800420 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800421 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100422 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800424 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100425 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800427 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700430 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100431 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800432 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700433 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800434 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700436
437 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700439 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700440 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100441 else if (spec.base != 10) { /* 8 or 16 */
442 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700443 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800444
445 if (spec.base == 16)
446 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700448 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700449 num >>= shift;
450 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700451 } else { /* base 10 */
452 i = put_dec(tmp, num) - tmp;
453 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700454
455 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800456 if (i > precision)
457 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700458 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800459 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700460 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800461 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700462 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *buf = ' ';
464 ++buf;
465 }
466 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700467 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700469 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 *buf = sign;
471 ++buf;
472 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700473 /* "0x" / "0" prefix */
474 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700475 if (spec.base == 16 || !is_zero) {
476 if (buf < end)
477 *buf = '0';
478 ++buf;
479 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100480 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700481 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100482 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ++buf;
484 }
485 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700486 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100487 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700488 char c = ' ' + (spec.flags & ZEROPAD);
489 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800490 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700491 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 *buf = c;
493 ++buf;
494 }
495 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700496 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800497 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700498 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *buf = '0';
500 ++buf;
501 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700502 /* actual digits of result */
503 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700504 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *buf = tmp[i];
506 ++buf;
507 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700508 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800509 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700510 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *buf = ' ';
512 ++buf;
513 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return buf;
516}
517
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800518static noinline_for_stack
519char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
520{
521 struct printf_spec spec;
522
523 spec.type = FORMAT_TYPE_PTR;
524 spec.field_width = 2 + 2 * size; /* 0x + hex */
525 spec.flags = SPECIAL | SMALL | ZEROPAD;
526 spec.base = 16;
527 spec.precision = -1;
528
529 return number(buf, end, num, spec);
530}
531
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800532static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400533{
534 size_t size;
535 if (buf >= end) /* nowhere to put anything */
536 return;
537 size = end - buf;
538 if (size <= spaces) {
539 memset(buf, ' ', size);
540 return;
541 }
542 if (len) {
543 if (len > size - spaces)
544 len = size - spaces;
545 memmove(buf + spaces, buf, len);
546 }
547 memset(buf, ' ', spaces);
548}
549
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800550/*
551 * Handle field width padding for a string.
552 * @buf: current buffer position
553 * @n: length of string
554 * @end: end of output buffer
555 * @spec: for field width and flags
556 * Returns: new buffer position after padding.
557 */
558static noinline_for_stack
559char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
560{
561 unsigned spaces;
562
563 if (likely(n >= spec.field_width))
564 return buf;
565 /* we want to pad the sucker */
566 spaces = spec.field_width - n;
567 if (!(spec.flags & LEFT)) {
568 move_right(buf - n, end, n, spaces);
569 return buf + spaces;
570 }
571 while (spaces--) {
572 if (buf < end)
573 *buf = ' ';
574 ++buf;
575 }
576 return buf;
577}
578
Al Viro4b6ccca2013-09-03 12:00:44 -0400579static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800580char *string(char *buf, char *end, const char *s, struct printf_spec spec)
581{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800582 int len = 0;
583 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800584
585 if ((unsigned long)s < PAGE_SIZE)
586 s = "(null)";
587
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800588 while (lim--) {
589 char c = *s++;
590 if (!c)
591 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800592 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800593 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800594 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800595 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800596 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800597 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800598}
599
600static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400601char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
602 const char *fmt)
603{
604 const char *array[4], *s;
605 const struct dentry *p;
606 int depth;
607 int i, n;
608
609 switch (fmt[1]) {
610 case '2': case '3': case '4':
611 depth = fmt[1] - '0';
612 break;
613 default:
614 depth = 1;
615 }
616
617 rcu_read_lock();
618 for (i = 0; i < depth; i++, d = p) {
619 p = ACCESS_ONCE(d->d_parent);
620 array[i] = ACCESS_ONCE(d->d_name.name);
621 if (p == d) {
622 if (i)
623 array[i] = "";
624 i++;
625 break;
626 }
627 }
628 s = array[--i];
629 for (n = 0; n != spec.precision; n++, buf++) {
630 char c = *s++;
631 if (!c) {
632 if (!i)
633 break;
634 c = '/';
635 s = array[--i];
636 }
637 if (buf < end)
638 *buf = c;
639 }
640 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800641 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400642}
643
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400644#ifdef CONFIG_BLOCK
645static noinline_for_stack
646char *bdev_name(char *buf, char *end, struct block_device *bdev,
647 struct printf_spec spec, const char *fmt)
648{
649 struct gendisk *hd = bdev->bd_disk;
650
651 buf = string(buf, end, hd->disk_name, spec);
652 if (bdev->bd_part->partno) {
653 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
654 if (buf < end)
655 *buf = 'p';
656 buf++;
657 }
658 buf = number(buf, end, bdev->bd_part->partno, spec);
659 }
660 return buf;
661}
662#endif
663
Joe Perchescf3b4292010-05-24 14:33:16 -0700664static noinline_for_stack
665char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800666 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700667{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800668 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700669#ifdef CONFIG_KALLSYMS
670 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800671#endif
672
673 if (fmt[1] == 'R')
674 ptr = __builtin_extract_return_addr(ptr);
675 value = (unsigned long)ptr;
676
677#ifdef CONFIG_KALLSYMS
678 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900679 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800680 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200681 sprint_symbol(sym, value);
682 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700683 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800684
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100685 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700686#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800687 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700688#endif
689}
690
Joe Perchescf3b4292010-05-24 14:33:16 -0700691static noinline_for_stack
692char *resource_string(char *buf, char *end, struct resource *res,
693 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100694{
695#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600696#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100697#endif
698
699#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600700#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100701#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700702 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100703 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700704 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100705 .precision = -1,
706 .flags = SPECIAL | SMALL | ZEROPAD,
707 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700708 static const struct printf_spec mem_spec = {
709 .base = 16,
710 .field_width = MEM_RSRC_PRINTK_SIZE,
711 .precision = -1,
712 .flags = SPECIAL | SMALL | ZEROPAD,
713 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700714 static const struct printf_spec bus_spec = {
715 .base = 16,
716 .field_width = 2,
717 .precision = -1,
718 .flags = SMALL | ZEROPAD,
719 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700720 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600721 .base = 10,
722 .precision = -1,
723 .flags = 0,
724 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700725 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600726 .field_width = -1,
727 .precision = 10,
728 .flags = LEFT,
729 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700730 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600731 .base = 16,
732 .precision = -1,
733 .flags = SPECIAL | SMALL,
734 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600735
736 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
737 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
738#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
739#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700740#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600741#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
742 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
743 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
744
Linus Torvalds332d2e72008-10-20 15:07:34 +1100745 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600746 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700747 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100748
749 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700750 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600751 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700752 specp = &io_spec;
753 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600754 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700755 specp = &mem_spec;
756 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600757 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700758 specp = &dec_spec;
759 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600760 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700761 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700762 } else if (res->flags & IORESOURCE_BUS) {
763 p = string(p, pend, "bus ", str_spec);
764 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700765 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600766 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700767 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600768 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600769 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700770 if (decode && res->flags & IORESOURCE_UNSET) {
771 p = string(p, pend, "size ", str_spec);
772 p = number(p, pend, resource_size(res), *specp);
773 } else {
774 p = number(p, pend, res->start, *specp);
775 if (res->start != res->end) {
776 *p++ = '-';
777 p = number(p, pend, res->end, *specp);
778 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600779 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600780 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600781 if (res->flags & IORESOURCE_MEM_64)
782 p = string(p, pend, " 64bit", str_spec);
783 if (res->flags & IORESOURCE_PREFETCH)
784 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700785 if (res->flags & IORESOURCE_WINDOW)
786 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600787 if (res->flags & IORESOURCE_DISABLED)
788 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600789 } else {
790 p = string(p, pend, " flags ", str_spec);
791 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600792 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100793 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600794 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100795
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100796 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100797}
798
Joe Perchescf3b4292010-05-24 14:33:16 -0700799static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700800char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
801 const char *fmt)
802{
Steven Rostedt360603a2013-05-28 15:47:39 -0400803 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700804 negative value, fallback to the default */
805 char separator;
806
807 if (spec.field_width == 0)
808 /* nothing to print */
809 return buf;
810
811 if (ZERO_OR_NULL_PTR(addr))
812 /* NULL pointer */
813 return string(buf, end, NULL, spec);
814
815 switch (fmt[1]) {
816 case 'C':
817 separator = ':';
818 break;
819 case 'D':
820 separator = '-';
821 break;
822 case 'N':
823 separator = 0;
824 break;
825 default:
826 separator = ' ';
827 break;
828 }
829
830 if (spec.field_width > 0)
831 len = min_t(int, spec.field_width, 64);
832
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700833 for (i = 0; i < len; ++i) {
834 if (buf < end)
835 *buf = hex_asc_hi(addr[i]);
836 ++buf;
837 if (buf < end)
838 *buf = hex_asc_lo(addr[i]);
839 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700840
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700841 if (separator && i != len - 1) {
842 if (buf < end)
843 *buf = separator;
844 ++buf;
845 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700846 }
847
848 return buf;
849}
850
851static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800852char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
853 struct printf_spec spec, const char *fmt)
854{
855 const int CHUNKSZ = 32;
856 int nr_bits = max_t(int, spec.field_width, 0);
857 int i, chunksz;
858 bool first = true;
859
860 /* reused to print numbers */
861 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
862
863 chunksz = nr_bits & (CHUNKSZ - 1);
864 if (chunksz == 0)
865 chunksz = CHUNKSZ;
866
867 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
868 for (; i >= 0; i -= CHUNKSZ) {
869 u32 chunkmask, val;
870 int word, bit;
871
872 chunkmask = ((1ULL << chunksz) - 1);
873 word = i / BITS_PER_LONG;
874 bit = i % BITS_PER_LONG;
875 val = (bitmap[word] >> bit) & chunkmask;
876
877 if (!first) {
878 if (buf < end)
879 *buf = ',';
880 buf++;
881 }
882 first = false;
883
884 spec.field_width = DIV_ROUND_UP(chunksz, 4);
885 buf = number(buf, end, val, spec);
886
887 chunksz = CHUNKSZ;
888 }
889 return buf;
890}
891
892static noinline_for_stack
893char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
894 struct printf_spec spec, const char *fmt)
895{
896 int nr_bits = max_t(int, spec.field_width, 0);
897 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
898 int cur, rbot, rtop;
899 bool first = true;
900
901 /* reused to print numbers */
902 spec = (struct printf_spec){ .base = 10 };
903
904 rbot = cur = find_first_bit(bitmap, nr_bits);
905 while (cur < nr_bits) {
906 rtop = cur;
907 cur = find_next_bit(bitmap, nr_bits, cur + 1);
908 if (cur < nr_bits && cur <= rtop + 1)
909 continue;
910
911 if (!first) {
912 if (buf < end)
913 *buf = ',';
914 buf++;
915 }
916 first = false;
917
918 buf = number(buf, end, rbot, spec);
919 if (rbot < rtop) {
920 if (buf < end)
921 *buf = '-';
922 buf++;
923
924 buf = number(buf, end, rtop, spec);
925 }
926
927 rbot = cur;
928 }
929 return buf;
930}
931
932static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700933char *mac_address_string(char *buf, char *end, u8 *addr,
934 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700935{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000936 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700937 char *p = mac_addr;
938 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000939 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700940 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000941
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700942 switch (fmt[1]) {
943 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000944 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700945 break;
946
947 case 'R':
948 reversed = true;
949 /* fall through */
950
951 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000952 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700953 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000954 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700955
956 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700957 if (reversed)
958 p = hex_byte_pack(p, addr[5 - i]);
959 else
960 p = hex_byte_pack(p, addr[i]);
961
Joe Perches8a27f7c2009-08-17 12:29:44 +0000962 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000963 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700964 }
965 *p = '\0';
966
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100967 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700968}
969
Joe Perchescf3b4292010-05-24 14:33:16 -0700970static noinline_for_stack
971char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700972{
Harvey Harrison689afa72008-10-28 16:04:44 -0700973 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800974 bool leading_zeros = (fmt[0] == 'i');
975 int index;
976 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700977
Joe Perches0159f242010-01-13 20:23:30 -0800978 switch (fmt[2]) {
979 case 'h':
980#ifdef __BIG_ENDIAN
981 index = 0;
982 step = 1;
983#else
984 index = 3;
985 step = -1;
986#endif
987 break;
988 case 'l':
989 index = 3;
990 step = -1;
991 break;
992 case 'n':
993 case 'b':
994 default:
995 index = 0;
996 step = 1;
997 break;
998 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000999 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001000 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001001 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001002 if (leading_zeros) {
1003 if (digits < 3)
1004 *p++ = '0';
1005 if (digits < 2)
1006 *p++ = '0';
1007 }
1008 /* reverse the digits in the quad */
1009 while (digits--)
1010 *p++ = temp[digits];
1011 if (i < 3)
1012 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001013 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001014 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001015 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001016
Joe Perches8a27f7c2009-08-17 12:29:44 +00001017 return p;
1018}
1019
Joe Perchescf3b4292010-05-24 14:33:16 -07001020static noinline_for_stack
1021char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001022{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001023 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001024 unsigned char zerolength[8];
1025 int longest = 1;
1026 int colonpos = -1;
1027 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001028 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001029 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001030 bool useIPv4;
1031 struct in6_addr in6;
1032
1033 memcpy(&in6, addr, sizeof(struct in6_addr));
1034
1035 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001036
1037 memset(zerolength, 0, sizeof(zerolength));
1038
1039 if (useIPv4)
1040 range = 6;
1041 else
1042 range = 8;
1043
1044 /* find position of longest 0 run */
1045 for (i = 0; i < range; i++) {
1046 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001047 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001048 break;
1049 zerolength[i]++;
1050 }
1051 }
1052 for (i = 0; i < range; i++) {
1053 if (zerolength[i] > longest) {
1054 longest = zerolength[i];
1055 colonpos = i;
1056 }
1057 }
Joe Perches29cf5192011-06-09 11:23:37 -07001058 if (longest == 1) /* don't compress a single 0 */
1059 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001060
1061 /* emit address */
1062 for (i = 0; i < range; i++) {
1063 if (i == colonpos) {
1064 if (needcolon || i == 0)
1065 *p++ = ':';
1066 *p++ = ':';
1067 needcolon = false;
1068 i += longest - 1;
1069 continue;
1070 }
1071 if (needcolon) {
1072 *p++ = ':';
1073 needcolon = false;
1074 }
1075 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001076 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001077 hi = word >> 8;
1078 lo = word & 0xff;
1079 if (hi) {
1080 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001081 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001082 else
1083 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001084 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001085 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001086 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001087 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001088 else
1089 *p++ = hex_asc_lo(lo);
1090 needcolon = true;
1091 }
1092
1093 if (useIPv4) {
1094 if (needcolon)
1095 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001096 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001097 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001098 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001099
Joe Perches8a27f7c2009-08-17 12:29:44 +00001100 return p;
1101}
1102
Joe Perchescf3b4292010-05-24 14:33:16 -07001103static noinline_for_stack
1104char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001105{
1106 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001107
Harvey Harrison689afa72008-10-28 16:04:44 -07001108 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001109 p = hex_byte_pack(p, *addr++);
1110 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001111 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001112 *p++ = ':';
1113 }
1114 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001115
Joe Perches8a27f7c2009-08-17 12:29:44 +00001116 return p;
1117}
1118
Joe Perchescf3b4292010-05-24 14:33:16 -07001119static noinline_for_stack
1120char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1121 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001122{
1123 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1124
1125 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001126 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001127 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001128 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001129
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001130 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001131}
1132
Joe Perchescf3b4292010-05-24 14:33:16 -07001133static noinline_for_stack
1134char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1135 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001136{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001137 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001138
Joe Perches0159f242010-01-13 20:23:30 -08001139 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001140
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001141 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001142}
1143
Joe Perchescf3b4292010-05-24 14:33:16 -07001144static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001145char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1146 struct printf_spec spec, const char *fmt)
1147{
1148 bool have_p = false, have_s = false, have_f = false, have_c = false;
1149 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1150 sizeof(":12345") + sizeof("/123456789") +
1151 sizeof("%1234567890")];
1152 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1153 const u8 *addr = (const u8 *) &sa->sin6_addr;
1154 char fmt6[2] = { fmt[0], '6' };
1155 u8 off = 0;
1156
1157 fmt++;
1158 while (isalpha(*++fmt)) {
1159 switch (*fmt) {
1160 case 'p':
1161 have_p = true;
1162 break;
1163 case 'f':
1164 have_f = true;
1165 break;
1166 case 's':
1167 have_s = true;
1168 break;
1169 case 'c':
1170 have_c = true;
1171 break;
1172 }
1173 }
1174
1175 if (have_p || have_s || have_f) {
1176 *p = '[';
1177 off = 1;
1178 }
1179
1180 if (fmt6[0] == 'I' && have_c)
1181 p = ip6_compressed_string(ip6_addr + off, addr);
1182 else
1183 p = ip6_string(ip6_addr + off, addr, fmt6);
1184
1185 if (have_p || have_s || have_f)
1186 *p++ = ']';
1187
1188 if (have_p) {
1189 *p++ = ':';
1190 p = number(p, pend, ntohs(sa->sin6_port), spec);
1191 }
1192 if (have_f) {
1193 *p++ = '/';
1194 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1195 IPV6_FLOWINFO_MASK), spec);
1196 }
1197 if (have_s) {
1198 *p++ = '%';
1199 p = number(p, pend, sa->sin6_scope_id, spec);
1200 }
1201 *p = '\0';
1202
1203 return string(buf, end, ip6_addr, spec);
1204}
1205
1206static noinline_for_stack
1207char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1208 struct printf_spec spec, const char *fmt)
1209{
1210 bool have_p = false;
1211 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1212 char *pend = ip4_addr + sizeof(ip4_addr);
1213 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1214 char fmt4[3] = { fmt[0], '4', 0 };
1215
1216 fmt++;
1217 while (isalpha(*++fmt)) {
1218 switch (*fmt) {
1219 case 'p':
1220 have_p = true;
1221 break;
1222 case 'h':
1223 case 'l':
1224 case 'n':
1225 case 'b':
1226 fmt4[2] = *fmt;
1227 break;
1228 }
1229 }
1230
1231 p = ip4_string(ip4_addr, addr, fmt4);
1232 if (have_p) {
1233 *p++ = ':';
1234 p = number(p, pend, ntohs(sa->sin_port), spec);
1235 }
1236 *p = '\0';
1237
1238 return string(buf, end, ip4_addr, spec);
1239}
1240
1241static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001242char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1243 const char *fmt)
1244{
1245 bool found = true;
1246 int count = 1;
1247 unsigned int flags = 0;
1248 int len;
1249
1250 if (spec.field_width == 0)
1251 return buf; /* nothing to print */
1252
1253 if (ZERO_OR_NULL_PTR(addr))
1254 return string(buf, end, NULL, spec); /* NULL pointer */
1255
1256
1257 do {
1258 switch (fmt[count++]) {
1259 case 'a':
1260 flags |= ESCAPE_ANY;
1261 break;
1262 case 'c':
1263 flags |= ESCAPE_SPECIAL;
1264 break;
1265 case 'h':
1266 flags |= ESCAPE_HEX;
1267 break;
1268 case 'n':
1269 flags |= ESCAPE_NULL;
1270 break;
1271 case 'o':
1272 flags |= ESCAPE_OCTAL;
1273 break;
1274 case 'p':
1275 flags |= ESCAPE_NP;
1276 break;
1277 case 's':
1278 flags |= ESCAPE_SPACE;
1279 break;
1280 default:
1281 found = false;
1282 break;
1283 }
1284 } while (found);
1285
1286 if (!flags)
1287 flags = ESCAPE_ANY_NP;
1288
1289 len = spec.field_width < 0 ? 1 : spec.field_width;
1290
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001291 /*
1292 * string_escape_mem() writes as many characters as it can to
1293 * the given buffer, and returns the total size of the output
1294 * had the buffer been big enough.
1295 */
1296 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001297
1298 return buf;
1299}
1300
1301static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001302char *uuid_string(char *buf, char *end, const u8 *addr,
1303 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001304{
1305 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1306 char *p = uuid;
1307 int i;
1308 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1309 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1310 const u8 *index = be;
1311 bool uc = false;
1312
1313 switch (*(++fmt)) {
1314 case 'L':
1315 uc = true; /* fall-through */
1316 case 'l':
1317 index = le;
1318 break;
1319 case 'B':
1320 uc = true;
1321 break;
1322 }
1323
1324 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001325 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001326 switch (i) {
1327 case 3:
1328 case 5:
1329 case 7:
1330 case 9:
1331 *p++ = '-';
1332 break;
1333 }
1334 }
1335
1336 *p = 0;
1337
1338 if (uc) {
1339 p = uuid;
1340 do {
1341 *p = toupper(*p);
1342 } while (*(++p));
1343 }
1344
1345 return string(buf, end, uuid, spec);
1346}
1347
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001348static
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001349char *netdev_feature_string(char *buf, char *end, const void *addr)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001350{
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001351 unsigned long long num = *(const netdev_features_t *)addr;
1352 int size = sizeof(netdev_features_t);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001353
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001354 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001355}
1356
Joe Perchesaaf07622014-01-23 15:54:17 -08001357static noinline_for_stack
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001358char *address_val(char *buf, char *end, const void *addr, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001359{
1360 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001361 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001362
1363 switch (fmt[1]) {
1364 case 'd':
1365 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001366 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001367 break;
1368 case 'p':
1369 default:
1370 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001371 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001372 break;
1373 }
1374
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001375 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001376}
1377
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001378static noinline_for_stack
1379char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1380 const char *fmt)
1381{
1382 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1383 return string(buf, end, NULL, spec);
1384
1385 switch (fmt[1]) {
1386 case 'r':
1387 return number(buf, end, clk_get_rate(clk), spec);
1388
1389 case 'n':
1390 default:
1391#ifdef CONFIG_COMMON_CLK
1392 return string(buf, end, __clk_get_name(clk), spec);
1393#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001394 return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001395#endif
1396 }
1397}
1398
Ingo Molnar411f05f2011-05-12 23:00:28 +02001399int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001400
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001401/*
1402 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1403 * by an extra set of alphanumeric characters that are extended format
1404 * specifiers.
1405 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001406 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001407 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001408 * - 'F' For symbolic function descriptor pointers with offset
1409 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001410 * - 'S' For symbolic direct pointers with offset
1411 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001412 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001413 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001414 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1415 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001416 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1417 * width which must be explicitly specified either as part of the
1418 * format string '%32b[l]' or through '%*b[l]', [l] selects
1419 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001420 * - 'M' For a 6-byte MAC address, it prints the address in the
1421 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001422 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001423 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001424 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001425 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001426 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1427 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1428 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001429 * [S][pfs]
1430 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1431 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001432 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1433 * IPv6 omits the colons (01020304...0f)
1434 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001435 * [S][pfs]
1436 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1437 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1438 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1439 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001440 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001441 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1442 * of the following flags (see string_escape_mem() for the
1443 * details):
1444 * a - ESCAPE_ANY
1445 * c - ESCAPE_SPECIAL
1446 * h - ESCAPE_HEX
1447 * n - ESCAPE_NULL
1448 * o - ESCAPE_OCTAL
1449 * p - ESCAPE_NP
1450 * s - ESCAPE_SPACE
1451 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001452 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1453 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1454 * Options for %pU are:
1455 * b big endian lower case hex (default)
1456 * B big endian UPPER case hex
1457 * l little endian lower case hex
1458 * L little endian UPPER case hex
1459 * big endian output byte order is:
1460 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1461 * little endian output byte order is:
1462 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001463 * - 'V' For a struct va_format which contains a format string * and va_list *,
1464 * call vsnprintf(->format, *->va_list).
1465 * Implements a "recursive vsnprintf".
1466 * Do not use this feature without some mechanism to verify the
1467 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001468 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001469 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001470 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1471 * a certain separator (' ' by default):
1472 * C colon
1473 * D dash
1474 * N no separator
1475 * The maximum supported length is 64 bytes of the input. Consider
1476 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001477 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1478 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001479 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1480 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001481 * - 'g' For block_device name (gendisk + partition number)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001482 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1483 * (legacy clock framework) of the clock
1484 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1485 * (legacy clock framework) of the clock
1486 * - 'Cr' For a clock, it prints the current rate of the clock
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001487 *
1488 * ** Please update also Documentation/printk-formats.txt when making changes **
Joe Perches9ac6e442009-12-14 18:01:09 -08001489 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001490 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1491 * function pointers are really function descriptors, which contain a
1492 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001493 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001494static noinline_for_stack
1495char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1496 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001497{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001498 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001499
Kees Cook9f36e2c2011-03-22 16:34:22 -07001500 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001501 /*
1502 * Print (null) with the same width as a pointer so it makes
1503 * tabular output look nice.
1504 */
1505 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001506 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001507 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001508 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001509
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001510 switch (*fmt) {
1511 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001512 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001513 ptr = dereference_function_descriptor(ptr);
1514 /* Fallthrough */
1515 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001516 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001517 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001518 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001519 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001520 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001521 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001522 case 'h':
1523 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001524 case 'b':
1525 switch (fmt[1]) {
1526 case 'l':
1527 return bitmap_list_string(buf, end, ptr, spec, fmt);
1528 default:
1529 return bitmap_string(buf, end, ptr, spec, fmt);
1530 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001531 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1532 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001533 /* [mM]F (FDDI) */
1534 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001535 return mac_address_string(buf, end, ptr, spec, fmt);
1536 case 'I': /* Formatted IP supported
1537 * 4: 1.2.3.4
1538 * 6: 0001:0203:...:0708
1539 * 6c: 1::708 or 1::1.2.3.4
1540 */
1541 case 'i': /* Contiguous:
1542 * 4: 001.002.003.004
1543 * 6: 000102...0f
1544 */
1545 switch (fmt[1]) {
1546 case '6':
1547 return ip6_addr_string(buf, end, ptr, spec, fmt);
1548 case '4':
1549 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001550 case 'S': {
1551 const union {
1552 struct sockaddr raw;
1553 struct sockaddr_in v4;
1554 struct sockaddr_in6 v6;
1555 } *sa = ptr;
1556
1557 switch (sa->raw.sa_family) {
1558 case AF_INET:
1559 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1560 case AF_INET6:
1561 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1562 default:
1563 return string(buf, end, "(invalid address)", spec);
1564 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001565 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001566 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001567 case 'E':
1568 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001569 case 'U':
1570 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001571 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001572 {
1573 va_list va;
1574
1575 va_copy(va, *((struct va_format *)ptr)->va);
1576 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1577 ((struct va_format *)ptr)->fmt, va);
1578 va_end(va);
1579 return buf;
1580 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001581 case 'K':
1582 /*
1583 * %pK cannot be used in IRQ context because its test
1584 * for CAP_SYSLOG would be meaningless.
1585 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001586 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1587 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001588 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001589 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001590 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001591 }
Ryan Mallon312b4e22013-11-12 15:08:51 -08001592
1593 switch (kptr_restrict) {
1594 case 0:
1595 /* Always print %pK values */
1596 break;
1597 case 1: {
1598 /*
1599 * Only print the real pointer value if the current
1600 * process has CAP_SYSLOG and is running with the
1601 * same credentials it started with. This is because
1602 * access to files is checked at open() time, but %pK
1603 * checks permission at read() time. We don't want to
1604 * leak pointer values if a binary opens a file using
1605 * %pK and then elevates privileges before reading it.
1606 */
1607 const struct cred *cred = current_cred();
1608
1609 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1610 !uid_eq(cred->euid, cred->uid) ||
1611 !gid_eq(cred->egid, cred->gid))
1612 ptr = NULL;
1613 break;
1614 }
1615 case 2:
1616 default:
1617 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -07001618 ptr = NULL;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001619 break;
1620 }
Joe Perches26297602011-03-22 16:34:19 -07001621 break;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001622
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001623 case 'N':
1624 switch (fmt[1]) {
1625 case 'F':
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001626 return netdev_feature_string(buf, end, ptr);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001627 }
1628 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001629 case 'a':
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001630 return address_val(buf, end, ptr, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001631 case 'd':
1632 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001633 case 'C':
1634 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001635 case 'D':
1636 return dentry_name(buf, end,
1637 ((const struct file *)ptr)->f_path.dentry,
1638 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001639#ifdef CONFIG_BLOCK
1640 case 'g':
1641 return bdev_name(buf, end, ptr, spec, fmt);
1642#endif
1643
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001644 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001645 spec.flags |= SMALL;
1646 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001647 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001648 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001649 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001650 spec.base = 16;
1651
1652 return number(buf, end, (unsigned long) ptr, spec);
1653}
1654
1655/*
1656 * Helper function to decode printf style format.
1657 * Each call decode a token from the format and return the
1658 * number of characters read (or likely the delta where it wants
1659 * to go on the next call).
1660 * The decoded token is returned through the parameters
1661 *
1662 * 'h', 'l', or 'L' for integer fields
1663 * 'z' support added 23/7/1999 S.H.
1664 * 'z' changed to 'Z' --davidm 1/25/99
1665 * 't' added for ptrdiff_t
1666 *
1667 * @fmt: the format string
1668 * @type of the token returned
1669 * @flags: various flags such as +, -, # tokens..
1670 * @field_width: overwritten width
1671 * @base: base of the number (octal, hex, ...)
1672 * @precision: precision of a number
1673 * @qualifier: qualifier of a number (long, size_t, ...)
1674 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001675static noinline_for_stack
1676int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001677{
1678 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001679 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001680
1681 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001682 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001683 if (spec->field_width < 0) {
1684 spec->field_width = -spec->field_width;
1685 spec->flags |= LEFT;
1686 }
1687 spec->type = FORMAT_TYPE_NONE;
1688 goto precision;
1689 }
1690
1691 /* we finished early by reading the precision */
1692 if (spec->type == FORMAT_TYPE_PRECISION) {
1693 if (spec->precision < 0)
1694 spec->precision = 0;
1695
1696 spec->type = FORMAT_TYPE_NONE;
1697 goto qualifier;
1698 }
1699
1700 /* By default */
1701 spec->type = FORMAT_TYPE_NONE;
1702
1703 for (; *fmt ; ++fmt) {
1704 if (*fmt == '%')
1705 break;
1706 }
1707
1708 /* Return the current non-format string */
1709 if (fmt != start || !*fmt)
1710 return fmt - start;
1711
1712 /* Process flags */
1713 spec->flags = 0;
1714
1715 while (1) { /* this also skips first '%' */
1716 bool found = true;
1717
1718 ++fmt;
1719
1720 switch (*fmt) {
1721 case '-': spec->flags |= LEFT; break;
1722 case '+': spec->flags |= PLUS; break;
1723 case ' ': spec->flags |= SPACE; break;
1724 case '#': spec->flags |= SPECIAL; break;
1725 case '0': spec->flags |= ZEROPAD; break;
1726 default: found = false;
1727 }
1728
1729 if (!found)
1730 break;
1731 }
1732
1733 /* get field width */
1734 spec->field_width = -1;
1735
1736 if (isdigit(*fmt))
1737 spec->field_width = skip_atoi(&fmt);
1738 else if (*fmt == '*') {
1739 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001740 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001741 return ++fmt - start;
1742 }
1743
1744precision:
1745 /* get the precision */
1746 spec->precision = -1;
1747 if (*fmt == '.') {
1748 ++fmt;
1749 if (isdigit(*fmt)) {
1750 spec->precision = skip_atoi(&fmt);
1751 if (spec->precision < 0)
1752 spec->precision = 0;
1753 } else if (*fmt == '*') {
1754 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001755 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001756 return ++fmt - start;
1757 }
1758 }
1759
1760qualifier:
1761 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001762 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001763 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1764 _tolower(*fmt) == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001765 qualifier = *fmt++;
1766 if (unlikely(qualifier == *fmt)) {
1767 if (qualifier == 'l') {
1768 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08001769 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001770 } else if (qualifier == 'h') {
1771 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08001772 ++fmt;
1773 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001774 }
1775 }
1776
1777 /* default base */
1778 spec->base = 10;
1779 switch (*fmt) {
1780 case 'c':
1781 spec->type = FORMAT_TYPE_CHAR;
1782 return ++fmt - start;
1783
1784 case 's':
1785 spec->type = FORMAT_TYPE_STR;
1786 return ++fmt - start;
1787
1788 case 'p':
1789 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001790 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001791
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001792 case '%':
1793 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1794 return ++fmt - start;
1795
1796 /* integer number formats - set up the flags and "break" */
1797 case 'o':
1798 spec->base = 8;
1799 break;
1800
1801 case 'x':
1802 spec->flags |= SMALL;
1803
1804 case 'X':
1805 spec->base = 16;
1806 break;
1807
1808 case 'd':
1809 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001810 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001811 case 'u':
1812 break;
1813
Ryan Mallon708d96f2014-04-03 14:48:37 -07001814 case 'n':
1815 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001816 * Since %n poses a greater security risk than
1817 * utility, treat it as any other invalid or
1818 * unsupported format specifier.
Ryan Mallon708d96f2014-04-03 14:48:37 -07001819 */
Ryan Mallon708d96f2014-04-03 14:48:37 -07001820 /* Fall-through */
1821
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001822 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001823 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001824 spec->type = FORMAT_TYPE_INVALID;
1825 return fmt - start;
1826 }
1827
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001828 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001829 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001830 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001831 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1832 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001833 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001834 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001835 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001836 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001837 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001838 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1839 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001840 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001841 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1842 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001843 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001844 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1845 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001846 }
1847
1848 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001849}
1850
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08001851static void
1852set_field_width(struct printf_spec *spec, int width)
1853{
1854 spec->field_width = width;
1855 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
1856 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
1857 }
1858}
1859
1860static void
1861set_precision(struct printf_spec *spec, int prec)
1862{
1863 spec->precision = prec;
1864 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
1865 spec->precision = clamp(prec, 0, PRECISION_MAX);
1866 }
1867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/**
1870 * vsnprintf - Format a string and place it in a buffer
1871 * @buf: The buffer to place the result into
1872 * @size: The size of the buffer, including the trailing null space
1873 * @fmt: The format string to use
1874 * @args: Arguments for the format string
1875 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08001876 * This function generally follows C99 vsnprintf, but has some
1877 * extensions and a few limitations:
1878 *
1879 * %n is unsupported
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001880 * %p* is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07001881 *
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001882 * See pointer() or Documentation/printk-formats.txt for more
1883 * extensive description.
1884 *
1885 * ** Please update the documentation in both places when making changes **
Andrew Morton80f548e2012-07-30 14:40:25 -07001886 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * The return value is the number of characters which would
1888 * be generated for the given input, excluding the trailing
1889 * '\0', as per ISO C99. If you want to have the exact
1890 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001891 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * return is greater than or equal to @size, the resulting
1893 * string is truncated.
1894 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001895 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
1897int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001900 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001901 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001903 /* Reject out-of-range values early. Large positive sizes are
1904 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08001905 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001909 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001911 /* Make sure end is always >= buf */
1912 if (end < buf) {
1913 end = ((void *)-1);
1914 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001917 while (*fmt) {
1918 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001919 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001920
1921 fmt += read;
1922
1923 switch (spec.type) {
1924 case FORMAT_TYPE_NONE: {
1925 int copy = read;
1926 if (str < end) {
1927 if (copy > end - str)
1928 copy = end - str;
1929 memcpy(str, old_fmt, copy);
1930 }
1931 str += read;
1932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
Vegard Nossumed681a92009-03-14 12:08:50 +01001935 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08001936 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001939 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08001940 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
André Goddard Rosad4be1512009-12-14 18:00:59 -08001943 case FORMAT_TYPE_CHAR: {
1944 char c;
1945
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001946 if (!(spec.flags & LEFT)) {
1947 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001948 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 *str = ' ';
1950 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001953 }
1954 c = (unsigned char) va_arg(args, int);
1955 if (str < end)
1956 *str = c;
1957 ++str;
1958 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001959 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001960 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001962 }
1963 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001966 case FORMAT_TYPE_STR:
1967 str = string(str, end, va_arg(args, char *), spec);
1968 break;
1969
1970 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001971 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001972 spec);
1973 while (isalnum(*fmt))
1974 fmt++;
1975 break;
1976
1977 case FORMAT_TYPE_PERCENT_CHAR:
1978 if (str < end)
1979 *str = '%';
1980 ++str;
1981 break;
1982
1983 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001984 /*
1985 * Presumably the arguments passed gcc's type
1986 * checking, but there is no safe or sane way
1987 * for us to continue parsing the format and
1988 * fetching from the va_list; the remaining
1989 * specifiers and arguments would be out of
1990 * sync.
1991 */
1992 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001993
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001994 default:
1995 switch (spec.type) {
1996 case FORMAT_TYPE_LONG_LONG:
1997 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001999 case FORMAT_TYPE_ULONG:
2000 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002002 case FORMAT_TYPE_LONG:
2003 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002005 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002006 if (spec.flags & SIGN)
2007 num = va_arg(args, ssize_t);
2008 else
2009 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002010 break;
2011 case FORMAT_TYPE_PTRDIFF:
2012 num = va_arg(args, ptrdiff_t);
2013 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002014 case FORMAT_TYPE_UBYTE:
2015 num = (unsigned char) va_arg(args, int);
2016 break;
2017 case FORMAT_TYPE_BYTE:
2018 num = (signed char) va_arg(args, int);
2019 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002020 case FORMAT_TYPE_USHORT:
2021 num = (unsigned short) va_arg(args, int);
2022 break;
2023 case FORMAT_TYPE_SHORT:
2024 num = (short) va_arg(args, int);
2025 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002026 case FORMAT_TYPE_INT:
2027 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002030 num = va_arg(args, unsigned int);
2031 }
2032
2033 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002036
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002037out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002038 if (size > 0) {
2039 if (str < end)
2040 *str = '\0';
2041 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002042 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002043 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002044
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002045 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049EXPORT_SYMBOL(vsnprintf);
2050
2051/**
2052 * vscnprintf - Format a string and place it in a buffer
2053 * @buf: The buffer to place the result into
2054 * @size: The size of the buffer, including the trailing null space
2055 * @fmt: The format string to use
2056 * @args: Arguments for the format string
2057 *
2058 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002059 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * returns 0.
2061 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002062 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002063 *
2064 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 */
2066int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2067{
2068 int i;
2069
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002070 i = vsnprintf(buf, size, fmt, args);
2071
Anton Arapovb921c692011-01-12 16:59:49 -08002072 if (likely(i < size))
2073 return i;
2074 if (size != 0)
2075 return size - 1;
2076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078EXPORT_SYMBOL(vscnprintf);
2079
2080/**
2081 * snprintf - Format a string and place it in a buffer
2082 * @buf: The buffer to place the result into
2083 * @size: The size of the buffer, including the trailing null space
2084 * @fmt: The format string to use
2085 * @...: Arguments for the format string
2086 *
2087 * The return value is the number of characters which would be
2088 * generated for the given input, excluding the trailing null,
2089 * as per ISO C99. If the return is greater than or equal to
2090 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002091 *
2092 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002094int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 va_list args;
2097 int i;
2098
2099 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002100 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 return i;
2104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105EXPORT_SYMBOL(snprintf);
2106
2107/**
2108 * scnprintf - Format a string and place it in a buffer
2109 * @buf: The buffer to place the result into
2110 * @size: The size of the buffer, including the trailing null space
2111 * @fmt: The format string to use
2112 * @...: Arguments for the format string
2113 *
2114 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002115 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 */
2117
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002118int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
2120 va_list args;
2121 int i;
2122
2123 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002124 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002126
Anton Arapovb921c692011-01-12 16:59:49 -08002127 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129EXPORT_SYMBOL(scnprintf);
2130
2131/**
2132 * vsprintf - Format a string and place it in a buffer
2133 * @buf: The buffer to place the result into
2134 * @fmt: The format string to use
2135 * @args: Arguments for the format string
2136 *
2137 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002138 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 * buffer overflows.
2140 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002141 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002142 *
2143 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
2145int vsprintf(char *buf, const char *fmt, va_list args)
2146{
2147 return vsnprintf(buf, INT_MAX, fmt, args);
2148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149EXPORT_SYMBOL(vsprintf);
2150
2151/**
2152 * sprintf - Format a string and place it in a buffer
2153 * @buf: The buffer to place the result into
2154 * @fmt: The format string to use
2155 * @...: Arguments for the format string
2156 *
2157 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002158 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002160 *
2161 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002163int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 va_list args;
2166 int i;
2167
2168 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002169 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return i;
2173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174EXPORT_SYMBOL(sprintf);
2175
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002176#ifdef CONFIG_BINARY_PRINTF
2177/*
2178 * bprintf service:
2179 * vbin_printf() - VA arguments to binary data
2180 * bstr_printf() - Binary data to text string
2181 */
2182
2183/**
2184 * vbin_printf - Parse a format string and place args' binary value in a buffer
2185 * @bin_buf: The buffer to place args' binary value
2186 * @size: The size of the buffer(by words(32bits), not characters)
2187 * @fmt: The format string to use
2188 * @args: Arguments for the format string
2189 *
2190 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002191 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002192 *
2193 * The return value is the number of words(32bits) which would be generated for
2194 * the given input.
2195 *
2196 * NOTE:
2197 * If the return value is greater than @size, the resulting bin_buf is NOT
2198 * valid for bstr_printf().
2199 */
2200int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2201{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002202 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002203 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002204
2205 str = (char *)bin_buf;
2206 end = (char *)(bin_buf + size);
2207
2208#define save_arg(type) \
2209do { \
2210 if (sizeof(type) == 8) { \
2211 unsigned long long value; \
2212 str = PTR_ALIGN(str, sizeof(u32)); \
2213 value = va_arg(args, unsigned long long); \
2214 if (str + sizeof(type) <= end) { \
2215 *(u32 *)str = *(u32 *)&value; \
2216 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2217 } \
2218 } else { \
2219 unsigned long value; \
2220 str = PTR_ALIGN(str, sizeof(type)); \
2221 value = va_arg(args, int); \
2222 if (str + sizeof(type) <= end) \
2223 *(typeof(type) *)str = (type)value; \
2224 } \
2225 str += sizeof(type); \
2226} while (0)
2227
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002228 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002229 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002230
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002231 fmt += read;
2232
2233 switch (spec.type) {
2234 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002235 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002236 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002237 case FORMAT_TYPE_INVALID:
2238 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002239
Vegard Nossumed681a92009-03-14 12:08:50 +01002240 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002241 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002242 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002243 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002244
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002245 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002246 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002247 break;
2248
2249 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002250 const char *save_str = va_arg(args, char *);
2251 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002252
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002253 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2254 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002255 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002256 len = strlen(save_str) + 1;
2257 if (str + len < end)
2258 memcpy(str, save_str, len);
2259 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002260 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002261 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002262
2263 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002264 save_arg(void *);
2265 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002266 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002267 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002268 break;
2269
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002270 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002271 switch (spec.type) {
2272
2273 case FORMAT_TYPE_LONG_LONG:
2274 save_arg(long long);
2275 break;
2276 case FORMAT_TYPE_ULONG:
2277 case FORMAT_TYPE_LONG:
2278 save_arg(unsigned long);
2279 break;
2280 case FORMAT_TYPE_SIZE_T:
2281 save_arg(size_t);
2282 break;
2283 case FORMAT_TYPE_PTRDIFF:
2284 save_arg(ptrdiff_t);
2285 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002286 case FORMAT_TYPE_UBYTE:
2287 case FORMAT_TYPE_BYTE:
2288 save_arg(char);
2289 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002290 case FORMAT_TYPE_USHORT:
2291 case FORMAT_TYPE_SHORT:
2292 save_arg(short);
2293 break;
2294 default:
2295 save_arg(int);
2296 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002297 }
2298 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002299
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002300out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002301 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002302#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002303}
2304EXPORT_SYMBOL_GPL(vbin_printf);
2305
2306/**
2307 * bstr_printf - Format a string from binary arguments and place it in a buffer
2308 * @buf: The buffer to place the result into
2309 * @size: The size of the buffer, including the trailing null space
2310 * @fmt: The format string to use
2311 * @bin_buf: Binary arguments for the format string
2312 *
2313 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2314 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2315 * a binary buffer that generated by vbin_printf.
2316 *
2317 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002318 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002319 *
2320 * The return value is the number of characters which would
2321 * be generated for the given input, excluding the trailing
2322 * '\0', as per ISO C99. If you want to have the exact
2323 * number of characters written into @buf as return value
2324 * (not including the trailing '\0'), use vscnprintf(). If the
2325 * return is greater than or equal to @size, the resulting
2326 * string is truncated.
2327 */
2328int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2329{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002330 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002331 char *str, *end;
2332 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002333
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002334 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002335 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002336
2337 str = buf;
2338 end = buf + size;
2339
2340#define get_arg(type) \
2341({ \
2342 typeof(type) value; \
2343 if (sizeof(type) == 8) { \
2344 args = PTR_ALIGN(args, sizeof(u32)); \
2345 *(u32 *)&value = *(u32 *)args; \
2346 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2347 } else { \
2348 args = PTR_ALIGN(args, sizeof(type)); \
2349 value = *(typeof(type) *)args; \
2350 } \
2351 args += sizeof(type); \
2352 value; \
2353})
2354
2355 /* Make sure end is always >= buf */
2356 if (end < buf) {
2357 end = ((void *)-1);
2358 size = end - buf;
2359 }
2360
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002361 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002362 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002363 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002364
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002365 fmt += read;
2366
2367 switch (spec.type) {
2368 case FORMAT_TYPE_NONE: {
2369 int copy = read;
2370 if (str < end) {
2371 if (copy > end - str)
2372 copy = end - str;
2373 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002374 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002375 str += read;
2376 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002377 }
2378
Vegard Nossumed681a92009-03-14 12:08:50 +01002379 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002380 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002381 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002382
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002383 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002384 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002385 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002386
André Goddard Rosad4be1512009-12-14 18:00:59 -08002387 case FORMAT_TYPE_CHAR: {
2388 char c;
2389
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002390 if (!(spec.flags & LEFT)) {
2391 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002392 if (str < end)
2393 *str = ' ';
2394 ++str;
2395 }
2396 }
2397 c = (unsigned char) get_arg(char);
2398 if (str < end)
2399 *str = c;
2400 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002401 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002402 if (str < end)
2403 *str = ' ';
2404 ++str;
2405 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002406 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002407 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002408
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002409 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002410 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002411 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002412 str = string(str, end, (char *)str_arg, spec);
2413 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002414 }
2415
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002416 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002417 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002418 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002419 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002420 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002421
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002422 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002423 if (str < end)
2424 *str = '%';
2425 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002426 break;
2427
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002428 case FORMAT_TYPE_INVALID:
2429 goto out;
2430
André Goddard Rosad4be1512009-12-14 18:00:59 -08002431 default: {
2432 unsigned long long num;
2433
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002434 switch (spec.type) {
2435
2436 case FORMAT_TYPE_LONG_LONG:
2437 num = get_arg(long long);
2438 break;
2439 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002440 case FORMAT_TYPE_LONG:
2441 num = get_arg(unsigned long);
2442 break;
2443 case FORMAT_TYPE_SIZE_T:
2444 num = get_arg(size_t);
2445 break;
2446 case FORMAT_TYPE_PTRDIFF:
2447 num = get_arg(ptrdiff_t);
2448 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002449 case FORMAT_TYPE_UBYTE:
2450 num = get_arg(unsigned char);
2451 break;
2452 case FORMAT_TYPE_BYTE:
2453 num = get_arg(signed char);
2454 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002455 case FORMAT_TYPE_USHORT:
2456 num = get_arg(unsigned short);
2457 break;
2458 case FORMAT_TYPE_SHORT:
2459 num = get_arg(short);
2460 break;
2461 case FORMAT_TYPE_UINT:
2462 num = get_arg(unsigned int);
2463 break;
2464 default:
2465 num = get_arg(int);
2466 }
2467
2468 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002469 } /* default: */
2470 } /* switch(spec.type) */
2471 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002472
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002473out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002474 if (size > 0) {
2475 if (str < end)
2476 *str = '\0';
2477 else
2478 end[-1] = '\0';
2479 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002480
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002481#undef get_arg
2482
2483 /* the trailing null byte doesn't count towards the total */
2484 return str - buf;
2485}
2486EXPORT_SYMBOL_GPL(bstr_printf);
2487
2488/**
2489 * bprintf - Parse a format string and place args' binary value in a buffer
2490 * @bin_buf: The buffer to place args' binary value
2491 * @size: The size of the buffer(by words(32bits), not characters)
2492 * @fmt: The format string to use
2493 * @...: Arguments for the format string
2494 *
2495 * The function returns the number of words(u32) written
2496 * into @bin_buf.
2497 */
2498int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2499{
2500 va_list args;
2501 int ret;
2502
2503 va_start(args, fmt);
2504 ret = vbin_printf(bin_buf, size, fmt, args);
2505 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002506
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002507 return ret;
2508}
2509EXPORT_SYMBOL_GPL(bprintf);
2510
2511#endif /* CONFIG_BINARY_PRINTF */
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513/**
2514 * vsscanf - Unformat a buffer into a list of arguments
2515 * @buf: input buffer
2516 * @fmt: format of buffer
2517 * @args: arguments
2518 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002519int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 const char *str = buf;
2522 char *next;
2523 char digit;
2524 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002525 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002526 unsigned int base;
2527 union {
2528 long long s;
2529 unsigned long long u;
2530 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002531 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002532 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Jan Beulichda990752012-10-04 17:13:24 -07002534 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /* skip any white space in format */
2536 /* white space in format matchs any amount of
2537 * white space, including none, in the input.
2538 */
2539 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002540 fmt = skip_spaces(++fmt);
2541 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543
2544 /* anything that is not a conversion must match exactly */
2545 if (*fmt != '%' && *fmt) {
2546 if (*fmt++ != *str++)
2547 break;
2548 continue;
2549 }
2550
2551 if (!*fmt)
2552 break;
2553 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /* skip this conversion.
2556 * advance both strings to next white space
2557 */
2558 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002559 if (!*str)
2560 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002561 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 fmt++;
2563 while (!isspace(*str) && *str)
2564 str++;
2565 continue;
2566 }
2567
2568 /* get field width */
2569 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002570 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002572 if (field_width <= 0)
2573 break;
2574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 /* get conversion qualifier */
2577 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002578 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2579 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 qualifier = *fmt++;
2581 if (unlikely(qualifier == *fmt)) {
2582 if (qualifier == 'h') {
2583 qualifier = 'H';
2584 fmt++;
2585 } else if (qualifier == 'l') {
2586 qualifier = 'L';
2587 fmt++;
2588 }
2589 }
2590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Jan Beulichda990752012-10-04 17:13:24 -07002592 if (!*fmt)
2593 break;
2594
2595 if (*fmt == 'n') {
2596 /* return number of characters read so far */
2597 *va_arg(args, int *) = str - buf;
2598 ++fmt;
2599 continue;
2600 }
2601
2602 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 break;
2604
André Goddard Rosad4be1512009-12-14 18:00:59 -08002605 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002606 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002607
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002608 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 case 'c':
2610 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002611 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (field_width == -1)
2613 field_width = 1;
2614 do {
2615 *s++ = *str++;
2616 } while (--field_width > 0 && *str);
2617 num++;
2618 }
2619 continue;
2620 case 's':
2621 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002622 char *s = (char *)va_arg(args, char *);
2623 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002624 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002626 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002629 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 *s = '\0';
2632 num++;
2633 }
2634 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 case 'o':
2636 base = 8;
2637 break;
2638 case 'x':
2639 case 'X':
2640 base = 16;
2641 break;
2642 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002643 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002645 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 case 'u':
2647 break;
2648 case '%':
2649 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002650 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 return num;
2652 continue;
2653 default:
2654 /* invalid format; stop here */
2655 return num;
2656 }
2657
2658 /* have some sort of integer conversion.
2659 * first, skip white space in buffer.
2660 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002661 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 digit = *str;
2664 if (is_sign && digit == '-')
2665 digit = *(str + 1);
2666
2667 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002668 || (base == 16 && !isxdigit(digit))
2669 || (base == 10 && !isdigit(digit))
2670 || (base == 8 && (!isdigit(digit) || digit > '7'))
2671 || (base == 0 && !isdigit(digit)))
2672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Jan Beulich53809752012-12-17 16:01:31 -08002674 if (is_sign)
2675 val.s = qualifier != 'L' ?
2676 simple_strtol(str, &next, base) :
2677 simple_strtoll(str, &next, base);
2678 else
2679 val.u = qualifier != 'L' ?
2680 simple_strtoul(str, &next, base) :
2681 simple_strtoull(str, &next, base);
2682
2683 if (field_width > 0 && next - str > field_width) {
2684 if (base == 0)
2685 _parse_integer_fixup_radix(str, &base);
2686 while (next - str > field_width) {
2687 if (is_sign)
2688 val.s = div_s64(val.s, base);
2689 else
2690 val.u = div_u64(val.u, base);
2691 --next;
2692 }
2693 }
2694
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002695 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002697 if (is_sign)
2698 *va_arg(args, signed char *) = val.s;
2699 else
2700 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 break;
2702 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002703 if (is_sign)
2704 *va_arg(args, short *) = val.s;
2705 else
2706 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002709 if (is_sign)
2710 *va_arg(args, long *) = val.s;
2711 else
2712 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 break;
2714 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002715 if (is_sign)
2716 *va_arg(args, long long *) = val.s;
2717 else
2718 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 break;
2720 case 'Z':
2721 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002722 *va_arg(args, size_t *) = val.u;
2723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 default:
Jan Beulich53809752012-12-17 16:01:31 -08002725 if (is_sign)
2726 *va_arg(args, int *) = val.s;
2727 else
2728 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 break;
2730 }
2731 num++;
2732
2733 if (!next)
2734 break;
2735 str = next;
2736 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return num;
2739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740EXPORT_SYMBOL(vsscanf);
2741
2742/**
2743 * sscanf - Unformat a buffer into a list of arguments
2744 * @buf: input buffer
2745 * @fmt: formatting of buffer
2746 * @...: resulting arguments
2747 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002748int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
2750 va_list args;
2751 int i;
2752
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002753 va_start(args, fmt);
2754 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return i;
2758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759EXPORT_SYMBOL(sscanf);