blob: 3add92329bae3d906200f9e09d1e916e97d14ae2 [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>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020033#include <linux/rtc.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070034#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020035#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000036#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110037#include <linux/siphash.h>
38#include <linux/compiler.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040039#ifdef CONFIG_BLOCK
40#include <linux/blkdev.h>
41#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070043#include "../mm/internal.h" /* For the trace_print_flags arrays */
44
Tim Schmielau4e57b682005-10-30 15:03:48 -080045#include <asm/page.h> /* for PAGE_SIZE */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070046#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Shevchenko71dca952014-10-13 15:55:18 -070048#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070049#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * simple_strtoull - convert a string to an unsigned long long
53 * @cp: The start of the string
54 * @endp: A pointer to the end of the parsed string will be placed here
55 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080056 *
57 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
Harvey Harrison22d27052008-10-16 13:40:35 -070059unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070061 unsigned long long result;
62 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070064 cp = _parse_integer_fixup_radix(cp, &base);
65 rv = _parse_integer(cp, base, &result);
66 /* FIXME */
67 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (endp)
70 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return result;
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074EXPORT_SYMBOL(simple_strtoull);
75
76/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080077 * simple_strtoul - convert a string to an unsigned long
78 * @cp: The start of the string
79 * @endp: A pointer to the end of the parsed string will be placed here
80 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080081 *
82 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080083 */
84unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
85{
86 return simple_strtoull(cp, endp, base);
87}
88EXPORT_SYMBOL(simple_strtoul);
89
90/**
91 * simple_strtol - convert a string to a signed long
92 * @cp: The start of the string
93 * @endp: A pointer to the end of the parsed string will be placed here
94 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080095 *
96 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080097 */
98long simple_strtol(const char *cp, char **endp, unsigned int base)
99{
100 if (*cp == '-')
101 return -simple_strtoul(cp + 1, endp, base);
102
103 return simple_strtoul(cp, endp, base);
104}
105EXPORT_SYMBOL(simple_strtol);
106
107/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * simple_strtoll - convert a string to a signed long long
109 * @cp: The start of the string
110 * @endp: A pointer to the end of the parsed string will be placed here
111 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800112 *
113 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700115long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800117 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700118 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800119
Harvey Harrison22d27052008-10-16 13:40:35 -0700120 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400122EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Joe Perchescf3b4292010-05-24 14:33:16 -0700124static noinline_for_stack
125int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800127 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800129 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800131 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return i;
134}
135
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700136/*
137 * Decimal conversion is by far the most typical, and is used for
138 * /proc and /sys data. This directly impacts e.g. top performance
139 * with many processes running. We optimize it for speed by emitting
140 * two characters at a time, using a 200 byte lookup table. This
141 * roughly halves the number of multiplications compared to computing
142 * the digits one at a time. Implementation strongly inspired by the
143 * previous version, which in turn used ideas described at
144 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
145 * from the author, Douglas W. Jones).
146 *
147 * It turns out there is precisely one 26 bit fixed-point
148 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
149 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
150 * range happens to be somewhat larger (x <= 1073741898), but that's
151 * irrelevant for our purpose.
152 *
153 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
154 * need a 32x32->64 bit multiply, so we simply use the same constant.
155 *
156 * For dividing a number in the range [100, 10^4-1] by 100, there are
157 * several options. The simplest is (x * 0x147b) >> 19, which is valid
158 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700159 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700160
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700161static const u16 decpair[100] = {
162#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
163 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
164 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
165 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
166 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
167 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
168 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
169 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
170 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
171 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
172 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
173#undef _
174};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700175
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700176/*
177 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700178 * immediately jump to out_r where two 0s would be written but only
179 * one of them accounted for in buf. This is needed by ip4_string
180 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700181*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700182static noinline_for_stack
183char *put_dec_trunc8(char *buf, unsigned r)
184{
185 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700186
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700187 /* 1 <= r < 10^8 */
188 if (r < 100)
189 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700190
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700191 /* 100 <= r < 10^8 */
192 q = (r * (u64)0x28f5c29) >> 32;
193 *((u16 *)buf) = decpair[r - 100*q];
194 buf += 2;
195
196 /* 1 <= q < 10^6 */
197 if (q < 100)
198 goto out_q;
199
200 /* 100 <= q < 10^6 */
201 r = (q * (u64)0x28f5c29) >> 32;
202 *((u16 *)buf) = decpair[q - 100*r];
203 buf += 2;
204
205 /* 1 <= r < 10^4 */
206 if (r < 100)
207 goto out_r;
208
209 /* 100 <= r < 10^4 */
210 q = (r * 0x147b) >> 19;
211 *((u16 *)buf) = decpair[r - 100*q];
212 buf += 2;
213out_q:
214 /* 1 <= q < 100 */
215 r = q;
216out_r:
217 /* 1 <= r < 100 */
218 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700219 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700220 return buf;
221}
222
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700223#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
224static noinline_for_stack
225char *put_dec_full8(char *buf, unsigned r)
226{
227 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700228
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700229 /* 0 <= r < 10^8 */
230 q = (r * (u64)0x28f5c29) >> 32;
231 *((u16 *)buf) = decpair[r - 100*q];
232 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700233
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700234 /* 0 <= q < 10^6 */
235 r = (q * (u64)0x28f5c29) >> 32;
236 *((u16 *)buf) = decpair[q - 100*r];
237 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700238
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700239 /* 0 <= r < 10^4 */
240 q = (r * 0x147b) >> 19;
241 *((u16 *)buf) = decpair[r - 100*q];
242 buf += 2;
243
244 /* 0 <= q < 100 */
245 *((u16 *)buf) = decpair[q];
246 buf += 2;
247 return buf;
248}
249
250static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700251char *put_dec(char *buf, unsigned long long n)
252{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700253 if (n >= 100*1000*1000)
254 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
255 /* 1 <= n <= 1.6e11 */
256 if (n >= 100*1000*1000)
257 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
258 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700259 return put_dec_trunc8(buf, n);
260}
261
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700262#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700263
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700264static void
265put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700266{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700267 unsigned q;
268
269 /* 0 <= r < 10^4 */
270 q = (r * 0x147b) >> 19;
271 *((u16 *)buf) = decpair[r - 100*q];
272 buf += 2;
273 /* 0 <= q < 100 */
274 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700275}
276
277/*
278 * Call put_dec_full4 on x % 10000, return x / 10000.
279 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
280 * holds for all x < 1,128,869,999. The largest value this
281 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700282 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700283 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700284static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700285unsigned put_dec_helper4(char *buf, unsigned x)
286{
287 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
288
289 put_dec_full4(buf, x - q * 10000);
290 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700291}
292
293/* Based on code by Douglas W. Jones found at
294 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
295 * (with permission from the author).
296 * Performs no 64-bit division and hence should be fast on 32-bit machines.
297 */
298static
299char *put_dec(char *buf, unsigned long long n)
300{
301 uint32_t d3, d2, d1, q, h;
302
303 if (n < 100*1000*1000)
304 return put_dec_trunc8(buf, n);
305
306 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
307 h = (n >> 32);
308 d2 = (h ) & 0xffff;
309 d3 = (h >> 16); /* implicit "& 0xffff" */
310
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700311 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
312 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700313 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700314 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700315
George Spelvin23591722012-10-04 17:12:29 -0700316 q += 7671 * d3 + 9496 * d2 + 6 * d1;
317 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700318
George Spelvin23591722012-10-04 17:12:29 -0700319 q += 4749 * d3 + 42 * d2;
320 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700321
George Spelvin23591722012-10-04 17:12:29 -0700322 q += 281 * d3;
323 buf += 12;
324 if (q)
325 buf = put_dec_trunc8(buf, q);
326 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700327 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800328
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700329 return buf;
330}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700331
332#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700333
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700334/*
335 * Convert passed number to decimal string.
336 * Returns the length of string. On buffer overflow, returns 0.
337 *
338 * If speed is not important, use snprintf(). It's easy to read the code.
339 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700340int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700341{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700342 /* put_dec requires 2-byte alignment of the buffer. */
343 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700344 int idx, len;
345
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700346 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
347 if (num <= 9) {
348 tmp[0] = '0' + num;
349 len = 1;
350 } else {
351 len = put_dec(tmp, num) - tmp;
352 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700353
Andrei Vagind1be35c2018-04-10 16:31:16 -0700354 if (len > size || width > size)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700355 return 0;
Andrei Vagind1be35c2018-04-10 16:31:16 -0700356
357 if (width > len) {
358 width = width - len;
359 for (idx = 0; idx < width; idx++)
360 buf[idx] = ' ';
361 } else {
362 width = 0;
363 }
364
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700365 for (idx = 0; idx < len; ++idx)
Andrei Vagind1be35c2018-04-10 16:31:16 -0700366 buf[idx + width] = tmp[len - idx - 1];
367
368 return len + width;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700369}
370
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700371#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700372#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373#define PLUS 4 /* show plus */
374#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700375#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700376#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
377#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100379enum format_type {
380 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100381 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100382 FORMAT_TYPE_PRECISION,
383 FORMAT_TYPE_CHAR,
384 FORMAT_TYPE_STR,
385 FORMAT_TYPE_PTR,
386 FORMAT_TYPE_PERCENT_CHAR,
387 FORMAT_TYPE_INVALID,
388 FORMAT_TYPE_LONG_LONG,
389 FORMAT_TYPE_ULONG,
390 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800391 FORMAT_TYPE_UBYTE,
392 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100393 FORMAT_TYPE_USHORT,
394 FORMAT_TYPE_SHORT,
395 FORMAT_TYPE_UINT,
396 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100397 FORMAT_TYPE_SIZE_T,
398 FORMAT_TYPE_PTRDIFF
399};
400
401struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800402 unsigned int type:8; /* format_type enum */
403 signed int field_width:24; /* width of output field */
404 unsigned int flags:8; /* flags to number() */
405 unsigned int base:8; /* number base, 8, 10 or 16 only */
406 signed int precision:16; /* # of digits/chars */
407} __packed;
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800408#define FIELD_WIDTH_MAX ((1 << 23) - 1)
409#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100410
Joe Perchescf3b4292010-05-24 14:33:16 -0700411static noinline_for_stack
412char *number(char *buf, char *end, unsigned long long num,
413 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700415 /* put_dec requires 2-byte alignment of the buffer. */
416 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100417 char sign;
418 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100419 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700421 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800422 int field_width = spec.field_width;
423 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800425 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
426
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100427 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
428 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100429 locase = (spec.flags & SMALL);
430 if (spec.flags & LEFT)
431 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100433 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800434 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800436 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800437 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100438 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800440 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100441 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800443 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700446 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100447 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800448 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700449 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800450 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700452
453 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700455 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700456 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100457 else if (spec.base != 10) { /* 8 or 16 */
458 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700459 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800460
461 if (spec.base == 16)
462 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700463 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700464 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700465 num >>= shift;
466 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700467 } else { /* base 10 */
468 i = put_dec(tmp, num) - tmp;
469 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700470
471 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800472 if (i > precision)
473 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700474 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800475 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700476 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800477 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700478 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *buf = ' ';
480 ++buf;
481 }
482 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700483 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700485 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *buf = sign;
487 ++buf;
488 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700489 /* "0x" / "0" prefix */
490 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700491 if (spec.base == 16 || !is_zero) {
492 if (buf < end)
493 *buf = '0';
494 ++buf;
495 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100496 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700497 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100498 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 ++buf;
500 }
501 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700502 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100503 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700504 char c = ' ' + (spec.flags & ZEROPAD);
505 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800506 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700507 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 *buf = c;
509 ++buf;
510 }
511 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700512 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800513 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700514 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *buf = '0';
516 ++buf;
517 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700518 /* actual digits of result */
519 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700520 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *buf = tmp[i];
522 ++buf;
523 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700524 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800525 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700526 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *buf = ' ';
528 ++buf;
529 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return buf;
532}
533
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800534static noinline_for_stack
535char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
536{
537 struct printf_spec spec;
538
539 spec.type = FORMAT_TYPE_PTR;
540 spec.field_width = 2 + 2 * size; /* 0x + hex */
541 spec.flags = SPECIAL | SMALL | ZEROPAD;
542 spec.base = 16;
543 spec.precision = -1;
544
545 return number(buf, end, num, spec);
546}
547
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800548static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400549{
550 size_t size;
551 if (buf >= end) /* nowhere to put anything */
552 return;
553 size = end - buf;
554 if (size <= spaces) {
555 memset(buf, ' ', size);
556 return;
557 }
558 if (len) {
559 if (len > size - spaces)
560 len = size - spaces;
561 memmove(buf + spaces, buf, len);
562 }
563 memset(buf, ' ', spaces);
564}
565
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800566/*
567 * Handle field width padding for a string.
568 * @buf: current buffer position
569 * @n: length of string
570 * @end: end of output buffer
571 * @spec: for field width and flags
572 * Returns: new buffer position after padding.
573 */
574static noinline_for_stack
575char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
576{
577 unsigned spaces;
578
579 if (likely(n >= spec.field_width))
580 return buf;
581 /* we want to pad the sucker */
582 spaces = spec.field_width - n;
583 if (!(spec.flags & LEFT)) {
584 move_right(buf - n, end, n, spaces);
585 return buf + spaces;
586 }
587 while (spaces--) {
588 if (buf < end)
589 *buf = ' ';
590 ++buf;
591 }
592 return buf;
593}
594
Al Viro4b6ccca2013-09-03 12:00:44 -0400595static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800596char *string(char *buf, char *end, const char *s, struct printf_spec spec)
597{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800598 int len = 0;
599 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800600
601 if ((unsigned long)s < PAGE_SIZE)
602 s = "(null)";
603
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800604 while (lim--) {
605 char c = *s++;
606 if (!c)
607 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800608 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800609 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800610 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800611 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800612 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800613 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800614}
615
616static noinline_for_stack
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200617char *pointer_string(char *buf, char *end, const void *ptr,
618 struct printf_spec spec)
619{
620 spec.base = 16;
621 spec.flags |= SMALL;
622 if (spec.field_width == -1) {
623 spec.field_width = 2 * sizeof(ptr);
624 spec.flags |= ZEROPAD;
625 }
626
627 return number(buf, end, (unsigned long int)ptr, spec);
628}
629
630/* Make pointers available for printing early in the boot sequence. */
631static int debug_boot_weak_hash __ro_after_init;
632
633static int __init debug_boot_weak_hash_enable(char *str)
634{
635 debug_boot_weak_hash = 1;
636 pr_info("debug_boot_weak_hash enabled\n");
637 return 0;
638}
639early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
640
641static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
642static siphash_key_t ptr_key __read_mostly;
643
644static void enable_ptr_key_workfn(struct work_struct *work)
645{
646 get_random_bytes(&ptr_key, sizeof(ptr_key));
647 /* Needs to run from preemptible context */
648 static_branch_disable(&not_filled_random_ptr_key);
649}
650
651static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
652
653static void fill_random_ptr_key(struct random_ready_callback *unused)
654{
655 /* This may be in an interrupt handler. */
656 queue_work(system_unbound_wq, &enable_ptr_key_work);
657}
658
659static struct random_ready_callback random_ready = {
660 .func = fill_random_ptr_key
661};
662
663static int __init initialize_ptr_random(void)
664{
665 int key_size = sizeof(ptr_key);
666 int ret;
667
668 /* Use hw RNG if available. */
669 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
670 static_branch_disable(&not_filled_random_ptr_key);
671 return 0;
672 }
673
674 ret = add_random_ready_callback(&random_ready);
675 if (!ret) {
676 return 0;
677 } else if (ret == -EALREADY) {
678 /* This is in preemptible context */
679 enable_ptr_key_workfn(&enable_ptr_key_work);
680 return 0;
681 }
682
683 return ret;
684}
685early_initcall(initialize_ptr_random);
686
687/* Maps a pointer to a 32 bit unique identifier. */
688static char *ptr_to_id(char *buf, char *end, const void *ptr,
689 struct printf_spec spec)
690{
691 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
692 unsigned long hashval;
693
694 /* When debugging early boot use non-cryptographically secure hash. */
695 if (unlikely(debug_boot_weak_hash)) {
696 hashval = hash_long((unsigned long)ptr, 32);
697 return pointer_string(buf, end, (const void *)hashval, spec);
698 }
699
700 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
701 spec.field_width = 2 * sizeof(ptr);
702 /* string length must be less than default_width */
703 return string(buf, end, str, spec);
704 }
705
706#ifdef CONFIG_64BIT
707 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
708 /*
709 * Mask off the first 32 bits, this makes explicit that we have
710 * modified the address (and 32 bits is plenty for a unique ID).
711 */
712 hashval = hashval & 0xffffffff;
713#else
714 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
715#endif
716 return pointer_string(buf, end, (const void *)hashval, spec);
717}
718
719static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400720char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
721 const char *fmt)
722{
723 const char *array[4], *s;
724 const struct dentry *p;
725 int depth;
726 int i, n;
727
728 switch (fmt[1]) {
729 case '2': case '3': case '4':
730 depth = fmt[1] - '0';
731 break;
732 default:
733 depth = 1;
734 }
735
736 rcu_read_lock();
737 for (i = 0; i < depth; i++, d = p) {
Mark Rutland6aa7de02017-10-23 14:07:29 -0700738 p = READ_ONCE(d->d_parent);
739 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400740 if (p == d) {
741 if (i)
742 array[i] = "";
743 i++;
744 break;
745 }
746 }
747 s = array[--i];
748 for (n = 0; n != spec.precision; n++, buf++) {
749 char c = *s++;
750 if (!c) {
751 if (!i)
752 break;
753 c = '/';
754 s = array[--i];
755 }
756 if (buf < end)
757 *buf = c;
758 }
759 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800760 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400761}
762
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400763#ifdef CONFIG_BLOCK
764static noinline_for_stack
765char *bdev_name(char *buf, char *end, struct block_device *bdev,
766 struct printf_spec spec, const char *fmt)
767{
768 struct gendisk *hd = bdev->bd_disk;
769
770 buf = string(buf, end, hd->disk_name, spec);
771 if (bdev->bd_part->partno) {
772 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
773 if (buf < end)
774 *buf = 'p';
775 buf++;
776 }
777 buf = number(buf, end, bdev->bd_part->partno, spec);
778 }
779 return buf;
780}
781#endif
782
Joe Perchescf3b4292010-05-24 14:33:16 -0700783static noinline_for_stack
784char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800785 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700786{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800787 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700788#ifdef CONFIG_KALLSYMS
789 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800790#endif
791
792 if (fmt[1] == 'R')
793 ptr = __builtin_extract_return_addr(ptr);
794 value = (unsigned long)ptr;
795
796#ifdef CONFIG_KALLSYMS
797 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900798 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800799 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200800 sprint_symbol(sym, value);
801 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700802 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800803
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100804 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700805#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800806 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700807#endif
808}
809
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200810static const struct printf_spec default_str_spec = {
811 .field_width = -1,
812 .precision = -1,
813};
814
Andy Shevchenko54433972018-02-16 23:07:06 +0200815static const struct printf_spec default_flag_spec = {
816 .base = 16,
817 .precision = -1,
818 .flags = SPECIAL | SMALL,
819};
820
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200821static const struct printf_spec default_dec_spec = {
822 .base = 10,
823 .precision = -1,
824};
825
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200826static const struct printf_spec default_dec02_spec = {
827 .base = 10,
828 .field_width = 2,
829 .precision = -1,
830 .flags = ZEROPAD,
831};
832
833static const struct printf_spec default_dec04_spec = {
834 .base = 10,
835 .field_width = 4,
836 .precision = -1,
837 .flags = ZEROPAD,
838};
839
Joe Perchescf3b4292010-05-24 14:33:16 -0700840static noinline_for_stack
841char *resource_string(char *buf, char *end, struct resource *res,
842 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100843{
844#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600845#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100846#endif
847
848#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600849#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100850#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700851 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100852 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700853 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100854 .precision = -1,
855 .flags = SPECIAL | SMALL | ZEROPAD,
856 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700857 static const struct printf_spec mem_spec = {
858 .base = 16,
859 .field_width = MEM_RSRC_PRINTK_SIZE,
860 .precision = -1,
861 .flags = SPECIAL | SMALL | ZEROPAD,
862 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700863 static const struct printf_spec bus_spec = {
864 .base = 16,
865 .field_width = 2,
866 .precision = -1,
867 .flags = SMALL | ZEROPAD,
868 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700869 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600870 .field_width = -1,
871 .precision = 10,
872 .flags = LEFT,
873 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600874
875 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
876 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
877#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
878#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700879#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600880#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
881 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
882 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
883
Linus Torvalds332d2e72008-10-20 15:07:34 +1100884 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600885 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700886 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100887
888 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700889 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600890 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700891 specp = &io_spec;
892 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600893 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700894 specp = &mem_spec;
895 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600896 p = string(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200897 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700898 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600899 p = string(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200900 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700901 } else if (res->flags & IORESOURCE_BUS) {
902 p = string(p, pend, "bus ", str_spec);
903 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700904 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600905 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700906 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600907 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600908 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700909 if (decode && res->flags & IORESOURCE_UNSET) {
910 p = string(p, pend, "size ", str_spec);
911 p = number(p, pend, resource_size(res), *specp);
912 } else {
913 p = number(p, pend, res->start, *specp);
914 if (res->start != res->end) {
915 *p++ = '-';
916 p = number(p, pend, res->end, *specp);
917 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600918 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600919 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600920 if (res->flags & IORESOURCE_MEM_64)
921 p = string(p, pend, " 64bit", str_spec);
922 if (res->flags & IORESOURCE_PREFETCH)
923 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700924 if (res->flags & IORESOURCE_WINDOW)
925 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600926 if (res->flags & IORESOURCE_DISABLED)
927 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600928 } else {
929 p = string(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +0200930 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600931 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100932 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600933 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100934
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100935 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100936}
937
Joe Perchescf3b4292010-05-24 14:33:16 -0700938static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700939char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
940 const char *fmt)
941{
Steven Rostedt360603a2013-05-28 15:47:39 -0400942 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700943 negative value, fallback to the default */
944 char separator;
945
946 if (spec.field_width == 0)
947 /* nothing to print */
948 return buf;
949
950 if (ZERO_OR_NULL_PTR(addr))
951 /* NULL pointer */
952 return string(buf, end, NULL, spec);
953
954 switch (fmt[1]) {
955 case 'C':
956 separator = ':';
957 break;
958 case 'D':
959 separator = '-';
960 break;
961 case 'N':
962 separator = 0;
963 break;
964 default:
965 separator = ' ';
966 break;
967 }
968
969 if (spec.field_width > 0)
970 len = min_t(int, spec.field_width, 64);
971
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700972 for (i = 0; i < len; ++i) {
973 if (buf < end)
974 *buf = hex_asc_hi(addr[i]);
975 ++buf;
976 if (buf < end)
977 *buf = hex_asc_lo(addr[i]);
978 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700979
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700980 if (separator && i != len - 1) {
981 if (buf < end)
982 *buf = separator;
983 ++buf;
984 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700985 }
986
987 return buf;
988}
989
990static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800991char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
992 struct printf_spec spec, const char *fmt)
993{
994 const int CHUNKSZ = 32;
995 int nr_bits = max_t(int, spec.field_width, 0);
996 int i, chunksz;
997 bool first = true;
998
999 /* reused to print numbers */
1000 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1001
1002 chunksz = nr_bits & (CHUNKSZ - 1);
1003 if (chunksz == 0)
1004 chunksz = CHUNKSZ;
1005
1006 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1007 for (; i >= 0; i -= CHUNKSZ) {
1008 u32 chunkmask, val;
1009 int word, bit;
1010
1011 chunkmask = ((1ULL << chunksz) - 1);
1012 word = i / BITS_PER_LONG;
1013 bit = i % BITS_PER_LONG;
1014 val = (bitmap[word] >> bit) & chunkmask;
1015
1016 if (!first) {
1017 if (buf < end)
1018 *buf = ',';
1019 buf++;
1020 }
1021 first = false;
1022
1023 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1024 buf = number(buf, end, val, spec);
1025
1026 chunksz = CHUNKSZ;
1027 }
1028 return buf;
1029}
1030
1031static noinline_for_stack
1032char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1033 struct printf_spec spec, const char *fmt)
1034{
1035 int nr_bits = max_t(int, spec.field_width, 0);
1036 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1037 int cur, rbot, rtop;
1038 bool first = true;
1039
Tejun Heodbc760b2015-02-13 14:36:53 -08001040 rbot = cur = find_first_bit(bitmap, nr_bits);
1041 while (cur < nr_bits) {
1042 rtop = cur;
1043 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1044 if (cur < nr_bits && cur <= rtop + 1)
1045 continue;
1046
1047 if (!first) {
1048 if (buf < end)
1049 *buf = ',';
1050 buf++;
1051 }
1052 first = false;
1053
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001054 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001055 if (rbot < rtop) {
1056 if (buf < end)
1057 *buf = '-';
1058 buf++;
1059
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001060 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001061 }
1062
1063 rbot = cur;
1064 }
1065 return buf;
1066}
1067
1068static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001069char *mac_address_string(char *buf, char *end, u8 *addr,
1070 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001071{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001072 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001073 char *p = mac_addr;
1074 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001075 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001076 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001077
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001078 switch (fmt[1]) {
1079 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001080 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001081 break;
1082
1083 case 'R':
1084 reversed = true;
1085 /* fall through */
1086
1087 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001088 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001089 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001090 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001091
1092 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001093 if (reversed)
1094 p = hex_byte_pack(p, addr[5 - i]);
1095 else
1096 p = hex_byte_pack(p, addr[i]);
1097
Joe Perches8a27f7c2009-08-17 12:29:44 +00001098 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001099 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001100 }
1101 *p = '\0';
1102
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001103 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001104}
1105
Joe Perchescf3b4292010-05-24 14:33:16 -07001106static noinline_for_stack
1107char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001108{
Harvey Harrison689afa72008-10-28 16:04:44 -07001109 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001110 bool leading_zeros = (fmt[0] == 'i');
1111 int index;
1112 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001113
Joe Perches0159f242010-01-13 20:23:30 -08001114 switch (fmt[2]) {
1115 case 'h':
1116#ifdef __BIG_ENDIAN
1117 index = 0;
1118 step = 1;
1119#else
1120 index = 3;
1121 step = -1;
1122#endif
1123 break;
1124 case 'l':
1125 index = 3;
1126 step = -1;
1127 break;
1128 case 'n':
1129 case 'b':
1130 default:
1131 index = 0;
1132 step = 1;
1133 break;
1134 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001135 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001136 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001137 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001138 if (leading_zeros) {
1139 if (digits < 3)
1140 *p++ = '0';
1141 if (digits < 2)
1142 *p++ = '0';
1143 }
1144 /* reverse the digits in the quad */
1145 while (digits--)
1146 *p++ = temp[digits];
1147 if (i < 3)
1148 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001149 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001150 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001151 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001152
Joe Perches8a27f7c2009-08-17 12:29:44 +00001153 return p;
1154}
1155
Joe Perchescf3b4292010-05-24 14:33:16 -07001156static noinline_for_stack
1157char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001158{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001159 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001160 unsigned char zerolength[8];
1161 int longest = 1;
1162 int colonpos = -1;
1163 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001164 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001165 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001166 bool useIPv4;
1167 struct in6_addr in6;
1168
1169 memcpy(&in6, addr, sizeof(struct in6_addr));
1170
1171 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001172
1173 memset(zerolength, 0, sizeof(zerolength));
1174
1175 if (useIPv4)
1176 range = 6;
1177 else
1178 range = 8;
1179
1180 /* find position of longest 0 run */
1181 for (i = 0; i < range; i++) {
1182 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001183 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001184 break;
1185 zerolength[i]++;
1186 }
1187 }
1188 for (i = 0; i < range; i++) {
1189 if (zerolength[i] > longest) {
1190 longest = zerolength[i];
1191 colonpos = i;
1192 }
1193 }
Joe Perches29cf5192011-06-09 11:23:37 -07001194 if (longest == 1) /* don't compress a single 0 */
1195 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001196
1197 /* emit address */
1198 for (i = 0; i < range; i++) {
1199 if (i == colonpos) {
1200 if (needcolon || i == 0)
1201 *p++ = ':';
1202 *p++ = ':';
1203 needcolon = false;
1204 i += longest - 1;
1205 continue;
1206 }
1207 if (needcolon) {
1208 *p++ = ':';
1209 needcolon = false;
1210 }
1211 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001212 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001213 hi = word >> 8;
1214 lo = word & 0xff;
1215 if (hi) {
1216 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001217 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001218 else
1219 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001220 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001221 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001222 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001223 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001224 else
1225 *p++ = hex_asc_lo(lo);
1226 needcolon = true;
1227 }
1228
1229 if (useIPv4) {
1230 if (needcolon)
1231 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001232 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001233 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001234 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001235
Joe Perches8a27f7c2009-08-17 12:29:44 +00001236 return p;
1237}
1238
Joe Perchescf3b4292010-05-24 14:33:16 -07001239static noinline_for_stack
1240char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001241{
1242 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001243
Harvey Harrison689afa72008-10-28 16:04:44 -07001244 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001245 p = hex_byte_pack(p, *addr++);
1246 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001247 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001248 *p++ = ':';
1249 }
1250 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001251
Joe Perches8a27f7c2009-08-17 12:29:44 +00001252 return p;
1253}
1254
Joe Perchescf3b4292010-05-24 14:33:16 -07001255static noinline_for_stack
1256char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1257 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001258{
1259 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1260
1261 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001262 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001263 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001264 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001265
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001266 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001267}
1268
Joe Perchescf3b4292010-05-24 14:33:16 -07001269static noinline_for_stack
1270char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1271 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001272{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001273 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001274
Joe Perches0159f242010-01-13 20:23:30 -08001275 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001276
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001277 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001278}
1279
Joe Perchescf3b4292010-05-24 14:33:16 -07001280static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001281char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1282 struct printf_spec spec, const char *fmt)
1283{
1284 bool have_p = false, have_s = false, have_f = false, have_c = false;
1285 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1286 sizeof(":12345") + sizeof("/123456789") +
1287 sizeof("%1234567890")];
1288 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1289 const u8 *addr = (const u8 *) &sa->sin6_addr;
1290 char fmt6[2] = { fmt[0], '6' };
1291 u8 off = 0;
1292
1293 fmt++;
1294 while (isalpha(*++fmt)) {
1295 switch (*fmt) {
1296 case 'p':
1297 have_p = true;
1298 break;
1299 case 'f':
1300 have_f = true;
1301 break;
1302 case 's':
1303 have_s = true;
1304 break;
1305 case 'c':
1306 have_c = true;
1307 break;
1308 }
1309 }
1310
1311 if (have_p || have_s || have_f) {
1312 *p = '[';
1313 off = 1;
1314 }
1315
1316 if (fmt6[0] == 'I' && have_c)
1317 p = ip6_compressed_string(ip6_addr + off, addr);
1318 else
1319 p = ip6_string(ip6_addr + off, addr, fmt6);
1320
1321 if (have_p || have_s || have_f)
1322 *p++ = ']';
1323
1324 if (have_p) {
1325 *p++ = ':';
1326 p = number(p, pend, ntohs(sa->sin6_port), spec);
1327 }
1328 if (have_f) {
1329 *p++ = '/';
1330 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1331 IPV6_FLOWINFO_MASK), spec);
1332 }
1333 if (have_s) {
1334 *p++ = '%';
1335 p = number(p, pend, sa->sin6_scope_id, spec);
1336 }
1337 *p = '\0';
1338
1339 return string(buf, end, ip6_addr, spec);
1340}
1341
1342static noinline_for_stack
1343char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1344 struct printf_spec spec, const char *fmt)
1345{
1346 bool have_p = false;
1347 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1348 char *pend = ip4_addr + sizeof(ip4_addr);
1349 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1350 char fmt4[3] = { fmt[0], '4', 0 };
1351
1352 fmt++;
1353 while (isalpha(*++fmt)) {
1354 switch (*fmt) {
1355 case 'p':
1356 have_p = true;
1357 break;
1358 case 'h':
1359 case 'l':
1360 case 'n':
1361 case 'b':
1362 fmt4[2] = *fmt;
1363 break;
1364 }
1365 }
1366
1367 p = ip4_string(ip4_addr, addr, fmt4);
1368 if (have_p) {
1369 *p++ = ':';
1370 p = number(p, pend, ntohs(sa->sin_port), spec);
1371 }
1372 *p = '\0';
1373
1374 return string(buf, end, ip4_addr, spec);
1375}
1376
1377static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001378char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1379 const char *fmt)
1380{
1381 bool found = true;
1382 int count = 1;
1383 unsigned int flags = 0;
1384 int len;
1385
1386 if (spec.field_width == 0)
1387 return buf; /* nothing to print */
1388
1389 if (ZERO_OR_NULL_PTR(addr))
1390 return string(buf, end, NULL, spec); /* NULL pointer */
1391
1392
1393 do {
1394 switch (fmt[count++]) {
1395 case 'a':
1396 flags |= ESCAPE_ANY;
1397 break;
1398 case 'c':
1399 flags |= ESCAPE_SPECIAL;
1400 break;
1401 case 'h':
1402 flags |= ESCAPE_HEX;
1403 break;
1404 case 'n':
1405 flags |= ESCAPE_NULL;
1406 break;
1407 case 'o':
1408 flags |= ESCAPE_OCTAL;
1409 break;
1410 case 'p':
1411 flags |= ESCAPE_NP;
1412 break;
1413 case 's':
1414 flags |= ESCAPE_SPACE;
1415 break;
1416 default:
1417 found = false;
1418 break;
1419 }
1420 } while (found);
1421
1422 if (!flags)
1423 flags = ESCAPE_ANY_NP;
1424
1425 len = spec.field_width < 0 ? 1 : spec.field_width;
1426
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001427 /*
1428 * string_escape_mem() writes as many characters as it can to
1429 * the given buffer, and returns the total size of the output
1430 * had the buffer been big enough.
1431 */
1432 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001433
1434 return buf;
1435}
1436
1437static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001438char *uuid_string(char *buf, char *end, const u8 *addr,
1439 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001440{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001441 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001442 char *p = uuid;
1443 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001444 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001445 bool uc = false;
1446
1447 switch (*(++fmt)) {
1448 case 'L':
1449 uc = true; /* fall-through */
1450 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001451 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001452 break;
1453 case 'B':
1454 uc = true;
1455 break;
1456 }
1457
1458 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001459 if (uc)
1460 p = hex_byte_pack_upper(p, addr[index[i]]);
1461 else
1462 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001463 switch (i) {
1464 case 3:
1465 case 5:
1466 case 7:
1467 case 9:
1468 *p++ = '-';
1469 break;
1470 }
1471 }
1472
1473 *p = 0;
1474
Joe Perches9ac6e442009-12-14 18:01:09 -08001475 return string(buf, end, uuid, spec);
1476}
1477
Tobin C. Harding57e73442017-11-23 10:56:39 +11001478int kptr_restrict __read_mostly;
1479
1480static noinline_for_stack
1481char *restricted_pointer(char *buf, char *end, const void *ptr,
1482 struct printf_spec spec)
1483{
Tobin C. Harding57e73442017-11-23 10:56:39 +11001484 switch (kptr_restrict) {
1485 case 0:
1486 /* Always print %pK values */
1487 break;
1488 case 1: {
1489 const struct cred *cred;
1490
1491 /*
1492 * kptr_restrict==1 cannot be used in IRQ context
1493 * because its test for CAP_SYSLOG would be meaningless.
1494 */
Andy Shevchenko496a9a52018-02-16 23:07:08 +02001495 if (in_irq() || in_serving_softirq() || in_nmi()) {
1496 if (spec.field_width == -1)
1497 spec.field_width = 2 * sizeof(ptr);
Tobin C. Harding57e73442017-11-23 10:56:39 +11001498 return string(buf, end, "pK-error", spec);
Andy Shevchenko496a9a52018-02-16 23:07:08 +02001499 }
Tobin C. Harding57e73442017-11-23 10:56:39 +11001500
1501 /*
1502 * Only print the real pointer value if the current
1503 * process has CAP_SYSLOG and is running with the
1504 * same credentials it started with. This is because
1505 * access to files is checked at open() time, but %pK
1506 * checks permission at read() time. We don't want to
1507 * leak pointer values if a binary opens a file using
1508 * %pK and then elevates privileges before reading it.
1509 */
1510 cred = current_cred();
1511 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1512 !uid_eq(cred->euid, cred->uid) ||
1513 !gid_eq(cred->egid, cred->gid))
1514 ptr = NULL;
1515 break;
1516 }
1517 case 2:
1518 default:
1519 /* Always print 0's for %pK */
1520 ptr = NULL;
1521 break;
1522 }
1523
Andy Shevchenko496a9a52018-02-16 23:07:08 +02001524 return pointer_string(buf, end, ptr, spec);
Tobin C. Harding57e73442017-11-23 10:56:39 +11001525}
1526
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001527static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001528char *netdev_bits(char *buf, char *end, const void *addr,
1529 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001530{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001531 unsigned long long num;
1532 int size;
1533
1534 switch (fmt[1]) {
1535 case 'F':
1536 num = *(const netdev_features_t *)addr;
1537 size = sizeof(netdev_features_t);
1538 break;
1539 default:
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001540 return ptr_to_id(buf, end, addr, spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001541 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001542
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001543 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001544}
1545
Joe Perchesaaf07622014-01-23 15:54:17 -08001546static noinline_for_stack
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001547char *address_val(char *buf, char *end, const void *addr, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001548{
1549 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001550 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001551
1552 switch (fmt[1]) {
1553 case 'd':
1554 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001555 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001556 break;
1557 case 'p':
1558 default:
1559 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001560 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001561 break;
1562 }
1563
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001564 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001565}
1566
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001567static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001568char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1569{
1570 int year = tm->tm_year + (r ? 0 : 1900);
1571 int mon = tm->tm_mon + (r ? 0 : 1);
1572
1573 buf = number(buf, end, year, default_dec04_spec);
1574 if (buf < end)
1575 *buf = '-';
1576 buf++;
1577
1578 buf = number(buf, end, mon, default_dec02_spec);
1579 if (buf < end)
1580 *buf = '-';
1581 buf++;
1582
1583 return number(buf, end, tm->tm_mday, default_dec02_spec);
1584}
1585
1586static noinline_for_stack
1587char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1588{
1589 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1590 if (buf < end)
1591 *buf = ':';
1592 buf++;
1593
1594 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1595 if (buf < end)
1596 *buf = ':';
1597 buf++;
1598
1599 return number(buf, end, tm->tm_sec, default_dec02_spec);
1600}
1601
1602static noinline_for_stack
1603char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt)
1604{
1605 bool have_t = true, have_d = true;
1606 bool raw = false;
1607 int count = 2;
1608
1609 switch (fmt[count]) {
1610 case 'd':
1611 have_t = false;
1612 count++;
1613 break;
1614 case 't':
1615 have_d = false;
1616 count++;
1617 break;
1618 }
1619
1620 raw = fmt[count] == 'r';
1621
1622 if (have_d)
1623 buf = date_str(buf, end, tm, raw);
1624 if (have_d && have_t) {
1625 /* Respect ISO 8601 */
1626 if (buf < end)
1627 *buf = 'T';
1628 buf++;
1629 }
1630 if (have_t)
1631 buf = time_str(buf, end, tm, raw);
1632
1633 return buf;
1634}
1635
1636static noinline_for_stack
1637char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1638 const char *fmt)
1639{
1640 switch (fmt[1]) {
1641 case 'R':
1642 return rtc_str(buf, end, (const struct rtc_time *)ptr, fmt);
1643 default:
1644 return ptr_to_id(buf, end, ptr, spec);
1645 }
1646}
1647
1648static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001649char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1650 const char *fmt)
1651{
1652 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1653 return string(buf, end, NULL, spec);
1654
1655 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001656 case 'n':
1657 default:
1658#ifdef CONFIG_COMMON_CLK
1659 return string(buf, end, __clk_get_name(clk), spec);
1660#else
Geert Uytterhoevenec12bc22018-10-11 10:42:48 +02001661 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001662#endif
1663 }
1664}
1665
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001666static
1667char *format_flags(char *buf, char *end, unsigned long flags,
1668 const struct trace_print_flags *names)
1669{
1670 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001671
1672 for ( ; flags && names->name; names++) {
1673 mask = names->mask;
1674 if ((flags & mask) != mask)
1675 continue;
1676
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001677 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001678
1679 flags &= ~mask;
1680 if (flags) {
1681 if (buf < end)
1682 *buf = '|';
1683 buf++;
1684 }
1685 }
1686
1687 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001688 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001689
1690 return buf;
1691}
1692
1693static noinline_for_stack
1694char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1695{
1696 unsigned long flags;
1697 const struct trace_print_flags *names;
1698
1699 switch (fmt[1]) {
1700 case 'p':
1701 flags = *(unsigned long *)flags_ptr;
1702 /* Remove zone id */
1703 flags &= (1UL << NR_PAGEFLAGS) - 1;
1704 names = pageflag_names;
1705 break;
1706 case 'v':
1707 flags = *(unsigned long *)flags_ptr;
1708 names = vmaflag_names;
1709 break;
1710 case 'g':
1711 flags = *(gfp_t *)flags_ptr;
1712 names = gfpflag_names;
1713 break;
1714 default:
1715 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1716 return buf;
1717 }
1718
1719 return format_flags(buf, end, flags, names);
1720}
1721
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001722static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1723{
1724 for ( ; np && depth; depth--)
1725 np = np->parent;
1726
1727 return kbasename(np->full_name);
1728}
1729
1730static noinline_for_stack
1731char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1732{
1733 int depth;
1734 const struct device_node *parent = np->parent;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001735
1736 /* special case for root node */
1737 if (!parent)
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001738 return string(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001739
1740 for (depth = 0; parent->parent; depth++)
1741 parent = parent->parent;
1742
1743 for ( ; depth >= 0; depth--) {
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001744 buf = string(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001745 buf = string(buf, end, device_node_name_for_depth(np, depth),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001746 default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001747 }
1748 return buf;
1749}
1750
1751static noinline_for_stack
1752char *device_node_string(char *buf, char *end, struct device_node *dn,
1753 struct printf_spec spec, const char *fmt)
1754{
1755 char tbuf[sizeof("xxxx") + 1];
1756 const char *p;
1757 int ret;
1758 char *buf_start = buf;
1759 struct property *prop;
1760 bool has_mult, pass;
1761 static const struct printf_spec num_spec = {
1762 .flags = SMALL,
1763 .field_width = -1,
1764 .precision = -1,
1765 .base = 10,
1766 };
1767
1768 struct printf_spec str_spec = spec;
1769 str_spec.field_width = -1;
1770
1771 if (!IS_ENABLED(CONFIG_OF))
1772 return string(buf, end, "(!OF)", spec);
1773
1774 if ((unsigned long)dn < PAGE_SIZE)
1775 return string(buf, end, "(null)", spec);
1776
1777 /* simple case without anything any more format specifiers */
1778 fmt++;
1779 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1780 fmt = "f";
1781
1782 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001783 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001784 if (pass) {
1785 if (buf < end)
1786 *buf = ':';
1787 buf++;
1788 }
1789
1790 switch (*fmt) {
1791 case 'f': /* full_name */
1792 buf = device_node_gen_full_name(dn, buf, end);
1793 break;
1794 case 'n': /* name */
Rob Herring6d0a70a2018-08-27 08:13:56 -05001795 p = kbasename(of_node_full_name(dn));
1796 precision = str_spec.precision;
1797 str_spec.precision = strchrnul(p, '@') - p;
1798 buf = string(buf, end, p, str_spec);
1799 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001800 break;
1801 case 'p': /* phandle */
1802 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1803 break;
1804 case 'P': /* path-spec */
1805 p = kbasename(of_node_full_name(dn));
1806 if (!p[1])
1807 p = "/";
1808 buf = string(buf, end, p, str_spec);
1809 break;
1810 case 'F': /* flags */
1811 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1812 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1813 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1814 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1815 tbuf[4] = 0;
1816 buf = string(buf, end, tbuf, str_spec);
1817 break;
1818 case 'c': /* major compatible string */
1819 ret = of_property_read_string(dn, "compatible", &p);
1820 if (!ret)
1821 buf = string(buf, end, p, str_spec);
1822 break;
1823 case 'C': /* full compatible string */
1824 has_mult = false;
1825 of_property_for_each_string(dn, "compatible", prop, p) {
1826 if (has_mult)
1827 buf = string(buf, end, ",", str_spec);
1828 buf = string(buf, end, "\"", str_spec);
1829 buf = string(buf, end, p, str_spec);
1830 buf = string(buf, end, "\"", str_spec);
1831
1832 has_mult = true;
1833 }
1834 break;
1835 default:
1836 break;
1837 }
1838 }
1839
1840 return widen_string(buf, buf - buf_start, end, spec);
1841}
1842
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001843/*
1844 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1845 * by an extra set of alphanumeric characters that are extended format
1846 * specifiers.
1847 *
Joe Perches0b523762017-05-08 15:55:36 -07001848 * Please update scripts/checkpatch.pl when adding/removing conversion
1849 * characters. (Search for "check for vsprintf extension").
1850 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001851 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001852 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09001853 * - 'S' For symbolic direct pointers (or function descriptors) with offset
1854 * - 's' For symbolic direct pointers (or function descriptors) without offset
1855 * - 'F' Same as 'S'
1856 * - 'f' Same as 's'
Joe Perchesb0d33c22012-12-12 10:18:50 -08001857 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001858 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001859 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1860 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001861 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1862 * width which must be explicitly specified either as part of the
1863 * format string '%32b[l]' or through '%*b[l]', [l] selects
1864 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001865 * - 'M' For a 6-byte MAC address, it prints the address in the
1866 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001867 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001868 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001869 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001870 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001871 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1872 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1873 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001874 * [S][pfs]
1875 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1876 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001877 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1878 * IPv6 omits the colons (01020304...0f)
1879 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001880 * [S][pfs]
1881 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1882 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1883 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1884 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001885 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001886 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1887 * of the following flags (see string_escape_mem() for the
1888 * details):
1889 * a - ESCAPE_ANY
1890 * c - ESCAPE_SPECIAL
1891 * h - ESCAPE_HEX
1892 * n - ESCAPE_NULL
1893 * o - ESCAPE_OCTAL
1894 * p - ESCAPE_NP
1895 * s - ESCAPE_SPACE
1896 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001897 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1898 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1899 * Options for %pU are:
1900 * b big endian lower case hex (default)
1901 * B big endian UPPER case hex
1902 * l little endian lower case hex
1903 * L little endian UPPER case hex
1904 * big endian output byte order is:
1905 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1906 * little endian output byte order is:
1907 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001908 * - 'V' For a struct va_format which contains a format string * and va_list *,
1909 * call vsnprintf(->format, *->va_list).
1910 * Implements a "recursive vsnprintf".
1911 * Do not use this feature without some mechanism to verify the
1912 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001913 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001914 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001915 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1916 * a certain separator (' ' by default):
1917 * C colon
1918 * D dash
1919 * N no separator
1920 * The maximum supported length is 64 bytes of the input. Consider
1921 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001922 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1923 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001924 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1925 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001926 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001927 * - 't[R][dt][r]' For time and date as represented:
1928 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001929 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1930 * (legacy clock framework) of the clock
1931 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1932 * (legacy clock framework) of the clock
1933 * - 'Cr' For a clock, it prints the current rate of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001934 * - 'G' For flags to be printed as a collection of symbolic strings that would
1935 * construct the specific value. Supported flags given by option:
1936 * p page flags (see struct page) given as pointer to unsigned long
1937 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1938 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02001939 * - 'OF[fnpPcCF]' For a device tree object
1940 * Without any optional arguments prints the full_name
1941 * f device node full_name
1942 * n device node name
1943 * p device node phandle
1944 * P device node path spec (name + @unit)
1945 * F device node flags
1946 * c major compatible string
1947 * C full compatible string
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11001948 * - 'x' For printing the address. Equivalent to "%lx".
1949 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11001950 * ** When making changes please also update:
1951 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08001952 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11001953 * Note: The default behaviour (unadorned %p) is to hash the address,
1954 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001955 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001956static noinline_for_stack
1957char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1958 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001959{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001960 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001961
Adam Borowski3a129cc2018-02-04 18:45:21 +01001962 if (!ptr && *fmt != 'K' && *fmt != 'x') {
Joe Perches5e057982010-10-26 14:22:50 -07001963 /*
1964 * Print (null) with the same width as a pointer so it makes
1965 * tabular output look nice.
1966 */
1967 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001968 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001969 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001970 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001971
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001972 switch (*fmt) {
1973 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001974 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001975 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001976 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09001977 ptr = dereference_symbol_descriptor(ptr);
1978 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001979 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001980 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001981 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001982 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001983 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001984 case 'h':
1985 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001986 case 'b':
1987 switch (fmt[1]) {
1988 case 'l':
1989 return bitmap_list_string(buf, end, ptr, spec, fmt);
1990 default:
1991 return bitmap_string(buf, end, ptr, spec, fmt);
1992 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001993 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1994 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001995 /* [mM]F (FDDI) */
1996 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001997 return mac_address_string(buf, end, ptr, spec, fmt);
1998 case 'I': /* Formatted IP supported
1999 * 4: 1.2.3.4
2000 * 6: 0001:0203:...:0708
2001 * 6c: 1::708 or 1::1.2.3.4
2002 */
2003 case 'i': /* Contiguous:
2004 * 4: 001.002.003.004
2005 * 6: 000102...0f
2006 */
2007 switch (fmt[1]) {
2008 case '6':
2009 return ip6_addr_string(buf, end, ptr, spec, fmt);
2010 case '4':
2011 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02002012 case 'S': {
2013 const union {
2014 struct sockaddr raw;
2015 struct sockaddr_in v4;
2016 struct sockaddr_in6 v6;
2017 } *sa = ptr;
2018
2019 switch (sa->raw.sa_family) {
2020 case AF_INET:
2021 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
2022 case AF_INET6:
2023 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
2024 default:
2025 return string(buf, end, "(invalid address)", spec);
2026 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00002027 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07002028 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07002029 case 'E':
2030 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002031 case 'U':
2032 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002033 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00002034 {
2035 va_list va;
2036
2037 va_copy(va, *((struct va_format *)ptr)->va);
2038 buf += vsnprintf(buf, end > buf ? end - buf : 0,
2039 ((struct va_format *)ptr)->fmt, va);
2040 va_end(va);
2041 return buf;
2042 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002043 case 'K':
Linus Torvaldsef0010a2017-11-29 11:28:09 -08002044 if (!kptr_restrict)
2045 break;
Tobin C. Harding57e73442017-11-23 10:56:39 +11002046 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002047 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002048 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002049 case 'a':
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08002050 return address_val(buf, end, ptr, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002051 case 'd':
2052 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002053 case 't':
2054 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002055 case 'C':
2056 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002057 case 'D':
2058 return dentry_name(buf, end,
2059 ((const struct file *)ptr)->f_path.dentry,
2060 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002061#ifdef CONFIG_BLOCK
2062 case 'g':
2063 return bdev_name(buf, end, ptr, spec, fmt);
2064#endif
2065
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002066 case 'G':
2067 return flags_string(buf, end, ptr, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002068 case 'O':
2069 switch (fmt[1]) {
2070 case 'F':
2071 return device_node_string(buf, end, ptr, spec, fmt + 1);
2072 }
Bart Van Assche554ec502018-08-06 15:34:21 -07002073 break;
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002074 case 'x':
2075 return pointer_string(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002076 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002077
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002078 /* default is to _not_ leak addresses, hash before printing */
2079 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002080}
2081
2082/*
2083 * Helper function to decode printf style format.
2084 * Each call decode a token from the format and return the
2085 * number of characters read (or likely the delta where it wants
2086 * to go on the next call).
2087 * The decoded token is returned through the parameters
2088 *
2089 * 'h', 'l', or 'L' for integer fields
2090 * 'z' support added 23/7/1999 S.H.
2091 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002092 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002093 * 't' added for ptrdiff_t
2094 *
2095 * @fmt: the format string
2096 * @type of the token returned
2097 * @flags: various flags such as +, -, # tokens..
2098 * @field_width: overwritten width
2099 * @base: base of the number (octal, hex, ...)
2100 * @precision: precision of a number
2101 * @qualifier: qualifier of a number (long, size_t, ...)
2102 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002103static noinline_for_stack
2104int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002105{
2106 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002107 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002108
2109 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002110 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002111 if (spec->field_width < 0) {
2112 spec->field_width = -spec->field_width;
2113 spec->flags |= LEFT;
2114 }
2115 spec->type = FORMAT_TYPE_NONE;
2116 goto precision;
2117 }
2118
2119 /* we finished early by reading the precision */
2120 if (spec->type == FORMAT_TYPE_PRECISION) {
2121 if (spec->precision < 0)
2122 spec->precision = 0;
2123
2124 spec->type = FORMAT_TYPE_NONE;
2125 goto qualifier;
2126 }
2127
2128 /* By default */
2129 spec->type = FORMAT_TYPE_NONE;
2130
2131 for (; *fmt ; ++fmt) {
2132 if (*fmt == '%')
2133 break;
2134 }
2135
2136 /* Return the current non-format string */
2137 if (fmt != start || !*fmt)
2138 return fmt - start;
2139
2140 /* Process flags */
2141 spec->flags = 0;
2142
2143 while (1) { /* this also skips first '%' */
2144 bool found = true;
2145
2146 ++fmt;
2147
2148 switch (*fmt) {
2149 case '-': spec->flags |= LEFT; break;
2150 case '+': spec->flags |= PLUS; break;
2151 case ' ': spec->flags |= SPACE; break;
2152 case '#': spec->flags |= SPECIAL; break;
2153 case '0': spec->flags |= ZEROPAD; break;
2154 default: found = false;
2155 }
2156
2157 if (!found)
2158 break;
2159 }
2160
2161 /* get field width */
2162 spec->field_width = -1;
2163
2164 if (isdigit(*fmt))
2165 spec->field_width = skip_atoi(&fmt);
2166 else if (*fmt == '*') {
2167 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002168 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002169 return ++fmt - start;
2170 }
2171
2172precision:
2173 /* get the precision */
2174 spec->precision = -1;
2175 if (*fmt == '.') {
2176 ++fmt;
2177 if (isdigit(*fmt)) {
2178 spec->precision = skip_atoi(&fmt);
2179 if (spec->precision < 0)
2180 spec->precision = 0;
2181 } else if (*fmt == '*') {
2182 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002183 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002184 return ++fmt - start;
2185 }
2186 }
2187
2188qualifier:
2189 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002190 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002191 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002192 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002193 qualifier = *fmt++;
2194 if (unlikely(qualifier == *fmt)) {
2195 if (qualifier == 'l') {
2196 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002197 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002198 } else if (qualifier == 'h') {
2199 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002200 ++fmt;
2201 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002202 }
2203 }
2204
2205 /* default base */
2206 spec->base = 10;
2207 switch (*fmt) {
2208 case 'c':
2209 spec->type = FORMAT_TYPE_CHAR;
2210 return ++fmt - start;
2211
2212 case 's':
2213 spec->type = FORMAT_TYPE_STR;
2214 return ++fmt - start;
2215
2216 case 'p':
2217 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002218 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002219
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002220 case '%':
2221 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2222 return ++fmt - start;
2223
2224 /* integer number formats - set up the flags and "break" */
2225 case 'o':
2226 spec->base = 8;
2227 break;
2228
2229 case 'x':
2230 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002231 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002232
2233 case 'X':
2234 spec->base = 16;
2235 break;
2236
2237 case 'd':
2238 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002239 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002240 case 'u':
2241 break;
2242
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002243 case 'n':
2244 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002245 * Since %n poses a greater security risk than
2246 * utility, treat it as any other invalid or
2247 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002248 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002249 /* Fall-through */
2250
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002251 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002252 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002253 spec->type = FORMAT_TYPE_INVALID;
2254 return fmt - start;
2255 }
2256
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002257 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002258 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002259 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002260 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2261 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002262 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002263 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002264 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002265 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002266 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002267 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2268 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002269 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002270 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2271 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002272 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002273 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2274 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002275 }
2276
2277 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002278}
2279
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002280static void
2281set_field_width(struct printf_spec *spec, int width)
2282{
2283 spec->field_width = width;
2284 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2285 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2286 }
2287}
2288
2289static void
2290set_precision(struct printf_spec *spec, int prec)
2291{
2292 spec->precision = prec;
2293 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2294 spec->precision = clamp(prec, 0, PRECISION_MAX);
2295 }
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298/**
2299 * vsnprintf - Format a string and place it in a buffer
2300 * @buf: The buffer to place the result into
2301 * @size: The size of the buffer, including the trailing null space
2302 * @fmt: The format string to use
2303 * @args: Arguments for the format string
2304 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002305 * This function generally follows C99 vsnprintf, but has some
2306 * extensions and a few limitations:
2307 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002308 * - ``%n`` is unsupported
2309 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002310 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002311 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002312 * extensive description.
2313 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002314 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002315 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 * The return value is the number of characters which would
2317 * be generated for the given input, excluding the trailing
2318 * '\0', as per ISO C99. If you want to have the exact
2319 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002320 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 * return is greater than or equal to @size, the resulting
2322 * string is truncated.
2323 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002324 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 */
2326int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002329 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002330 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002332 /* Reject out-of-range values early. Large positive sizes are
2333 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002334 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002338 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002340 /* Make sure end is always >= buf */
2341 if (end < buf) {
2342 end = ((void *)-1);
2343 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
2345
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002346 while (*fmt) {
2347 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002348 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002349
2350 fmt += read;
2351
2352 switch (spec.type) {
2353 case FORMAT_TYPE_NONE: {
2354 int copy = read;
2355 if (str < end) {
2356 if (copy > end - str)
2357 copy = end - str;
2358 memcpy(str, old_fmt, copy);
2359 }
2360 str += read;
2361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363
Vegard Nossumed681a92009-03-14 12:08:50 +01002364 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002365 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002368 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002369 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
André Goddard Rosad4be1512009-12-14 18:00:59 -08002372 case FORMAT_TYPE_CHAR: {
2373 char c;
2374
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002375 if (!(spec.flags & LEFT)) {
2376 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002377 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 *str = ' ';
2379 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002382 }
2383 c = (unsigned char) va_arg(args, int);
2384 if (str < end)
2385 *str = c;
2386 ++str;
2387 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002388 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002389 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002391 }
2392 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002395 case FORMAT_TYPE_STR:
2396 str = string(str, end, va_arg(args, char *), spec);
2397 break;
2398
2399 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002400 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002401 spec);
2402 while (isalnum(*fmt))
2403 fmt++;
2404 break;
2405
2406 case FORMAT_TYPE_PERCENT_CHAR:
2407 if (str < end)
2408 *str = '%';
2409 ++str;
2410 break;
2411
2412 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002413 /*
2414 * Presumably the arguments passed gcc's type
2415 * checking, but there is no safe or sane way
2416 * for us to continue parsing the format and
2417 * fetching from the va_list; the remaining
2418 * specifiers and arguments would be out of
2419 * sync.
2420 */
2421 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002422
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002423 default:
2424 switch (spec.type) {
2425 case FORMAT_TYPE_LONG_LONG:
2426 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002428 case FORMAT_TYPE_ULONG:
2429 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002431 case FORMAT_TYPE_LONG:
2432 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002434 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002435 if (spec.flags & SIGN)
2436 num = va_arg(args, ssize_t);
2437 else
2438 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002439 break;
2440 case FORMAT_TYPE_PTRDIFF:
2441 num = va_arg(args, ptrdiff_t);
2442 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002443 case FORMAT_TYPE_UBYTE:
2444 num = (unsigned char) va_arg(args, int);
2445 break;
2446 case FORMAT_TYPE_BYTE:
2447 num = (signed char) va_arg(args, int);
2448 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002449 case FORMAT_TYPE_USHORT:
2450 num = (unsigned short) va_arg(args, int);
2451 break;
2452 case FORMAT_TYPE_SHORT:
2453 num = (short) va_arg(args, int);
2454 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002455 case FORMAT_TYPE_INT:
2456 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002459 num = va_arg(args, unsigned int);
2460 }
2461
2462 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002465
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002466out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002467 if (size > 0) {
2468 if (str < end)
2469 *str = '\0';
2470 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002471 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002472 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002473
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002474 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478EXPORT_SYMBOL(vsnprintf);
2479
2480/**
2481 * vscnprintf - Format a string and place it in a buffer
2482 * @buf: The buffer to place the result into
2483 * @size: The size of the buffer, including the trailing null space
2484 * @fmt: The format string to use
2485 * @args: Arguments for the format string
2486 *
2487 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002488 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 * returns 0.
2490 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002491 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002492 *
2493 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 */
2495int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2496{
2497 int i;
2498
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002499 i = vsnprintf(buf, size, fmt, args);
2500
Anton Arapovb921c692011-01-12 16:59:49 -08002501 if (likely(i < size))
2502 return i;
2503 if (size != 0)
2504 return size - 1;
2505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507EXPORT_SYMBOL(vscnprintf);
2508
2509/**
2510 * snprintf - Format a string and place it in a buffer
2511 * @buf: The buffer to place the result into
2512 * @size: The size of the buffer, including the trailing null space
2513 * @fmt: The format string to use
2514 * @...: Arguments for the format string
2515 *
2516 * The return value is the number of characters which would be
2517 * generated for the given input, excluding the trailing null,
2518 * as per ISO C99. If the return is greater than or equal to
2519 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002520 *
2521 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002523int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 va_list args;
2526 int i;
2527
2528 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002529 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return i;
2533}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534EXPORT_SYMBOL(snprintf);
2535
2536/**
2537 * scnprintf - Format a string and place it in a buffer
2538 * @buf: The buffer to place the result into
2539 * @size: The size of the buffer, including the trailing null space
2540 * @fmt: The format string to use
2541 * @...: Arguments for the format string
2542 *
2543 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002544 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 */
2546
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002547int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 va_list args;
2550 int i;
2551
2552 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002553 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002555
Anton Arapovb921c692011-01-12 16:59:49 -08002556 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558EXPORT_SYMBOL(scnprintf);
2559
2560/**
2561 * vsprintf - Format a string and place it in a buffer
2562 * @buf: The buffer to place the result into
2563 * @fmt: The format string to use
2564 * @args: Arguments for the format string
2565 *
2566 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002567 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * buffer overflows.
2569 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002570 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002571 *
2572 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 */
2574int vsprintf(char *buf, const char *fmt, va_list args)
2575{
2576 return vsnprintf(buf, INT_MAX, fmt, args);
2577}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578EXPORT_SYMBOL(vsprintf);
2579
2580/**
2581 * sprintf - Format a string and place it in a buffer
2582 * @buf: The buffer to place the result into
2583 * @fmt: The format string to use
2584 * @...: Arguments for the format string
2585 *
2586 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002587 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002589 *
2590 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002592int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 va_list args;
2595 int i;
2596
2597 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002598 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return i;
2602}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603EXPORT_SYMBOL(sprintf);
2604
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002605#ifdef CONFIG_BINARY_PRINTF
2606/*
2607 * bprintf service:
2608 * vbin_printf() - VA arguments to binary data
2609 * bstr_printf() - Binary data to text string
2610 */
2611
2612/**
2613 * vbin_printf - Parse a format string and place args' binary value in a buffer
2614 * @bin_buf: The buffer to place args' binary value
2615 * @size: The size of the buffer(by words(32bits), not characters)
2616 * @fmt: The format string to use
2617 * @args: Arguments for the format string
2618 *
2619 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002620 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002621 *
2622 * The return value is the number of words(32bits) which would be generated for
2623 * the given input.
2624 *
2625 * NOTE:
2626 * If the return value is greater than @size, the resulting bin_buf is NOT
2627 * valid for bstr_printf().
2628 */
2629int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2630{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002631 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002632 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002633 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002634
2635 str = (char *)bin_buf;
2636 end = (char *)(bin_buf + size);
2637
2638#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002639({ \
2640 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002641 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002642 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002643 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002644 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002645 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002646 *(u32 *)str = *(u32 *)&val8; \
2647 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002648 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002649 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002650 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002651 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002652 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002653 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002654 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002655 *(typeof(type) *)str = (type)(long)val4; \
2656 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002657 } \
2658 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002659 value; \
2660})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002661
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002662 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002663 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002664
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002665 fmt += read;
2666
2667 switch (spec.type) {
2668 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002669 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002670 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002671 case FORMAT_TYPE_INVALID:
2672 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002673
Vegard Nossumed681a92009-03-14 12:08:50 +01002674 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002675 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002676 width = (int)save_arg(int);
2677 /* Pointers may require the width */
2678 if (*fmt == 'p')
2679 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002680 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002681
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002682 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002683 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002684 break;
2685
2686 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002687 const char *save_str = va_arg(args, char *);
2688 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002689
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002690 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2691 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002692 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002693 len = strlen(save_str) + 1;
2694 if (str + len < end)
2695 memcpy(str, save_str, len);
2696 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002697 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002698 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002699
2700 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002701 /* Dereferenced pointers must be done now */
2702 switch (*fmt) {
2703 /* Dereference of functions is still OK */
2704 case 'S':
2705 case 's':
2706 case 'F':
2707 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002708 case 'x':
2709 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002710 save_arg(void *);
2711 break;
2712 default:
2713 if (!isalnum(*fmt)) {
2714 save_arg(void *);
2715 break;
2716 }
2717 str = pointer(fmt, str, end, va_arg(args, void *),
2718 spec);
2719 if (str + 1 < end)
2720 *str++ = '\0';
2721 else
2722 end[-1] = '\0'; /* Must be nul terminated */
2723 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002724 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002725 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002726 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002727 break;
2728
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002729 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002730 switch (spec.type) {
2731
2732 case FORMAT_TYPE_LONG_LONG:
2733 save_arg(long long);
2734 break;
2735 case FORMAT_TYPE_ULONG:
2736 case FORMAT_TYPE_LONG:
2737 save_arg(unsigned long);
2738 break;
2739 case FORMAT_TYPE_SIZE_T:
2740 save_arg(size_t);
2741 break;
2742 case FORMAT_TYPE_PTRDIFF:
2743 save_arg(ptrdiff_t);
2744 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002745 case FORMAT_TYPE_UBYTE:
2746 case FORMAT_TYPE_BYTE:
2747 save_arg(char);
2748 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002749 case FORMAT_TYPE_USHORT:
2750 case FORMAT_TYPE_SHORT:
2751 save_arg(short);
2752 break;
2753 default:
2754 save_arg(int);
2755 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002756 }
2757 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002758
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002759out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002760 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002761#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002762}
2763EXPORT_SYMBOL_GPL(vbin_printf);
2764
2765/**
2766 * bstr_printf - Format a string from binary arguments and place it in a buffer
2767 * @buf: The buffer to place the result into
2768 * @size: The size of the buffer, including the trailing null space
2769 * @fmt: The format string to use
2770 * @bin_buf: Binary arguments for the format string
2771 *
2772 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2773 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2774 * a binary buffer that generated by vbin_printf.
2775 *
2776 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002777 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002778 *
2779 * The return value is the number of characters which would
2780 * be generated for the given input, excluding the trailing
2781 * '\0', as per ISO C99. If you want to have the exact
2782 * number of characters written into @buf as return value
2783 * (not including the trailing '\0'), use vscnprintf(). If the
2784 * return is greater than or equal to @size, the resulting
2785 * string is truncated.
2786 */
2787int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2788{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002789 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002790 char *str, *end;
2791 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002792
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002793 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002794 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002795
2796 str = buf;
2797 end = buf + size;
2798
2799#define get_arg(type) \
2800({ \
2801 typeof(type) value; \
2802 if (sizeof(type) == 8) { \
2803 args = PTR_ALIGN(args, sizeof(u32)); \
2804 *(u32 *)&value = *(u32 *)args; \
2805 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2806 } else { \
2807 args = PTR_ALIGN(args, sizeof(type)); \
2808 value = *(typeof(type) *)args; \
2809 } \
2810 args += sizeof(type); \
2811 value; \
2812})
2813
2814 /* Make sure end is always >= buf */
2815 if (end < buf) {
2816 end = ((void *)-1);
2817 size = end - buf;
2818 }
2819
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002820 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002821 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002822 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002823
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002824 fmt += read;
2825
2826 switch (spec.type) {
2827 case FORMAT_TYPE_NONE: {
2828 int copy = read;
2829 if (str < end) {
2830 if (copy > end - str)
2831 copy = end - str;
2832 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002833 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002834 str += read;
2835 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002836 }
2837
Vegard Nossumed681a92009-03-14 12:08:50 +01002838 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002839 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002840 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002841
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002842 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002843 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002844 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002845
André Goddard Rosad4be1512009-12-14 18:00:59 -08002846 case FORMAT_TYPE_CHAR: {
2847 char c;
2848
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002849 if (!(spec.flags & LEFT)) {
2850 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002851 if (str < end)
2852 *str = ' ';
2853 ++str;
2854 }
2855 }
2856 c = (unsigned char) get_arg(char);
2857 if (str < end)
2858 *str = c;
2859 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002860 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002861 if (str < end)
2862 *str = ' ';
2863 ++str;
2864 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002865 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002866 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002867
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002868 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002869 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002870 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002871 str = string(str, end, (char *)str_arg, spec);
2872 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002873 }
2874
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002875 case FORMAT_TYPE_PTR: {
2876 bool process = false;
2877 int copy, len;
2878 /* Non function dereferences were already done */
2879 switch (*fmt) {
2880 case 'S':
2881 case 's':
2882 case 'F':
2883 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002884 case 'x':
2885 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002886 process = true;
2887 break;
2888 default:
2889 if (!isalnum(*fmt)) {
2890 process = true;
2891 break;
2892 }
2893 /* Pointer dereference was already processed */
2894 if (str < end) {
2895 len = copy = strlen(args);
2896 if (copy > end - str)
2897 copy = end - str;
2898 memcpy(str, args, copy);
2899 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04002900 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002901 }
2902 }
2903 if (process)
2904 str = pointer(fmt, str, end, get_arg(void *), spec);
2905
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002906 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002907 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002908 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002909 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002910
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002911 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002912 if (str < end)
2913 *str = '%';
2914 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002915 break;
2916
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002917 case FORMAT_TYPE_INVALID:
2918 goto out;
2919
André Goddard Rosad4be1512009-12-14 18:00:59 -08002920 default: {
2921 unsigned long long num;
2922
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002923 switch (spec.type) {
2924
2925 case FORMAT_TYPE_LONG_LONG:
2926 num = get_arg(long long);
2927 break;
2928 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002929 case FORMAT_TYPE_LONG:
2930 num = get_arg(unsigned long);
2931 break;
2932 case FORMAT_TYPE_SIZE_T:
2933 num = get_arg(size_t);
2934 break;
2935 case FORMAT_TYPE_PTRDIFF:
2936 num = get_arg(ptrdiff_t);
2937 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002938 case FORMAT_TYPE_UBYTE:
2939 num = get_arg(unsigned char);
2940 break;
2941 case FORMAT_TYPE_BYTE:
2942 num = get_arg(signed char);
2943 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002944 case FORMAT_TYPE_USHORT:
2945 num = get_arg(unsigned short);
2946 break;
2947 case FORMAT_TYPE_SHORT:
2948 num = get_arg(short);
2949 break;
2950 case FORMAT_TYPE_UINT:
2951 num = get_arg(unsigned int);
2952 break;
2953 default:
2954 num = get_arg(int);
2955 }
2956
2957 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002958 } /* default: */
2959 } /* switch(spec.type) */
2960 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002961
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002962out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002963 if (size > 0) {
2964 if (str < end)
2965 *str = '\0';
2966 else
2967 end[-1] = '\0';
2968 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002969
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002970#undef get_arg
2971
2972 /* the trailing null byte doesn't count towards the total */
2973 return str - buf;
2974}
2975EXPORT_SYMBOL_GPL(bstr_printf);
2976
2977/**
2978 * bprintf - Parse a format string and place args' binary value in a buffer
2979 * @bin_buf: The buffer to place args' binary value
2980 * @size: The size of the buffer(by words(32bits), not characters)
2981 * @fmt: The format string to use
2982 * @...: Arguments for the format string
2983 *
2984 * The function returns the number of words(u32) written
2985 * into @bin_buf.
2986 */
2987int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2988{
2989 va_list args;
2990 int ret;
2991
2992 va_start(args, fmt);
2993 ret = vbin_printf(bin_buf, size, fmt, args);
2994 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002995
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002996 return ret;
2997}
2998EXPORT_SYMBOL_GPL(bprintf);
2999
3000#endif /* CONFIG_BINARY_PRINTF */
3001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002/**
3003 * vsscanf - Unformat a buffer into a list of arguments
3004 * @buf: input buffer
3005 * @fmt: format of buffer
3006 * @args: arguments
3007 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003008int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010 const char *str = buf;
3011 char *next;
3012 char digit;
3013 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003014 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003015 unsigned int base;
3016 union {
3017 long long s;
3018 unsigned long long u;
3019 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003020 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003021 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Jan Beulichda990752012-10-04 17:13:24 -07003023 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /* skip any white space in format */
3025 /* white space in format matchs any amount of
3026 * white space, including none, in the input.
3027 */
3028 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003029 fmt = skip_spaces(++fmt);
3030 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
3032
3033 /* anything that is not a conversion must match exactly */
3034 if (*fmt != '%' && *fmt) {
3035 if (*fmt++ != *str++)
3036 break;
3037 continue;
3038 }
3039
3040 if (!*fmt)
3041 break;
3042 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /* skip this conversion.
3045 * advance both strings to next white space
3046 */
3047 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003048 if (!*str)
3049 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003050 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3051 /* '%*[' not yet supported, invalid format */
3052 if (*fmt == '[')
3053 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 while (!isspace(*str) && *str)
3057 str++;
3058 continue;
3059 }
3060
3061 /* get field width */
3062 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003063 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003065 if (field_width <= 0)
3066 break;
3067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 /* get conversion qualifier */
3070 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003071 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003072 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 qualifier = *fmt++;
3074 if (unlikely(qualifier == *fmt)) {
3075 if (qualifier == 'h') {
3076 qualifier = 'H';
3077 fmt++;
3078 } else if (qualifier == 'l') {
3079 qualifier = 'L';
3080 fmt++;
3081 }
3082 }
3083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Jan Beulichda990752012-10-04 17:13:24 -07003085 if (!*fmt)
3086 break;
3087
3088 if (*fmt == 'n') {
3089 /* return number of characters read so far */
3090 *va_arg(args, int *) = str - buf;
3091 ++fmt;
3092 continue;
3093 }
3094
3095 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 break;
3097
André Goddard Rosad4be1512009-12-14 18:00:59 -08003098 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003099 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003100
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003101 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 case 'c':
3103 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003104 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (field_width == -1)
3106 field_width = 1;
3107 do {
3108 *s++ = *str++;
3109 } while (--field_width > 0 && *str);
3110 num++;
3111 }
3112 continue;
3113 case 's':
3114 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003115 char *s = (char *)va_arg(args, char *);
3116 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003117 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003119 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003122 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 *s = '\0';
3125 num++;
3126 }
3127 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003128 /*
3129 * Warning: This implementation of the '[' conversion specifier
3130 * deviates from its glibc counterpart in the following ways:
3131 * (1) It does NOT support ranges i.e. '-' is NOT a special
3132 * character
3133 * (2) It cannot match the closing bracket ']' itself
3134 * (3) A field width is required
3135 * (4) '%*[' (discard matching input) is currently not supported
3136 *
3137 * Example usage:
3138 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3139 * buf1, buf2, buf3);
3140 * if (ret < 3)
3141 * // etc..
3142 */
3143 case '[':
3144 {
3145 char *s = (char *)va_arg(args, char *);
3146 DECLARE_BITMAP(set, 256) = {0};
3147 unsigned int len = 0;
3148 bool negate = (*fmt == '^');
3149
3150 /* field width is required */
3151 if (field_width == -1)
3152 return num;
3153
3154 if (negate)
3155 ++fmt;
3156
3157 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3158 set_bit((u8)*fmt, set);
3159
3160 /* no ']' or no character set found */
3161 if (!*fmt || !len)
3162 return num;
3163 ++fmt;
3164
3165 if (negate) {
3166 bitmap_complement(set, set, 256);
3167 /* exclude null '\0' byte */
3168 clear_bit(0, set);
3169 }
3170
3171 /* match must be non-empty */
3172 if (!test_bit((u8)*str, set))
3173 return num;
3174
3175 while (test_bit((u8)*str, set) && field_width--)
3176 *s++ = *str++;
3177 *s = '\0';
3178 ++num;
3179 }
3180 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 case 'o':
3182 base = 8;
3183 break;
3184 case 'x':
3185 case 'X':
3186 base = 16;
3187 break;
3188 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003189 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003190 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003192 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003193 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 case 'u':
3195 break;
3196 case '%':
3197 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003198 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return num;
3200 continue;
3201 default:
3202 /* invalid format; stop here */
3203 return num;
3204 }
3205
3206 /* have some sort of integer conversion.
3207 * first, skip white space in buffer.
3208 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003209 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 digit = *str;
3212 if (is_sign && digit == '-')
3213 digit = *(str + 1);
3214
3215 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003216 || (base == 16 && !isxdigit(digit))
3217 || (base == 10 && !isdigit(digit))
3218 || (base == 8 && (!isdigit(digit) || digit > '7'))
3219 || (base == 0 && !isdigit(digit)))
3220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Jan Beulich53809752012-12-17 16:01:31 -08003222 if (is_sign)
3223 val.s = qualifier != 'L' ?
3224 simple_strtol(str, &next, base) :
3225 simple_strtoll(str, &next, base);
3226 else
3227 val.u = qualifier != 'L' ?
3228 simple_strtoul(str, &next, base) :
3229 simple_strtoull(str, &next, base);
3230
3231 if (field_width > 0 && next - str > field_width) {
3232 if (base == 0)
3233 _parse_integer_fixup_radix(str, &base);
3234 while (next - str > field_width) {
3235 if (is_sign)
3236 val.s = div_s64(val.s, base);
3237 else
3238 val.u = div_u64(val.u, base);
3239 --next;
3240 }
3241 }
3242
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003243 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003245 if (is_sign)
3246 *va_arg(args, signed char *) = val.s;
3247 else
3248 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 break;
3250 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003251 if (is_sign)
3252 *va_arg(args, short *) = val.s;
3253 else
3254 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 break;
3256 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003257 if (is_sign)
3258 *va_arg(args, long *) = val.s;
3259 else
3260 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 break;
3262 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003263 if (is_sign)
3264 *va_arg(args, long long *) = val.s;
3265 else
3266 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003269 *va_arg(args, size_t *) = val.u;
3270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 default:
Jan Beulich53809752012-12-17 16:01:31 -08003272 if (is_sign)
3273 *va_arg(args, int *) = val.s;
3274 else
3275 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 break;
3277 }
3278 num++;
3279
3280 if (!next)
3281 break;
3282 str = next;
3283 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return num;
3286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287EXPORT_SYMBOL(vsscanf);
3288
3289/**
3290 * sscanf - Unformat a buffer into a list of arguments
3291 * @buf: input buffer
3292 * @fmt: formatting of buffer
3293 * @...: resulting arguments
3294 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003295int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 va_list args;
3298 int i;
3299
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003300 va_start(args, fmt);
3301 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return i;
3305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306EXPORT_SYMBOL(sscanf);