blob: 2af48948a973570da3259f21c193ad43efc69555 [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>
Rasmus Villemoesef27ac12019-03-07 16:27:03 -080020#include <linux/build_bug.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070021#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070022#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050023#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/ctype.h>
27#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070028#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080029#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070030#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110031#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040032#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080033#include <linux/cred.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020034#include <linux/rtc.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070035#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020036#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000037#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110038#include <linux/siphash.h>
39#include <linux/compiler.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040040#ifdef CONFIG_BLOCK
41#include <linux/blkdev.h>
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070044#include "../mm/internal.h" /* For the trace_print_flags arrays */
45
Tim Schmielau4e57b682005-10-30 15:03:48 -080046#include <asm/page.h> /* for PAGE_SIZE */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070047#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Shevchenko71dca952014-10-13 15:55:18 -070049#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070050#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * simple_strtoull - convert a string to an unsigned long long
54 * @cp: The start of the string
55 * @endp: A pointer to the end of the parsed string will be placed here
56 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080057 *
58 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Harvey Harrison22d27052008-10-16 13:40:35 -070060unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070062 unsigned long long result;
63 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070065 cp = _parse_integer_fixup_radix(cp, &base);
66 rv = _parse_integer(cp, base, &result);
67 /* FIXME */
68 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (endp)
71 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return result;
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075EXPORT_SYMBOL(simple_strtoull);
76
77/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080078 * simple_strtoul - convert a string to an unsigned long
79 * @cp: The start of the string
80 * @endp: A pointer to the end of the parsed string will be placed here
81 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080082 *
83 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080084 */
85unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
86{
87 return simple_strtoull(cp, endp, base);
88}
89EXPORT_SYMBOL(simple_strtoul);
90
91/**
92 * simple_strtol - convert a string to a signed long
93 * @cp: The start of the string
94 * @endp: A pointer to the end of the parsed string will be placed here
95 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080096 *
97 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080098 */
99long simple_strtol(const char *cp, char **endp, unsigned int base)
100{
101 if (*cp == '-')
102 return -simple_strtoul(cp + 1, endp, base);
103
104 return simple_strtoul(cp, endp, base);
105}
106EXPORT_SYMBOL(simple_strtol);
107
108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800113 *
114 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700116long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800118 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700119 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800120
Harvey Harrison22d27052008-10-16 13:40:35 -0700121 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400123EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Joe Perchescf3b4292010-05-24 14:33:16 -0700125static noinline_for_stack
126int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800128 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800130 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800132 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return i;
135}
136
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700137/*
138 * Decimal conversion is by far the most typical, and is used for
139 * /proc and /sys data. This directly impacts e.g. top performance
140 * with many processes running. We optimize it for speed by emitting
141 * two characters at a time, using a 200 byte lookup table. This
142 * roughly halves the number of multiplications compared to computing
143 * the digits one at a time. Implementation strongly inspired by the
144 * previous version, which in turn used ideas described at
145 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
146 * from the author, Douglas W. Jones).
147 *
148 * It turns out there is precisely one 26 bit fixed-point
149 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
150 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
151 * range happens to be somewhat larger (x <= 1073741898), but that's
152 * irrelevant for our purpose.
153 *
154 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
155 * need a 32x32->64 bit multiply, so we simply use the same constant.
156 *
157 * For dividing a number in the range [100, 10^4-1] by 100, there are
158 * several options. The simplest is (x * 0x147b) >> 19, which is valid
159 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700160 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700161
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700162static const u16 decpair[100] = {
163#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
164 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
165 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
166 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
167 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
168 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
169 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
170 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
171 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
172 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
173 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
174#undef _
175};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700176
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700177/*
178 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700179 * immediately jump to out_r where two 0s would be written but only
180 * one of them accounted for in buf. This is needed by ip4_string
181 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700182*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700183static noinline_for_stack
184char *put_dec_trunc8(char *buf, unsigned r)
185{
186 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700187
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700188 /* 1 <= r < 10^8 */
189 if (r < 100)
190 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700191
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700192 /* 100 <= r < 10^8 */
193 q = (r * (u64)0x28f5c29) >> 32;
194 *((u16 *)buf) = decpair[r - 100*q];
195 buf += 2;
196
197 /* 1 <= q < 10^6 */
198 if (q < 100)
199 goto out_q;
200
201 /* 100 <= q < 10^6 */
202 r = (q * (u64)0x28f5c29) >> 32;
203 *((u16 *)buf) = decpair[q - 100*r];
204 buf += 2;
205
206 /* 1 <= r < 10^4 */
207 if (r < 100)
208 goto out_r;
209
210 /* 100 <= r < 10^4 */
211 q = (r * 0x147b) >> 19;
212 *((u16 *)buf) = decpair[r - 100*q];
213 buf += 2;
214out_q:
215 /* 1 <= q < 100 */
216 r = q;
217out_r:
218 /* 1 <= r < 100 */
219 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700220 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700221 return buf;
222}
223
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700224#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
225static noinline_for_stack
226char *put_dec_full8(char *buf, unsigned r)
227{
228 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700229
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700230 /* 0 <= r < 10^8 */
231 q = (r * (u64)0x28f5c29) >> 32;
232 *((u16 *)buf) = decpair[r - 100*q];
233 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700234
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700235 /* 0 <= q < 10^6 */
236 r = (q * (u64)0x28f5c29) >> 32;
237 *((u16 *)buf) = decpair[q - 100*r];
238 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700239
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700240 /* 0 <= r < 10^4 */
241 q = (r * 0x147b) >> 19;
242 *((u16 *)buf) = decpair[r - 100*q];
243 buf += 2;
244
245 /* 0 <= q < 100 */
246 *((u16 *)buf) = decpair[q];
247 buf += 2;
248 return buf;
249}
250
251static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700252char *put_dec(char *buf, unsigned long long n)
253{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700254 if (n >= 100*1000*1000)
255 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
256 /* 1 <= n <= 1.6e11 */
257 if (n >= 100*1000*1000)
258 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
259 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700260 return put_dec_trunc8(buf, n);
261}
262
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700263#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700264
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700265static void
266put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700267{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700268 unsigned q;
269
270 /* 0 <= r < 10^4 */
271 q = (r * 0x147b) >> 19;
272 *((u16 *)buf) = decpair[r - 100*q];
273 buf += 2;
274 /* 0 <= q < 100 */
275 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700276}
277
278/*
279 * Call put_dec_full4 on x % 10000, return x / 10000.
280 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
281 * holds for all x < 1,128,869,999. The largest value this
282 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700283 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700284 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700285static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700286unsigned put_dec_helper4(char *buf, unsigned x)
287{
288 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
289
290 put_dec_full4(buf, x - q * 10000);
291 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700292}
293
294/* Based on code by Douglas W. Jones found at
295 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
296 * (with permission from the author).
297 * Performs no 64-bit division and hence should be fast on 32-bit machines.
298 */
299static
300char *put_dec(char *buf, unsigned long long n)
301{
302 uint32_t d3, d2, d1, q, h;
303
304 if (n < 100*1000*1000)
305 return put_dec_trunc8(buf, n);
306
307 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
308 h = (n >> 32);
309 d2 = (h ) & 0xffff;
310 d3 = (h >> 16); /* implicit "& 0xffff" */
311
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700312 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
313 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700314 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700315 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700316
George Spelvin23591722012-10-04 17:12:29 -0700317 q += 7671 * d3 + 9496 * d2 + 6 * d1;
318 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700319
George Spelvin23591722012-10-04 17:12:29 -0700320 q += 4749 * d3 + 42 * d2;
321 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700322
George Spelvin23591722012-10-04 17:12:29 -0700323 q += 281 * d3;
324 buf += 12;
325 if (q)
326 buf = put_dec_trunc8(buf, q);
327 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700328 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800329
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700330 return buf;
331}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700332
333#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700334
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700335/*
336 * Convert passed number to decimal string.
337 * Returns the length of string. On buffer overflow, returns 0.
338 *
339 * If speed is not important, use snprintf(). It's easy to read the code.
340 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700341int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700342{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700343 /* put_dec requires 2-byte alignment of the buffer. */
344 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700345 int idx, len;
346
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700347 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
348 if (num <= 9) {
349 tmp[0] = '0' + num;
350 len = 1;
351 } else {
352 len = put_dec(tmp, num) - tmp;
353 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700354
Andrei Vagind1be35c2018-04-10 16:31:16 -0700355 if (len > size || width > size)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700356 return 0;
Andrei Vagind1be35c2018-04-10 16:31:16 -0700357
358 if (width > len) {
359 width = width - len;
360 for (idx = 0; idx < width; idx++)
361 buf[idx] = ' ';
362 } else {
363 width = 0;
364 }
365
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700366 for (idx = 0; idx < len; ++idx)
Andrei Vagind1be35c2018-04-10 16:31:16 -0700367 buf[idx + width] = tmp[len - idx - 1];
368
369 return len + width;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700370}
371
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700372#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700373#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#define PLUS 4 /* show plus */
375#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700376#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700377#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
378#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100380enum format_type {
381 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100382 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100383 FORMAT_TYPE_PRECISION,
384 FORMAT_TYPE_CHAR,
385 FORMAT_TYPE_STR,
386 FORMAT_TYPE_PTR,
387 FORMAT_TYPE_PERCENT_CHAR,
388 FORMAT_TYPE_INVALID,
389 FORMAT_TYPE_LONG_LONG,
390 FORMAT_TYPE_ULONG,
391 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800392 FORMAT_TYPE_UBYTE,
393 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100394 FORMAT_TYPE_USHORT,
395 FORMAT_TYPE_SHORT,
396 FORMAT_TYPE_UINT,
397 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100398 FORMAT_TYPE_SIZE_T,
399 FORMAT_TYPE_PTRDIFF
400};
401
402struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800403 unsigned int type:8; /* format_type enum */
404 signed int field_width:24; /* width of output field */
405 unsigned int flags:8; /* flags to number() */
406 unsigned int base:8; /* number base, 8, 10 or 16 only */
407 signed int precision:16; /* # of digits/chars */
408} __packed;
Rasmus Villemoesef27ac12019-03-07 16:27:03 -0800409static_assert(sizeof(struct printf_spec) == 8);
410
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800411#define FIELD_WIDTH_MAX ((1 << 23) - 1)
412#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100413
Joe Perchescf3b4292010-05-24 14:33:16 -0700414static noinline_for_stack
415char *number(char *buf, char *end, unsigned long long num,
416 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700418 /* put_dec requires 2-byte alignment of the buffer. */
419 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100420 char sign;
421 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100422 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700424 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800425 int field_width = spec.field_width;
426 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100428 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
429 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100430 locase = (spec.flags & SMALL);
431 if (spec.flags & LEFT)
432 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100434 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800435 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800437 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800438 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100439 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800441 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100442 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800444 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100448 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800449 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700450 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800451 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700453
454 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700456 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700457 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100458 else if (spec.base != 10) { /* 8 or 16 */
459 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800461
462 if (spec.base == 16)
463 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700464 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700465 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700466 num >>= shift;
467 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700468 } else { /* base 10 */
469 i = put_dec(tmp, num) - tmp;
470 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700471
472 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800473 if (i > precision)
474 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700475 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800476 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700477 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800478 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700479 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *buf = ' ';
481 ++buf;
482 }
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700486 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *buf = sign;
488 ++buf;
489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* "0x" / "0" prefix */
491 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700492 if (spec.base == 16 || !is_zero) {
493 if (buf < end)
494 *buf = '0';
495 ++buf;
496 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100497 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700498 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100499 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ++buf;
501 }
502 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700503 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100504 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700505 char c = ' ' + (spec.flags & ZEROPAD);
506 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800507 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700508 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buf = c;
510 ++buf;
511 }
512 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700513 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800514 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700515 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *buf = '0';
517 ++buf;
518 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700519 /* actual digits of result */
520 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700521 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *buf = tmp[i];
523 ++buf;
524 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700525 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800526 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700527 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *buf = ' ';
529 ++buf;
530 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return buf;
533}
534
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800535static noinline_for_stack
536char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
537{
538 struct printf_spec spec;
539
540 spec.type = FORMAT_TYPE_PTR;
541 spec.field_width = 2 + 2 * size; /* 0x + hex */
542 spec.flags = SPECIAL | SMALL | ZEROPAD;
543 spec.base = 16;
544 spec.precision = -1;
545
546 return number(buf, end, num, spec);
547}
548
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800549static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400550{
551 size_t size;
552 if (buf >= end) /* nowhere to put anything */
553 return;
554 size = end - buf;
555 if (size <= spaces) {
556 memset(buf, ' ', size);
557 return;
558 }
559 if (len) {
560 if (len > size - spaces)
561 len = size - spaces;
562 memmove(buf + spaces, buf, len);
563 }
564 memset(buf, ' ', spaces);
565}
566
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800567/*
568 * Handle field width padding for a string.
569 * @buf: current buffer position
570 * @n: length of string
571 * @end: end of output buffer
572 * @spec: for field width and flags
573 * Returns: new buffer position after padding.
574 */
575static noinline_for_stack
576char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
577{
578 unsigned spaces;
579
580 if (likely(n >= spec.field_width))
581 return buf;
582 /* we want to pad the sucker */
583 spaces = spec.field_width - n;
584 if (!(spec.flags & LEFT)) {
585 move_right(buf - n, end, n, spaces);
586 return buf + spaces;
587 }
588 while (spaces--) {
589 if (buf < end)
590 *buf = ' ';
591 ++buf;
592 }
593 return buf;
594}
595
Al Viro4b6ccca2013-09-03 12:00:44 -0400596static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800597char *string(char *buf, char *end, const char *s, struct printf_spec spec)
598{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800599 int len = 0;
600 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800601
602 if ((unsigned long)s < PAGE_SIZE)
603 s = "(null)";
604
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800605 while (lim--) {
606 char c = *s++;
607 if (!c)
608 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800609 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800610 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800611 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800612 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800613 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800614 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800615}
616
617static noinline_for_stack
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200618char *pointer_string(char *buf, char *end, const void *ptr,
619 struct printf_spec spec)
620{
621 spec.base = 16;
622 spec.flags |= SMALL;
623 if (spec.field_width == -1) {
624 spec.field_width = 2 * sizeof(ptr);
625 spec.flags |= ZEROPAD;
626 }
627
628 return number(buf, end, (unsigned long int)ptr, spec);
629}
630
631/* Make pointers available for printing early in the boot sequence. */
632static int debug_boot_weak_hash __ro_after_init;
633
634static int __init debug_boot_weak_hash_enable(char *str)
635{
636 debug_boot_weak_hash = 1;
637 pr_info("debug_boot_weak_hash enabled\n");
638 return 0;
639}
640early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
641
642static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
643static siphash_key_t ptr_key __read_mostly;
644
645static void enable_ptr_key_workfn(struct work_struct *work)
646{
647 get_random_bytes(&ptr_key, sizeof(ptr_key));
648 /* Needs to run from preemptible context */
649 static_branch_disable(&not_filled_random_ptr_key);
650}
651
652static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
653
654static void fill_random_ptr_key(struct random_ready_callback *unused)
655{
656 /* This may be in an interrupt handler. */
657 queue_work(system_unbound_wq, &enable_ptr_key_work);
658}
659
660static struct random_ready_callback random_ready = {
661 .func = fill_random_ptr_key
662};
663
664static int __init initialize_ptr_random(void)
665{
666 int key_size = sizeof(ptr_key);
667 int ret;
668
669 /* Use hw RNG if available. */
670 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
671 static_branch_disable(&not_filled_random_ptr_key);
672 return 0;
673 }
674
675 ret = add_random_ready_callback(&random_ready);
676 if (!ret) {
677 return 0;
678 } else if (ret == -EALREADY) {
679 /* This is in preemptible context */
680 enable_ptr_key_workfn(&enable_ptr_key_work);
681 return 0;
682 }
683
684 return ret;
685}
686early_initcall(initialize_ptr_random);
687
688/* Maps a pointer to a 32 bit unique identifier. */
689static char *ptr_to_id(char *buf, char *end, const void *ptr,
690 struct printf_spec spec)
691{
692 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
693 unsigned long hashval;
694
695 /* When debugging early boot use non-cryptographically secure hash. */
696 if (unlikely(debug_boot_weak_hash)) {
697 hashval = hash_long((unsigned long)ptr, 32);
698 return pointer_string(buf, end, (const void *)hashval, spec);
699 }
700
701 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
702 spec.field_width = 2 * sizeof(ptr);
703 /* string length must be less than default_width */
704 return string(buf, end, str, spec);
705 }
706
707#ifdef CONFIG_64BIT
708 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
709 /*
710 * Mask off the first 32 bits, this makes explicit that we have
711 * modified the address (and 32 bits is plenty for a unique ID).
712 */
713 hashval = hashval & 0xffffffff;
714#else
715 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
716#endif
717 return pointer_string(buf, end, (const void *)hashval, spec);
718}
719
Petr Mladek6eea2422019-04-17 13:53:41 +0200720int kptr_restrict __read_mostly;
721
722static noinline_for_stack
723char *restricted_pointer(char *buf, char *end, const void *ptr,
724 struct printf_spec spec)
725{
726 switch (kptr_restrict) {
727 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200728 /* Handle as %p, hash and do _not_ leak addresses. */
729 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200730 case 1: {
731 const struct cred *cred;
732
733 /*
734 * kptr_restrict==1 cannot be used in IRQ context
735 * because its test for CAP_SYSLOG would be meaningless.
736 */
737 if (in_irq() || in_serving_softirq() || in_nmi()) {
738 if (spec.field_width == -1)
739 spec.field_width = 2 * sizeof(ptr);
740 return string(buf, end, "pK-error", spec);
741 }
742
743 /*
744 * Only print the real pointer value if the current
745 * process has CAP_SYSLOG and is running with the
746 * same credentials it started with. This is because
747 * access to files is checked at open() time, but %pK
748 * checks permission at read() time. We don't want to
749 * leak pointer values if a binary opens a file using
750 * %pK and then elevates privileges before reading it.
751 */
752 cred = current_cred();
753 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
754 !uid_eq(cred->euid, cred->uid) ||
755 !gid_eq(cred->egid, cred->gid))
756 ptr = NULL;
757 break;
758 }
759 case 2:
760 default:
761 /* Always print 0's for %pK */
762 ptr = NULL;
763 break;
764 }
765
766 return pointer_string(buf, end, ptr, spec);
767}
768
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200769static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400770char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
771 const char *fmt)
772{
773 const char *array[4], *s;
774 const struct dentry *p;
775 int depth;
776 int i, n;
777
778 switch (fmt[1]) {
779 case '2': case '3': case '4':
780 depth = fmt[1] - '0';
781 break;
782 default:
783 depth = 1;
784 }
785
786 rcu_read_lock();
787 for (i = 0; i < depth; i++, d = p) {
Mark Rutland6aa7de02017-10-23 14:07:29 -0700788 p = READ_ONCE(d->d_parent);
789 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400790 if (p == d) {
791 if (i)
792 array[i] = "";
793 i++;
794 break;
795 }
796 }
797 s = array[--i];
798 for (n = 0; n != spec.precision; n++, buf++) {
799 char c = *s++;
800 if (!c) {
801 if (!i)
802 break;
803 c = '/';
804 s = array[--i];
805 }
806 if (buf < end)
807 *buf = c;
808 }
809 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800810 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400811}
812
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400813#ifdef CONFIG_BLOCK
814static noinline_for_stack
815char *bdev_name(char *buf, char *end, struct block_device *bdev,
816 struct printf_spec spec, const char *fmt)
817{
818 struct gendisk *hd = bdev->bd_disk;
819
820 buf = string(buf, end, hd->disk_name, spec);
821 if (bdev->bd_part->partno) {
822 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
823 if (buf < end)
824 *buf = 'p';
825 buf++;
826 }
827 buf = number(buf, end, bdev->bd_part->partno, spec);
828 }
829 return buf;
830}
831#endif
832
Joe Perchescf3b4292010-05-24 14:33:16 -0700833static noinline_for_stack
834char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800835 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700836{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800837 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700838#ifdef CONFIG_KALLSYMS
839 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800840#endif
841
842 if (fmt[1] == 'R')
843 ptr = __builtin_extract_return_addr(ptr);
844 value = (unsigned long)ptr;
845
846#ifdef CONFIG_KALLSYMS
847 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900848 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800849 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200850 sprint_symbol(sym, value);
851 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700852 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800853
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100854 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700855#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800856 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700857#endif
858}
859
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200860static const struct printf_spec default_str_spec = {
861 .field_width = -1,
862 .precision = -1,
863};
864
Andy Shevchenko54433972018-02-16 23:07:06 +0200865static const struct printf_spec default_flag_spec = {
866 .base = 16,
867 .precision = -1,
868 .flags = SPECIAL | SMALL,
869};
870
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200871static const struct printf_spec default_dec_spec = {
872 .base = 10,
873 .precision = -1,
874};
875
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200876static const struct printf_spec default_dec02_spec = {
877 .base = 10,
878 .field_width = 2,
879 .precision = -1,
880 .flags = ZEROPAD,
881};
882
883static const struct printf_spec default_dec04_spec = {
884 .base = 10,
885 .field_width = 4,
886 .precision = -1,
887 .flags = ZEROPAD,
888};
889
Joe Perchescf3b4292010-05-24 14:33:16 -0700890static noinline_for_stack
891char *resource_string(char *buf, char *end, struct resource *res,
892 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100893{
894#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600895#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100896#endif
897
898#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600899#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100900#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700901 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100902 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700903 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100904 .precision = -1,
905 .flags = SPECIAL | SMALL | ZEROPAD,
906 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700907 static const struct printf_spec mem_spec = {
908 .base = 16,
909 .field_width = MEM_RSRC_PRINTK_SIZE,
910 .precision = -1,
911 .flags = SPECIAL | SMALL | ZEROPAD,
912 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700913 static const struct printf_spec bus_spec = {
914 .base = 16,
915 .field_width = 2,
916 .precision = -1,
917 .flags = SMALL | ZEROPAD,
918 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700919 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600920 .field_width = -1,
921 .precision = 10,
922 .flags = LEFT,
923 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600924
925 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
926 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
927#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
928#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700929#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600930#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
931 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
932 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
933
Linus Torvalds332d2e72008-10-20 15:07:34 +1100934 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600935 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700936 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100937
938 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700939 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600940 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700941 specp = &io_spec;
942 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600943 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700944 specp = &mem_spec;
945 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600946 p = string(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200947 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700948 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600949 p = string(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200950 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700951 } else if (res->flags & IORESOURCE_BUS) {
952 p = string(p, pend, "bus ", str_spec);
953 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700954 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600955 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700956 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600957 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600958 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700959 if (decode && res->flags & IORESOURCE_UNSET) {
960 p = string(p, pend, "size ", str_spec);
961 p = number(p, pend, resource_size(res), *specp);
962 } else {
963 p = number(p, pend, res->start, *specp);
964 if (res->start != res->end) {
965 *p++ = '-';
966 p = number(p, pend, res->end, *specp);
967 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600968 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600969 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600970 if (res->flags & IORESOURCE_MEM_64)
971 p = string(p, pend, " 64bit", str_spec);
972 if (res->flags & IORESOURCE_PREFETCH)
973 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700974 if (res->flags & IORESOURCE_WINDOW)
975 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600976 if (res->flags & IORESOURCE_DISABLED)
977 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600978 } else {
979 p = string(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +0200980 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600981 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100982 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600983 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100984
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100985 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100986}
987
Joe Perchescf3b4292010-05-24 14:33:16 -0700988static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700989char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
990 const char *fmt)
991{
Steven Rostedt360603a2013-05-28 15:47:39 -0400992 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700993 negative value, fallback to the default */
994 char separator;
995
996 if (spec.field_width == 0)
997 /* nothing to print */
998 return buf;
999
1000 if (ZERO_OR_NULL_PTR(addr))
1001 /* NULL pointer */
1002 return string(buf, end, NULL, spec);
1003
1004 switch (fmt[1]) {
1005 case 'C':
1006 separator = ':';
1007 break;
1008 case 'D':
1009 separator = '-';
1010 break;
1011 case 'N':
1012 separator = 0;
1013 break;
1014 default:
1015 separator = ' ';
1016 break;
1017 }
1018
1019 if (spec.field_width > 0)
1020 len = min_t(int, spec.field_width, 64);
1021
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001022 for (i = 0; i < len; ++i) {
1023 if (buf < end)
1024 *buf = hex_asc_hi(addr[i]);
1025 ++buf;
1026 if (buf < end)
1027 *buf = hex_asc_lo(addr[i]);
1028 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001029
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001030 if (separator && i != len - 1) {
1031 if (buf < end)
1032 *buf = separator;
1033 ++buf;
1034 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001035 }
1036
1037 return buf;
1038}
1039
1040static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001041char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1042 struct printf_spec spec, const char *fmt)
1043{
1044 const int CHUNKSZ = 32;
1045 int nr_bits = max_t(int, spec.field_width, 0);
1046 int i, chunksz;
1047 bool first = true;
1048
1049 /* reused to print numbers */
1050 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1051
1052 chunksz = nr_bits & (CHUNKSZ - 1);
1053 if (chunksz == 0)
1054 chunksz = CHUNKSZ;
1055
1056 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1057 for (; i >= 0; i -= CHUNKSZ) {
1058 u32 chunkmask, val;
1059 int word, bit;
1060
1061 chunkmask = ((1ULL << chunksz) - 1);
1062 word = i / BITS_PER_LONG;
1063 bit = i % BITS_PER_LONG;
1064 val = (bitmap[word] >> bit) & chunkmask;
1065
1066 if (!first) {
1067 if (buf < end)
1068 *buf = ',';
1069 buf++;
1070 }
1071 first = false;
1072
1073 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1074 buf = number(buf, end, val, spec);
1075
1076 chunksz = CHUNKSZ;
1077 }
1078 return buf;
1079}
1080
1081static noinline_for_stack
1082char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1083 struct printf_spec spec, const char *fmt)
1084{
1085 int nr_bits = max_t(int, spec.field_width, 0);
1086 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1087 int cur, rbot, rtop;
1088 bool first = true;
1089
Tejun Heodbc760b2015-02-13 14:36:53 -08001090 rbot = cur = find_first_bit(bitmap, nr_bits);
1091 while (cur < nr_bits) {
1092 rtop = cur;
1093 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1094 if (cur < nr_bits && cur <= rtop + 1)
1095 continue;
1096
1097 if (!first) {
1098 if (buf < end)
1099 *buf = ',';
1100 buf++;
1101 }
1102 first = false;
1103
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001104 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001105 if (rbot < rtop) {
1106 if (buf < end)
1107 *buf = '-';
1108 buf++;
1109
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001110 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001111 }
1112
1113 rbot = cur;
1114 }
1115 return buf;
1116}
1117
1118static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001119char *mac_address_string(char *buf, char *end, u8 *addr,
1120 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001121{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001122 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001123 char *p = mac_addr;
1124 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001125 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001126 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001127
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001128 switch (fmt[1]) {
1129 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001130 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001131 break;
1132
1133 case 'R':
1134 reversed = true;
1135 /* fall through */
1136
1137 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001138 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001139 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001140 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001141
1142 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001143 if (reversed)
1144 p = hex_byte_pack(p, addr[5 - i]);
1145 else
1146 p = hex_byte_pack(p, addr[i]);
1147
Joe Perches8a27f7c2009-08-17 12:29:44 +00001148 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001149 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001150 }
1151 *p = '\0';
1152
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001153 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001154}
1155
Joe Perchescf3b4292010-05-24 14:33:16 -07001156static noinline_for_stack
1157char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001158{
Harvey Harrison689afa72008-10-28 16:04:44 -07001159 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001160 bool leading_zeros = (fmt[0] == 'i');
1161 int index;
1162 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001163
Joe Perches0159f242010-01-13 20:23:30 -08001164 switch (fmt[2]) {
1165 case 'h':
1166#ifdef __BIG_ENDIAN
1167 index = 0;
1168 step = 1;
1169#else
1170 index = 3;
1171 step = -1;
1172#endif
1173 break;
1174 case 'l':
1175 index = 3;
1176 step = -1;
1177 break;
1178 case 'n':
1179 case 'b':
1180 default:
1181 index = 0;
1182 step = 1;
1183 break;
1184 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001185 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001186 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001187 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001188 if (leading_zeros) {
1189 if (digits < 3)
1190 *p++ = '0';
1191 if (digits < 2)
1192 *p++ = '0';
1193 }
1194 /* reverse the digits in the quad */
1195 while (digits--)
1196 *p++ = temp[digits];
1197 if (i < 3)
1198 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001199 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001200 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001201 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001202
Joe Perches8a27f7c2009-08-17 12:29:44 +00001203 return p;
1204}
1205
Joe Perchescf3b4292010-05-24 14:33:16 -07001206static noinline_for_stack
1207char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001208{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001209 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001210 unsigned char zerolength[8];
1211 int longest = 1;
1212 int colonpos = -1;
1213 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001214 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001215 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001216 bool useIPv4;
1217 struct in6_addr in6;
1218
1219 memcpy(&in6, addr, sizeof(struct in6_addr));
1220
1221 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001222
1223 memset(zerolength, 0, sizeof(zerolength));
1224
1225 if (useIPv4)
1226 range = 6;
1227 else
1228 range = 8;
1229
1230 /* find position of longest 0 run */
1231 for (i = 0; i < range; i++) {
1232 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001233 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001234 break;
1235 zerolength[i]++;
1236 }
1237 }
1238 for (i = 0; i < range; i++) {
1239 if (zerolength[i] > longest) {
1240 longest = zerolength[i];
1241 colonpos = i;
1242 }
1243 }
Joe Perches29cf5192011-06-09 11:23:37 -07001244 if (longest == 1) /* don't compress a single 0 */
1245 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001246
1247 /* emit address */
1248 for (i = 0; i < range; i++) {
1249 if (i == colonpos) {
1250 if (needcolon || i == 0)
1251 *p++ = ':';
1252 *p++ = ':';
1253 needcolon = false;
1254 i += longest - 1;
1255 continue;
1256 }
1257 if (needcolon) {
1258 *p++ = ':';
1259 needcolon = false;
1260 }
1261 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001262 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001263 hi = word >> 8;
1264 lo = word & 0xff;
1265 if (hi) {
1266 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001267 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001268 else
1269 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001270 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001271 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001272 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001273 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001274 else
1275 *p++ = hex_asc_lo(lo);
1276 needcolon = true;
1277 }
1278
1279 if (useIPv4) {
1280 if (needcolon)
1281 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001282 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001283 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001284 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001285
Joe Perches8a27f7c2009-08-17 12:29:44 +00001286 return p;
1287}
1288
Joe Perchescf3b4292010-05-24 14:33:16 -07001289static noinline_for_stack
1290char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001291{
1292 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001293
Harvey Harrison689afa72008-10-28 16:04:44 -07001294 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001295 p = hex_byte_pack(p, *addr++);
1296 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001297 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001298 *p++ = ':';
1299 }
1300 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001301
Joe Perches8a27f7c2009-08-17 12:29:44 +00001302 return p;
1303}
1304
Joe Perchescf3b4292010-05-24 14:33:16 -07001305static noinline_for_stack
1306char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1307 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001308{
1309 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1310
1311 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001312 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001313 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001314 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001315
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001316 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001317}
1318
Joe Perchescf3b4292010-05-24 14:33:16 -07001319static noinline_for_stack
1320char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1321 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001322{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001323 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001324
Joe Perches0159f242010-01-13 20:23:30 -08001325 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001326
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001327 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001328}
1329
Joe Perchescf3b4292010-05-24 14:33:16 -07001330static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001331char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1332 struct printf_spec spec, const char *fmt)
1333{
1334 bool have_p = false, have_s = false, have_f = false, have_c = false;
1335 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1336 sizeof(":12345") + sizeof("/123456789") +
1337 sizeof("%1234567890")];
1338 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1339 const u8 *addr = (const u8 *) &sa->sin6_addr;
1340 char fmt6[2] = { fmt[0], '6' };
1341 u8 off = 0;
1342
1343 fmt++;
1344 while (isalpha(*++fmt)) {
1345 switch (*fmt) {
1346 case 'p':
1347 have_p = true;
1348 break;
1349 case 'f':
1350 have_f = true;
1351 break;
1352 case 's':
1353 have_s = true;
1354 break;
1355 case 'c':
1356 have_c = true;
1357 break;
1358 }
1359 }
1360
1361 if (have_p || have_s || have_f) {
1362 *p = '[';
1363 off = 1;
1364 }
1365
1366 if (fmt6[0] == 'I' && have_c)
1367 p = ip6_compressed_string(ip6_addr + off, addr);
1368 else
1369 p = ip6_string(ip6_addr + off, addr, fmt6);
1370
1371 if (have_p || have_s || have_f)
1372 *p++ = ']';
1373
1374 if (have_p) {
1375 *p++ = ':';
1376 p = number(p, pend, ntohs(sa->sin6_port), spec);
1377 }
1378 if (have_f) {
1379 *p++ = '/';
1380 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1381 IPV6_FLOWINFO_MASK), spec);
1382 }
1383 if (have_s) {
1384 *p++ = '%';
1385 p = number(p, pend, sa->sin6_scope_id, spec);
1386 }
1387 *p = '\0';
1388
1389 return string(buf, end, ip6_addr, spec);
1390}
1391
1392static noinline_for_stack
1393char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1394 struct printf_spec spec, const char *fmt)
1395{
1396 bool have_p = false;
1397 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1398 char *pend = ip4_addr + sizeof(ip4_addr);
1399 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1400 char fmt4[3] = { fmt[0], '4', 0 };
1401
1402 fmt++;
1403 while (isalpha(*++fmt)) {
1404 switch (*fmt) {
1405 case 'p':
1406 have_p = true;
1407 break;
1408 case 'h':
1409 case 'l':
1410 case 'n':
1411 case 'b':
1412 fmt4[2] = *fmt;
1413 break;
1414 }
1415 }
1416
1417 p = ip4_string(ip4_addr, addr, fmt4);
1418 if (have_p) {
1419 *p++ = ':';
1420 p = number(p, pend, ntohs(sa->sin_port), spec);
1421 }
1422 *p = '\0';
1423
1424 return string(buf, end, ip4_addr, spec);
1425}
1426
1427static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001428char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1429 const char *fmt)
1430{
1431 bool found = true;
1432 int count = 1;
1433 unsigned int flags = 0;
1434 int len;
1435
1436 if (spec.field_width == 0)
1437 return buf; /* nothing to print */
1438
1439 if (ZERO_OR_NULL_PTR(addr))
1440 return string(buf, end, NULL, spec); /* NULL pointer */
1441
1442
1443 do {
1444 switch (fmt[count++]) {
1445 case 'a':
1446 flags |= ESCAPE_ANY;
1447 break;
1448 case 'c':
1449 flags |= ESCAPE_SPECIAL;
1450 break;
1451 case 'h':
1452 flags |= ESCAPE_HEX;
1453 break;
1454 case 'n':
1455 flags |= ESCAPE_NULL;
1456 break;
1457 case 'o':
1458 flags |= ESCAPE_OCTAL;
1459 break;
1460 case 'p':
1461 flags |= ESCAPE_NP;
1462 break;
1463 case 's':
1464 flags |= ESCAPE_SPACE;
1465 break;
1466 default:
1467 found = false;
1468 break;
1469 }
1470 } while (found);
1471
1472 if (!flags)
1473 flags = ESCAPE_ANY_NP;
1474
1475 len = spec.field_width < 0 ? 1 : spec.field_width;
1476
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001477 /*
1478 * string_escape_mem() writes as many characters as it can to
1479 * the given buffer, and returns the total size of the output
1480 * had the buffer been big enough.
1481 */
1482 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001483
1484 return buf;
1485}
1486
1487static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001488char *uuid_string(char *buf, char *end, const u8 *addr,
1489 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001490{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001491 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001492 char *p = uuid;
1493 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001494 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001495 bool uc = false;
1496
1497 switch (*(++fmt)) {
1498 case 'L':
1499 uc = true; /* fall-through */
1500 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001501 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001502 break;
1503 case 'B':
1504 uc = true;
1505 break;
1506 }
1507
1508 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001509 if (uc)
1510 p = hex_byte_pack_upper(p, addr[index[i]]);
1511 else
1512 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001513 switch (i) {
1514 case 3:
1515 case 5:
1516 case 7:
1517 case 9:
1518 *p++ = '-';
1519 break;
1520 }
1521 }
1522
1523 *p = 0;
1524
Joe Perches9ac6e442009-12-14 18:01:09 -08001525 return string(buf, end, uuid, spec);
1526}
1527
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001528static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001529char *netdev_bits(char *buf, char *end, const void *addr,
1530 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001531{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001532 unsigned long long num;
1533 int size;
1534
1535 switch (fmt[1]) {
1536 case 'F':
1537 num = *(const netdev_features_t *)addr;
1538 size = sizeof(netdev_features_t);
1539 break;
1540 default:
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001541 return ptr_to_id(buf, end, addr, spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001542 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001543
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001544 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001545}
1546
Joe Perchesaaf07622014-01-23 15:54:17 -08001547static noinline_for_stack
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001548char *address_val(char *buf, char *end, const void *addr, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001549{
1550 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001551 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001552
1553 switch (fmt[1]) {
1554 case 'd':
1555 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001556 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001557 break;
1558 case 'p':
1559 default:
1560 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001561 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001562 break;
1563 }
1564
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001565 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001566}
1567
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001568static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001569char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1570{
1571 int year = tm->tm_year + (r ? 0 : 1900);
1572 int mon = tm->tm_mon + (r ? 0 : 1);
1573
1574 buf = number(buf, end, year, default_dec04_spec);
1575 if (buf < end)
1576 *buf = '-';
1577 buf++;
1578
1579 buf = number(buf, end, mon, default_dec02_spec);
1580 if (buf < end)
1581 *buf = '-';
1582 buf++;
1583
1584 return number(buf, end, tm->tm_mday, default_dec02_spec);
1585}
1586
1587static noinline_for_stack
1588char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1589{
1590 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1591 if (buf < end)
1592 *buf = ':';
1593 buf++;
1594
1595 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1596 if (buf < end)
1597 *buf = ':';
1598 buf++;
1599
1600 return number(buf, end, tm->tm_sec, default_dec02_spec);
1601}
1602
1603static noinline_for_stack
1604char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt)
1605{
1606 bool have_t = true, have_d = true;
1607 bool raw = false;
1608 int count = 2;
1609
1610 switch (fmt[count]) {
1611 case 'd':
1612 have_t = false;
1613 count++;
1614 break;
1615 case 't':
1616 have_d = false;
1617 count++;
1618 break;
1619 }
1620
1621 raw = fmt[count] == 'r';
1622
1623 if (have_d)
1624 buf = date_str(buf, end, tm, raw);
1625 if (have_d && have_t) {
1626 /* Respect ISO 8601 */
1627 if (buf < end)
1628 *buf = 'T';
1629 buf++;
1630 }
1631 if (have_t)
1632 buf = time_str(buf, end, tm, raw);
1633
1634 return buf;
1635}
1636
1637static noinline_for_stack
1638char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1639 const char *fmt)
1640{
1641 switch (fmt[1]) {
1642 case 'R':
1643 return rtc_str(buf, end, (const struct rtc_time *)ptr, fmt);
1644 default:
1645 return ptr_to_id(buf, end, ptr, spec);
1646 }
1647}
1648
1649static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001650char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1651 const char *fmt)
1652{
1653 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1654 return string(buf, end, NULL, spec);
1655
1656 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001657 case 'n':
1658 default:
1659#ifdef CONFIG_COMMON_CLK
1660 return string(buf, end, __clk_get_name(clk), spec);
1661#else
Geert Uytterhoevenec12bc22018-10-11 10:42:48 +02001662 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001663#endif
1664 }
1665}
1666
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001667static
1668char *format_flags(char *buf, char *end, unsigned long flags,
1669 const struct trace_print_flags *names)
1670{
1671 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001672
1673 for ( ; flags && names->name; names++) {
1674 mask = names->mask;
1675 if ((flags & mask) != mask)
1676 continue;
1677
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001678 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001679
1680 flags &= ~mask;
1681 if (flags) {
1682 if (buf < end)
1683 *buf = '|';
1684 buf++;
1685 }
1686 }
1687
1688 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001689 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001690
1691 return buf;
1692}
1693
1694static noinline_for_stack
1695char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1696{
1697 unsigned long flags;
1698 const struct trace_print_flags *names;
1699
1700 switch (fmt[1]) {
1701 case 'p':
1702 flags = *(unsigned long *)flags_ptr;
1703 /* Remove zone id */
1704 flags &= (1UL << NR_PAGEFLAGS) - 1;
1705 names = pageflag_names;
1706 break;
1707 case 'v':
1708 flags = *(unsigned long *)flags_ptr;
1709 names = vmaflag_names;
1710 break;
1711 case 'g':
1712 flags = *(gfp_t *)flags_ptr;
1713 names = gfpflag_names;
1714 break;
1715 default:
1716 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1717 return buf;
1718 }
1719
1720 return format_flags(buf, end, flags, names);
1721}
1722
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001723static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1724{
1725 for ( ; np && depth; depth--)
1726 np = np->parent;
1727
1728 return kbasename(np->full_name);
1729}
1730
1731static noinline_for_stack
1732char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1733{
1734 int depth;
1735 const struct device_node *parent = np->parent;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001736
1737 /* special case for root node */
1738 if (!parent)
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001739 return string(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001740
1741 for (depth = 0; parent->parent; depth++)
1742 parent = parent->parent;
1743
1744 for ( ; depth >= 0; depth--) {
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001745 buf = string(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001746 buf = string(buf, end, device_node_name_for_depth(np, depth),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001747 default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001748 }
1749 return buf;
1750}
1751
1752static noinline_for_stack
1753char *device_node_string(char *buf, char *end, struct device_node *dn,
1754 struct printf_spec spec, const char *fmt)
1755{
1756 char tbuf[sizeof("xxxx") + 1];
1757 const char *p;
1758 int ret;
1759 char *buf_start = buf;
1760 struct property *prop;
1761 bool has_mult, pass;
1762 static const struct printf_spec num_spec = {
1763 .flags = SMALL,
1764 .field_width = -1,
1765 .precision = -1,
1766 .base = 10,
1767 };
1768
1769 struct printf_spec str_spec = spec;
1770 str_spec.field_width = -1;
1771
1772 if (!IS_ENABLED(CONFIG_OF))
1773 return string(buf, end, "(!OF)", spec);
1774
1775 if ((unsigned long)dn < PAGE_SIZE)
1776 return string(buf, end, "(null)", spec);
1777
1778 /* simple case without anything any more format specifiers */
1779 fmt++;
1780 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1781 fmt = "f";
1782
1783 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001784 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001785 if (pass) {
1786 if (buf < end)
1787 *buf = ':';
1788 buf++;
1789 }
1790
1791 switch (*fmt) {
1792 case 'f': /* full_name */
1793 buf = device_node_gen_full_name(dn, buf, end);
1794 break;
1795 case 'n': /* name */
Rob Herring6d0a70a2018-08-27 08:13:56 -05001796 p = kbasename(of_node_full_name(dn));
1797 precision = str_spec.precision;
1798 str_spec.precision = strchrnul(p, '@') - p;
1799 buf = string(buf, end, p, str_spec);
1800 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001801 break;
1802 case 'p': /* phandle */
1803 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1804 break;
1805 case 'P': /* path-spec */
1806 p = kbasename(of_node_full_name(dn));
1807 if (!p[1])
1808 p = "/";
1809 buf = string(buf, end, p, str_spec);
1810 break;
1811 case 'F': /* flags */
1812 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1813 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1814 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1815 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1816 tbuf[4] = 0;
1817 buf = string(buf, end, tbuf, str_spec);
1818 break;
1819 case 'c': /* major compatible string */
1820 ret = of_property_read_string(dn, "compatible", &p);
1821 if (!ret)
1822 buf = string(buf, end, p, str_spec);
1823 break;
1824 case 'C': /* full compatible string */
1825 has_mult = false;
1826 of_property_for_each_string(dn, "compatible", prop, p) {
1827 if (has_mult)
1828 buf = string(buf, end, ",", str_spec);
1829 buf = string(buf, end, "\"", str_spec);
1830 buf = string(buf, end, p, str_spec);
1831 buf = string(buf, end, "\"", str_spec);
1832
1833 has_mult = true;
1834 }
1835 break;
1836 default:
1837 break;
1838 }
1839 }
1840
1841 return widen_string(buf, buf - buf_start, end, spec);
1842}
1843
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001844/*
1845 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1846 * by an extra set of alphanumeric characters that are extended format
1847 * specifiers.
1848 *
Joe Perches0b523762017-05-08 15:55:36 -07001849 * Please update scripts/checkpatch.pl when adding/removing conversion
1850 * characters. (Search for "check for vsprintf extension").
1851 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001852 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001853 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09001854 * - 'S' For symbolic direct pointers (or function descriptors) with offset
1855 * - 's' For symbolic direct pointers (or function descriptors) without offset
1856 * - 'F' Same as 'S'
1857 * - 'f' Same as 's'
Joe Perchesb0d33c22012-12-12 10:18:50 -08001858 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001859 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001860 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1861 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001862 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1863 * width which must be explicitly specified either as part of the
1864 * format string '%32b[l]' or through '%*b[l]', [l] selects
1865 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001866 * - 'M' For a 6-byte MAC address, it prints the address in the
1867 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001868 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001869 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001870 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001871 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001872 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1873 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1874 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001875 * [S][pfs]
1876 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1877 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001878 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1879 * IPv6 omits the colons (01020304...0f)
1880 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001881 * [S][pfs]
1882 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1883 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1884 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1885 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001886 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001887 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1888 * of the following flags (see string_escape_mem() for the
1889 * details):
1890 * a - ESCAPE_ANY
1891 * c - ESCAPE_SPECIAL
1892 * h - ESCAPE_HEX
1893 * n - ESCAPE_NULL
1894 * o - ESCAPE_OCTAL
1895 * p - ESCAPE_NP
1896 * s - ESCAPE_SPACE
1897 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001898 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1899 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1900 * Options for %pU are:
1901 * b big endian lower case hex (default)
1902 * B big endian UPPER case hex
1903 * l little endian lower case hex
1904 * L little endian UPPER case hex
1905 * big endian output byte order is:
1906 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1907 * little endian output byte order is:
1908 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001909 * - 'V' For a struct va_format which contains a format string * and va_list *,
1910 * call vsnprintf(->format, *->va_list).
1911 * Implements a "recursive vsnprintf".
1912 * Do not use this feature without some mechanism to verify the
1913 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001914 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001915 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001916 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1917 * a certain separator (' ' by default):
1918 * C colon
1919 * D dash
1920 * N no separator
1921 * The maximum supported length is 64 bytes of the input. Consider
1922 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001923 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1924 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001925 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1926 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001927 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001928 * - 't[R][dt][r]' For time and date as represented:
1929 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001930 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1931 * (legacy clock framework) of the clock
1932 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1933 * (legacy clock framework) 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':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002044 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002045 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002046 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002047 case 'a':
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08002048 return address_val(buf, end, ptr, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002049 case 'd':
2050 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002051 case 't':
2052 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002053 case 'C':
2054 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002055 case 'D':
2056 return dentry_name(buf, end,
2057 ((const struct file *)ptr)->f_path.dentry,
2058 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002059#ifdef CONFIG_BLOCK
2060 case 'g':
2061 return bdev_name(buf, end, ptr, spec, fmt);
2062#endif
2063
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002064 case 'G':
2065 return flags_string(buf, end, ptr, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002066 case 'O':
2067 switch (fmt[1]) {
2068 case 'F':
2069 return device_node_string(buf, end, ptr, spec, fmt + 1);
2070 }
Bart Van Assche554ec502018-08-06 15:34:21 -07002071 break;
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002072 case 'x':
2073 return pointer_string(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002074 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002075
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002076 /* default is to _not_ leak addresses, hash before printing */
2077 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002078}
2079
2080/*
2081 * Helper function to decode printf style format.
2082 * Each call decode a token from the format and return the
2083 * number of characters read (or likely the delta where it wants
2084 * to go on the next call).
2085 * The decoded token is returned through the parameters
2086 *
2087 * 'h', 'l', or 'L' for integer fields
2088 * 'z' support added 23/7/1999 S.H.
2089 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002090 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002091 * 't' added for ptrdiff_t
2092 *
2093 * @fmt: the format string
2094 * @type of the token returned
2095 * @flags: various flags such as +, -, # tokens..
2096 * @field_width: overwritten width
2097 * @base: base of the number (octal, hex, ...)
2098 * @precision: precision of a number
2099 * @qualifier: qualifier of a number (long, size_t, ...)
2100 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002101static noinline_for_stack
2102int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002103{
2104 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002105 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002106
2107 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002108 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002109 if (spec->field_width < 0) {
2110 spec->field_width = -spec->field_width;
2111 spec->flags |= LEFT;
2112 }
2113 spec->type = FORMAT_TYPE_NONE;
2114 goto precision;
2115 }
2116
2117 /* we finished early by reading the precision */
2118 if (spec->type == FORMAT_TYPE_PRECISION) {
2119 if (spec->precision < 0)
2120 spec->precision = 0;
2121
2122 spec->type = FORMAT_TYPE_NONE;
2123 goto qualifier;
2124 }
2125
2126 /* By default */
2127 spec->type = FORMAT_TYPE_NONE;
2128
2129 for (; *fmt ; ++fmt) {
2130 if (*fmt == '%')
2131 break;
2132 }
2133
2134 /* Return the current non-format string */
2135 if (fmt != start || !*fmt)
2136 return fmt - start;
2137
2138 /* Process flags */
2139 spec->flags = 0;
2140
2141 while (1) { /* this also skips first '%' */
2142 bool found = true;
2143
2144 ++fmt;
2145
2146 switch (*fmt) {
2147 case '-': spec->flags |= LEFT; break;
2148 case '+': spec->flags |= PLUS; break;
2149 case ' ': spec->flags |= SPACE; break;
2150 case '#': spec->flags |= SPECIAL; break;
2151 case '0': spec->flags |= ZEROPAD; break;
2152 default: found = false;
2153 }
2154
2155 if (!found)
2156 break;
2157 }
2158
2159 /* get field width */
2160 spec->field_width = -1;
2161
2162 if (isdigit(*fmt))
2163 spec->field_width = skip_atoi(&fmt);
2164 else if (*fmt == '*') {
2165 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002166 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002167 return ++fmt - start;
2168 }
2169
2170precision:
2171 /* get the precision */
2172 spec->precision = -1;
2173 if (*fmt == '.') {
2174 ++fmt;
2175 if (isdigit(*fmt)) {
2176 spec->precision = skip_atoi(&fmt);
2177 if (spec->precision < 0)
2178 spec->precision = 0;
2179 } else if (*fmt == '*') {
2180 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002181 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002182 return ++fmt - start;
2183 }
2184 }
2185
2186qualifier:
2187 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002188 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002189 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002190 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002191 qualifier = *fmt++;
2192 if (unlikely(qualifier == *fmt)) {
2193 if (qualifier == 'l') {
2194 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002195 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002196 } else if (qualifier == 'h') {
2197 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002198 ++fmt;
2199 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002200 }
2201 }
2202
2203 /* default base */
2204 spec->base = 10;
2205 switch (*fmt) {
2206 case 'c':
2207 spec->type = FORMAT_TYPE_CHAR;
2208 return ++fmt - start;
2209
2210 case 's':
2211 spec->type = FORMAT_TYPE_STR;
2212 return ++fmt - start;
2213
2214 case 'p':
2215 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002216 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002217
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002218 case '%':
2219 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2220 return ++fmt - start;
2221
2222 /* integer number formats - set up the flags and "break" */
2223 case 'o':
2224 spec->base = 8;
2225 break;
2226
2227 case 'x':
2228 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002229 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002230
2231 case 'X':
2232 spec->base = 16;
2233 break;
2234
2235 case 'd':
2236 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002237 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002238 case 'u':
2239 break;
2240
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002241 case 'n':
2242 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002243 * Since %n poses a greater security risk than
2244 * utility, treat it as any other invalid or
2245 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002246 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002247 /* Fall-through */
2248
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002249 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002250 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002251 spec->type = FORMAT_TYPE_INVALID;
2252 return fmt - start;
2253 }
2254
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002255 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002256 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002257 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002258 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2259 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002260 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002261 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002262 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002263 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002264 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002265 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2266 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002267 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002268 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2269 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002270 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002271 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2272 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002273 }
2274
2275 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002276}
2277
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002278static void
2279set_field_width(struct printf_spec *spec, int width)
2280{
2281 spec->field_width = width;
2282 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2283 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2284 }
2285}
2286
2287static void
2288set_precision(struct printf_spec *spec, int prec)
2289{
2290 spec->precision = prec;
2291 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2292 spec->precision = clamp(prec, 0, PRECISION_MAX);
2293 }
2294}
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296/**
2297 * vsnprintf - Format a string and place it in a buffer
2298 * @buf: The buffer to place the result into
2299 * @size: The size of the buffer, including the trailing null space
2300 * @fmt: The format string to use
2301 * @args: Arguments for the format string
2302 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002303 * This function generally follows C99 vsnprintf, but has some
2304 * extensions and a few limitations:
2305 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002306 * - ``%n`` is unsupported
2307 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002308 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002309 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002310 * extensive description.
2311 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002312 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002313 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 * The return value is the number of characters which would
2315 * be generated for the given input, excluding the trailing
2316 * '\0', as per ISO C99. If you want to have the exact
2317 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002318 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 * return is greater than or equal to @size, the resulting
2320 * string is truncated.
2321 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002322 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 */
2324int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002327 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002328 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002330 /* Reject out-of-range values early. Large positive sizes are
2331 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002332 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002336 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002338 /* Make sure end is always >= buf */
2339 if (end < buf) {
2340 end = ((void *)-1);
2341 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002344 while (*fmt) {
2345 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002346 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002347
2348 fmt += read;
2349
2350 switch (spec.type) {
2351 case FORMAT_TYPE_NONE: {
2352 int copy = read;
2353 if (str < end) {
2354 if (copy > end - str)
2355 copy = end - str;
2356 memcpy(str, old_fmt, copy);
2357 }
2358 str += read;
2359 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361
Vegard Nossumed681a92009-03-14 12:08:50 +01002362 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002363 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002364 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002366 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002367 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
André Goddard Rosad4be1512009-12-14 18:00:59 -08002370 case FORMAT_TYPE_CHAR: {
2371 char c;
2372
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002373 if (!(spec.flags & LEFT)) {
2374 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002375 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 *str = ' ';
2377 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002380 }
2381 c = (unsigned char) va_arg(args, int);
2382 if (str < end)
2383 *str = c;
2384 ++str;
2385 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002386 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002387 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002389 }
2390 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002393 case FORMAT_TYPE_STR:
2394 str = string(str, end, va_arg(args, char *), spec);
2395 break;
2396
2397 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002398 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002399 spec);
2400 while (isalnum(*fmt))
2401 fmt++;
2402 break;
2403
2404 case FORMAT_TYPE_PERCENT_CHAR:
2405 if (str < end)
2406 *str = '%';
2407 ++str;
2408 break;
2409
2410 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002411 /*
2412 * Presumably the arguments passed gcc's type
2413 * checking, but there is no safe or sane way
2414 * for us to continue parsing the format and
2415 * fetching from the va_list; the remaining
2416 * specifiers and arguments would be out of
2417 * sync.
2418 */
2419 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002420
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002421 default:
2422 switch (spec.type) {
2423 case FORMAT_TYPE_LONG_LONG:
2424 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002426 case FORMAT_TYPE_ULONG:
2427 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002429 case FORMAT_TYPE_LONG:
2430 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002432 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002433 if (spec.flags & SIGN)
2434 num = va_arg(args, ssize_t);
2435 else
2436 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002437 break;
2438 case FORMAT_TYPE_PTRDIFF:
2439 num = va_arg(args, ptrdiff_t);
2440 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002441 case FORMAT_TYPE_UBYTE:
2442 num = (unsigned char) va_arg(args, int);
2443 break;
2444 case FORMAT_TYPE_BYTE:
2445 num = (signed char) va_arg(args, int);
2446 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002447 case FORMAT_TYPE_USHORT:
2448 num = (unsigned short) va_arg(args, int);
2449 break;
2450 case FORMAT_TYPE_SHORT:
2451 num = (short) va_arg(args, int);
2452 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002453 case FORMAT_TYPE_INT:
2454 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002457 num = va_arg(args, unsigned int);
2458 }
2459
2460 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002463
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002464out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002465 if (size > 0) {
2466 if (str < end)
2467 *str = '\0';
2468 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002469 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002470 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002471
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002472 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476EXPORT_SYMBOL(vsnprintf);
2477
2478/**
2479 * vscnprintf - Format a string and place it in a buffer
2480 * @buf: The buffer to place the result into
2481 * @size: The size of the buffer, including the trailing null space
2482 * @fmt: The format string to use
2483 * @args: Arguments for the format string
2484 *
2485 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002486 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 * returns 0.
2488 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002489 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002490 *
2491 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 */
2493int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2494{
2495 int i;
2496
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002497 i = vsnprintf(buf, size, fmt, args);
2498
Anton Arapovb921c692011-01-12 16:59:49 -08002499 if (likely(i < size))
2500 return i;
2501 if (size != 0)
2502 return size - 1;
2503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505EXPORT_SYMBOL(vscnprintf);
2506
2507/**
2508 * snprintf - Format a string and place it in a buffer
2509 * @buf: The buffer to place the result into
2510 * @size: The size of the buffer, including the trailing null space
2511 * @fmt: The format string to use
2512 * @...: Arguments for the format string
2513 *
2514 * The return value is the number of characters which would be
2515 * generated for the given input, excluding the trailing null,
2516 * as per ISO C99. If the return is greater than or equal to
2517 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002518 *
2519 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002521int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 va_list args;
2524 int i;
2525
2526 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002527 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return i;
2531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532EXPORT_SYMBOL(snprintf);
2533
2534/**
2535 * scnprintf - Format a string and place it in a buffer
2536 * @buf: The buffer to place the result into
2537 * @size: The size of the buffer, including the trailing null space
2538 * @fmt: The format string to use
2539 * @...: Arguments for the format string
2540 *
2541 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002542 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 */
2544
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002545int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 va_list args;
2548 int i;
2549
2550 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002551 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002553
Anton Arapovb921c692011-01-12 16:59:49 -08002554 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556EXPORT_SYMBOL(scnprintf);
2557
2558/**
2559 * vsprintf - Format a string and place it in a buffer
2560 * @buf: The buffer to place the result into
2561 * @fmt: The format string to use
2562 * @args: Arguments for the format string
2563 *
2564 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002565 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 * buffer overflows.
2567 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002568 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002569 *
2570 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 */
2572int vsprintf(char *buf, const char *fmt, va_list args)
2573{
2574 return vsnprintf(buf, INT_MAX, fmt, args);
2575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576EXPORT_SYMBOL(vsprintf);
2577
2578/**
2579 * sprintf - Format a string and place it in a buffer
2580 * @buf: The buffer to place the result into
2581 * @fmt: The format string to use
2582 * @...: Arguments for the format string
2583 *
2584 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002585 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002587 *
2588 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002590int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 va_list args;
2593 int i;
2594
2595 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002596 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return i;
2600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601EXPORT_SYMBOL(sprintf);
2602
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002603#ifdef CONFIG_BINARY_PRINTF
2604/*
2605 * bprintf service:
2606 * vbin_printf() - VA arguments to binary data
2607 * bstr_printf() - Binary data to text string
2608 */
2609
2610/**
2611 * vbin_printf - Parse a format string and place args' binary value in a buffer
2612 * @bin_buf: The buffer to place args' binary value
2613 * @size: The size of the buffer(by words(32bits), not characters)
2614 * @fmt: The format string to use
2615 * @args: Arguments for the format string
2616 *
2617 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002618 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002619 *
2620 * The return value is the number of words(32bits) which would be generated for
2621 * the given input.
2622 *
2623 * NOTE:
2624 * If the return value is greater than @size, the resulting bin_buf is NOT
2625 * valid for bstr_printf().
2626 */
2627int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2628{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002629 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002630 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002631 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002632
2633 str = (char *)bin_buf;
2634 end = (char *)(bin_buf + size);
2635
2636#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002637({ \
2638 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002639 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002640 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002641 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002642 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002643 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002644 *(u32 *)str = *(u32 *)&val8; \
2645 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002646 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002647 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002648 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002649 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002650 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002651 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002652 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002653 *(typeof(type) *)str = (type)(long)val4; \
2654 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002655 } \
2656 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002657 value; \
2658})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002659
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002660 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002661 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002662
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002663 fmt += read;
2664
2665 switch (spec.type) {
2666 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002667 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002668 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002669 case FORMAT_TYPE_INVALID:
2670 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002671
Vegard Nossumed681a92009-03-14 12:08:50 +01002672 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002673 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002674 width = (int)save_arg(int);
2675 /* Pointers may require the width */
2676 if (*fmt == 'p')
2677 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002678 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002679
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002680 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002681 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002682 break;
2683
2684 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002685 const char *save_str = va_arg(args, char *);
2686 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002687
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002688 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2689 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002690 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002691 len = strlen(save_str) + 1;
2692 if (str + len < end)
2693 memcpy(str, save_str, len);
2694 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002695 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002696 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002697
2698 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002699 /* Dereferenced pointers must be done now */
2700 switch (*fmt) {
2701 /* Dereference of functions is still OK */
2702 case 'S':
2703 case 's':
2704 case 'F':
2705 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002706 case 'x':
2707 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002708 save_arg(void *);
2709 break;
2710 default:
2711 if (!isalnum(*fmt)) {
2712 save_arg(void *);
2713 break;
2714 }
2715 str = pointer(fmt, str, end, va_arg(args, void *),
2716 spec);
2717 if (str + 1 < end)
2718 *str++ = '\0';
2719 else
2720 end[-1] = '\0'; /* Must be nul terminated */
2721 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002722 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002723 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002724 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002725 break;
2726
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002727 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002728 switch (spec.type) {
2729
2730 case FORMAT_TYPE_LONG_LONG:
2731 save_arg(long long);
2732 break;
2733 case FORMAT_TYPE_ULONG:
2734 case FORMAT_TYPE_LONG:
2735 save_arg(unsigned long);
2736 break;
2737 case FORMAT_TYPE_SIZE_T:
2738 save_arg(size_t);
2739 break;
2740 case FORMAT_TYPE_PTRDIFF:
2741 save_arg(ptrdiff_t);
2742 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002743 case FORMAT_TYPE_UBYTE:
2744 case FORMAT_TYPE_BYTE:
2745 save_arg(char);
2746 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002747 case FORMAT_TYPE_USHORT:
2748 case FORMAT_TYPE_SHORT:
2749 save_arg(short);
2750 break;
2751 default:
2752 save_arg(int);
2753 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002754 }
2755 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002756
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002757out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002758 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002759#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002760}
2761EXPORT_SYMBOL_GPL(vbin_printf);
2762
2763/**
2764 * bstr_printf - Format a string from binary arguments and place it in a buffer
2765 * @buf: The buffer to place the result into
2766 * @size: The size of the buffer, including the trailing null space
2767 * @fmt: The format string to use
2768 * @bin_buf: Binary arguments for the format string
2769 *
2770 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2771 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2772 * a binary buffer that generated by vbin_printf.
2773 *
2774 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002775 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002776 *
2777 * The return value is the number of characters which would
2778 * be generated for the given input, excluding the trailing
2779 * '\0', as per ISO C99. If you want to have the exact
2780 * number of characters written into @buf as return value
2781 * (not including the trailing '\0'), use vscnprintf(). If the
2782 * return is greater than or equal to @size, the resulting
2783 * string is truncated.
2784 */
2785int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2786{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002787 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002788 char *str, *end;
2789 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002790
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002791 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002792 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002793
2794 str = buf;
2795 end = buf + size;
2796
2797#define get_arg(type) \
2798({ \
2799 typeof(type) value; \
2800 if (sizeof(type) == 8) { \
2801 args = PTR_ALIGN(args, sizeof(u32)); \
2802 *(u32 *)&value = *(u32 *)args; \
2803 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2804 } else { \
2805 args = PTR_ALIGN(args, sizeof(type)); \
2806 value = *(typeof(type) *)args; \
2807 } \
2808 args += sizeof(type); \
2809 value; \
2810})
2811
2812 /* Make sure end is always >= buf */
2813 if (end < buf) {
2814 end = ((void *)-1);
2815 size = end - buf;
2816 }
2817
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002818 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002819 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002820 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002821
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002822 fmt += read;
2823
2824 switch (spec.type) {
2825 case FORMAT_TYPE_NONE: {
2826 int copy = read;
2827 if (str < end) {
2828 if (copy > end - str)
2829 copy = end - str;
2830 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002831 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002832 str += read;
2833 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002834 }
2835
Vegard Nossumed681a92009-03-14 12:08:50 +01002836 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002837 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002838 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002839
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002840 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002841 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002842 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002843
André Goddard Rosad4be1512009-12-14 18:00:59 -08002844 case FORMAT_TYPE_CHAR: {
2845 char c;
2846
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002847 if (!(spec.flags & LEFT)) {
2848 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002849 if (str < end)
2850 *str = ' ';
2851 ++str;
2852 }
2853 }
2854 c = (unsigned char) get_arg(char);
2855 if (str < end)
2856 *str = c;
2857 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002858 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002859 if (str < end)
2860 *str = ' ';
2861 ++str;
2862 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002863 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002864 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002865
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002866 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002867 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002868 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002869 str = string(str, end, (char *)str_arg, spec);
2870 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002871 }
2872
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002873 case FORMAT_TYPE_PTR: {
2874 bool process = false;
2875 int copy, len;
2876 /* Non function dereferences were already done */
2877 switch (*fmt) {
2878 case 'S':
2879 case 's':
2880 case 'F':
2881 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002882 case 'x':
2883 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002884 process = true;
2885 break;
2886 default:
2887 if (!isalnum(*fmt)) {
2888 process = true;
2889 break;
2890 }
2891 /* Pointer dereference was already processed */
2892 if (str < end) {
2893 len = copy = strlen(args);
2894 if (copy > end - str)
2895 copy = end - str;
2896 memcpy(str, args, copy);
2897 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04002898 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002899 }
2900 }
2901 if (process)
2902 str = pointer(fmt, str, end, get_arg(void *), spec);
2903
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002904 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002905 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002906 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002907 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002908
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002909 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002910 if (str < end)
2911 *str = '%';
2912 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002913 break;
2914
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002915 case FORMAT_TYPE_INVALID:
2916 goto out;
2917
André Goddard Rosad4be1512009-12-14 18:00:59 -08002918 default: {
2919 unsigned long long num;
2920
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002921 switch (spec.type) {
2922
2923 case FORMAT_TYPE_LONG_LONG:
2924 num = get_arg(long long);
2925 break;
2926 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002927 case FORMAT_TYPE_LONG:
2928 num = get_arg(unsigned long);
2929 break;
2930 case FORMAT_TYPE_SIZE_T:
2931 num = get_arg(size_t);
2932 break;
2933 case FORMAT_TYPE_PTRDIFF:
2934 num = get_arg(ptrdiff_t);
2935 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002936 case FORMAT_TYPE_UBYTE:
2937 num = get_arg(unsigned char);
2938 break;
2939 case FORMAT_TYPE_BYTE:
2940 num = get_arg(signed char);
2941 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002942 case FORMAT_TYPE_USHORT:
2943 num = get_arg(unsigned short);
2944 break;
2945 case FORMAT_TYPE_SHORT:
2946 num = get_arg(short);
2947 break;
2948 case FORMAT_TYPE_UINT:
2949 num = get_arg(unsigned int);
2950 break;
2951 default:
2952 num = get_arg(int);
2953 }
2954
2955 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002956 } /* default: */
2957 } /* switch(spec.type) */
2958 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002959
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002960out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002961 if (size > 0) {
2962 if (str < end)
2963 *str = '\0';
2964 else
2965 end[-1] = '\0';
2966 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002967
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002968#undef get_arg
2969
2970 /* the trailing null byte doesn't count towards the total */
2971 return str - buf;
2972}
2973EXPORT_SYMBOL_GPL(bstr_printf);
2974
2975/**
2976 * bprintf - Parse a format string and place args' binary value in a buffer
2977 * @bin_buf: The buffer to place args' binary value
2978 * @size: The size of the buffer(by words(32bits), not characters)
2979 * @fmt: The format string to use
2980 * @...: Arguments for the format string
2981 *
2982 * The function returns the number of words(u32) written
2983 * into @bin_buf.
2984 */
2985int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2986{
2987 va_list args;
2988 int ret;
2989
2990 va_start(args, fmt);
2991 ret = vbin_printf(bin_buf, size, fmt, args);
2992 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002993
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002994 return ret;
2995}
2996EXPORT_SYMBOL_GPL(bprintf);
2997
2998#endif /* CONFIG_BINARY_PRINTF */
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000/**
3001 * vsscanf - Unformat a buffer into a list of arguments
3002 * @buf: input buffer
3003 * @fmt: format of buffer
3004 * @args: arguments
3005 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003006int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 const char *str = buf;
3009 char *next;
3010 char digit;
3011 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003012 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003013 unsigned int base;
3014 union {
3015 long long s;
3016 unsigned long long u;
3017 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003018 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003019 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Jan Beulichda990752012-10-04 17:13:24 -07003021 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 /* skip any white space in format */
3023 /* white space in format matchs any amount of
3024 * white space, including none, in the input.
3025 */
3026 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003027 fmt = skip_spaces(++fmt);
3028 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030
3031 /* anything that is not a conversion must match exactly */
3032 if (*fmt != '%' && *fmt) {
3033 if (*fmt++ != *str++)
3034 break;
3035 continue;
3036 }
3037
3038 if (!*fmt)
3039 break;
3040 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 /* skip this conversion.
3043 * advance both strings to next white space
3044 */
3045 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003046 if (!*str)
3047 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003048 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3049 /* '%*[' not yet supported, invalid format */
3050 if (*fmt == '[')
3051 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 while (!isspace(*str) && *str)
3055 str++;
3056 continue;
3057 }
3058
3059 /* get field width */
3060 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003061 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003063 if (field_width <= 0)
3064 break;
3065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 /* get conversion qualifier */
3068 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003069 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003070 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 qualifier = *fmt++;
3072 if (unlikely(qualifier == *fmt)) {
3073 if (qualifier == 'h') {
3074 qualifier = 'H';
3075 fmt++;
3076 } else if (qualifier == 'l') {
3077 qualifier = 'L';
3078 fmt++;
3079 }
3080 }
3081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Jan Beulichda990752012-10-04 17:13:24 -07003083 if (!*fmt)
3084 break;
3085
3086 if (*fmt == 'n') {
3087 /* return number of characters read so far */
3088 *va_arg(args, int *) = str - buf;
3089 ++fmt;
3090 continue;
3091 }
3092
3093 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 break;
3095
André Goddard Rosad4be1512009-12-14 18:00:59 -08003096 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003097 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003098
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003099 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 case 'c':
3101 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003102 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (field_width == -1)
3104 field_width = 1;
3105 do {
3106 *s++ = *str++;
3107 } while (--field_width > 0 && *str);
3108 num++;
3109 }
3110 continue;
3111 case 's':
3112 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003113 char *s = (char *)va_arg(args, char *);
3114 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003115 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003117 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003120 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 *s = '\0';
3123 num++;
3124 }
3125 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003126 /*
3127 * Warning: This implementation of the '[' conversion specifier
3128 * deviates from its glibc counterpart in the following ways:
3129 * (1) It does NOT support ranges i.e. '-' is NOT a special
3130 * character
3131 * (2) It cannot match the closing bracket ']' itself
3132 * (3) A field width is required
3133 * (4) '%*[' (discard matching input) is currently not supported
3134 *
3135 * Example usage:
3136 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3137 * buf1, buf2, buf3);
3138 * if (ret < 3)
3139 * // etc..
3140 */
3141 case '[':
3142 {
3143 char *s = (char *)va_arg(args, char *);
3144 DECLARE_BITMAP(set, 256) = {0};
3145 unsigned int len = 0;
3146 bool negate = (*fmt == '^');
3147
3148 /* field width is required */
3149 if (field_width == -1)
3150 return num;
3151
3152 if (negate)
3153 ++fmt;
3154
3155 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3156 set_bit((u8)*fmt, set);
3157
3158 /* no ']' or no character set found */
3159 if (!*fmt || !len)
3160 return num;
3161 ++fmt;
3162
3163 if (negate) {
3164 bitmap_complement(set, set, 256);
3165 /* exclude null '\0' byte */
3166 clear_bit(0, set);
3167 }
3168
3169 /* match must be non-empty */
3170 if (!test_bit((u8)*str, set))
3171 return num;
3172
3173 while (test_bit((u8)*str, set) && field_width--)
3174 *s++ = *str++;
3175 *s = '\0';
3176 ++num;
3177 }
3178 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 case 'o':
3180 base = 8;
3181 break;
3182 case 'x':
3183 case 'X':
3184 base = 16;
3185 break;
3186 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003187 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003188 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003190 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003191 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 case 'u':
3193 break;
3194 case '%':
3195 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003196 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return num;
3198 continue;
3199 default:
3200 /* invalid format; stop here */
3201 return num;
3202 }
3203
3204 /* have some sort of integer conversion.
3205 * first, skip white space in buffer.
3206 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003207 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 digit = *str;
3210 if (is_sign && digit == '-')
3211 digit = *(str + 1);
3212
3213 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003214 || (base == 16 && !isxdigit(digit))
3215 || (base == 10 && !isdigit(digit))
3216 || (base == 8 && (!isdigit(digit) || digit > '7'))
3217 || (base == 0 && !isdigit(digit)))
3218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Jan Beulich53809752012-12-17 16:01:31 -08003220 if (is_sign)
3221 val.s = qualifier != 'L' ?
3222 simple_strtol(str, &next, base) :
3223 simple_strtoll(str, &next, base);
3224 else
3225 val.u = qualifier != 'L' ?
3226 simple_strtoul(str, &next, base) :
3227 simple_strtoull(str, &next, base);
3228
3229 if (field_width > 0 && next - str > field_width) {
3230 if (base == 0)
3231 _parse_integer_fixup_radix(str, &base);
3232 while (next - str > field_width) {
3233 if (is_sign)
3234 val.s = div_s64(val.s, base);
3235 else
3236 val.u = div_u64(val.u, base);
3237 --next;
3238 }
3239 }
3240
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003241 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003243 if (is_sign)
3244 *va_arg(args, signed char *) = val.s;
3245 else
3246 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 break;
3248 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003249 if (is_sign)
3250 *va_arg(args, short *) = val.s;
3251 else
3252 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 break;
3254 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003255 if (is_sign)
3256 *va_arg(args, long *) = val.s;
3257 else
3258 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 break;
3260 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003261 if (is_sign)
3262 *va_arg(args, long long *) = val.s;
3263 else
3264 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003267 *va_arg(args, size_t *) = val.u;
3268 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 default:
Jan Beulich53809752012-12-17 16:01:31 -08003270 if (is_sign)
3271 *va_arg(args, int *) = val.s;
3272 else
3273 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 break;
3275 }
3276 num++;
3277
3278 if (!next)
3279 break;
3280 str = next;
3281 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return num;
3284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285EXPORT_SYMBOL(vsscanf);
3286
3287/**
3288 * sscanf - Unformat a buffer into a list of arguments
3289 * @buf: input buffer
3290 * @fmt: formatting of buffer
3291 * @...: resulting arguments
3292 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003293int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
3295 va_list args;
3296 int i;
3297
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003298 va_start(args, fmt);
3299 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return i;
3303}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304EXPORT_SYMBOL(sscanf);