blob: c537a73a62e858ec1fe695a34ae4a33bdbf4a3f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070020#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070021#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050022#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ctype.h>
26#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070027#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080028#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070029#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110030#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040031#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080032#include <linux/cred.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000033#include <net/addrconf.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040034#ifdef CONFIG_BLOCK
35#include <linux/blkdev.h>
36#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Tim Schmielau4e57b682005-10-30 15:03:48 -080038#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050039#include <asm/sections.h> /* for dereference_function_descriptor() */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070040#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Andy Shevchenko71dca952014-10-13 15:55:18 -070042#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070043#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * simple_strtoull - convert a string to an unsigned long long
47 * @cp: The start of the string
48 * @endp: A pointer to the end of the parsed string will be placed here
49 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080050 *
51 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Harvey Harrison22d27052008-10-16 13:40:35 -070053unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070055 unsigned long long result;
56 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070058 cp = _parse_integer_fixup_radix(cp, &base);
59 rv = _parse_integer(cp, base, &result);
60 /* FIXME */
61 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (endp)
64 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return result;
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068EXPORT_SYMBOL(simple_strtoull);
69
70/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080071 * simple_strtoul - convert a string to an unsigned long
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080075 *
76 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080077 */
78unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
79{
80 return simple_strtoull(cp, endp, base);
81}
82EXPORT_SYMBOL(simple_strtoul);
83
84/**
85 * simple_strtol - convert a string to a signed long
86 * @cp: The start of the string
87 * @endp: A pointer to the end of the parsed string will be placed here
88 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080089 *
90 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080091 */
92long simple_strtol(const char *cp, char **endp, unsigned int base)
93{
94 if (*cp == '-')
95 return -simple_strtoul(cp + 1, endp, base);
96
97 return simple_strtoul(cp, endp, base);
98}
99EXPORT_SYMBOL(simple_strtol);
100
101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * simple_strtoll - convert a string to a signed long long
103 * @cp: The start of the string
104 * @endp: A pointer to the end of the parsed string will be placed here
105 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800106 *
107 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700109long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800111 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700112 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800113
Harvey Harrison22d27052008-10-16 13:40:35 -0700114 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400116EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Joe Perchescf3b4292010-05-24 14:33:16 -0700118static noinline_for_stack
119int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800121 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800123 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800125 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return i;
128}
129
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700130/*
131 * Decimal conversion is by far the most typical, and is used for
132 * /proc and /sys data. This directly impacts e.g. top performance
133 * with many processes running. We optimize it for speed by emitting
134 * two characters at a time, using a 200 byte lookup table. This
135 * roughly halves the number of multiplications compared to computing
136 * the digits one at a time. Implementation strongly inspired by the
137 * previous version, which in turn used ideas described at
138 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
139 * from the author, Douglas W. Jones).
140 *
141 * It turns out there is precisely one 26 bit fixed-point
142 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
143 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
144 * range happens to be somewhat larger (x <= 1073741898), but that's
145 * irrelevant for our purpose.
146 *
147 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
148 * need a 32x32->64 bit multiply, so we simply use the same constant.
149 *
150 * For dividing a number in the range [100, 10^4-1] by 100, there are
151 * several options. The simplest is (x * 0x147b) >> 19, which is valid
152 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700153 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700154
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700155static const u16 decpair[100] = {
156#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
157 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
158 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
159 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
160 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
161 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
162 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
163 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
164 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
165 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
166 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
167#undef _
168};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700169
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700170/*
171 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700172 * immediately jump to out_r where two 0s would be written but only
173 * one of them accounted for in buf. This is needed by ip4_string
174 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700175*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700176static noinline_for_stack
177char *put_dec_trunc8(char *buf, unsigned r)
178{
179 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700180
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700181 /* 1 <= r < 10^8 */
182 if (r < 100)
183 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700184
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700185 /* 100 <= r < 10^8 */
186 q = (r * (u64)0x28f5c29) >> 32;
187 *((u16 *)buf) = decpair[r - 100*q];
188 buf += 2;
189
190 /* 1 <= q < 10^6 */
191 if (q < 100)
192 goto out_q;
193
194 /* 100 <= q < 10^6 */
195 r = (q * (u64)0x28f5c29) >> 32;
196 *((u16 *)buf) = decpair[q - 100*r];
197 buf += 2;
198
199 /* 1 <= r < 10^4 */
200 if (r < 100)
201 goto out_r;
202
203 /* 100 <= r < 10^4 */
204 q = (r * 0x147b) >> 19;
205 *((u16 *)buf) = decpair[r - 100*q];
206 buf += 2;
207out_q:
208 /* 1 <= q < 100 */
209 r = q;
210out_r:
211 /* 1 <= r < 100 */
212 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700213 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700214 return buf;
215}
216
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700217#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
218static noinline_for_stack
219char *put_dec_full8(char *buf, unsigned r)
220{
221 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700222
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700223 /* 0 <= r < 10^8 */
224 q = (r * (u64)0x28f5c29) >> 32;
225 *((u16 *)buf) = decpair[r - 100*q];
226 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700227
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700228 /* 0 <= q < 10^6 */
229 r = (q * (u64)0x28f5c29) >> 32;
230 *((u16 *)buf) = decpair[q - 100*r];
231 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700232
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700233 /* 0 <= r < 10^4 */
234 q = (r * 0x147b) >> 19;
235 *((u16 *)buf) = decpair[r - 100*q];
236 buf += 2;
237
238 /* 0 <= q < 100 */
239 *((u16 *)buf) = decpair[q];
240 buf += 2;
241 return buf;
242}
243
244static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700245char *put_dec(char *buf, unsigned long long n)
246{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700247 if (n >= 100*1000*1000)
248 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
249 /* 1 <= n <= 1.6e11 */
250 if (n >= 100*1000*1000)
251 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
252 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700253 return put_dec_trunc8(buf, n);
254}
255
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700256#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700257
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700258static void
259put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700260{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700261 unsigned q;
262
263 /* 0 <= r < 10^4 */
264 q = (r * 0x147b) >> 19;
265 *((u16 *)buf) = decpair[r - 100*q];
266 buf += 2;
267 /* 0 <= q < 100 */
268 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700269}
270
271/*
272 * Call put_dec_full4 on x % 10000, return x / 10000.
273 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
274 * holds for all x < 1,128,869,999. The largest value this
275 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700276 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700277 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700278static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700279unsigned put_dec_helper4(char *buf, unsigned x)
280{
281 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
282
283 put_dec_full4(buf, x - q * 10000);
284 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700285}
286
287/* Based on code by Douglas W. Jones found at
288 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
289 * (with permission from the author).
290 * Performs no 64-bit division and hence should be fast on 32-bit machines.
291 */
292static
293char *put_dec(char *buf, unsigned long long n)
294{
295 uint32_t d3, d2, d1, q, h;
296
297 if (n < 100*1000*1000)
298 return put_dec_trunc8(buf, n);
299
300 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
301 h = (n >> 32);
302 d2 = (h ) & 0xffff;
303 d3 = (h >> 16); /* implicit "& 0xffff" */
304
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700305 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
306 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700307 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700308 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700309
George Spelvin23591722012-10-04 17:12:29 -0700310 q += 7671 * d3 + 9496 * d2 + 6 * d1;
311 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700312
George Spelvin23591722012-10-04 17:12:29 -0700313 q += 4749 * d3 + 42 * d2;
314 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700315
George Spelvin23591722012-10-04 17:12:29 -0700316 q += 281 * d3;
317 buf += 12;
318 if (q)
319 buf = put_dec_trunc8(buf, q);
320 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700321 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800322
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700323 return buf;
324}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700325
326#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700327
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700328/*
329 * Convert passed number to decimal string.
330 * Returns the length of string. On buffer overflow, returns 0.
331 *
332 * If speed is not important, use snprintf(). It's easy to read the code.
333 */
334int num_to_str(char *buf, int size, unsigned long long num)
335{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700336 /* put_dec requires 2-byte alignment of the buffer. */
337 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700338 int idx, len;
339
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700340 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
341 if (num <= 9) {
342 tmp[0] = '0' + num;
343 len = 1;
344 } else {
345 len = put_dec(tmp, num) - tmp;
346 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700347
348 if (len > size)
349 return 0;
350 for (idx = 0; idx < len; ++idx)
351 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700352 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700353}
354
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700355#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700356#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#define PLUS 4 /* show plus */
358#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700359#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700360#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
361#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100363enum format_type {
364 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100365 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100366 FORMAT_TYPE_PRECISION,
367 FORMAT_TYPE_CHAR,
368 FORMAT_TYPE_STR,
369 FORMAT_TYPE_PTR,
370 FORMAT_TYPE_PERCENT_CHAR,
371 FORMAT_TYPE_INVALID,
372 FORMAT_TYPE_LONG_LONG,
373 FORMAT_TYPE_ULONG,
374 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800375 FORMAT_TYPE_UBYTE,
376 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100377 FORMAT_TYPE_USHORT,
378 FORMAT_TYPE_SHORT,
379 FORMAT_TYPE_UINT,
380 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100381 FORMAT_TYPE_SIZE_T,
382 FORMAT_TYPE_PTRDIFF
383};
384
385struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700386 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800387 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700388 u8 base; /* number base, 8, 10 or 16 only */
389 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
390 s16 field_width; /* width of output field */
391 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100392};
393
Joe Perchescf3b4292010-05-24 14:33:16 -0700394static noinline_for_stack
395char *number(char *buf, char *end, unsigned long long num,
396 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700398 /* put_dec requires 2-byte alignment of the buffer. */
399 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100400 char sign;
401 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100402 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700404 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100406 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
407 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100408 locase = (spec.flags & SMALL);
409 if (spec.flags & LEFT)
410 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100412 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800413 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800415 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100416 spec.field_width--;
417 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100419 spec.field_width--;
420 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100422 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700425 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100426 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700427 spec.field_width -= 2;
428 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100429 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700431
432 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700434 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700435 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100436 else if (spec.base != 10) { /* 8 or 16 */
437 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700438 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800439
440 if (spec.base == 16)
441 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700442 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700443 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700444 num >>= shift;
445 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700446 } else { /* base 10 */
447 i = put_dec(tmp, num) - tmp;
448 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700449
450 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100451 if (i > spec.precision)
452 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700453 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100454 spec.field_width -= spec.precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700455 if (!(spec.flags & (ZEROPAD | LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800456 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700457 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *buf = ' ';
459 ++buf;
460 }
461 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700462 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700464 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *buf = sign;
466 ++buf;
467 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700468 /* "0x" / "0" prefix */
469 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700470 if (spec.base == 16 || !is_zero) {
471 if (buf < end)
472 *buf = '0';
473 ++buf;
474 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100475 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700476 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100477 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ++buf;
479 }
480 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700481 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100482 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700483 char c = ' ' + (spec.flags & ZEROPAD);
484 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100485 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700486 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *buf = c;
488 ++buf;
489 }
490 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700491 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100492 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700493 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *buf = '0';
495 ++buf;
496 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700497 /* actual digits of result */
498 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700499 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *buf = tmp[i];
501 ++buf;
502 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700503 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100504 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700505 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *buf = ' ';
507 ++buf;
508 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return buf;
511}
512
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800513static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400514{
515 size_t size;
516 if (buf >= end) /* nowhere to put anything */
517 return;
518 size = end - buf;
519 if (size <= spaces) {
520 memset(buf, ' ', size);
521 return;
522 }
523 if (len) {
524 if (len > size - spaces)
525 len = size - spaces;
526 memmove(buf + spaces, buf, len);
527 }
528 memset(buf, ' ', spaces);
529}
530
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800531/*
532 * Handle field width padding for a string.
533 * @buf: current buffer position
534 * @n: length of string
535 * @end: end of output buffer
536 * @spec: for field width and flags
537 * Returns: new buffer position after padding.
538 */
539static noinline_for_stack
540char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
541{
542 unsigned spaces;
543
544 if (likely(n >= spec.field_width))
545 return buf;
546 /* we want to pad the sucker */
547 spaces = spec.field_width - n;
548 if (!(spec.flags & LEFT)) {
549 move_right(buf - n, end, n, spaces);
550 return buf + spaces;
551 }
552 while (spaces--) {
553 if (buf < end)
554 *buf = ' ';
555 ++buf;
556 }
557 return buf;
558}
559
Al Viro4b6ccca2013-09-03 12:00:44 -0400560static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800561char *string(char *buf, char *end, const char *s, struct printf_spec spec)
562{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800563 int len = 0;
564 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800565
566 if ((unsigned long)s < PAGE_SIZE)
567 s = "(null)";
568
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800569 while (lim--) {
570 char c = *s++;
571 if (!c)
572 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800573 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800574 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800575 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800576 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800577 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800578 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800579}
580
581static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400582char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
583 const char *fmt)
584{
585 const char *array[4], *s;
586 const struct dentry *p;
587 int depth;
588 int i, n;
589
590 switch (fmt[1]) {
591 case '2': case '3': case '4':
592 depth = fmt[1] - '0';
593 break;
594 default:
595 depth = 1;
596 }
597
598 rcu_read_lock();
599 for (i = 0; i < depth; i++, d = p) {
600 p = ACCESS_ONCE(d->d_parent);
601 array[i] = ACCESS_ONCE(d->d_name.name);
602 if (p == d) {
603 if (i)
604 array[i] = "";
605 i++;
606 break;
607 }
608 }
609 s = array[--i];
610 for (n = 0; n != spec.precision; n++, buf++) {
611 char c = *s++;
612 if (!c) {
613 if (!i)
614 break;
615 c = '/';
616 s = array[--i];
617 }
618 if (buf < end)
619 *buf = c;
620 }
621 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800622 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400623}
624
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400625#ifdef CONFIG_BLOCK
626static noinline_for_stack
627char *bdev_name(char *buf, char *end, struct block_device *bdev,
628 struct printf_spec spec, const char *fmt)
629{
630 struct gendisk *hd = bdev->bd_disk;
631
632 buf = string(buf, end, hd->disk_name, spec);
633 if (bdev->bd_part->partno) {
634 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
635 if (buf < end)
636 *buf = 'p';
637 buf++;
638 }
639 buf = number(buf, end, bdev->bd_part->partno, spec);
640 }
641 return buf;
642}
643#endif
644
Joe Perchescf3b4292010-05-24 14:33:16 -0700645static noinline_for_stack
646char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800647 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700648{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800649 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700650#ifdef CONFIG_KALLSYMS
651 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800652#endif
653
654 if (fmt[1] == 'R')
655 ptr = __builtin_extract_return_addr(ptr);
656 value = (unsigned long)ptr;
657
658#ifdef CONFIG_KALLSYMS
659 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900660 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800661 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200662 sprint_symbol(sym, value);
663 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700664 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800665
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100666 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700667#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800668 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100669 spec.flags |= SPECIAL | SMALL | ZEROPAD;
670 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800671
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100672 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700673#endif
674}
675
Joe Perchescf3b4292010-05-24 14:33:16 -0700676static noinline_for_stack
677char *resource_string(char *buf, char *end, struct resource *res,
678 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100679{
680#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600681#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100682#endif
683
684#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600685#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100686#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700687 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100688 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700689 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100690 .precision = -1,
691 .flags = SPECIAL | SMALL | ZEROPAD,
692 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700693 static const struct printf_spec mem_spec = {
694 .base = 16,
695 .field_width = MEM_RSRC_PRINTK_SIZE,
696 .precision = -1,
697 .flags = SPECIAL | SMALL | ZEROPAD,
698 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700699 static const struct printf_spec bus_spec = {
700 .base = 16,
701 .field_width = 2,
702 .precision = -1,
703 .flags = SMALL | ZEROPAD,
704 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700705 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600706 .base = 10,
707 .precision = -1,
708 .flags = 0,
709 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700710 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600711 .field_width = -1,
712 .precision = 10,
713 .flags = LEFT,
714 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700715 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600716 .base = 16,
717 .precision = -1,
718 .flags = SPECIAL | SMALL,
719 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600720
721 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
722 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
723#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
724#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700725#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600726#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
727 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
728 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
729
Linus Torvalds332d2e72008-10-20 15:07:34 +1100730 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600731 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700732 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100733
734 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700735 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600736 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700737 specp = &io_spec;
738 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600739 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700740 specp = &mem_spec;
741 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600742 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700743 specp = &dec_spec;
744 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600745 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700746 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700747 } else if (res->flags & IORESOURCE_BUS) {
748 p = string(p, pend, "bus ", str_spec);
749 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700750 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600751 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700752 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600753 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600754 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700755 if (decode && res->flags & IORESOURCE_UNSET) {
756 p = string(p, pend, "size ", str_spec);
757 p = number(p, pend, resource_size(res), *specp);
758 } else {
759 p = number(p, pend, res->start, *specp);
760 if (res->start != res->end) {
761 *p++ = '-';
762 p = number(p, pend, res->end, *specp);
763 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600764 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600765 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600766 if (res->flags & IORESOURCE_MEM_64)
767 p = string(p, pend, " 64bit", str_spec);
768 if (res->flags & IORESOURCE_PREFETCH)
769 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700770 if (res->flags & IORESOURCE_WINDOW)
771 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600772 if (res->flags & IORESOURCE_DISABLED)
773 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600774 } else {
775 p = string(p, pend, " flags ", str_spec);
776 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600777 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100778 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600779 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100780
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100781 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100782}
783
Joe Perchescf3b4292010-05-24 14:33:16 -0700784static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700785char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
786 const char *fmt)
787{
Steven Rostedt360603a2013-05-28 15:47:39 -0400788 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700789 negative value, fallback to the default */
790 char separator;
791
792 if (spec.field_width == 0)
793 /* nothing to print */
794 return buf;
795
796 if (ZERO_OR_NULL_PTR(addr))
797 /* NULL pointer */
798 return string(buf, end, NULL, spec);
799
800 switch (fmt[1]) {
801 case 'C':
802 separator = ':';
803 break;
804 case 'D':
805 separator = '-';
806 break;
807 case 'N':
808 separator = 0;
809 break;
810 default:
811 separator = ' ';
812 break;
813 }
814
815 if (spec.field_width > 0)
816 len = min_t(int, spec.field_width, 64);
817
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700818 for (i = 0; i < len; ++i) {
819 if (buf < end)
820 *buf = hex_asc_hi(addr[i]);
821 ++buf;
822 if (buf < end)
823 *buf = hex_asc_lo(addr[i]);
824 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700825
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700826 if (separator && i != len - 1) {
827 if (buf < end)
828 *buf = separator;
829 ++buf;
830 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700831 }
832
833 return buf;
834}
835
836static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800837char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
838 struct printf_spec spec, const char *fmt)
839{
840 const int CHUNKSZ = 32;
841 int nr_bits = max_t(int, spec.field_width, 0);
842 int i, chunksz;
843 bool first = true;
844
845 /* reused to print numbers */
846 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
847
848 chunksz = nr_bits & (CHUNKSZ - 1);
849 if (chunksz == 0)
850 chunksz = CHUNKSZ;
851
852 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
853 for (; i >= 0; i -= CHUNKSZ) {
854 u32 chunkmask, val;
855 int word, bit;
856
857 chunkmask = ((1ULL << chunksz) - 1);
858 word = i / BITS_PER_LONG;
859 bit = i % BITS_PER_LONG;
860 val = (bitmap[word] >> bit) & chunkmask;
861
862 if (!first) {
863 if (buf < end)
864 *buf = ',';
865 buf++;
866 }
867 first = false;
868
869 spec.field_width = DIV_ROUND_UP(chunksz, 4);
870 buf = number(buf, end, val, spec);
871
872 chunksz = CHUNKSZ;
873 }
874 return buf;
875}
876
877static noinline_for_stack
878char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
879 struct printf_spec spec, const char *fmt)
880{
881 int nr_bits = max_t(int, spec.field_width, 0);
882 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
883 int cur, rbot, rtop;
884 bool first = true;
885
886 /* reused to print numbers */
887 spec = (struct printf_spec){ .base = 10 };
888
889 rbot = cur = find_first_bit(bitmap, nr_bits);
890 while (cur < nr_bits) {
891 rtop = cur;
892 cur = find_next_bit(bitmap, nr_bits, cur + 1);
893 if (cur < nr_bits && cur <= rtop + 1)
894 continue;
895
896 if (!first) {
897 if (buf < end)
898 *buf = ',';
899 buf++;
900 }
901 first = false;
902
903 buf = number(buf, end, rbot, spec);
904 if (rbot < rtop) {
905 if (buf < end)
906 *buf = '-';
907 buf++;
908
909 buf = number(buf, end, rtop, spec);
910 }
911
912 rbot = cur;
913 }
914 return buf;
915}
916
917static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700918char *mac_address_string(char *buf, char *end, u8 *addr,
919 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700920{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000921 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700922 char *p = mac_addr;
923 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000924 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700925 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000926
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700927 switch (fmt[1]) {
928 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000929 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700930 break;
931
932 case 'R':
933 reversed = true;
934 /* fall through */
935
936 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000937 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700938 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000939 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700940
941 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700942 if (reversed)
943 p = hex_byte_pack(p, addr[5 - i]);
944 else
945 p = hex_byte_pack(p, addr[i]);
946
Joe Perches8a27f7c2009-08-17 12:29:44 +0000947 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000948 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700949 }
950 *p = '\0';
951
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100952 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700953}
954
Joe Perchescf3b4292010-05-24 14:33:16 -0700955static noinline_for_stack
956char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700957{
Harvey Harrison689afa72008-10-28 16:04:44 -0700958 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800959 bool leading_zeros = (fmt[0] == 'i');
960 int index;
961 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700962
Joe Perches0159f242010-01-13 20:23:30 -0800963 switch (fmt[2]) {
964 case 'h':
965#ifdef __BIG_ENDIAN
966 index = 0;
967 step = 1;
968#else
969 index = 3;
970 step = -1;
971#endif
972 break;
973 case 'l':
974 index = 3;
975 step = -1;
976 break;
977 case 'n':
978 case 'b':
979 default:
980 index = 0;
981 step = 1;
982 break;
983 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000984 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700985 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700986 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000987 if (leading_zeros) {
988 if (digits < 3)
989 *p++ = '0';
990 if (digits < 2)
991 *p++ = '0';
992 }
993 /* reverse the digits in the quad */
994 while (digits--)
995 *p++ = temp[digits];
996 if (i < 3)
997 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800998 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000999 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001000 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001001
Joe Perches8a27f7c2009-08-17 12:29:44 +00001002 return p;
1003}
1004
Joe Perchescf3b4292010-05-24 14:33:16 -07001005static noinline_for_stack
1006char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001007{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001008 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001009 unsigned char zerolength[8];
1010 int longest = 1;
1011 int colonpos = -1;
1012 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001013 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001014 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001015 bool useIPv4;
1016 struct in6_addr in6;
1017
1018 memcpy(&in6, addr, sizeof(struct in6_addr));
1019
1020 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001021
1022 memset(zerolength, 0, sizeof(zerolength));
1023
1024 if (useIPv4)
1025 range = 6;
1026 else
1027 range = 8;
1028
1029 /* find position of longest 0 run */
1030 for (i = 0; i < range; i++) {
1031 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001032 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001033 break;
1034 zerolength[i]++;
1035 }
1036 }
1037 for (i = 0; i < range; i++) {
1038 if (zerolength[i] > longest) {
1039 longest = zerolength[i];
1040 colonpos = i;
1041 }
1042 }
Joe Perches29cf5192011-06-09 11:23:37 -07001043 if (longest == 1) /* don't compress a single 0 */
1044 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001045
1046 /* emit address */
1047 for (i = 0; i < range; i++) {
1048 if (i == colonpos) {
1049 if (needcolon || i == 0)
1050 *p++ = ':';
1051 *p++ = ':';
1052 needcolon = false;
1053 i += longest - 1;
1054 continue;
1055 }
1056 if (needcolon) {
1057 *p++ = ':';
1058 needcolon = false;
1059 }
1060 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001061 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001062 hi = word >> 8;
1063 lo = word & 0xff;
1064 if (hi) {
1065 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001066 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001067 else
1068 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001069 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001070 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001071 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001072 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001073 else
1074 *p++ = hex_asc_lo(lo);
1075 needcolon = true;
1076 }
1077
1078 if (useIPv4) {
1079 if (needcolon)
1080 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001081 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001082 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001083 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001084
Joe Perches8a27f7c2009-08-17 12:29:44 +00001085 return p;
1086}
1087
Joe Perchescf3b4292010-05-24 14:33:16 -07001088static noinline_for_stack
1089char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001090{
1091 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001092
Harvey Harrison689afa72008-10-28 16:04:44 -07001093 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001094 p = hex_byte_pack(p, *addr++);
1095 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001096 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001097 *p++ = ':';
1098 }
1099 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001100
Joe Perches8a27f7c2009-08-17 12:29:44 +00001101 return p;
1102}
1103
Joe Perchescf3b4292010-05-24 14:33:16 -07001104static noinline_for_stack
1105char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1106 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001107{
1108 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1109
1110 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001111 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001112 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001113 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001114
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001115 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001116}
1117
Joe Perchescf3b4292010-05-24 14:33:16 -07001118static noinline_for_stack
1119char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1120 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001121{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001122 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001123
Joe Perches0159f242010-01-13 20:23:30 -08001124 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001125
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001126 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001127}
1128
Joe Perchescf3b4292010-05-24 14:33:16 -07001129static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001130char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1131 struct printf_spec spec, const char *fmt)
1132{
1133 bool have_p = false, have_s = false, have_f = false, have_c = false;
1134 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1135 sizeof(":12345") + sizeof("/123456789") +
1136 sizeof("%1234567890")];
1137 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1138 const u8 *addr = (const u8 *) &sa->sin6_addr;
1139 char fmt6[2] = { fmt[0], '6' };
1140 u8 off = 0;
1141
1142 fmt++;
1143 while (isalpha(*++fmt)) {
1144 switch (*fmt) {
1145 case 'p':
1146 have_p = true;
1147 break;
1148 case 'f':
1149 have_f = true;
1150 break;
1151 case 's':
1152 have_s = true;
1153 break;
1154 case 'c':
1155 have_c = true;
1156 break;
1157 }
1158 }
1159
1160 if (have_p || have_s || have_f) {
1161 *p = '[';
1162 off = 1;
1163 }
1164
1165 if (fmt6[0] == 'I' && have_c)
1166 p = ip6_compressed_string(ip6_addr + off, addr);
1167 else
1168 p = ip6_string(ip6_addr + off, addr, fmt6);
1169
1170 if (have_p || have_s || have_f)
1171 *p++ = ']';
1172
1173 if (have_p) {
1174 *p++ = ':';
1175 p = number(p, pend, ntohs(sa->sin6_port), spec);
1176 }
1177 if (have_f) {
1178 *p++ = '/';
1179 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1180 IPV6_FLOWINFO_MASK), spec);
1181 }
1182 if (have_s) {
1183 *p++ = '%';
1184 p = number(p, pend, sa->sin6_scope_id, spec);
1185 }
1186 *p = '\0';
1187
1188 return string(buf, end, ip6_addr, spec);
1189}
1190
1191static noinline_for_stack
1192char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1193 struct printf_spec spec, const char *fmt)
1194{
1195 bool have_p = false;
1196 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1197 char *pend = ip4_addr + sizeof(ip4_addr);
1198 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1199 char fmt4[3] = { fmt[0], '4', 0 };
1200
1201 fmt++;
1202 while (isalpha(*++fmt)) {
1203 switch (*fmt) {
1204 case 'p':
1205 have_p = true;
1206 break;
1207 case 'h':
1208 case 'l':
1209 case 'n':
1210 case 'b':
1211 fmt4[2] = *fmt;
1212 break;
1213 }
1214 }
1215
1216 p = ip4_string(ip4_addr, addr, fmt4);
1217 if (have_p) {
1218 *p++ = ':';
1219 p = number(p, pend, ntohs(sa->sin_port), spec);
1220 }
1221 *p = '\0';
1222
1223 return string(buf, end, ip4_addr, spec);
1224}
1225
1226static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001227char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1228 const char *fmt)
1229{
1230 bool found = true;
1231 int count = 1;
1232 unsigned int flags = 0;
1233 int len;
1234
1235 if (spec.field_width == 0)
1236 return buf; /* nothing to print */
1237
1238 if (ZERO_OR_NULL_PTR(addr))
1239 return string(buf, end, NULL, spec); /* NULL pointer */
1240
1241
1242 do {
1243 switch (fmt[count++]) {
1244 case 'a':
1245 flags |= ESCAPE_ANY;
1246 break;
1247 case 'c':
1248 flags |= ESCAPE_SPECIAL;
1249 break;
1250 case 'h':
1251 flags |= ESCAPE_HEX;
1252 break;
1253 case 'n':
1254 flags |= ESCAPE_NULL;
1255 break;
1256 case 'o':
1257 flags |= ESCAPE_OCTAL;
1258 break;
1259 case 'p':
1260 flags |= ESCAPE_NP;
1261 break;
1262 case 's':
1263 flags |= ESCAPE_SPACE;
1264 break;
1265 default:
1266 found = false;
1267 break;
1268 }
1269 } while (found);
1270
1271 if (!flags)
1272 flags = ESCAPE_ANY_NP;
1273
1274 len = spec.field_width < 0 ? 1 : spec.field_width;
1275
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001276 /*
1277 * string_escape_mem() writes as many characters as it can to
1278 * the given buffer, and returns the total size of the output
1279 * had the buffer been big enough.
1280 */
1281 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001282
1283 return buf;
1284}
1285
1286static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001287char *uuid_string(char *buf, char *end, const u8 *addr,
1288 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001289{
1290 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1291 char *p = uuid;
1292 int i;
1293 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1294 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1295 const u8 *index = be;
1296 bool uc = false;
1297
1298 switch (*(++fmt)) {
1299 case 'L':
1300 uc = true; /* fall-through */
1301 case 'l':
1302 index = le;
1303 break;
1304 case 'B':
1305 uc = true;
1306 break;
1307 }
1308
1309 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001310 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001311 switch (i) {
1312 case 3:
1313 case 5:
1314 case 7:
1315 case 9:
1316 *p++ = '-';
1317 break;
1318 }
1319 }
1320
1321 *p = 0;
1322
1323 if (uc) {
1324 p = uuid;
1325 do {
1326 *p = toupper(*p);
1327 } while (*(++p));
1328 }
1329
1330 return string(buf, end, uuid, spec);
1331}
1332
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001333static
1334char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1335 struct printf_spec spec)
1336{
1337 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1338 if (spec.field_width == -1)
1339 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1340 spec.base = 16;
1341
1342 return number(buf, end, *(const netdev_features_t *)addr, spec);
1343}
1344
Joe Perchesaaf07622014-01-23 15:54:17 -08001345static noinline_for_stack
1346char *address_val(char *buf, char *end, const void *addr,
1347 struct printf_spec spec, const char *fmt)
1348{
1349 unsigned long long num;
1350
1351 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1352 spec.base = 16;
1353
1354 switch (fmt[1]) {
1355 case 'd':
1356 num = *(const dma_addr_t *)addr;
1357 spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1358 break;
1359 case 'p':
1360 default:
1361 num = *(const phys_addr_t *)addr;
1362 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1363 break;
1364 }
1365
1366 return number(buf, end, num, spec);
1367}
1368
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001369static noinline_for_stack
1370char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1371 const char *fmt)
1372{
1373 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1374 return string(buf, end, NULL, spec);
1375
1376 switch (fmt[1]) {
1377 case 'r':
1378 return number(buf, end, clk_get_rate(clk), spec);
1379
1380 case 'n':
1381 default:
1382#ifdef CONFIG_COMMON_CLK
1383 return string(buf, end, __clk_get_name(clk), spec);
1384#else
1385 spec.base = 16;
1386 spec.field_width = sizeof(unsigned long) * 2 + 2;
1387 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1388 return number(buf, end, (unsigned long)clk, spec);
1389#endif
1390 }
1391}
1392
Ingo Molnar411f05f2011-05-12 23:00:28 +02001393int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001394
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001395/*
1396 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1397 * by an extra set of alphanumeric characters that are extended format
1398 * specifiers.
1399 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001400 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001401 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001402 * - 'F' For symbolic function descriptor pointers with offset
1403 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001404 * - 'S' For symbolic direct pointers with offset
1405 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001406 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001407 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001408 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1409 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001410 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1411 * width which must be explicitly specified either as part of the
1412 * format string '%32b[l]' or through '%*b[l]', [l] selects
1413 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001414 * - 'M' For a 6-byte MAC address, it prints the address in the
1415 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001416 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001417 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001418 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001419 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001420 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1421 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1422 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001423 * [S][pfs]
1424 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1425 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001426 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1427 * IPv6 omits the colons (01020304...0f)
1428 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001429 * [S][pfs]
1430 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1431 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1432 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1433 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001434 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001435 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1436 * of the following flags (see string_escape_mem() for the
1437 * details):
1438 * a - ESCAPE_ANY
1439 * c - ESCAPE_SPECIAL
1440 * h - ESCAPE_HEX
1441 * n - ESCAPE_NULL
1442 * o - ESCAPE_OCTAL
1443 * p - ESCAPE_NP
1444 * s - ESCAPE_SPACE
1445 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001446 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1447 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1448 * Options for %pU are:
1449 * b big endian lower case hex (default)
1450 * B big endian UPPER case hex
1451 * l little endian lower case hex
1452 * L little endian UPPER case hex
1453 * big endian output byte order is:
1454 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1455 * little endian output byte order is:
1456 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001457 * - 'V' For a struct va_format which contains a format string * and va_list *,
1458 * call vsnprintf(->format, *->va_list).
1459 * Implements a "recursive vsnprintf".
1460 * Do not use this feature without some mechanism to verify the
1461 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001462 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001463 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001464 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1465 * a certain separator (' ' by default):
1466 * C colon
1467 * D dash
1468 * N no separator
1469 * The maximum supported length is 64 bytes of the input. Consider
1470 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001471 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1472 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001473 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1474 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001475 * - 'g' For block_device name (gendisk + partition number)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001476 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1477 * (legacy clock framework) of the clock
1478 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1479 * (legacy clock framework) of the clock
1480 * - 'Cr' For a clock, it prints the current rate of the clock
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001481 *
1482 * ** Please update also Documentation/printk-formats.txt when making changes **
Joe Perches9ac6e442009-12-14 18:01:09 -08001483 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001484 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1485 * function pointers are really function descriptors, which contain a
1486 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001487 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001488static noinline_for_stack
1489char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1490 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001491{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001492 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001493
Kees Cook9f36e2c2011-03-22 16:34:22 -07001494 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001495 /*
1496 * Print (null) with the same width as a pointer so it makes
1497 * tabular output look nice.
1498 */
1499 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001500 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001501 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001502 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001503
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001504 switch (*fmt) {
1505 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001506 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001507 ptr = dereference_function_descriptor(ptr);
1508 /* Fallthrough */
1509 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001510 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001511 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001512 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001513 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001514 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001515 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001516 case 'h':
1517 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001518 case 'b':
1519 switch (fmt[1]) {
1520 case 'l':
1521 return bitmap_list_string(buf, end, ptr, spec, fmt);
1522 default:
1523 return bitmap_string(buf, end, ptr, spec, fmt);
1524 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001525 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1526 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001527 /* [mM]F (FDDI) */
1528 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001529 return mac_address_string(buf, end, ptr, spec, fmt);
1530 case 'I': /* Formatted IP supported
1531 * 4: 1.2.3.4
1532 * 6: 0001:0203:...:0708
1533 * 6c: 1::708 or 1::1.2.3.4
1534 */
1535 case 'i': /* Contiguous:
1536 * 4: 001.002.003.004
1537 * 6: 000102...0f
1538 */
1539 switch (fmt[1]) {
1540 case '6':
1541 return ip6_addr_string(buf, end, ptr, spec, fmt);
1542 case '4':
1543 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001544 case 'S': {
1545 const union {
1546 struct sockaddr raw;
1547 struct sockaddr_in v4;
1548 struct sockaddr_in6 v6;
1549 } *sa = ptr;
1550
1551 switch (sa->raw.sa_family) {
1552 case AF_INET:
1553 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1554 case AF_INET6:
1555 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1556 default:
1557 return string(buf, end, "(invalid address)", spec);
1558 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001559 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001560 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001561 case 'E':
1562 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001563 case 'U':
1564 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001565 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001566 {
1567 va_list va;
1568
1569 va_copy(va, *((struct va_format *)ptr)->va);
1570 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1571 ((struct va_format *)ptr)->fmt, va);
1572 va_end(va);
1573 return buf;
1574 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001575 case 'K':
1576 /*
1577 * %pK cannot be used in IRQ context because its test
1578 * for CAP_SYSLOG would be meaningless.
1579 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001580 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1581 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001582 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001583 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001584 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001585 }
Ryan Mallon312b4e22013-11-12 15:08:51 -08001586
1587 switch (kptr_restrict) {
1588 case 0:
1589 /* Always print %pK values */
1590 break;
1591 case 1: {
1592 /*
1593 * Only print the real pointer value if the current
1594 * process has CAP_SYSLOG and is running with the
1595 * same credentials it started with. This is because
1596 * access to files is checked at open() time, but %pK
1597 * checks permission at read() time. We don't want to
1598 * leak pointer values if a binary opens a file using
1599 * %pK and then elevates privileges before reading it.
1600 */
1601 const struct cred *cred = current_cred();
1602
1603 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1604 !uid_eq(cred->euid, cred->uid) ||
1605 !gid_eq(cred->egid, cred->gid))
1606 ptr = NULL;
1607 break;
1608 }
1609 case 2:
1610 default:
1611 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -07001612 ptr = NULL;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001613 break;
1614 }
Joe Perches26297602011-03-22 16:34:19 -07001615 break;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001616
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001617 case 'N':
1618 switch (fmt[1]) {
1619 case 'F':
1620 return netdev_feature_string(buf, end, ptr, spec);
1621 }
1622 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001623 case 'a':
Joe Perchesaaf07622014-01-23 15:54:17 -08001624 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001625 case 'd':
1626 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001627 case 'C':
1628 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001629 case 'D':
1630 return dentry_name(buf, end,
1631 ((const struct file *)ptr)->f_path.dentry,
1632 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001633#ifdef CONFIG_BLOCK
1634 case 'g':
1635 return bdev_name(buf, end, ptr, spec, fmt);
1636#endif
1637
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001638 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001639 spec.flags |= SMALL;
1640 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001641 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001642 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001643 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001644 spec.base = 16;
1645
1646 return number(buf, end, (unsigned long) ptr, spec);
1647}
1648
1649/*
1650 * Helper function to decode printf style format.
1651 * Each call decode a token from the format and return the
1652 * number of characters read (or likely the delta where it wants
1653 * to go on the next call).
1654 * The decoded token is returned through the parameters
1655 *
1656 * 'h', 'l', or 'L' for integer fields
1657 * 'z' support added 23/7/1999 S.H.
1658 * 'z' changed to 'Z' --davidm 1/25/99
1659 * 't' added for ptrdiff_t
1660 *
1661 * @fmt: the format string
1662 * @type of the token returned
1663 * @flags: various flags such as +, -, # tokens..
1664 * @field_width: overwritten width
1665 * @base: base of the number (octal, hex, ...)
1666 * @precision: precision of a number
1667 * @qualifier: qualifier of a number (long, size_t, ...)
1668 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001669static noinline_for_stack
1670int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001671{
1672 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001673
1674 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001675 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001676 if (spec->field_width < 0) {
1677 spec->field_width = -spec->field_width;
1678 spec->flags |= LEFT;
1679 }
1680 spec->type = FORMAT_TYPE_NONE;
1681 goto precision;
1682 }
1683
1684 /* we finished early by reading the precision */
1685 if (spec->type == FORMAT_TYPE_PRECISION) {
1686 if (spec->precision < 0)
1687 spec->precision = 0;
1688
1689 spec->type = FORMAT_TYPE_NONE;
1690 goto qualifier;
1691 }
1692
1693 /* By default */
1694 spec->type = FORMAT_TYPE_NONE;
1695
1696 for (; *fmt ; ++fmt) {
1697 if (*fmt == '%')
1698 break;
1699 }
1700
1701 /* Return the current non-format string */
1702 if (fmt != start || !*fmt)
1703 return fmt - start;
1704
1705 /* Process flags */
1706 spec->flags = 0;
1707
1708 while (1) { /* this also skips first '%' */
1709 bool found = true;
1710
1711 ++fmt;
1712
1713 switch (*fmt) {
1714 case '-': spec->flags |= LEFT; break;
1715 case '+': spec->flags |= PLUS; break;
1716 case ' ': spec->flags |= SPACE; break;
1717 case '#': spec->flags |= SPECIAL; break;
1718 case '0': spec->flags |= ZEROPAD; break;
1719 default: found = false;
1720 }
1721
1722 if (!found)
1723 break;
1724 }
1725
1726 /* get field width */
1727 spec->field_width = -1;
1728
1729 if (isdigit(*fmt))
1730 spec->field_width = skip_atoi(&fmt);
1731 else if (*fmt == '*') {
1732 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001733 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001734 return ++fmt - start;
1735 }
1736
1737precision:
1738 /* get the precision */
1739 spec->precision = -1;
1740 if (*fmt == '.') {
1741 ++fmt;
1742 if (isdigit(*fmt)) {
1743 spec->precision = skip_atoi(&fmt);
1744 if (spec->precision < 0)
1745 spec->precision = 0;
1746 } else if (*fmt == '*') {
1747 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001748 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001749 return ++fmt - start;
1750 }
1751 }
1752
1753qualifier:
1754 /* get the conversion qualifier */
1755 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001756 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1757 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001758 spec->qualifier = *fmt++;
1759 if (unlikely(spec->qualifier == *fmt)) {
1760 if (spec->qualifier == 'l') {
1761 spec->qualifier = 'L';
1762 ++fmt;
1763 } else if (spec->qualifier == 'h') {
1764 spec->qualifier = 'H';
1765 ++fmt;
1766 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001767 }
1768 }
1769
1770 /* default base */
1771 spec->base = 10;
1772 switch (*fmt) {
1773 case 'c':
1774 spec->type = FORMAT_TYPE_CHAR;
1775 return ++fmt - start;
1776
1777 case 's':
1778 spec->type = FORMAT_TYPE_STR;
1779 return ++fmt - start;
1780
1781 case 'p':
1782 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001783 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001784
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001785 case '%':
1786 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1787 return ++fmt - start;
1788
1789 /* integer number formats - set up the flags and "break" */
1790 case 'o':
1791 spec->base = 8;
1792 break;
1793
1794 case 'x':
1795 spec->flags |= SMALL;
1796
1797 case 'X':
1798 spec->base = 16;
1799 break;
1800
1801 case 'd':
1802 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001803 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001804 case 'u':
1805 break;
1806
Ryan Mallon708d96f2014-04-03 14:48:37 -07001807 case 'n':
1808 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001809 * Since %n poses a greater security risk than
1810 * utility, treat it as any other invalid or
1811 * unsupported format specifier.
Ryan Mallon708d96f2014-04-03 14:48:37 -07001812 */
Ryan Mallon708d96f2014-04-03 14:48:37 -07001813 /* Fall-through */
1814
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001815 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001816 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001817 spec->type = FORMAT_TYPE_INVALID;
1818 return fmt - start;
1819 }
1820
1821 if (spec->qualifier == 'L')
1822 spec->type = FORMAT_TYPE_LONG_LONG;
1823 else if (spec->qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001824 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1825 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001826 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001827 spec->type = FORMAT_TYPE_SIZE_T;
1828 } else if (spec->qualifier == 't') {
1829 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001830 } else if (spec->qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001831 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1832 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001833 } else if (spec->qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001834 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1835 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001836 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001837 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1838 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001839 }
1840
1841 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844/**
1845 * vsnprintf - Format a string and place it in a buffer
1846 * @buf: The buffer to place the result into
1847 * @size: The size of the buffer, including the trailing null space
1848 * @fmt: The format string to use
1849 * @args: Arguments for the format string
1850 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08001851 * This function generally follows C99 vsnprintf, but has some
1852 * extensions and a few limitations:
1853 *
1854 * %n is unsupported
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001855 * %p* is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07001856 *
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001857 * See pointer() or Documentation/printk-formats.txt for more
1858 * extensive description.
1859 *
1860 * ** Please update the documentation in both places when making changes **
Andrew Morton80f548e2012-07-30 14:40:25 -07001861 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * The return value is the number of characters which would
1863 * be generated for the given input, excluding the trailing
1864 * '\0', as per ISO C99. If you want to have the exact
1865 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001866 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * return is greater than or equal to @size, the resulting
1868 * string is truncated.
1869 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001870 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
1872int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1873{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001875 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001876 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001878 /* Reject out-of-range values early. Large positive sizes are
1879 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08001880 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001884 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001886 /* Make sure end is always >= buf */
1887 if (end < buf) {
1888 end = ((void *)-1);
1889 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001892 while (*fmt) {
1893 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001894 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001895
1896 fmt += read;
1897
1898 switch (spec.type) {
1899 case FORMAT_TYPE_NONE: {
1900 int copy = read;
1901 if (str < end) {
1902 if (copy > end - str)
1903 copy = end - str;
1904 memcpy(str, old_fmt, copy);
1905 }
1906 str += read;
1907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
Vegard Nossumed681a92009-03-14 12:08:50 +01001910 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001911 spec.field_width = va_arg(args, int);
1912 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001914 case FORMAT_TYPE_PRECISION:
1915 spec.precision = va_arg(args, int);
1916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
André Goddard Rosad4be1512009-12-14 18:00:59 -08001918 case FORMAT_TYPE_CHAR: {
1919 char c;
1920
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001921 if (!(spec.flags & LEFT)) {
1922 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001923 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 *str = ' ';
1925 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001928 }
1929 c = (unsigned char) va_arg(args, int);
1930 if (str < end)
1931 *str = c;
1932 ++str;
1933 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001934 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001935 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001937 }
1938 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001941 case FORMAT_TYPE_STR:
1942 str = string(str, end, va_arg(args, char *), spec);
1943 break;
1944
1945 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001946 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001947 spec);
1948 while (isalnum(*fmt))
1949 fmt++;
1950 break;
1951
1952 case FORMAT_TYPE_PERCENT_CHAR:
1953 if (str < end)
1954 *str = '%';
1955 ++str;
1956 break;
1957
1958 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001959 /*
1960 * Presumably the arguments passed gcc's type
1961 * checking, but there is no safe or sane way
1962 * for us to continue parsing the format and
1963 * fetching from the va_list; the remaining
1964 * specifiers and arguments would be out of
1965 * sync.
1966 */
1967 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001968
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001969 default:
1970 switch (spec.type) {
1971 case FORMAT_TYPE_LONG_LONG:
1972 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001974 case FORMAT_TYPE_ULONG:
1975 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001977 case FORMAT_TYPE_LONG:
1978 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001980 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001981 if (spec.flags & SIGN)
1982 num = va_arg(args, ssize_t);
1983 else
1984 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001985 break;
1986 case FORMAT_TYPE_PTRDIFF:
1987 num = va_arg(args, ptrdiff_t);
1988 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001989 case FORMAT_TYPE_UBYTE:
1990 num = (unsigned char) va_arg(args, int);
1991 break;
1992 case FORMAT_TYPE_BYTE:
1993 num = (signed char) va_arg(args, int);
1994 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001995 case FORMAT_TYPE_USHORT:
1996 num = (unsigned short) va_arg(args, int);
1997 break;
1998 case FORMAT_TYPE_SHORT:
1999 num = (short) va_arg(args, int);
2000 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002001 case FORMAT_TYPE_INT:
2002 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002005 num = va_arg(args, unsigned int);
2006 }
2007
2008 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002011
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002012out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002013 if (size > 0) {
2014 if (str < end)
2015 *str = '\0';
2016 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002017 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002018 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002019
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002020 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024EXPORT_SYMBOL(vsnprintf);
2025
2026/**
2027 * vscnprintf - Format a string and place it in a buffer
2028 * @buf: The buffer to place the result into
2029 * @size: The size of the buffer, including the trailing null space
2030 * @fmt: The format string to use
2031 * @args: Arguments for the format string
2032 *
2033 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002034 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 * returns 0.
2036 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002037 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002038 *
2039 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 */
2041int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2042{
2043 int i;
2044
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002045 i = vsnprintf(buf, size, fmt, args);
2046
Anton Arapovb921c692011-01-12 16:59:49 -08002047 if (likely(i < size))
2048 return i;
2049 if (size != 0)
2050 return size - 1;
2051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053EXPORT_SYMBOL(vscnprintf);
2054
2055/**
2056 * snprintf - Format a string and place it in a buffer
2057 * @buf: The buffer to place the result into
2058 * @size: The size of the buffer, including the trailing null space
2059 * @fmt: The format string to use
2060 * @...: Arguments for the format string
2061 *
2062 * The return value is the number of characters which would be
2063 * generated for the given input, excluding the trailing null,
2064 * as per ISO C99. If the return is greater than or equal to
2065 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002066 *
2067 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002069int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 va_list args;
2072 int i;
2073
2074 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002075 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return i;
2079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080EXPORT_SYMBOL(snprintf);
2081
2082/**
2083 * scnprintf - Format a string and place it in a buffer
2084 * @buf: The buffer to place the result into
2085 * @size: The size of the buffer, including the trailing null space
2086 * @fmt: The format string to use
2087 * @...: Arguments for the format string
2088 *
2089 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002090 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 */
2092
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002093int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 va_list args;
2096 int i;
2097
2098 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002099 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002101
Anton Arapovb921c692011-01-12 16:59:49 -08002102 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104EXPORT_SYMBOL(scnprintf);
2105
2106/**
2107 * vsprintf - Format a string and place it in a buffer
2108 * @buf: The buffer to place the result into
2109 * @fmt: The format string to use
2110 * @args: Arguments for the format string
2111 *
2112 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002113 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 * buffer overflows.
2115 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002116 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002117 *
2118 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 */
2120int vsprintf(char *buf, const char *fmt, va_list args)
2121{
2122 return vsnprintf(buf, INT_MAX, fmt, args);
2123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124EXPORT_SYMBOL(vsprintf);
2125
2126/**
2127 * sprintf - Format a string and place it in a buffer
2128 * @buf: The buffer to place the result into
2129 * @fmt: The format string to use
2130 * @...: Arguments for the format string
2131 *
2132 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002133 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002135 *
2136 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002138int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 va_list args;
2141 int i;
2142
2143 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002144 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return i;
2148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149EXPORT_SYMBOL(sprintf);
2150
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002151#ifdef CONFIG_BINARY_PRINTF
2152/*
2153 * bprintf service:
2154 * vbin_printf() - VA arguments to binary data
2155 * bstr_printf() - Binary data to text string
2156 */
2157
2158/**
2159 * vbin_printf - Parse a format string and place args' binary value in a buffer
2160 * @bin_buf: The buffer to place args' binary value
2161 * @size: The size of the buffer(by words(32bits), not characters)
2162 * @fmt: The format string to use
2163 * @args: Arguments for the format string
2164 *
2165 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002166 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002167 *
2168 * The return value is the number of words(32bits) which would be generated for
2169 * the given input.
2170 *
2171 * NOTE:
2172 * If the return value is greater than @size, the resulting bin_buf is NOT
2173 * valid for bstr_printf().
2174 */
2175int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2176{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002177 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002178 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002179
2180 str = (char *)bin_buf;
2181 end = (char *)(bin_buf + size);
2182
2183#define save_arg(type) \
2184do { \
2185 if (sizeof(type) == 8) { \
2186 unsigned long long value; \
2187 str = PTR_ALIGN(str, sizeof(u32)); \
2188 value = va_arg(args, unsigned long long); \
2189 if (str + sizeof(type) <= end) { \
2190 *(u32 *)str = *(u32 *)&value; \
2191 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2192 } \
2193 } else { \
2194 unsigned long value; \
2195 str = PTR_ALIGN(str, sizeof(type)); \
2196 value = va_arg(args, int); \
2197 if (str + sizeof(type) <= end) \
2198 *(typeof(type) *)str = (type)value; \
2199 } \
2200 str += sizeof(type); \
2201} while (0)
2202
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002203 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002204 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002205
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002206 fmt += read;
2207
2208 switch (spec.type) {
2209 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002210 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002211 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002212 case FORMAT_TYPE_INVALID:
2213 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002214
Vegard Nossumed681a92009-03-14 12:08:50 +01002215 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002216 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002217 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002218 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002219
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002220 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002221 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002222 break;
2223
2224 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002225 const char *save_str = va_arg(args, char *);
2226 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002227
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002228 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2229 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002230 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002231 len = strlen(save_str) + 1;
2232 if (str + len < end)
2233 memcpy(str, save_str, len);
2234 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002235 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002236 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002237
2238 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002239 save_arg(void *);
2240 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002241 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002242 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002243 break;
2244
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002245 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002246 switch (spec.type) {
2247
2248 case FORMAT_TYPE_LONG_LONG:
2249 save_arg(long long);
2250 break;
2251 case FORMAT_TYPE_ULONG:
2252 case FORMAT_TYPE_LONG:
2253 save_arg(unsigned long);
2254 break;
2255 case FORMAT_TYPE_SIZE_T:
2256 save_arg(size_t);
2257 break;
2258 case FORMAT_TYPE_PTRDIFF:
2259 save_arg(ptrdiff_t);
2260 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002261 case FORMAT_TYPE_UBYTE:
2262 case FORMAT_TYPE_BYTE:
2263 save_arg(char);
2264 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002265 case FORMAT_TYPE_USHORT:
2266 case FORMAT_TYPE_SHORT:
2267 save_arg(short);
2268 break;
2269 default:
2270 save_arg(int);
2271 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002272 }
2273 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002274
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002275out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002276 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002277#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002278}
2279EXPORT_SYMBOL_GPL(vbin_printf);
2280
2281/**
2282 * bstr_printf - Format a string from binary arguments and place it in a buffer
2283 * @buf: The buffer to place the result into
2284 * @size: The size of the buffer, including the trailing null space
2285 * @fmt: The format string to use
2286 * @bin_buf: Binary arguments for the format string
2287 *
2288 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2289 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2290 * a binary buffer that generated by vbin_printf.
2291 *
2292 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002293 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002294 *
2295 * The return value is the number of characters which would
2296 * be generated for the given input, excluding the trailing
2297 * '\0', as per ISO C99. If you want to have the exact
2298 * number of characters written into @buf as return value
2299 * (not including the trailing '\0'), use vscnprintf(). If the
2300 * return is greater than or equal to @size, the resulting
2301 * string is truncated.
2302 */
2303int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2304{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002305 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002306 char *str, *end;
2307 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002308
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002309 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002310 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002311
2312 str = buf;
2313 end = buf + size;
2314
2315#define get_arg(type) \
2316({ \
2317 typeof(type) value; \
2318 if (sizeof(type) == 8) { \
2319 args = PTR_ALIGN(args, sizeof(u32)); \
2320 *(u32 *)&value = *(u32 *)args; \
2321 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2322 } else { \
2323 args = PTR_ALIGN(args, sizeof(type)); \
2324 value = *(typeof(type) *)args; \
2325 } \
2326 args += sizeof(type); \
2327 value; \
2328})
2329
2330 /* Make sure end is always >= buf */
2331 if (end < buf) {
2332 end = ((void *)-1);
2333 size = end - buf;
2334 }
2335
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002336 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002337 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002338 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002339
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002340 fmt += read;
2341
2342 switch (spec.type) {
2343 case FORMAT_TYPE_NONE: {
2344 int copy = read;
2345 if (str < end) {
2346 if (copy > end - str)
2347 copy = end - str;
2348 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002349 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002350 str += read;
2351 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002352 }
2353
Vegard Nossumed681a92009-03-14 12:08:50 +01002354 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002355 spec.field_width = get_arg(int);
2356 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002357
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002358 case FORMAT_TYPE_PRECISION:
2359 spec.precision = get_arg(int);
2360 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002361
André Goddard Rosad4be1512009-12-14 18:00:59 -08002362 case FORMAT_TYPE_CHAR: {
2363 char c;
2364
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002365 if (!(spec.flags & LEFT)) {
2366 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002367 if (str < end)
2368 *str = ' ';
2369 ++str;
2370 }
2371 }
2372 c = (unsigned char) get_arg(char);
2373 if (str < end)
2374 *str = c;
2375 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002376 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002377 if (str < end)
2378 *str = ' ';
2379 ++str;
2380 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002381 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002382 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002383
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002384 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002385 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002386 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002387 str = string(str, end, (char *)str_arg, spec);
2388 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002389 }
2390
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002391 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002392 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002393 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002394 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002395 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002396
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002397 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002398 if (str < end)
2399 *str = '%';
2400 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002401 break;
2402
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002403 case FORMAT_TYPE_INVALID:
2404 goto out;
2405
André Goddard Rosad4be1512009-12-14 18:00:59 -08002406 default: {
2407 unsigned long long num;
2408
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002409 switch (spec.type) {
2410
2411 case FORMAT_TYPE_LONG_LONG:
2412 num = get_arg(long long);
2413 break;
2414 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002415 case FORMAT_TYPE_LONG:
2416 num = get_arg(unsigned long);
2417 break;
2418 case FORMAT_TYPE_SIZE_T:
2419 num = get_arg(size_t);
2420 break;
2421 case FORMAT_TYPE_PTRDIFF:
2422 num = get_arg(ptrdiff_t);
2423 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002424 case FORMAT_TYPE_UBYTE:
2425 num = get_arg(unsigned char);
2426 break;
2427 case FORMAT_TYPE_BYTE:
2428 num = get_arg(signed char);
2429 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002430 case FORMAT_TYPE_USHORT:
2431 num = get_arg(unsigned short);
2432 break;
2433 case FORMAT_TYPE_SHORT:
2434 num = get_arg(short);
2435 break;
2436 case FORMAT_TYPE_UINT:
2437 num = get_arg(unsigned int);
2438 break;
2439 default:
2440 num = get_arg(int);
2441 }
2442
2443 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002444 } /* default: */
2445 } /* switch(spec.type) */
2446 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002447
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002448out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002449 if (size > 0) {
2450 if (str < end)
2451 *str = '\0';
2452 else
2453 end[-1] = '\0';
2454 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002455
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002456#undef get_arg
2457
2458 /* the trailing null byte doesn't count towards the total */
2459 return str - buf;
2460}
2461EXPORT_SYMBOL_GPL(bstr_printf);
2462
2463/**
2464 * bprintf - Parse a format string and place args' binary value in a buffer
2465 * @bin_buf: The buffer to place args' binary value
2466 * @size: The size of the buffer(by words(32bits), not characters)
2467 * @fmt: The format string to use
2468 * @...: Arguments for the format string
2469 *
2470 * The function returns the number of words(u32) written
2471 * into @bin_buf.
2472 */
2473int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2474{
2475 va_list args;
2476 int ret;
2477
2478 va_start(args, fmt);
2479 ret = vbin_printf(bin_buf, size, fmt, args);
2480 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002481
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002482 return ret;
2483}
2484EXPORT_SYMBOL_GPL(bprintf);
2485
2486#endif /* CONFIG_BINARY_PRINTF */
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/**
2489 * vsscanf - Unformat a buffer into a list of arguments
2490 * @buf: input buffer
2491 * @fmt: format of buffer
2492 * @args: arguments
2493 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002494int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
2496 const char *str = buf;
2497 char *next;
2498 char digit;
2499 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002500 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002501 unsigned int base;
2502 union {
2503 long long s;
2504 unsigned long long u;
2505 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002506 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002507 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Jan Beulichda990752012-10-04 17:13:24 -07002509 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 /* skip any white space in format */
2511 /* white space in format matchs any amount of
2512 * white space, including none, in the input.
2513 */
2514 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002515 fmt = skip_spaces(++fmt);
2516 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518
2519 /* anything that is not a conversion must match exactly */
2520 if (*fmt != '%' && *fmt) {
2521 if (*fmt++ != *str++)
2522 break;
2523 continue;
2524 }
2525
2526 if (!*fmt)
2527 break;
2528 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /* skip this conversion.
2531 * advance both strings to next white space
2532 */
2533 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002534 if (!*str)
2535 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002536 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 fmt++;
2538 while (!isspace(*str) && *str)
2539 str++;
2540 continue;
2541 }
2542
2543 /* get field width */
2544 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002545 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002547 if (field_width <= 0)
2548 break;
2549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /* get conversion qualifier */
2552 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002553 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2554 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 qualifier = *fmt++;
2556 if (unlikely(qualifier == *fmt)) {
2557 if (qualifier == 'h') {
2558 qualifier = 'H';
2559 fmt++;
2560 } else if (qualifier == 'l') {
2561 qualifier = 'L';
2562 fmt++;
2563 }
2564 }
2565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Jan Beulichda990752012-10-04 17:13:24 -07002567 if (!*fmt)
2568 break;
2569
2570 if (*fmt == 'n') {
2571 /* return number of characters read so far */
2572 *va_arg(args, int *) = str - buf;
2573 ++fmt;
2574 continue;
2575 }
2576
2577 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 break;
2579
André Goddard Rosad4be1512009-12-14 18:00:59 -08002580 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002581 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002582
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002583 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 case 'c':
2585 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002586 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (field_width == -1)
2588 field_width = 1;
2589 do {
2590 *s++ = *str++;
2591 } while (--field_width > 0 && *str);
2592 num++;
2593 }
2594 continue;
2595 case 's':
2596 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002597 char *s = (char *)va_arg(args, char *);
2598 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002599 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002601 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002604 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 *s = '\0';
2607 num++;
2608 }
2609 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 case 'o':
2611 base = 8;
2612 break;
2613 case 'x':
2614 case 'X':
2615 base = 16;
2616 break;
2617 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002618 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002620 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 case 'u':
2622 break;
2623 case '%':
2624 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002625 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return num;
2627 continue;
2628 default:
2629 /* invalid format; stop here */
2630 return num;
2631 }
2632
2633 /* have some sort of integer conversion.
2634 * first, skip white space in buffer.
2635 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002636 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 digit = *str;
2639 if (is_sign && digit == '-')
2640 digit = *(str + 1);
2641
2642 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002643 || (base == 16 && !isxdigit(digit))
2644 || (base == 10 && !isdigit(digit))
2645 || (base == 8 && (!isdigit(digit) || digit > '7'))
2646 || (base == 0 && !isdigit(digit)))
2647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Jan Beulich53809752012-12-17 16:01:31 -08002649 if (is_sign)
2650 val.s = qualifier != 'L' ?
2651 simple_strtol(str, &next, base) :
2652 simple_strtoll(str, &next, base);
2653 else
2654 val.u = qualifier != 'L' ?
2655 simple_strtoul(str, &next, base) :
2656 simple_strtoull(str, &next, base);
2657
2658 if (field_width > 0 && next - str > field_width) {
2659 if (base == 0)
2660 _parse_integer_fixup_radix(str, &base);
2661 while (next - str > field_width) {
2662 if (is_sign)
2663 val.s = div_s64(val.s, base);
2664 else
2665 val.u = div_u64(val.u, base);
2666 --next;
2667 }
2668 }
2669
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002670 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002672 if (is_sign)
2673 *va_arg(args, signed char *) = val.s;
2674 else
2675 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 break;
2677 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002678 if (is_sign)
2679 *va_arg(args, short *) = val.s;
2680 else
2681 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 break;
2683 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002684 if (is_sign)
2685 *va_arg(args, long *) = val.s;
2686 else
2687 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 break;
2689 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002690 if (is_sign)
2691 *va_arg(args, long long *) = val.s;
2692 else
2693 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 break;
2695 case 'Z':
2696 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002697 *va_arg(args, size_t *) = val.u;
2698 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 default:
Jan Beulich53809752012-12-17 16:01:31 -08002700 if (is_sign)
2701 *va_arg(args, int *) = val.s;
2702 else
2703 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 break;
2705 }
2706 num++;
2707
2708 if (!next)
2709 break;
2710 str = next;
2711 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return num;
2714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715EXPORT_SYMBOL(vsscanf);
2716
2717/**
2718 * sscanf - Unformat a buffer into a list of arguments
2719 * @buf: input buffer
2720 * @fmt: formatting of buffer
2721 * @...: resulting arguments
2722 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002723int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 va_list args;
2726 int i;
2727
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002728 va_start(args, fmt);
2729 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return i;
2733}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734EXPORT_SYMBOL(sscanf);