blob: d69452a0f2fa506917ec6ec8e5b6a7fca52ca5aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070020#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070021#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050022#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ctype.h>
26#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070027#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080028#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070029#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110030#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040031#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080032#include <linux/cred.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070033#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020034#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000035#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110036#include <linux/siphash.h>
37#include <linux/compiler.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040038#ifdef CONFIG_BLOCK
39#include <linux/blkdev.h>
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070042#include "../mm/internal.h" /* For the trace_print_flags arrays */
43
Tim Schmielau4e57b682005-10-30 15:03:48 -080044#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050045#include <asm/sections.h> /* for dereference_function_descriptor() */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070046#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andy Shevchenko71dca952014-10-13 15:55:18 -070048#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070049#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * simple_strtoull - convert a string to an unsigned long long
53 * @cp: The start of the string
54 * @endp: A pointer to the end of the parsed string will be placed here
55 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080056 *
57 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
Harvey Harrison22d27052008-10-16 13:40:35 -070059unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070061 unsigned long long result;
62 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070064 cp = _parse_integer_fixup_radix(cp, &base);
65 rv = _parse_integer(cp, base, &result);
66 /* FIXME */
67 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (endp)
70 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return result;
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074EXPORT_SYMBOL(simple_strtoull);
75
76/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080077 * simple_strtoul - convert a string to an unsigned long
78 * @cp: The start of the string
79 * @endp: A pointer to the end of the parsed string will be placed here
80 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080081 *
82 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080083 */
84unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
85{
86 return simple_strtoull(cp, endp, base);
87}
88EXPORT_SYMBOL(simple_strtoul);
89
90/**
91 * simple_strtol - convert a string to a signed long
92 * @cp: The start of the string
93 * @endp: A pointer to the end of the parsed string will be placed here
94 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080095 *
96 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080097 */
98long simple_strtol(const char *cp, char **endp, unsigned int base)
99{
100 if (*cp == '-')
101 return -simple_strtoul(cp + 1, endp, base);
102
103 return simple_strtoul(cp, endp, base);
104}
105EXPORT_SYMBOL(simple_strtol);
106
107/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * simple_strtoll - convert a string to a signed long long
109 * @cp: The start of the string
110 * @endp: A pointer to the end of the parsed string will be placed here
111 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800112 *
113 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700115long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800117 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700118 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800119
Harvey Harrison22d27052008-10-16 13:40:35 -0700120 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400122EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Joe Perchescf3b4292010-05-24 14:33:16 -0700124static noinline_for_stack
125int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800127 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800129 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800131 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return i;
134}
135
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700136/*
137 * Decimal conversion is by far the most typical, and is used for
138 * /proc and /sys data. This directly impacts e.g. top performance
139 * with many processes running. We optimize it for speed by emitting
140 * two characters at a time, using a 200 byte lookup table. This
141 * roughly halves the number of multiplications compared to computing
142 * the digits one at a time. Implementation strongly inspired by the
143 * previous version, which in turn used ideas described at
144 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
145 * from the author, Douglas W. Jones).
146 *
147 * It turns out there is precisely one 26 bit fixed-point
148 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
149 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
150 * range happens to be somewhat larger (x <= 1073741898), but that's
151 * irrelevant for our purpose.
152 *
153 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
154 * need a 32x32->64 bit multiply, so we simply use the same constant.
155 *
156 * For dividing a number in the range [100, 10^4-1] by 100, there are
157 * several options. The simplest is (x * 0x147b) >> 19, which is valid
158 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700159 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700160
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700161static const u16 decpair[100] = {
162#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
163 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
164 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
165 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
166 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
167 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
168 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
169 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
170 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
171 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
172 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
173#undef _
174};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700175
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700176/*
177 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700178 * immediately jump to out_r where two 0s would be written but only
179 * one of them accounted for in buf. This is needed by ip4_string
180 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700181*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700182static noinline_for_stack
183char *put_dec_trunc8(char *buf, unsigned r)
184{
185 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700186
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700187 /* 1 <= r < 10^8 */
188 if (r < 100)
189 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700190
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700191 /* 100 <= r < 10^8 */
192 q = (r * (u64)0x28f5c29) >> 32;
193 *((u16 *)buf) = decpair[r - 100*q];
194 buf += 2;
195
196 /* 1 <= q < 10^6 */
197 if (q < 100)
198 goto out_q;
199
200 /* 100 <= q < 10^6 */
201 r = (q * (u64)0x28f5c29) >> 32;
202 *((u16 *)buf) = decpair[q - 100*r];
203 buf += 2;
204
205 /* 1 <= r < 10^4 */
206 if (r < 100)
207 goto out_r;
208
209 /* 100 <= r < 10^4 */
210 q = (r * 0x147b) >> 19;
211 *((u16 *)buf) = decpair[r - 100*q];
212 buf += 2;
213out_q:
214 /* 1 <= q < 100 */
215 r = q;
216out_r:
217 /* 1 <= r < 100 */
218 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700219 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700220 return buf;
221}
222
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700223#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
224static noinline_for_stack
225char *put_dec_full8(char *buf, unsigned r)
226{
227 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700228
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700229 /* 0 <= r < 10^8 */
230 q = (r * (u64)0x28f5c29) >> 32;
231 *((u16 *)buf) = decpair[r - 100*q];
232 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700233
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700234 /* 0 <= q < 10^6 */
235 r = (q * (u64)0x28f5c29) >> 32;
236 *((u16 *)buf) = decpair[q - 100*r];
237 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700238
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700239 /* 0 <= r < 10^4 */
240 q = (r * 0x147b) >> 19;
241 *((u16 *)buf) = decpair[r - 100*q];
242 buf += 2;
243
244 /* 0 <= q < 100 */
245 *((u16 *)buf) = decpair[q];
246 buf += 2;
247 return buf;
248}
249
250static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700251char *put_dec(char *buf, unsigned long long n)
252{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700253 if (n >= 100*1000*1000)
254 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
255 /* 1 <= n <= 1.6e11 */
256 if (n >= 100*1000*1000)
257 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
258 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700259 return put_dec_trunc8(buf, n);
260}
261
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700262#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700263
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700264static void
265put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700266{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700267 unsigned q;
268
269 /* 0 <= r < 10^4 */
270 q = (r * 0x147b) >> 19;
271 *((u16 *)buf) = decpair[r - 100*q];
272 buf += 2;
273 /* 0 <= q < 100 */
274 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700275}
276
277/*
278 * Call put_dec_full4 on x % 10000, return x / 10000.
279 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
280 * holds for all x < 1,128,869,999. The largest value this
281 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700282 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700283 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700284static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700285unsigned put_dec_helper4(char *buf, unsigned x)
286{
287 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
288
289 put_dec_full4(buf, x - q * 10000);
290 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700291}
292
293/* Based on code by Douglas W. Jones found at
294 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
295 * (with permission from the author).
296 * Performs no 64-bit division and hence should be fast on 32-bit machines.
297 */
298static
299char *put_dec(char *buf, unsigned long long n)
300{
301 uint32_t d3, d2, d1, q, h;
302
303 if (n < 100*1000*1000)
304 return put_dec_trunc8(buf, n);
305
306 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
307 h = (n >> 32);
308 d2 = (h ) & 0xffff;
309 d3 = (h >> 16); /* implicit "& 0xffff" */
310
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700311 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
312 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700313 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700314 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700315
George Spelvin23591722012-10-04 17:12:29 -0700316 q += 7671 * d3 + 9496 * d2 + 6 * d1;
317 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700318
George Spelvin23591722012-10-04 17:12:29 -0700319 q += 4749 * d3 + 42 * d2;
320 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700321
George Spelvin23591722012-10-04 17:12:29 -0700322 q += 281 * d3;
323 buf += 12;
324 if (q)
325 buf = put_dec_trunc8(buf, q);
326 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700327 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800328
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700329 return buf;
330}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700331
332#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700333
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700334/*
335 * Convert passed number to decimal string.
336 * Returns the length of string. On buffer overflow, returns 0.
337 *
338 * If speed is not important, use snprintf(). It's easy to read the code.
339 */
340int num_to_str(char *buf, int size, unsigned long long num)
341{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700342 /* put_dec requires 2-byte alignment of the buffer. */
343 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700344 int idx, len;
345
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700346 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
347 if (num <= 9) {
348 tmp[0] = '0' + num;
349 len = 1;
350 } else {
351 len = put_dec(tmp, num) - tmp;
352 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700353
354 if (len > size)
355 return 0;
356 for (idx = 0; idx < len; ++idx)
357 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700358 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700359}
360
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700361#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700362#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#define PLUS 4 /* show plus */
364#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700365#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700366#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
367#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100369enum format_type {
370 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100371 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100372 FORMAT_TYPE_PRECISION,
373 FORMAT_TYPE_CHAR,
374 FORMAT_TYPE_STR,
375 FORMAT_TYPE_PTR,
376 FORMAT_TYPE_PERCENT_CHAR,
377 FORMAT_TYPE_INVALID,
378 FORMAT_TYPE_LONG_LONG,
379 FORMAT_TYPE_ULONG,
380 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800381 FORMAT_TYPE_UBYTE,
382 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100383 FORMAT_TYPE_USHORT,
384 FORMAT_TYPE_SHORT,
385 FORMAT_TYPE_UINT,
386 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100387 FORMAT_TYPE_SIZE_T,
388 FORMAT_TYPE_PTRDIFF
389};
390
391struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800392 unsigned int type:8; /* format_type enum */
393 signed int field_width:24; /* width of output field */
394 unsigned int flags:8; /* flags to number() */
395 unsigned int base:8; /* number base, 8, 10 or 16 only */
396 signed int precision:16; /* # of digits/chars */
397} __packed;
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800398#define FIELD_WIDTH_MAX ((1 << 23) - 1)
399#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100400
Joe Perchescf3b4292010-05-24 14:33:16 -0700401static noinline_for_stack
402char *number(char *buf, char *end, unsigned long long num,
403 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700405 /* put_dec requires 2-byte alignment of the buffer. */
406 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100407 char sign;
408 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100409 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700411 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800412 int field_width = spec.field_width;
413 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800415 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
416
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100417 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
418 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100419 locase = (spec.flags & SMALL);
420 if (spec.flags & LEFT)
421 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100423 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800424 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800426 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800427 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100428 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800430 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100431 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800433 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700436 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100437 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800438 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700439 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800440 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700442
443 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700445 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700446 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100447 else if (spec.base != 10) { /* 8 or 16 */
448 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700449 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800450
451 if (spec.base == 16)
452 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700453 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700454 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700455 num >>= shift;
456 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700457 } else { /* base 10 */
458 i = put_dec(tmp, num) - tmp;
459 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460
461 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800462 if (i > precision)
463 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700464 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800465 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700466 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800467 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700468 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *buf = ' ';
470 ++buf;
471 }
472 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700473 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700475 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *buf = sign;
477 ++buf;
478 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 /* "0x" / "0" prefix */
480 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700481 if (spec.base == 16 || !is_zero) {
482 if (buf < end)
483 *buf = '0';
484 ++buf;
485 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100486 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700487 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100488 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ++buf;
490 }
491 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700492 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100493 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700494 char c = ' ' + (spec.flags & ZEROPAD);
495 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800496 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700497 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 *buf = c;
499 ++buf;
500 }
501 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700502 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800503 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700504 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *buf = '0';
506 ++buf;
507 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700508 /* actual digits of result */
509 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700510 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *buf = tmp[i];
512 ++buf;
513 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700514 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800515 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700516 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *buf = ' ';
518 ++buf;
519 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return buf;
522}
523
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800524static noinline_for_stack
525char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
526{
527 struct printf_spec spec;
528
529 spec.type = FORMAT_TYPE_PTR;
530 spec.field_width = 2 + 2 * size; /* 0x + hex */
531 spec.flags = SPECIAL | SMALL | ZEROPAD;
532 spec.base = 16;
533 spec.precision = -1;
534
535 return number(buf, end, num, spec);
536}
537
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800538static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400539{
540 size_t size;
541 if (buf >= end) /* nowhere to put anything */
542 return;
543 size = end - buf;
544 if (size <= spaces) {
545 memset(buf, ' ', size);
546 return;
547 }
548 if (len) {
549 if (len > size - spaces)
550 len = size - spaces;
551 memmove(buf + spaces, buf, len);
552 }
553 memset(buf, ' ', spaces);
554}
555
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800556/*
557 * Handle field width padding for a string.
558 * @buf: current buffer position
559 * @n: length of string
560 * @end: end of output buffer
561 * @spec: for field width and flags
562 * Returns: new buffer position after padding.
563 */
564static noinline_for_stack
565char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
566{
567 unsigned spaces;
568
569 if (likely(n >= spec.field_width))
570 return buf;
571 /* we want to pad the sucker */
572 spaces = spec.field_width - n;
573 if (!(spec.flags & LEFT)) {
574 move_right(buf - n, end, n, spaces);
575 return buf + spaces;
576 }
577 while (spaces--) {
578 if (buf < end)
579 *buf = ' ';
580 ++buf;
581 }
582 return buf;
583}
584
Al Viro4b6ccca2013-09-03 12:00:44 -0400585static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800586char *string(char *buf, char *end, const char *s, struct printf_spec spec)
587{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800588 int len = 0;
589 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800590
591 if ((unsigned long)s < PAGE_SIZE)
592 s = "(null)";
593
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800594 while (lim--) {
595 char c = *s++;
596 if (!c)
597 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800598 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800599 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800600 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800601 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800602 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800603 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800604}
605
606static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400607char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
608 const char *fmt)
609{
610 const char *array[4], *s;
611 const struct dentry *p;
612 int depth;
613 int i, n;
614
615 switch (fmt[1]) {
616 case '2': case '3': case '4':
617 depth = fmt[1] - '0';
618 break;
619 default:
620 depth = 1;
621 }
622
623 rcu_read_lock();
624 for (i = 0; i < depth; i++, d = p) {
Mark Rutland6aa7de02017-10-23 14:07:29 -0700625 p = READ_ONCE(d->d_parent);
626 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400627 if (p == d) {
628 if (i)
629 array[i] = "";
630 i++;
631 break;
632 }
633 }
634 s = array[--i];
635 for (n = 0; n != spec.precision; n++, buf++) {
636 char c = *s++;
637 if (!c) {
638 if (!i)
639 break;
640 c = '/';
641 s = array[--i];
642 }
643 if (buf < end)
644 *buf = c;
645 }
646 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800647 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400648}
649
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400650#ifdef CONFIG_BLOCK
651static noinline_for_stack
652char *bdev_name(char *buf, char *end, struct block_device *bdev,
653 struct printf_spec spec, const char *fmt)
654{
655 struct gendisk *hd = bdev->bd_disk;
656
657 buf = string(buf, end, hd->disk_name, spec);
658 if (bdev->bd_part->partno) {
659 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
660 if (buf < end)
661 *buf = 'p';
662 buf++;
663 }
664 buf = number(buf, end, bdev->bd_part->partno, spec);
665 }
666 return buf;
667}
668#endif
669
Joe Perchescf3b4292010-05-24 14:33:16 -0700670static noinline_for_stack
671char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800672 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700673{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800674 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700675#ifdef CONFIG_KALLSYMS
676 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800677#endif
678
679 if (fmt[1] == 'R')
680 ptr = __builtin_extract_return_addr(ptr);
681 value = (unsigned long)ptr;
682
683#ifdef CONFIG_KALLSYMS
684 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900685 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800686 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200687 sprint_symbol(sym, value);
688 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700689 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800690
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100691 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700692#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800693 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700694#endif
695}
696
Joe Perchescf3b4292010-05-24 14:33:16 -0700697static noinline_for_stack
698char *resource_string(char *buf, char *end, struct resource *res,
699 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100700{
701#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600702#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100703#endif
704
705#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600706#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100707#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700708 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100709 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700710 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100711 .precision = -1,
712 .flags = SPECIAL | SMALL | ZEROPAD,
713 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700714 static const struct printf_spec mem_spec = {
715 .base = 16,
716 .field_width = MEM_RSRC_PRINTK_SIZE,
717 .precision = -1,
718 .flags = SPECIAL | SMALL | ZEROPAD,
719 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700720 static const struct printf_spec bus_spec = {
721 .base = 16,
722 .field_width = 2,
723 .precision = -1,
724 .flags = SMALL | ZEROPAD,
725 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700726 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600727 .base = 10,
728 .precision = -1,
729 .flags = 0,
730 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700731 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600732 .field_width = -1,
733 .precision = 10,
734 .flags = LEFT,
735 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700736 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600737 .base = 16,
738 .precision = -1,
739 .flags = SPECIAL | SMALL,
740 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600741
742 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
743 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
744#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
745#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700746#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600747#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
748 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
749 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
750
Linus Torvalds332d2e72008-10-20 15:07:34 +1100751 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600752 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700753 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100754
755 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700756 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600757 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700758 specp = &io_spec;
759 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600760 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700761 specp = &mem_spec;
762 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600763 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700764 specp = &dec_spec;
765 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600766 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700767 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700768 } else if (res->flags & IORESOURCE_BUS) {
769 p = string(p, pend, "bus ", str_spec);
770 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700771 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600772 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700773 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600774 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600775 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700776 if (decode && res->flags & IORESOURCE_UNSET) {
777 p = string(p, pend, "size ", str_spec);
778 p = number(p, pend, resource_size(res), *specp);
779 } else {
780 p = number(p, pend, res->start, *specp);
781 if (res->start != res->end) {
782 *p++ = '-';
783 p = number(p, pend, res->end, *specp);
784 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600785 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600786 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600787 if (res->flags & IORESOURCE_MEM_64)
788 p = string(p, pend, " 64bit", str_spec);
789 if (res->flags & IORESOURCE_PREFETCH)
790 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700791 if (res->flags & IORESOURCE_WINDOW)
792 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600793 if (res->flags & IORESOURCE_DISABLED)
794 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600795 } else {
796 p = string(p, pend, " flags ", str_spec);
797 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600798 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100799 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600800 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100801
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100802 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100803}
804
Joe Perchescf3b4292010-05-24 14:33:16 -0700805static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700806char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
807 const char *fmt)
808{
Steven Rostedt360603a2013-05-28 15:47:39 -0400809 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700810 negative value, fallback to the default */
811 char separator;
812
813 if (spec.field_width == 0)
814 /* nothing to print */
815 return buf;
816
817 if (ZERO_OR_NULL_PTR(addr))
818 /* NULL pointer */
819 return string(buf, end, NULL, spec);
820
821 switch (fmt[1]) {
822 case 'C':
823 separator = ':';
824 break;
825 case 'D':
826 separator = '-';
827 break;
828 case 'N':
829 separator = 0;
830 break;
831 default:
832 separator = ' ';
833 break;
834 }
835
836 if (spec.field_width > 0)
837 len = min_t(int, spec.field_width, 64);
838
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700839 for (i = 0; i < len; ++i) {
840 if (buf < end)
841 *buf = hex_asc_hi(addr[i]);
842 ++buf;
843 if (buf < end)
844 *buf = hex_asc_lo(addr[i]);
845 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700846
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700847 if (separator && i != len - 1) {
848 if (buf < end)
849 *buf = separator;
850 ++buf;
851 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700852 }
853
854 return buf;
855}
856
857static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800858char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
859 struct printf_spec spec, const char *fmt)
860{
861 const int CHUNKSZ = 32;
862 int nr_bits = max_t(int, spec.field_width, 0);
863 int i, chunksz;
864 bool first = true;
865
866 /* reused to print numbers */
867 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
868
869 chunksz = nr_bits & (CHUNKSZ - 1);
870 if (chunksz == 0)
871 chunksz = CHUNKSZ;
872
873 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
874 for (; i >= 0; i -= CHUNKSZ) {
875 u32 chunkmask, val;
876 int word, bit;
877
878 chunkmask = ((1ULL << chunksz) - 1);
879 word = i / BITS_PER_LONG;
880 bit = i % BITS_PER_LONG;
881 val = (bitmap[word] >> bit) & chunkmask;
882
883 if (!first) {
884 if (buf < end)
885 *buf = ',';
886 buf++;
887 }
888 first = false;
889
890 spec.field_width = DIV_ROUND_UP(chunksz, 4);
891 buf = number(buf, end, val, spec);
892
893 chunksz = CHUNKSZ;
894 }
895 return buf;
896}
897
898static noinline_for_stack
899char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
900 struct printf_spec spec, const char *fmt)
901{
902 int nr_bits = max_t(int, spec.field_width, 0);
903 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
904 int cur, rbot, rtop;
905 bool first = true;
906
907 /* reused to print numbers */
908 spec = (struct printf_spec){ .base = 10 };
909
910 rbot = cur = find_first_bit(bitmap, nr_bits);
911 while (cur < nr_bits) {
912 rtop = cur;
913 cur = find_next_bit(bitmap, nr_bits, cur + 1);
914 if (cur < nr_bits && cur <= rtop + 1)
915 continue;
916
917 if (!first) {
918 if (buf < end)
919 *buf = ',';
920 buf++;
921 }
922 first = false;
923
924 buf = number(buf, end, rbot, spec);
925 if (rbot < rtop) {
926 if (buf < end)
927 *buf = '-';
928 buf++;
929
930 buf = number(buf, end, rtop, spec);
931 }
932
933 rbot = cur;
934 }
935 return buf;
936}
937
938static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700939char *mac_address_string(char *buf, char *end, u8 *addr,
940 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700941{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000942 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700943 char *p = mac_addr;
944 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000945 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700946 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000947
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700948 switch (fmt[1]) {
949 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000950 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700951 break;
952
953 case 'R':
954 reversed = true;
955 /* fall through */
956
957 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000958 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700959 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000960 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700961
962 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700963 if (reversed)
964 p = hex_byte_pack(p, addr[5 - i]);
965 else
966 p = hex_byte_pack(p, addr[i]);
967
Joe Perches8a27f7c2009-08-17 12:29:44 +0000968 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000969 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700970 }
971 *p = '\0';
972
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100973 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700974}
975
Joe Perchescf3b4292010-05-24 14:33:16 -0700976static noinline_for_stack
977char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700978{
Harvey Harrison689afa72008-10-28 16:04:44 -0700979 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800980 bool leading_zeros = (fmt[0] == 'i');
981 int index;
982 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700983
Joe Perches0159f242010-01-13 20:23:30 -0800984 switch (fmt[2]) {
985 case 'h':
986#ifdef __BIG_ENDIAN
987 index = 0;
988 step = 1;
989#else
990 index = 3;
991 step = -1;
992#endif
993 break;
994 case 'l':
995 index = 3;
996 step = -1;
997 break;
998 case 'n':
999 case 'b':
1000 default:
1001 index = 0;
1002 step = 1;
1003 break;
1004 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001005 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001006 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001007 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001008 if (leading_zeros) {
1009 if (digits < 3)
1010 *p++ = '0';
1011 if (digits < 2)
1012 *p++ = '0';
1013 }
1014 /* reverse the digits in the quad */
1015 while (digits--)
1016 *p++ = temp[digits];
1017 if (i < 3)
1018 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001019 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001020 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001021 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001022
Joe Perches8a27f7c2009-08-17 12:29:44 +00001023 return p;
1024}
1025
Joe Perchescf3b4292010-05-24 14:33:16 -07001026static noinline_for_stack
1027char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001028{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001029 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001030 unsigned char zerolength[8];
1031 int longest = 1;
1032 int colonpos = -1;
1033 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001034 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001035 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001036 bool useIPv4;
1037 struct in6_addr in6;
1038
1039 memcpy(&in6, addr, sizeof(struct in6_addr));
1040
1041 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001042
1043 memset(zerolength, 0, sizeof(zerolength));
1044
1045 if (useIPv4)
1046 range = 6;
1047 else
1048 range = 8;
1049
1050 /* find position of longest 0 run */
1051 for (i = 0; i < range; i++) {
1052 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001053 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001054 break;
1055 zerolength[i]++;
1056 }
1057 }
1058 for (i = 0; i < range; i++) {
1059 if (zerolength[i] > longest) {
1060 longest = zerolength[i];
1061 colonpos = i;
1062 }
1063 }
Joe Perches29cf5192011-06-09 11:23:37 -07001064 if (longest == 1) /* don't compress a single 0 */
1065 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001066
1067 /* emit address */
1068 for (i = 0; i < range; i++) {
1069 if (i == colonpos) {
1070 if (needcolon || i == 0)
1071 *p++ = ':';
1072 *p++ = ':';
1073 needcolon = false;
1074 i += longest - 1;
1075 continue;
1076 }
1077 if (needcolon) {
1078 *p++ = ':';
1079 needcolon = false;
1080 }
1081 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001082 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001083 hi = word >> 8;
1084 lo = word & 0xff;
1085 if (hi) {
1086 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001087 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001088 else
1089 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001090 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001091 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001092 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001093 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001094 else
1095 *p++ = hex_asc_lo(lo);
1096 needcolon = true;
1097 }
1098
1099 if (useIPv4) {
1100 if (needcolon)
1101 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001102 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001103 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001104 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001105
Joe Perches8a27f7c2009-08-17 12:29:44 +00001106 return p;
1107}
1108
Joe Perchescf3b4292010-05-24 14:33:16 -07001109static noinline_for_stack
1110char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001111{
1112 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001113
Harvey Harrison689afa72008-10-28 16:04:44 -07001114 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001115 p = hex_byte_pack(p, *addr++);
1116 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001117 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001118 *p++ = ':';
1119 }
1120 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001121
Joe Perches8a27f7c2009-08-17 12:29:44 +00001122 return p;
1123}
1124
Joe Perchescf3b4292010-05-24 14:33:16 -07001125static noinline_for_stack
1126char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1127 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001128{
1129 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1130
1131 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001132 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001133 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001134 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001135
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001136 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001137}
1138
Joe Perchescf3b4292010-05-24 14:33:16 -07001139static noinline_for_stack
1140char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1141 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001142{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001143 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001144
Joe Perches0159f242010-01-13 20:23:30 -08001145 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001146
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001147 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001148}
1149
Joe Perchescf3b4292010-05-24 14:33:16 -07001150static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001151char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1152 struct printf_spec spec, const char *fmt)
1153{
1154 bool have_p = false, have_s = false, have_f = false, have_c = false;
1155 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1156 sizeof(":12345") + sizeof("/123456789") +
1157 sizeof("%1234567890")];
1158 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1159 const u8 *addr = (const u8 *) &sa->sin6_addr;
1160 char fmt6[2] = { fmt[0], '6' };
1161 u8 off = 0;
1162
1163 fmt++;
1164 while (isalpha(*++fmt)) {
1165 switch (*fmt) {
1166 case 'p':
1167 have_p = true;
1168 break;
1169 case 'f':
1170 have_f = true;
1171 break;
1172 case 's':
1173 have_s = true;
1174 break;
1175 case 'c':
1176 have_c = true;
1177 break;
1178 }
1179 }
1180
1181 if (have_p || have_s || have_f) {
1182 *p = '[';
1183 off = 1;
1184 }
1185
1186 if (fmt6[0] == 'I' && have_c)
1187 p = ip6_compressed_string(ip6_addr + off, addr);
1188 else
1189 p = ip6_string(ip6_addr + off, addr, fmt6);
1190
1191 if (have_p || have_s || have_f)
1192 *p++ = ']';
1193
1194 if (have_p) {
1195 *p++ = ':';
1196 p = number(p, pend, ntohs(sa->sin6_port), spec);
1197 }
1198 if (have_f) {
1199 *p++ = '/';
1200 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1201 IPV6_FLOWINFO_MASK), spec);
1202 }
1203 if (have_s) {
1204 *p++ = '%';
1205 p = number(p, pend, sa->sin6_scope_id, spec);
1206 }
1207 *p = '\0';
1208
1209 return string(buf, end, ip6_addr, spec);
1210}
1211
1212static noinline_for_stack
1213char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1214 struct printf_spec spec, const char *fmt)
1215{
1216 bool have_p = false;
1217 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1218 char *pend = ip4_addr + sizeof(ip4_addr);
1219 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1220 char fmt4[3] = { fmt[0], '4', 0 };
1221
1222 fmt++;
1223 while (isalpha(*++fmt)) {
1224 switch (*fmt) {
1225 case 'p':
1226 have_p = true;
1227 break;
1228 case 'h':
1229 case 'l':
1230 case 'n':
1231 case 'b':
1232 fmt4[2] = *fmt;
1233 break;
1234 }
1235 }
1236
1237 p = ip4_string(ip4_addr, addr, fmt4);
1238 if (have_p) {
1239 *p++ = ':';
1240 p = number(p, pend, ntohs(sa->sin_port), spec);
1241 }
1242 *p = '\0';
1243
1244 return string(buf, end, ip4_addr, spec);
1245}
1246
1247static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001248char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1249 const char *fmt)
1250{
1251 bool found = true;
1252 int count = 1;
1253 unsigned int flags = 0;
1254 int len;
1255
1256 if (spec.field_width == 0)
1257 return buf; /* nothing to print */
1258
1259 if (ZERO_OR_NULL_PTR(addr))
1260 return string(buf, end, NULL, spec); /* NULL pointer */
1261
1262
1263 do {
1264 switch (fmt[count++]) {
1265 case 'a':
1266 flags |= ESCAPE_ANY;
1267 break;
1268 case 'c':
1269 flags |= ESCAPE_SPECIAL;
1270 break;
1271 case 'h':
1272 flags |= ESCAPE_HEX;
1273 break;
1274 case 'n':
1275 flags |= ESCAPE_NULL;
1276 break;
1277 case 'o':
1278 flags |= ESCAPE_OCTAL;
1279 break;
1280 case 'p':
1281 flags |= ESCAPE_NP;
1282 break;
1283 case 's':
1284 flags |= ESCAPE_SPACE;
1285 break;
1286 default:
1287 found = false;
1288 break;
1289 }
1290 } while (found);
1291
1292 if (!flags)
1293 flags = ESCAPE_ANY_NP;
1294
1295 len = spec.field_width < 0 ? 1 : spec.field_width;
1296
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001297 /*
1298 * string_escape_mem() writes as many characters as it can to
1299 * the given buffer, and returns the total size of the output
1300 * had the buffer been big enough.
1301 */
1302 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001303
1304 return buf;
1305}
1306
1307static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001308char *uuid_string(char *buf, char *end, const u8 *addr,
1309 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001310{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001311 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001312 char *p = uuid;
1313 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001314 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001315 bool uc = false;
1316
1317 switch (*(++fmt)) {
1318 case 'L':
1319 uc = true; /* fall-through */
1320 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001321 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001322 break;
1323 case 'B':
1324 uc = true;
1325 break;
1326 }
1327
1328 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001329 if (uc)
1330 p = hex_byte_pack_upper(p, addr[index[i]]);
1331 else
1332 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001333 switch (i) {
1334 case 3:
1335 case 5:
1336 case 7:
1337 case 9:
1338 *p++ = '-';
1339 break;
1340 }
1341 }
1342
1343 *p = 0;
1344
Joe Perches9ac6e442009-12-14 18:01:09 -08001345 return string(buf, end, uuid, spec);
1346}
1347
Tobin C. Harding57e73442017-11-23 10:56:39 +11001348int kptr_restrict __read_mostly;
1349
1350static noinline_for_stack
1351char *restricted_pointer(char *buf, char *end, const void *ptr,
1352 struct printf_spec spec)
1353{
1354 spec.base = 16;
1355 spec.flags |= SMALL;
1356 if (spec.field_width == -1) {
1357 spec.field_width = 2 * sizeof(ptr);
1358 spec.flags |= ZEROPAD;
1359 }
1360
1361 switch (kptr_restrict) {
1362 case 0:
1363 /* Always print %pK values */
1364 break;
1365 case 1: {
1366 const struct cred *cred;
1367
1368 /*
1369 * kptr_restrict==1 cannot be used in IRQ context
1370 * because its test for CAP_SYSLOG would be meaningless.
1371 */
1372 if (in_irq() || in_serving_softirq() || in_nmi())
1373 return string(buf, end, "pK-error", spec);
1374
1375 /*
1376 * Only print the real pointer value if the current
1377 * process has CAP_SYSLOG and is running with the
1378 * same credentials it started with. This is because
1379 * access to files is checked at open() time, but %pK
1380 * checks permission at read() time. We don't want to
1381 * leak pointer values if a binary opens a file using
1382 * %pK and then elevates privileges before reading it.
1383 */
1384 cred = current_cred();
1385 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1386 !uid_eq(cred->euid, cred->uid) ||
1387 !gid_eq(cred->egid, cred->gid))
1388 ptr = NULL;
1389 break;
1390 }
1391 case 2:
1392 default:
1393 /* Always print 0's for %pK */
1394 ptr = NULL;
1395 break;
1396 }
1397
1398 return number(buf, end, (unsigned long)ptr, spec);
1399}
1400
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001401static noinline_for_stack
1402char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001403{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001404 unsigned long long num;
1405 int size;
1406
1407 switch (fmt[1]) {
1408 case 'F':
1409 num = *(const netdev_features_t *)addr;
1410 size = sizeof(netdev_features_t);
1411 break;
1412 default:
1413 num = (unsigned long)addr;
1414 size = sizeof(unsigned long);
1415 break;
1416 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001417
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001418 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001419}
1420
Joe Perchesaaf07622014-01-23 15:54:17 -08001421static noinline_for_stack
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001422char *address_val(char *buf, char *end, const void *addr, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001423{
1424 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001425 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001426
1427 switch (fmt[1]) {
1428 case 'd':
1429 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001430 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001431 break;
1432 case 'p':
1433 default:
1434 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001435 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001436 break;
1437 }
1438
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001439 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001440}
1441
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001442static noinline_for_stack
1443char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1444 const char *fmt)
1445{
1446 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1447 return string(buf, end, NULL, spec);
1448
1449 switch (fmt[1]) {
1450 case 'r':
1451 return number(buf, end, clk_get_rate(clk), spec);
1452
1453 case 'n':
1454 default:
1455#ifdef CONFIG_COMMON_CLK
1456 return string(buf, end, __clk_get_name(clk), spec);
1457#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001458 return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001459#endif
1460 }
1461}
1462
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001463static
1464char *format_flags(char *buf, char *end, unsigned long flags,
1465 const struct trace_print_flags *names)
1466{
1467 unsigned long mask;
1468 const struct printf_spec strspec = {
1469 .field_width = -1,
1470 .precision = -1,
1471 };
1472 const struct printf_spec numspec = {
1473 .flags = SPECIAL|SMALL,
1474 .field_width = -1,
1475 .precision = -1,
1476 .base = 16,
1477 };
1478
1479 for ( ; flags && names->name; names++) {
1480 mask = names->mask;
1481 if ((flags & mask) != mask)
1482 continue;
1483
1484 buf = string(buf, end, names->name, strspec);
1485
1486 flags &= ~mask;
1487 if (flags) {
1488 if (buf < end)
1489 *buf = '|';
1490 buf++;
1491 }
1492 }
1493
1494 if (flags)
1495 buf = number(buf, end, flags, numspec);
1496
1497 return buf;
1498}
1499
1500static noinline_for_stack
1501char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1502{
1503 unsigned long flags;
1504 const struct trace_print_flags *names;
1505
1506 switch (fmt[1]) {
1507 case 'p':
1508 flags = *(unsigned long *)flags_ptr;
1509 /* Remove zone id */
1510 flags &= (1UL << NR_PAGEFLAGS) - 1;
1511 names = pageflag_names;
1512 break;
1513 case 'v':
1514 flags = *(unsigned long *)flags_ptr;
1515 names = vmaflag_names;
1516 break;
1517 case 'g':
1518 flags = *(gfp_t *)flags_ptr;
1519 names = gfpflag_names;
1520 break;
1521 default:
1522 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1523 return buf;
1524 }
1525
1526 return format_flags(buf, end, flags, names);
1527}
1528
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001529static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1530{
1531 for ( ; np && depth; depth--)
1532 np = np->parent;
1533
1534 return kbasename(np->full_name);
1535}
1536
1537static noinline_for_stack
1538char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1539{
1540 int depth;
1541 const struct device_node *parent = np->parent;
1542 static const struct printf_spec strspec = {
1543 .field_width = -1,
1544 .precision = -1,
1545 };
1546
1547 /* special case for root node */
1548 if (!parent)
1549 return string(buf, end, "/", strspec);
1550
1551 for (depth = 0; parent->parent; depth++)
1552 parent = parent->parent;
1553
1554 for ( ; depth >= 0; depth--) {
1555 buf = string(buf, end, "/", strspec);
1556 buf = string(buf, end, device_node_name_for_depth(np, depth),
1557 strspec);
1558 }
1559 return buf;
1560}
1561
1562static noinline_for_stack
1563char *device_node_string(char *buf, char *end, struct device_node *dn,
1564 struct printf_spec spec, const char *fmt)
1565{
1566 char tbuf[sizeof("xxxx") + 1];
1567 const char *p;
1568 int ret;
1569 char *buf_start = buf;
1570 struct property *prop;
1571 bool has_mult, pass;
1572 static const struct printf_spec num_spec = {
1573 .flags = SMALL,
1574 .field_width = -1,
1575 .precision = -1,
1576 .base = 10,
1577 };
1578
1579 struct printf_spec str_spec = spec;
1580 str_spec.field_width = -1;
1581
1582 if (!IS_ENABLED(CONFIG_OF))
1583 return string(buf, end, "(!OF)", spec);
1584
1585 if ((unsigned long)dn < PAGE_SIZE)
1586 return string(buf, end, "(null)", spec);
1587
1588 /* simple case without anything any more format specifiers */
1589 fmt++;
1590 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1591 fmt = "f";
1592
1593 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1594 if (pass) {
1595 if (buf < end)
1596 *buf = ':';
1597 buf++;
1598 }
1599
1600 switch (*fmt) {
1601 case 'f': /* full_name */
1602 buf = device_node_gen_full_name(dn, buf, end);
1603 break;
1604 case 'n': /* name */
1605 buf = string(buf, end, dn->name, str_spec);
1606 break;
1607 case 'p': /* phandle */
1608 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1609 break;
1610 case 'P': /* path-spec */
1611 p = kbasename(of_node_full_name(dn));
1612 if (!p[1])
1613 p = "/";
1614 buf = string(buf, end, p, str_spec);
1615 break;
1616 case 'F': /* flags */
1617 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1618 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1619 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1620 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1621 tbuf[4] = 0;
1622 buf = string(buf, end, tbuf, str_spec);
1623 break;
1624 case 'c': /* major compatible string */
1625 ret = of_property_read_string(dn, "compatible", &p);
1626 if (!ret)
1627 buf = string(buf, end, p, str_spec);
1628 break;
1629 case 'C': /* full compatible string */
1630 has_mult = false;
1631 of_property_for_each_string(dn, "compatible", prop, p) {
1632 if (has_mult)
1633 buf = string(buf, end, ",", str_spec);
1634 buf = string(buf, end, "\"", str_spec);
1635 buf = string(buf, end, p, str_spec);
1636 buf = string(buf, end, "\"", str_spec);
1637
1638 has_mult = true;
1639 }
1640 break;
1641 default:
1642 break;
1643 }
1644 }
1645
1646 return widen_string(buf, buf - buf_start, end, spec);
1647}
1648
Tobin C. Hardingad67b742017-11-01 15:32:23 +11001649static bool have_filled_random_ptr_key __read_mostly;
1650static siphash_key_t ptr_key __read_mostly;
1651
1652static void fill_random_ptr_key(struct random_ready_callback *unused)
1653{
1654 get_random_bytes(&ptr_key, sizeof(ptr_key));
1655 /*
1656 * have_filled_random_ptr_key==true is dependent on get_random_bytes().
1657 * ptr_to_id() needs to see have_filled_random_ptr_key==true
1658 * after get_random_bytes() returns.
1659 */
1660 smp_mb();
1661 WRITE_ONCE(have_filled_random_ptr_key, true);
1662}
1663
1664static struct random_ready_callback random_ready = {
1665 .func = fill_random_ptr_key
1666};
1667
1668static int __init initialize_ptr_random(void)
1669{
1670 int ret = add_random_ready_callback(&random_ready);
1671
1672 if (!ret) {
1673 return 0;
1674 } else if (ret == -EALREADY) {
1675 fill_random_ptr_key(&random_ready);
1676 return 0;
1677 }
1678
1679 return ret;
1680}
1681early_initcall(initialize_ptr_random);
1682
1683/* Maps a pointer to a 32 bit unique identifier. */
1684static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1685{
1686 unsigned long hashval;
1687 const int default_width = 2 * sizeof(ptr);
1688
1689 if (unlikely(!have_filled_random_ptr_key)) {
1690 spec.field_width = default_width;
1691 /* string length must be less than default_width */
1692 return string(buf, end, "(ptrval)", spec);
1693 }
1694
1695#ifdef CONFIG_64BIT
1696 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1697 /*
1698 * Mask off the first 32 bits, this makes explicit that we have
1699 * modified the address (and 32 bits is plenty for a unique ID).
1700 */
1701 hashval = hashval & 0xffffffff;
1702#else
1703 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1704#endif
1705
1706 spec.flags |= SMALL;
1707 if (spec.field_width == -1) {
1708 spec.field_width = default_width;
1709 spec.flags |= ZEROPAD;
1710 }
1711 spec.base = 16;
1712
1713 return number(buf, end, hashval, spec);
1714}
1715
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001716/*
1717 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1718 * by an extra set of alphanumeric characters that are extended format
1719 * specifiers.
1720 *
Joe Perches0b523762017-05-08 15:55:36 -07001721 * Please update scripts/checkpatch.pl when adding/removing conversion
1722 * characters. (Search for "check for vsprintf extension").
1723 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001724 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001725 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001726 * - 'F' For symbolic function descriptor pointers with offset
1727 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001728 * - 'S' For symbolic direct pointers with offset
1729 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001730 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001731 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001732 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1733 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001734 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1735 * width which must be explicitly specified either as part of the
1736 * format string '%32b[l]' or through '%*b[l]', [l] selects
1737 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001738 * - 'M' For a 6-byte MAC address, it prints the address in the
1739 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001740 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001741 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001742 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001743 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001744 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1745 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1746 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001747 * [S][pfs]
1748 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1749 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001750 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1751 * IPv6 omits the colons (01020304...0f)
1752 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001753 * [S][pfs]
1754 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1755 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1756 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1757 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001758 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001759 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1760 * of the following flags (see string_escape_mem() for the
1761 * details):
1762 * a - ESCAPE_ANY
1763 * c - ESCAPE_SPECIAL
1764 * h - ESCAPE_HEX
1765 * n - ESCAPE_NULL
1766 * o - ESCAPE_OCTAL
1767 * p - ESCAPE_NP
1768 * s - ESCAPE_SPACE
1769 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001770 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1771 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1772 * Options for %pU are:
1773 * b big endian lower case hex (default)
1774 * B big endian UPPER case hex
1775 * l little endian lower case hex
1776 * L little endian UPPER case hex
1777 * big endian output byte order is:
1778 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1779 * little endian output byte order is:
1780 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001781 * - 'V' For a struct va_format which contains a format string * and va_list *,
1782 * call vsnprintf(->format, *->va_list).
1783 * Implements a "recursive vsnprintf".
1784 * Do not use this feature without some mechanism to verify the
1785 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001786 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001787 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001788 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1789 * a certain separator (' ' by default):
1790 * C colon
1791 * D dash
1792 * N no separator
1793 * The maximum supported length is 64 bytes of the input. Consider
1794 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001795 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1796 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001797 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1798 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001799 * - 'g' For block_device name (gendisk + partition number)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001800 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1801 * (legacy clock framework) of the clock
1802 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1803 * (legacy clock framework) of the clock
1804 * - 'Cr' For a clock, it prints the current rate of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001805 * - 'G' For flags to be printed as a collection of symbolic strings that would
1806 * construct the specific value. Supported flags given by option:
1807 * p page flags (see struct page) given as pointer to unsigned long
1808 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1809 * v vma flags (VM_*) given as pointer to unsigned long
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001810 * - 'O' For a kobject based struct. Must be one of the following:
1811 * - 'OF[fnpPcCF]' For a device tree object
1812 * Without any optional arguments prints the full_name
1813 * f device node full_name
1814 * n device node name
1815 * p device node phandle
1816 * P device node path spec (name + @unit)
1817 * F device node flags
1818 * c major compatible string
1819 * C full compatible string
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001820 *
1821 * ** Please update also Documentation/printk-formats.txt when making changes **
Joe Perches9ac6e442009-12-14 18:01:09 -08001822 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001823 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1824 * function pointers are really function descriptors, which contain a
1825 * pointer to the real address.
Tobin C. Hardingad67b742017-11-01 15:32:23 +11001826 *
1827 * Note: The default behaviour (unadorned %p) is to hash the address,
1828 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001829 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001830static noinline_for_stack
1831char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1832 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001833{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001834 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001835
Kees Cook9f36e2c2011-03-22 16:34:22 -07001836 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001837 /*
1838 * Print (null) with the same width as a pointer so it makes
1839 * tabular output look nice.
1840 */
1841 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001842 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001843 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001844 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001845
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001846 switch (*fmt) {
1847 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001848 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001849 ptr = dereference_function_descriptor(ptr);
1850 /* Fallthrough */
1851 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001852 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001853 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001854 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001855 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001856 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001857 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001858 case 'h':
1859 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001860 case 'b':
1861 switch (fmt[1]) {
1862 case 'l':
1863 return bitmap_list_string(buf, end, ptr, spec, fmt);
1864 default:
1865 return bitmap_string(buf, end, ptr, spec, fmt);
1866 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001867 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1868 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001869 /* [mM]F (FDDI) */
1870 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001871 return mac_address_string(buf, end, ptr, spec, fmt);
1872 case 'I': /* Formatted IP supported
1873 * 4: 1.2.3.4
1874 * 6: 0001:0203:...:0708
1875 * 6c: 1::708 or 1::1.2.3.4
1876 */
1877 case 'i': /* Contiguous:
1878 * 4: 001.002.003.004
1879 * 6: 000102...0f
1880 */
1881 switch (fmt[1]) {
1882 case '6':
1883 return ip6_addr_string(buf, end, ptr, spec, fmt);
1884 case '4':
1885 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001886 case 'S': {
1887 const union {
1888 struct sockaddr raw;
1889 struct sockaddr_in v4;
1890 struct sockaddr_in6 v6;
1891 } *sa = ptr;
1892
1893 switch (sa->raw.sa_family) {
1894 case AF_INET:
1895 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1896 case AF_INET6:
1897 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1898 default:
1899 return string(buf, end, "(invalid address)", spec);
1900 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001901 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001902 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001903 case 'E':
1904 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001905 case 'U':
1906 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001907 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001908 {
1909 va_list va;
1910
1911 va_copy(va, *((struct va_format *)ptr)->va);
1912 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1913 ((struct va_format *)ptr)->fmt, va);
1914 va_end(va);
1915 return buf;
1916 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001917 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11001918 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001919 case 'N':
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001920 return netdev_bits(buf, end, ptr, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001921 case 'a':
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001922 return address_val(buf, end, ptr, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001923 case 'd':
1924 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001925 case 'C':
1926 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001927 case 'D':
1928 return dentry_name(buf, end,
1929 ((const struct file *)ptr)->f_path.dentry,
1930 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001931#ifdef CONFIG_BLOCK
1932 case 'g':
1933 return bdev_name(buf, end, ptr, spec, fmt);
1934#endif
1935
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001936 case 'G':
1937 return flags_string(buf, end, ptr, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001938 case 'O':
1939 switch (fmt[1]) {
1940 case 'F':
1941 return device_node_string(buf, end, ptr, spec, fmt + 1);
1942 }
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001943 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001944
Tobin C. Hardingad67b742017-11-01 15:32:23 +11001945 /* default is to _not_ leak addresses, hash before printing */
1946 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001947}
1948
1949/*
1950 * Helper function to decode printf style format.
1951 * Each call decode a token from the format and return the
1952 * number of characters read (or likely the delta where it wants
1953 * to go on the next call).
1954 * The decoded token is returned through the parameters
1955 *
1956 * 'h', 'l', or 'L' for integer fields
1957 * 'z' support added 23/7/1999 S.H.
1958 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001959 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001960 * 't' added for ptrdiff_t
1961 *
1962 * @fmt: the format string
1963 * @type of the token returned
1964 * @flags: various flags such as +, -, # tokens..
1965 * @field_width: overwritten width
1966 * @base: base of the number (octal, hex, ...)
1967 * @precision: precision of a number
1968 * @qualifier: qualifier of a number (long, size_t, ...)
1969 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001970static noinline_for_stack
1971int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001972{
1973 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001974 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001975
1976 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001977 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001978 if (spec->field_width < 0) {
1979 spec->field_width = -spec->field_width;
1980 spec->flags |= LEFT;
1981 }
1982 spec->type = FORMAT_TYPE_NONE;
1983 goto precision;
1984 }
1985
1986 /* we finished early by reading the precision */
1987 if (spec->type == FORMAT_TYPE_PRECISION) {
1988 if (spec->precision < 0)
1989 spec->precision = 0;
1990
1991 spec->type = FORMAT_TYPE_NONE;
1992 goto qualifier;
1993 }
1994
1995 /* By default */
1996 spec->type = FORMAT_TYPE_NONE;
1997
1998 for (; *fmt ; ++fmt) {
1999 if (*fmt == '%')
2000 break;
2001 }
2002
2003 /* Return the current non-format string */
2004 if (fmt != start || !*fmt)
2005 return fmt - start;
2006
2007 /* Process flags */
2008 spec->flags = 0;
2009
2010 while (1) { /* this also skips first '%' */
2011 bool found = true;
2012
2013 ++fmt;
2014
2015 switch (*fmt) {
2016 case '-': spec->flags |= LEFT; break;
2017 case '+': spec->flags |= PLUS; break;
2018 case ' ': spec->flags |= SPACE; break;
2019 case '#': spec->flags |= SPECIAL; break;
2020 case '0': spec->flags |= ZEROPAD; break;
2021 default: found = false;
2022 }
2023
2024 if (!found)
2025 break;
2026 }
2027
2028 /* get field width */
2029 spec->field_width = -1;
2030
2031 if (isdigit(*fmt))
2032 spec->field_width = skip_atoi(&fmt);
2033 else if (*fmt == '*') {
2034 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002035 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002036 return ++fmt - start;
2037 }
2038
2039precision:
2040 /* get the precision */
2041 spec->precision = -1;
2042 if (*fmt == '.') {
2043 ++fmt;
2044 if (isdigit(*fmt)) {
2045 spec->precision = skip_atoi(&fmt);
2046 if (spec->precision < 0)
2047 spec->precision = 0;
2048 } else if (*fmt == '*') {
2049 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002050 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002051 return ++fmt - start;
2052 }
2053 }
2054
2055qualifier:
2056 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002057 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002058 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002059 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002060 qualifier = *fmt++;
2061 if (unlikely(qualifier == *fmt)) {
2062 if (qualifier == 'l') {
2063 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002064 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002065 } else if (qualifier == 'h') {
2066 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002067 ++fmt;
2068 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002069 }
2070 }
2071
2072 /* default base */
2073 spec->base = 10;
2074 switch (*fmt) {
2075 case 'c':
2076 spec->type = FORMAT_TYPE_CHAR;
2077 return ++fmt - start;
2078
2079 case 's':
2080 spec->type = FORMAT_TYPE_STR;
2081 return ++fmt - start;
2082
2083 case 'p':
2084 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002085 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002086
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002087 case '%':
2088 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2089 return ++fmt - start;
2090
2091 /* integer number formats - set up the flags and "break" */
2092 case 'o':
2093 spec->base = 8;
2094 break;
2095
2096 case 'x':
2097 spec->flags |= SMALL;
2098
2099 case 'X':
2100 spec->base = 16;
2101 break;
2102
2103 case 'd':
2104 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002105 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002106 case 'u':
2107 break;
2108
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002109 case 'n':
2110 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002111 * Since %n poses a greater security risk than
2112 * utility, treat it as any other invalid or
2113 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002114 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002115 /* Fall-through */
2116
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002117 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002118 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002119 spec->type = FORMAT_TYPE_INVALID;
2120 return fmt - start;
2121 }
2122
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002123 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002124 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002125 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002126 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2127 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002128 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002129 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002130 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002131 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002132 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002133 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2134 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002135 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002136 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2137 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002138 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002139 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2140 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002141 }
2142
2143 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002144}
2145
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002146static void
2147set_field_width(struct printf_spec *spec, int width)
2148{
2149 spec->field_width = width;
2150 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2151 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2152 }
2153}
2154
2155static void
2156set_precision(struct printf_spec *spec, int prec)
2157{
2158 spec->precision = prec;
2159 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2160 spec->precision = clamp(prec, 0, PRECISION_MAX);
2161 }
2162}
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164/**
2165 * vsnprintf - Format a string and place it in a buffer
2166 * @buf: The buffer to place the result into
2167 * @size: The size of the buffer, including the trailing null space
2168 * @fmt: The format string to use
2169 * @args: Arguments for the format string
2170 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002171 * This function generally follows C99 vsnprintf, but has some
2172 * extensions and a few limitations:
2173 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002174 * - ``%n`` is unsupported
2175 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002176 *
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002177 * See pointer() or Documentation/printk-formats.txt for more
2178 * extensive description.
2179 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002180 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002181 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 * The return value is the number of characters which would
2183 * be generated for the given input, excluding the trailing
2184 * '\0', as per ISO C99. If you want to have the exact
2185 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002186 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 * return is greater than or equal to @size, the resulting
2188 * string is truncated.
2189 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002190 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 */
2192int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2193{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002195 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002196 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002198 /* Reject out-of-range values early. Large positive sizes are
2199 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002200 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002204 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002206 /* Make sure end is always >= buf */
2207 if (end < buf) {
2208 end = ((void *)-1);
2209 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002212 while (*fmt) {
2213 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002214 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002215
2216 fmt += read;
2217
2218 switch (spec.type) {
2219 case FORMAT_TYPE_NONE: {
2220 int copy = read;
2221 if (str < end) {
2222 if (copy > end - str)
2223 copy = end - str;
2224 memcpy(str, old_fmt, copy);
2225 }
2226 str += read;
2227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229
Vegard Nossumed681a92009-03-14 12:08:50 +01002230 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002231 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002234 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002235 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
André Goddard Rosad4be1512009-12-14 18:00:59 -08002238 case FORMAT_TYPE_CHAR: {
2239 char c;
2240
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002241 if (!(spec.flags & LEFT)) {
2242 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002243 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 *str = ' ';
2245 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002248 }
2249 c = (unsigned char) va_arg(args, int);
2250 if (str < end)
2251 *str = c;
2252 ++str;
2253 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002254 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002255 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002257 }
2258 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002261 case FORMAT_TYPE_STR:
2262 str = string(str, end, va_arg(args, char *), spec);
2263 break;
2264
2265 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002266 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002267 spec);
2268 while (isalnum(*fmt))
2269 fmt++;
2270 break;
2271
2272 case FORMAT_TYPE_PERCENT_CHAR:
2273 if (str < end)
2274 *str = '%';
2275 ++str;
2276 break;
2277
2278 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002279 /*
2280 * Presumably the arguments passed gcc's type
2281 * checking, but there is no safe or sane way
2282 * for us to continue parsing the format and
2283 * fetching from the va_list; the remaining
2284 * specifiers and arguments would be out of
2285 * sync.
2286 */
2287 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002288
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002289 default:
2290 switch (spec.type) {
2291 case FORMAT_TYPE_LONG_LONG:
2292 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002294 case FORMAT_TYPE_ULONG:
2295 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002297 case FORMAT_TYPE_LONG:
2298 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002300 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002301 if (spec.flags & SIGN)
2302 num = va_arg(args, ssize_t);
2303 else
2304 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002305 break;
2306 case FORMAT_TYPE_PTRDIFF:
2307 num = va_arg(args, ptrdiff_t);
2308 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002309 case FORMAT_TYPE_UBYTE:
2310 num = (unsigned char) va_arg(args, int);
2311 break;
2312 case FORMAT_TYPE_BYTE:
2313 num = (signed char) va_arg(args, int);
2314 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002315 case FORMAT_TYPE_USHORT:
2316 num = (unsigned short) va_arg(args, int);
2317 break;
2318 case FORMAT_TYPE_SHORT:
2319 num = (short) va_arg(args, int);
2320 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002321 case FORMAT_TYPE_INT:
2322 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002325 num = va_arg(args, unsigned int);
2326 }
2327
2328 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002331
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002332out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002333 if (size > 0) {
2334 if (str < end)
2335 *str = '\0';
2336 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002337 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002338 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002339
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002340 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344EXPORT_SYMBOL(vsnprintf);
2345
2346/**
2347 * vscnprintf - Format a string and place it in a buffer
2348 * @buf: The buffer to place the result into
2349 * @size: The size of the buffer, including the trailing null space
2350 * @fmt: The format string to use
2351 * @args: Arguments for the format string
2352 *
2353 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002354 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 * returns 0.
2356 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002357 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002358 *
2359 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
2361int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2362{
2363 int i;
2364
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002365 i = vsnprintf(buf, size, fmt, args);
2366
Anton Arapovb921c692011-01-12 16:59:49 -08002367 if (likely(i < size))
2368 return i;
2369 if (size != 0)
2370 return size - 1;
2371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373EXPORT_SYMBOL(vscnprintf);
2374
2375/**
2376 * snprintf - Format a string and place it in a buffer
2377 * @buf: The buffer to place the result into
2378 * @size: The size of the buffer, including the trailing null space
2379 * @fmt: The format string to use
2380 * @...: Arguments for the format string
2381 *
2382 * The return value is the number of characters which would be
2383 * generated for the given input, excluding the trailing null,
2384 * as per ISO C99. If the return is greater than or equal to
2385 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002386 *
2387 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002389int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391 va_list args;
2392 int i;
2393
2394 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002395 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return i;
2399}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400EXPORT_SYMBOL(snprintf);
2401
2402/**
2403 * scnprintf - Format a string and place it in a buffer
2404 * @buf: The buffer to place the result into
2405 * @size: The size of the buffer, including the trailing null space
2406 * @fmt: The format string to use
2407 * @...: Arguments for the format string
2408 *
2409 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002410 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 */
2412
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002413int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
2415 va_list args;
2416 int i;
2417
2418 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002419 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002421
Anton Arapovb921c692011-01-12 16:59:49 -08002422 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424EXPORT_SYMBOL(scnprintf);
2425
2426/**
2427 * vsprintf - Format a string and place it in a buffer
2428 * @buf: The buffer to place the result into
2429 * @fmt: The format string to use
2430 * @args: Arguments for the format string
2431 *
2432 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002433 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 * buffer overflows.
2435 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002436 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002437 *
2438 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 */
2440int vsprintf(char *buf, const char *fmt, va_list args)
2441{
2442 return vsnprintf(buf, INT_MAX, fmt, args);
2443}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444EXPORT_SYMBOL(vsprintf);
2445
2446/**
2447 * sprintf - Format a string and place it in a buffer
2448 * @buf: The buffer to place the result into
2449 * @fmt: The format string to use
2450 * @...: Arguments for the format string
2451 *
2452 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002453 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002455 *
2456 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002458int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 va_list args;
2461 int i;
2462
2463 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002464 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return i;
2468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469EXPORT_SYMBOL(sprintf);
2470
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002471#ifdef CONFIG_BINARY_PRINTF
2472/*
2473 * bprintf service:
2474 * vbin_printf() - VA arguments to binary data
2475 * bstr_printf() - Binary data to text string
2476 */
2477
2478/**
2479 * vbin_printf - Parse a format string and place args' binary value in a buffer
2480 * @bin_buf: The buffer to place args' binary value
2481 * @size: The size of the buffer(by words(32bits), not characters)
2482 * @fmt: The format string to use
2483 * @args: Arguments for the format string
2484 *
2485 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002486 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002487 *
2488 * The return value is the number of words(32bits) which would be generated for
2489 * the given input.
2490 *
2491 * NOTE:
2492 * If the return value is greater than @size, the resulting bin_buf is NOT
2493 * valid for bstr_printf().
2494 */
2495int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2496{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002497 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002498 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002499
2500 str = (char *)bin_buf;
2501 end = (char *)(bin_buf + size);
2502
2503#define save_arg(type) \
2504do { \
2505 if (sizeof(type) == 8) { \
2506 unsigned long long value; \
2507 str = PTR_ALIGN(str, sizeof(u32)); \
2508 value = va_arg(args, unsigned long long); \
2509 if (str + sizeof(type) <= end) { \
2510 *(u32 *)str = *(u32 *)&value; \
2511 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2512 } \
2513 } else { \
2514 unsigned long value; \
2515 str = PTR_ALIGN(str, sizeof(type)); \
2516 value = va_arg(args, int); \
2517 if (str + sizeof(type) <= end) \
2518 *(typeof(type) *)str = (type)value; \
2519 } \
2520 str += sizeof(type); \
2521} while (0)
2522
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002523 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002524 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002525
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002526 fmt += read;
2527
2528 switch (spec.type) {
2529 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002530 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002531 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002532 case FORMAT_TYPE_INVALID:
2533 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002534
Vegard Nossumed681a92009-03-14 12:08:50 +01002535 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002536 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002537 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002538 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002539
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002540 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002541 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002542 break;
2543
2544 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002545 const char *save_str = va_arg(args, char *);
2546 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002547
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002548 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2549 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002550 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002551 len = strlen(save_str) + 1;
2552 if (str + len < end)
2553 memcpy(str, save_str, len);
2554 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002555 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002556 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002557
2558 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002559 save_arg(void *);
2560 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002561 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002562 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002563 break;
2564
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002565 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002566 switch (spec.type) {
2567
2568 case FORMAT_TYPE_LONG_LONG:
2569 save_arg(long long);
2570 break;
2571 case FORMAT_TYPE_ULONG:
2572 case FORMAT_TYPE_LONG:
2573 save_arg(unsigned long);
2574 break;
2575 case FORMAT_TYPE_SIZE_T:
2576 save_arg(size_t);
2577 break;
2578 case FORMAT_TYPE_PTRDIFF:
2579 save_arg(ptrdiff_t);
2580 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002581 case FORMAT_TYPE_UBYTE:
2582 case FORMAT_TYPE_BYTE:
2583 save_arg(char);
2584 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002585 case FORMAT_TYPE_USHORT:
2586 case FORMAT_TYPE_SHORT:
2587 save_arg(short);
2588 break;
2589 default:
2590 save_arg(int);
2591 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002592 }
2593 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002594
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002595out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002596 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002597#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002598}
2599EXPORT_SYMBOL_GPL(vbin_printf);
2600
2601/**
2602 * bstr_printf - Format a string from binary arguments and place it in a buffer
2603 * @buf: The buffer to place the result into
2604 * @size: The size of the buffer, including the trailing null space
2605 * @fmt: The format string to use
2606 * @bin_buf: Binary arguments for the format string
2607 *
2608 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2609 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2610 * a binary buffer that generated by vbin_printf.
2611 *
2612 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002613 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002614 *
2615 * The return value is the number of characters which would
2616 * be generated for the given input, excluding the trailing
2617 * '\0', as per ISO C99. If you want to have the exact
2618 * number of characters written into @buf as return value
2619 * (not including the trailing '\0'), use vscnprintf(). If the
2620 * return is greater than or equal to @size, the resulting
2621 * string is truncated.
2622 */
2623int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2624{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002625 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002626 char *str, *end;
2627 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002628
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002629 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002630 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002631
2632 str = buf;
2633 end = buf + size;
2634
2635#define get_arg(type) \
2636({ \
2637 typeof(type) value; \
2638 if (sizeof(type) == 8) { \
2639 args = PTR_ALIGN(args, sizeof(u32)); \
2640 *(u32 *)&value = *(u32 *)args; \
2641 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2642 } else { \
2643 args = PTR_ALIGN(args, sizeof(type)); \
2644 value = *(typeof(type) *)args; \
2645 } \
2646 args += sizeof(type); \
2647 value; \
2648})
2649
2650 /* Make sure end is always >= buf */
2651 if (end < buf) {
2652 end = ((void *)-1);
2653 size = end - buf;
2654 }
2655
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002656 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002657 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002658 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002659
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002660 fmt += read;
2661
2662 switch (spec.type) {
2663 case FORMAT_TYPE_NONE: {
2664 int copy = read;
2665 if (str < end) {
2666 if (copy > end - str)
2667 copy = end - str;
2668 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002669 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002670 str += read;
2671 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002672 }
2673
Vegard Nossumed681a92009-03-14 12:08:50 +01002674 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002675 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002676 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002677
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002678 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002679 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002680 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002681
André Goddard Rosad4be1512009-12-14 18:00:59 -08002682 case FORMAT_TYPE_CHAR: {
2683 char c;
2684
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002685 if (!(spec.flags & LEFT)) {
2686 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002687 if (str < end)
2688 *str = ' ';
2689 ++str;
2690 }
2691 }
2692 c = (unsigned char) get_arg(char);
2693 if (str < end)
2694 *str = c;
2695 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002696 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002697 if (str < end)
2698 *str = ' ';
2699 ++str;
2700 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002701 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002702 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002703
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002704 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002705 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002706 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002707 str = string(str, end, (char *)str_arg, spec);
2708 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002709 }
2710
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002711 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002712 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002713 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002714 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002715 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002716
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002717 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002718 if (str < end)
2719 *str = '%';
2720 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002721 break;
2722
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002723 case FORMAT_TYPE_INVALID:
2724 goto out;
2725
André Goddard Rosad4be1512009-12-14 18:00:59 -08002726 default: {
2727 unsigned long long num;
2728
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002729 switch (spec.type) {
2730
2731 case FORMAT_TYPE_LONG_LONG:
2732 num = get_arg(long long);
2733 break;
2734 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002735 case FORMAT_TYPE_LONG:
2736 num = get_arg(unsigned long);
2737 break;
2738 case FORMAT_TYPE_SIZE_T:
2739 num = get_arg(size_t);
2740 break;
2741 case FORMAT_TYPE_PTRDIFF:
2742 num = get_arg(ptrdiff_t);
2743 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002744 case FORMAT_TYPE_UBYTE:
2745 num = get_arg(unsigned char);
2746 break;
2747 case FORMAT_TYPE_BYTE:
2748 num = get_arg(signed char);
2749 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002750 case FORMAT_TYPE_USHORT:
2751 num = get_arg(unsigned short);
2752 break;
2753 case FORMAT_TYPE_SHORT:
2754 num = get_arg(short);
2755 break;
2756 case FORMAT_TYPE_UINT:
2757 num = get_arg(unsigned int);
2758 break;
2759 default:
2760 num = get_arg(int);
2761 }
2762
2763 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002764 } /* default: */
2765 } /* switch(spec.type) */
2766 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002767
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002768out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002769 if (size > 0) {
2770 if (str < end)
2771 *str = '\0';
2772 else
2773 end[-1] = '\0';
2774 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002775
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002776#undef get_arg
2777
2778 /* the trailing null byte doesn't count towards the total */
2779 return str - buf;
2780}
2781EXPORT_SYMBOL_GPL(bstr_printf);
2782
2783/**
2784 * bprintf - Parse a format string and place args' binary value in a buffer
2785 * @bin_buf: The buffer to place args' binary value
2786 * @size: The size of the buffer(by words(32bits), not characters)
2787 * @fmt: The format string to use
2788 * @...: Arguments for the format string
2789 *
2790 * The function returns the number of words(u32) written
2791 * into @bin_buf.
2792 */
2793int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2794{
2795 va_list args;
2796 int ret;
2797
2798 va_start(args, fmt);
2799 ret = vbin_printf(bin_buf, size, fmt, args);
2800 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002801
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002802 return ret;
2803}
2804EXPORT_SYMBOL_GPL(bprintf);
2805
2806#endif /* CONFIG_BINARY_PRINTF */
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808/**
2809 * vsscanf - Unformat a buffer into a list of arguments
2810 * @buf: input buffer
2811 * @fmt: format of buffer
2812 * @args: arguments
2813 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002814int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
2816 const char *str = buf;
2817 char *next;
2818 char digit;
2819 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002820 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002821 unsigned int base;
2822 union {
2823 long long s;
2824 unsigned long long u;
2825 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002826 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002827 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Jan Beulichda990752012-10-04 17:13:24 -07002829 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /* skip any white space in format */
2831 /* white space in format matchs any amount of
2832 * white space, including none, in the input.
2833 */
2834 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002835 fmt = skip_spaces(++fmt);
2836 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838
2839 /* anything that is not a conversion must match exactly */
2840 if (*fmt != '%' && *fmt) {
2841 if (*fmt++ != *str++)
2842 break;
2843 continue;
2844 }
2845
2846 if (!*fmt)
2847 break;
2848 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /* skip this conversion.
2851 * advance both strings to next white space
2852 */
2853 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002854 if (!*str)
2855 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07002856 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2857 /* '%*[' not yet supported, invalid format */
2858 if (*fmt == '[')
2859 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07002861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 while (!isspace(*str) && *str)
2863 str++;
2864 continue;
2865 }
2866
2867 /* get field width */
2868 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002869 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002871 if (field_width <= 0)
2872 break;
2873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 /* get conversion qualifier */
2876 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002877 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002878 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 qualifier = *fmt++;
2880 if (unlikely(qualifier == *fmt)) {
2881 if (qualifier == 'h') {
2882 qualifier = 'H';
2883 fmt++;
2884 } else if (qualifier == 'l') {
2885 qualifier = 'L';
2886 fmt++;
2887 }
2888 }
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Jan Beulichda990752012-10-04 17:13:24 -07002891 if (!*fmt)
2892 break;
2893
2894 if (*fmt == 'n') {
2895 /* return number of characters read so far */
2896 *va_arg(args, int *) = str - buf;
2897 ++fmt;
2898 continue;
2899 }
2900
2901 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 break;
2903
André Goddard Rosad4be1512009-12-14 18:00:59 -08002904 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002905 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002906
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002907 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 case 'c':
2909 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002910 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (field_width == -1)
2912 field_width = 1;
2913 do {
2914 *s++ = *str++;
2915 } while (--field_width > 0 && *str);
2916 num++;
2917 }
2918 continue;
2919 case 's':
2920 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002921 char *s = (char *)va_arg(args, char *);
2922 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002923 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002925 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002928 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 *s = '\0';
2931 num++;
2932 }
2933 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07002934 /*
2935 * Warning: This implementation of the '[' conversion specifier
2936 * deviates from its glibc counterpart in the following ways:
2937 * (1) It does NOT support ranges i.e. '-' is NOT a special
2938 * character
2939 * (2) It cannot match the closing bracket ']' itself
2940 * (3) A field width is required
2941 * (4) '%*[' (discard matching input) is currently not supported
2942 *
2943 * Example usage:
2944 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
2945 * buf1, buf2, buf3);
2946 * if (ret < 3)
2947 * // etc..
2948 */
2949 case '[':
2950 {
2951 char *s = (char *)va_arg(args, char *);
2952 DECLARE_BITMAP(set, 256) = {0};
2953 unsigned int len = 0;
2954 bool negate = (*fmt == '^');
2955
2956 /* field width is required */
2957 if (field_width == -1)
2958 return num;
2959
2960 if (negate)
2961 ++fmt;
2962
2963 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
2964 set_bit((u8)*fmt, set);
2965
2966 /* no ']' or no character set found */
2967 if (!*fmt || !len)
2968 return num;
2969 ++fmt;
2970
2971 if (negate) {
2972 bitmap_complement(set, set, 256);
2973 /* exclude null '\0' byte */
2974 clear_bit(0, set);
2975 }
2976
2977 /* match must be non-empty */
2978 if (!test_bit((u8)*str, set))
2979 return num;
2980
2981 while (test_bit((u8)*str, set) && field_width--)
2982 *s++ = *str++;
2983 *s = '\0';
2984 ++num;
2985 }
2986 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 case 'o':
2988 base = 8;
2989 break;
2990 case 'x':
2991 case 'X':
2992 base = 16;
2993 break;
2994 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002995 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002997 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 case 'u':
2999 break;
3000 case '%':
3001 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003002 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return num;
3004 continue;
3005 default:
3006 /* invalid format; stop here */
3007 return num;
3008 }
3009
3010 /* have some sort of integer conversion.
3011 * first, skip white space in buffer.
3012 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003013 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 digit = *str;
3016 if (is_sign && digit == '-')
3017 digit = *(str + 1);
3018
3019 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003020 || (base == 16 && !isxdigit(digit))
3021 || (base == 10 && !isdigit(digit))
3022 || (base == 8 && (!isdigit(digit) || digit > '7'))
3023 || (base == 0 && !isdigit(digit)))
3024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Jan Beulich53809752012-12-17 16:01:31 -08003026 if (is_sign)
3027 val.s = qualifier != 'L' ?
3028 simple_strtol(str, &next, base) :
3029 simple_strtoll(str, &next, base);
3030 else
3031 val.u = qualifier != 'L' ?
3032 simple_strtoul(str, &next, base) :
3033 simple_strtoull(str, &next, base);
3034
3035 if (field_width > 0 && next - str > field_width) {
3036 if (base == 0)
3037 _parse_integer_fixup_radix(str, &base);
3038 while (next - str > field_width) {
3039 if (is_sign)
3040 val.s = div_s64(val.s, base);
3041 else
3042 val.u = div_u64(val.u, base);
3043 --next;
3044 }
3045 }
3046
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003047 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003049 if (is_sign)
3050 *va_arg(args, signed char *) = val.s;
3051 else
3052 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 break;
3054 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003055 if (is_sign)
3056 *va_arg(args, short *) = val.s;
3057 else
3058 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 break;
3060 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003061 if (is_sign)
3062 *va_arg(args, long *) = val.s;
3063 else
3064 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 break;
3066 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003067 if (is_sign)
3068 *va_arg(args, long long *) = val.s;
3069 else
3070 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003073 *va_arg(args, size_t *) = val.u;
3074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 default:
Jan Beulich53809752012-12-17 16:01:31 -08003076 if (is_sign)
3077 *va_arg(args, int *) = val.s;
3078 else
3079 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 break;
3081 }
3082 num++;
3083
3084 if (!next)
3085 break;
3086 str = next;
3087 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 return num;
3090}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091EXPORT_SYMBOL(vsscanf);
3092
3093/**
3094 * sscanf - Unformat a buffer into a list of arguments
3095 * @buf: input buffer
3096 * @fmt: formatting of buffer
3097 * @...: resulting arguments
3098 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003099int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 va_list args;
3102 int i;
3103
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003104 va_start(args, fmt);
3105 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return i;
3109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110EXPORT_SYMBOL(sscanf);