blob: c09182e07b05a6bce20082e95fd516e621ae863a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070020#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070021#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050022#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ctype.h>
26#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070027#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080028#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070029#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110030#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040031#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080032#include <linux/cred.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000033#include <net/addrconf.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040034#ifdef CONFIG_BLOCK
35#include <linux/blkdev.h>
36#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Tim Schmielau4e57b682005-10-30 15:03:48 -080038#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050039#include <asm/sections.h> /* for dereference_function_descriptor() */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070040#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Andy Shevchenko71dca952014-10-13 15:55:18 -070042#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070043#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * simple_strtoull - convert a string to an unsigned long long
47 * @cp: The start of the string
48 * @endp: A pointer to the end of the parsed string will be placed here
49 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080050 *
51 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Harvey Harrison22d27052008-10-16 13:40:35 -070053unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070055 unsigned long long result;
56 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070058 cp = _parse_integer_fixup_radix(cp, &base);
59 rv = _parse_integer(cp, base, &result);
60 /* FIXME */
61 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (endp)
64 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return result;
67}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068EXPORT_SYMBOL(simple_strtoull);
69
70/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080071 * simple_strtoul - convert a string to an unsigned long
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080075 *
76 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080077 */
78unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
79{
80 return simple_strtoull(cp, endp, base);
81}
82EXPORT_SYMBOL(simple_strtoul);
83
84/**
85 * simple_strtol - convert a string to a signed long
86 * @cp: The start of the string
87 * @endp: A pointer to the end of the parsed string will be placed here
88 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080089 *
90 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080091 */
92long simple_strtol(const char *cp, char **endp, unsigned int base)
93{
94 if (*cp == '-')
95 return -simple_strtoul(cp + 1, endp, base);
96
97 return simple_strtoul(cp, endp, base);
98}
99EXPORT_SYMBOL(simple_strtol);
100
101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * simple_strtoll - convert a string to a signed long long
103 * @cp: The start of the string
104 * @endp: A pointer to the end of the parsed string will be placed here
105 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800106 *
107 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700109long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800111 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700112 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800113
Harvey Harrison22d27052008-10-16 13:40:35 -0700114 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400116EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Joe Perchescf3b4292010-05-24 14:33:16 -0700118static noinline_for_stack
119int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800121 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800123 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800125 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return i;
128}
129
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700130/*
131 * Decimal conversion is by far the most typical, and is used for
132 * /proc and /sys data. This directly impacts e.g. top performance
133 * with many processes running. We optimize it for speed by emitting
134 * two characters at a time, using a 200 byte lookup table. This
135 * roughly halves the number of multiplications compared to computing
136 * the digits one at a time. Implementation strongly inspired by the
137 * previous version, which in turn used ideas described at
138 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
139 * from the author, Douglas W. Jones).
140 *
141 * It turns out there is precisely one 26 bit fixed-point
142 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
143 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
144 * range happens to be somewhat larger (x <= 1073741898), but that's
145 * irrelevant for our purpose.
146 *
147 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
148 * need a 32x32->64 bit multiply, so we simply use the same constant.
149 *
150 * For dividing a number in the range [100, 10^4-1] by 100, there are
151 * several options. The simplest is (x * 0x147b) >> 19, which is valid
152 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700153 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700154
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700155static const u16 decpair[100] = {
156#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
157 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
158 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
159 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
160 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
161 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
162 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
163 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
164 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
165 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
166 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
167#undef _
168};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700169
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700170/*
171 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700172 * immediately jump to out_r where two 0s would be written but only
173 * one of them accounted for in buf. This is needed by ip4_string
174 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700175*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700176static noinline_for_stack
177char *put_dec_trunc8(char *buf, unsigned r)
178{
179 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700180
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700181 /* 1 <= r < 10^8 */
182 if (r < 100)
183 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700184
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700185 /* 100 <= r < 10^8 */
186 q = (r * (u64)0x28f5c29) >> 32;
187 *((u16 *)buf) = decpair[r - 100*q];
188 buf += 2;
189
190 /* 1 <= q < 10^6 */
191 if (q < 100)
192 goto out_q;
193
194 /* 100 <= q < 10^6 */
195 r = (q * (u64)0x28f5c29) >> 32;
196 *((u16 *)buf) = decpair[q - 100*r];
197 buf += 2;
198
199 /* 1 <= r < 10^4 */
200 if (r < 100)
201 goto out_r;
202
203 /* 100 <= r < 10^4 */
204 q = (r * 0x147b) >> 19;
205 *((u16 *)buf) = decpair[r - 100*q];
206 buf += 2;
207out_q:
208 /* 1 <= q < 100 */
209 r = q;
210out_r:
211 /* 1 <= r < 100 */
212 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700213 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700214 return buf;
215}
216
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700217#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
218static noinline_for_stack
219char *put_dec_full8(char *buf, unsigned r)
220{
221 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700222
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700223 /* 0 <= r < 10^8 */
224 q = (r * (u64)0x28f5c29) >> 32;
225 *((u16 *)buf) = decpair[r - 100*q];
226 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700227
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700228 /* 0 <= q < 10^6 */
229 r = (q * (u64)0x28f5c29) >> 32;
230 *((u16 *)buf) = decpair[q - 100*r];
231 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700232
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700233 /* 0 <= r < 10^4 */
234 q = (r * 0x147b) >> 19;
235 *((u16 *)buf) = decpair[r - 100*q];
236 buf += 2;
237
238 /* 0 <= q < 100 */
239 *((u16 *)buf) = decpair[q];
240 buf += 2;
241 return buf;
242}
243
244static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700245char *put_dec(char *buf, unsigned long long n)
246{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700247 if (n >= 100*1000*1000)
248 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
249 /* 1 <= n <= 1.6e11 */
250 if (n >= 100*1000*1000)
251 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
252 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700253 return put_dec_trunc8(buf, n);
254}
255
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700256#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700257
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700258static void
259put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700260{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700261 unsigned q;
262
263 /* 0 <= r < 10^4 */
264 q = (r * 0x147b) >> 19;
265 *((u16 *)buf) = decpair[r - 100*q];
266 buf += 2;
267 /* 0 <= q < 100 */
268 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700269}
270
271/*
272 * Call put_dec_full4 on x % 10000, return x / 10000.
273 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
274 * holds for all x < 1,128,869,999. The largest value this
275 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700276 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700277 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700278static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700279unsigned put_dec_helper4(char *buf, unsigned x)
280{
281 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
282
283 put_dec_full4(buf, x - q * 10000);
284 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700285}
286
287/* Based on code by Douglas W. Jones found at
288 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
289 * (with permission from the author).
290 * Performs no 64-bit division and hence should be fast on 32-bit machines.
291 */
292static
293char *put_dec(char *buf, unsigned long long n)
294{
295 uint32_t d3, d2, d1, q, h;
296
297 if (n < 100*1000*1000)
298 return put_dec_trunc8(buf, n);
299
300 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
301 h = (n >> 32);
302 d2 = (h ) & 0xffff;
303 d3 = (h >> 16); /* implicit "& 0xffff" */
304
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700305 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
306 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700307 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700308 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700309
George Spelvin23591722012-10-04 17:12:29 -0700310 q += 7671 * d3 + 9496 * d2 + 6 * d1;
311 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700312
George Spelvin23591722012-10-04 17:12:29 -0700313 q += 4749 * d3 + 42 * d2;
314 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700315
George Spelvin23591722012-10-04 17:12:29 -0700316 q += 281 * d3;
317 buf += 12;
318 if (q)
319 buf = put_dec_trunc8(buf, q);
320 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700321 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800322
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700323 return buf;
324}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700325
326#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700327
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700328/*
329 * Convert passed number to decimal string.
330 * Returns the length of string. On buffer overflow, returns 0.
331 *
332 * If speed is not important, use snprintf(). It's easy to read the code.
333 */
334int num_to_str(char *buf, int size, unsigned long long num)
335{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700336 /* put_dec requires 2-byte alignment of the buffer. */
337 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700338 int idx, len;
339
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700340 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
341 if (num <= 9) {
342 tmp[0] = '0' + num;
343 len = 1;
344 } else {
345 len = put_dec(tmp, num) - tmp;
346 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700347
348 if (len > size)
349 return 0;
350 for (idx = 0; idx < len; ++idx)
351 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700352 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700353}
354
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700355#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700356#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#define PLUS 4 /* show plus */
358#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700359#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700360#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
361#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100363enum format_type {
364 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100365 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100366 FORMAT_TYPE_PRECISION,
367 FORMAT_TYPE_CHAR,
368 FORMAT_TYPE_STR,
369 FORMAT_TYPE_PTR,
370 FORMAT_TYPE_PERCENT_CHAR,
371 FORMAT_TYPE_INVALID,
372 FORMAT_TYPE_LONG_LONG,
373 FORMAT_TYPE_ULONG,
374 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800375 FORMAT_TYPE_UBYTE,
376 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100377 FORMAT_TYPE_USHORT,
378 FORMAT_TYPE_SHORT,
379 FORMAT_TYPE_UINT,
380 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100381 FORMAT_TYPE_SIZE_T,
382 FORMAT_TYPE_PTRDIFF
383};
384
385struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800386 unsigned int type:8; /* format_type enum */
387 signed int field_width:24; /* width of output field */
388 unsigned int flags:8; /* flags to number() */
389 unsigned int base:8; /* number base, 8, 10 or 16 only */
390 signed int precision:16; /* # of digits/chars */
391} __packed;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100392
Joe Perchescf3b4292010-05-24 14:33:16 -0700393static noinline_for_stack
394char *number(char *buf, char *end, unsigned long long num,
395 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700397 /* put_dec requires 2-byte alignment of the buffer. */
398 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100399 char sign;
400 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100401 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700403 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800405 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
406
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100407 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
408 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100409 locase = (spec.flags & SMALL);
410 if (spec.flags & LEFT)
411 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100413 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800414 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800416 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100417 spec.field_width--;
418 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100420 spec.field_width--;
421 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100423 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700426 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100427 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700428 spec.field_width -= 2;
429 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100430 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700432
433 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700435 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700436 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100437 else if (spec.base != 10) { /* 8 or 16 */
438 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700439 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800440
441 if (spec.base == 16)
442 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700443 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700444 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700445 num >>= shift;
446 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700447 } else { /* base 10 */
448 i = put_dec(tmp, num) - tmp;
449 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450
451 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100452 if (i > spec.precision)
453 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700454 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100455 spec.field_width -= spec.precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700456 if (!(spec.flags & (ZEROPAD | LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800457 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700458 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *buf = ' ';
460 ++buf;
461 }
462 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700463 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700465 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *buf = sign;
467 ++buf;
468 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700469 /* "0x" / "0" prefix */
470 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700471 if (spec.base == 16 || !is_zero) {
472 if (buf < end)
473 *buf = '0';
474 ++buf;
475 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100476 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700477 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100478 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 ++buf;
480 }
481 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700482 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100483 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700484 char c = ' ' + (spec.flags & ZEROPAD);
485 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100486 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700487 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *buf = c;
489 ++buf;
490 }
491 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700492 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100493 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700494 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 *buf = '0';
496 ++buf;
497 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700498 /* actual digits of result */
499 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700500 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 *buf = tmp[i];
502 ++buf;
503 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700504 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100505 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700506 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *buf = ' ';
508 ++buf;
509 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return buf;
512}
513
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800514static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400515{
516 size_t size;
517 if (buf >= end) /* nowhere to put anything */
518 return;
519 size = end - buf;
520 if (size <= spaces) {
521 memset(buf, ' ', size);
522 return;
523 }
524 if (len) {
525 if (len > size - spaces)
526 len = size - spaces;
527 memmove(buf + spaces, buf, len);
528 }
529 memset(buf, ' ', spaces);
530}
531
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800532/*
533 * Handle field width padding for a string.
534 * @buf: current buffer position
535 * @n: length of string
536 * @end: end of output buffer
537 * @spec: for field width and flags
538 * Returns: new buffer position after padding.
539 */
540static noinline_for_stack
541char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
542{
543 unsigned spaces;
544
545 if (likely(n >= spec.field_width))
546 return buf;
547 /* we want to pad the sucker */
548 spaces = spec.field_width - n;
549 if (!(spec.flags & LEFT)) {
550 move_right(buf - n, end, n, spaces);
551 return buf + spaces;
552 }
553 while (spaces--) {
554 if (buf < end)
555 *buf = ' ';
556 ++buf;
557 }
558 return buf;
559}
560
Al Viro4b6ccca2013-09-03 12:00:44 -0400561static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800562char *string(char *buf, char *end, const char *s, struct printf_spec spec)
563{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800564 int len = 0;
565 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800566
567 if ((unsigned long)s < PAGE_SIZE)
568 s = "(null)";
569
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800570 while (lim--) {
571 char c = *s++;
572 if (!c)
573 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800574 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800575 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800576 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800577 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800578 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800579 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800580}
581
582static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400583char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
584 const char *fmt)
585{
586 const char *array[4], *s;
587 const struct dentry *p;
588 int depth;
589 int i, n;
590
591 switch (fmt[1]) {
592 case '2': case '3': case '4':
593 depth = fmt[1] - '0';
594 break;
595 default:
596 depth = 1;
597 }
598
599 rcu_read_lock();
600 for (i = 0; i < depth; i++, d = p) {
601 p = ACCESS_ONCE(d->d_parent);
602 array[i] = ACCESS_ONCE(d->d_name.name);
603 if (p == d) {
604 if (i)
605 array[i] = "";
606 i++;
607 break;
608 }
609 }
610 s = array[--i];
611 for (n = 0; n != spec.precision; n++, buf++) {
612 char c = *s++;
613 if (!c) {
614 if (!i)
615 break;
616 c = '/';
617 s = array[--i];
618 }
619 if (buf < end)
620 *buf = c;
621 }
622 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800623 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400624}
625
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400626#ifdef CONFIG_BLOCK
627static noinline_for_stack
628char *bdev_name(char *buf, char *end, struct block_device *bdev,
629 struct printf_spec spec, const char *fmt)
630{
631 struct gendisk *hd = bdev->bd_disk;
632
633 buf = string(buf, end, hd->disk_name, spec);
634 if (bdev->bd_part->partno) {
635 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
636 if (buf < end)
637 *buf = 'p';
638 buf++;
639 }
640 buf = number(buf, end, bdev->bd_part->partno, spec);
641 }
642 return buf;
643}
644#endif
645
Joe Perchescf3b4292010-05-24 14:33:16 -0700646static noinline_for_stack
647char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800648 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700649{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800650 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700651#ifdef CONFIG_KALLSYMS
652 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800653#endif
654
655 if (fmt[1] == 'R')
656 ptr = __builtin_extract_return_addr(ptr);
657 value = (unsigned long)ptr;
658
659#ifdef CONFIG_KALLSYMS
660 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900661 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800662 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200663 sprint_symbol(sym, value);
664 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700665 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800666
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100667 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700668#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800669 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100670 spec.flags |= SPECIAL | SMALL | ZEROPAD;
671 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800672
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100673 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700674#endif
675}
676
Joe Perchescf3b4292010-05-24 14:33:16 -0700677static noinline_for_stack
678char *resource_string(char *buf, char *end, struct resource *res,
679 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100680{
681#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600682#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100683#endif
684
685#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600686#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100687#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700688 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100689 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700690 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100691 .precision = -1,
692 .flags = SPECIAL | SMALL | ZEROPAD,
693 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700694 static const struct printf_spec mem_spec = {
695 .base = 16,
696 .field_width = MEM_RSRC_PRINTK_SIZE,
697 .precision = -1,
698 .flags = SPECIAL | SMALL | ZEROPAD,
699 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700700 static const struct printf_spec bus_spec = {
701 .base = 16,
702 .field_width = 2,
703 .precision = -1,
704 .flags = SMALL | ZEROPAD,
705 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700706 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600707 .base = 10,
708 .precision = -1,
709 .flags = 0,
710 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700711 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600712 .field_width = -1,
713 .precision = 10,
714 .flags = LEFT,
715 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700716 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600717 .base = 16,
718 .precision = -1,
719 .flags = SPECIAL | SMALL,
720 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600721
722 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
723 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
724#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
725#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700726#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600727#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
728 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
729 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
730
Linus Torvalds332d2e72008-10-20 15:07:34 +1100731 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600732 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700733 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100734
735 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700736 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600737 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700738 specp = &io_spec;
739 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600740 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700741 specp = &mem_spec;
742 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600743 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700744 specp = &dec_spec;
745 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600746 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700747 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700748 } else if (res->flags & IORESOURCE_BUS) {
749 p = string(p, pend, "bus ", str_spec);
750 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700751 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600752 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700753 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600754 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600755 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700756 if (decode && res->flags & IORESOURCE_UNSET) {
757 p = string(p, pend, "size ", str_spec);
758 p = number(p, pend, resource_size(res), *specp);
759 } else {
760 p = number(p, pend, res->start, *specp);
761 if (res->start != res->end) {
762 *p++ = '-';
763 p = number(p, pend, res->end, *specp);
764 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600765 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600766 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600767 if (res->flags & IORESOURCE_MEM_64)
768 p = string(p, pend, " 64bit", str_spec);
769 if (res->flags & IORESOURCE_PREFETCH)
770 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700771 if (res->flags & IORESOURCE_WINDOW)
772 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600773 if (res->flags & IORESOURCE_DISABLED)
774 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600775 } else {
776 p = string(p, pend, " flags ", str_spec);
777 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600778 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100779 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600780 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100781
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100782 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100783}
784
Joe Perchescf3b4292010-05-24 14:33:16 -0700785static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700786char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
787 const char *fmt)
788{
Steven Rostedt360603a2013-05-28 15:47:39 -0400789 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700790 negative value, fallback to the default */
791 char separator;
792
793 if (spec.field_width == 0)
794 /* nothing to print */
795 return buf;
796
797 if (ZERO_OR_NULL_PTR(addr))
798 /* NULL pointer */
799 return string(buf, end, NULL, spec);
800
801 switch (fmt[1]) {
802 case 'C':
803 separator = ':';
804 break;
805 case 'D':
806 separator = '-';
807 break;
808 case 'N':
809 separator = 0;
810 break;
811 default:
812 separator = ' ';
813 break;
814 }
815
816 if (spec.field_width > 0)
817 len = min_t(int, spec.field_width, 64);
818
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700819 for (i = 0; i < len; ++i) {
820 if (buf < end)
821 *buf = hex_asc_hi(addr[i]);
822 ++buf;
823 if (buf < end)
824 *buf = hex_asc_lo(addr[i]);
825 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700826
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700827 if (separator && i != len - 1) {
828 if (buf < end)
829 *buf = separator;
830 ++buf;
831 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700832 }
833
834 return buf;
835}
836
837static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800838char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
839 struct printf_spec spec, const char *fmt)
840{
841 const int CHUNKSZ = 32;
842 int nr_bits = max_t(int, spec.field_width, 0);
843 int i, chunksz;
844 bool first = true;
845
846 /* reused to print numbers */
847 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
848
849 chunksz = nr_bits & (CHUNKSZ - 1);
850 if (chunksz == 0)
851 chunksz = CHUNKSZ;
852
853 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
854 for (; i >= 0; i -= CHUNKSZ) {
855 u32 chunkmask, val;
856 int word, bit;
857
858 chunkmask = ((1ULL << chunksz) - 1);
859 word = i / BITS_PER_LONG;
860 bit = i % BITS_PER_LONG;
861 val = (bitmap[word] >> bit) & chunkmask;
862
863 if (!first) {
864 if (buf < end)
865 *buf = ',';
866 buf++;
867 }
868 first = false;
869
870 spec.field_width = DIV_ROUND_UP(chunksz, 4);
871 buf = number(buf, end, val, spec);
872
873 chunksz = CHUNKSZ;
874 }
875 return buf;
876}
877
878static noinline_for_stack
879char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
880 struct printf_spec spec, const char *fmt)
881{
882 int nr_bits = max_t(int, spec.field_width, 0);
883 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
884 int cur, rbot, rtop;
885 bool first = true;
886
887 /* reused to print numbers */
888 spec = (struct printf_spec){ .base = 10 };
889
890 rbot = cur = find_first_bit(bitmap, nr_bits);
891 while (cur < nr_bits) {
892 rtop = cur;
893 cur = find_next_bit(bitmap, nr_bits, cur + 1);
894 if (cur < nr_bits && cur <= rtop + 1)
895 continue;
896
897 if (!first) {
898 if (buf < end)
899 *buf = ',';
900 buf++;
901 }
902 first = false;
903
904 buf = number(buf, end, rbot, spec);
905 if (rbot < rtop) {
906 if (buf < end)
907 *buf = '-';
908 buf++;
909
910 buf = number(buf, end, rtop, spec);
911 }
912
913 rbot = cur;
914 }
915 return buf;
916}
917
918static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700919char *mac_address_string(char *buf, char *end, u8 *addr,
920 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700921{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000922 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700923 char *p = mac_addr;
924 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000925 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700926 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000927
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700928 switch (fmt[1]) {
929 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000930 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700931 break;
932
933 case 'R':
934 reversed = true;
935 /* fall through */
936
937 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000938 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700939 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000940 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700941
942 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700943 if (reversed)
944 p = hex_byte_pack(p, addr[5 - i]);
945 else
946 p = hex_byte_pack(p, addr[i]);
947
Joe Perches8a27f7c2009-08-17 12:29:44 +0000948 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000949 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700950 }
951 *p = '\0';
952
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100953 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700954}
955
Joe Perchescf3b4292010-05-24 14:33:16 -0700956static noinline_for_stack
957char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700958{
Harvey Harrison689afa72008-10-28 16:04:44 -0700959 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800960 bool leading_zeros = (fmt[0] == 'i');
961 int index;
962 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700963
Joe Perches0159f242010-01-13 20:23:30 -0800964 switch (fmt[2]) {
965 case 'h':
966#ifdef __BIG_ENDIAN
967 index = 0;
968 step = 1;
969#else
970 index = 3;
971 step = -1;
972#endif
973 break;
974 case 'l':
975 index = 3;
976 step = -1;
977 break;
978 case 'n':
979 case 'b':
980 default:
981 index = 0;
982 step = 1;
983 break;
984 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000985 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700986 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700987 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000988 if (leading_zeros) {
989 if (digits < 3)
990 *p++ = '0';
991 if (digits < 2)
992 *p++ = '0';
993 }
994 /* reverse the digits in the quad */
995 while (digits--)
996 *p++ = temp[digits];
997 if (i < 3)
998 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800999 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001000 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001001 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001002
Joe Perches8a27f7c2009-08-17 12:29:44 +00001003 return p;
1004}
1005
Joe Perchescf3b4292010-05-24 14:33:16 -07001006static noinline_for_stack
1007char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001008{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001009 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001010 unsigned char zerolength[8];
1011 int longest = 1;
1012 int colonpos = -1;
1013 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001014 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001015 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001016 bool useIPv4;
1017 struct in6_addr in6;
1018
1019 memcpy(&in6, addr, sizeof(struct in6_addr));
1020
1021 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001022
1023 memset(zerolength, 0, sizeof(zerolength));
1024
1025 if (useIPv4)
1026 range = 6;
1027 else
1028 range = 8;
1029
1030 /* find position of longest 0 run */
1031 for (i = 0; i < range; i++) {
1032 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001033 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001034 break;
1035 zerolength[i]++;
1036 }
1037 }
1038 for (i = 0; i < range; i++) {
1039 if (zerolength[i] > longest) {
1040 longest = zerolength[i];
1041 colonpos = i;
1042 }
1043 }
Joe Perches29cf5192011-06-09 11:23:37 -07001044 if (longest == 1) /* don't compress a single 0 */
1045 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001046
1047 /* emit address */
1048 for (i = 0; i < range; i++) {
1049 if (i == colonpos) {
1050 if (needcolon || i == 0)
1051 *p++ = ':';
1052 *p++ = ':';
1053 needcolon = false;
1054 i += longest - 1;
1055 continue;
1056 }
1057 if (needcolon) {
1058 *p++ = ':';
1059 needcolon = false;
1060 }
1061 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001062 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001063 hi = word >> 8;
1064 lo = word & 0xff;
1065 if (hi) {
1066 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001067 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001068 else
1069 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001070 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001071 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001072 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001073 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001074 else
1075 *p++ = hex_asc_lo(lo);
1076 needcolon = true;
1077 }
1078
1079 if (useIPv4) {
1080 if (needcolon)
1081 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001082 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001083 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001084 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001085
Joe Perches8a27f7c2009-08-17 12:29:44 +00001086 return p;
1087}
1088
Joe Perchescf3b4292010-05-24 14:33:16 -07001089static noinline_for_stack
1090char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001091{
1092 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001093
Harvey Harrison689afa72008-10-28 16:04:44 -07001094 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001095 p = hex_byte_pack(p, *addr++);
1096 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001097 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001098 *p++ = ':';
1099 }
1100 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001101
Joe Perches8a27f7c2009-08-17 12:29:44 +00001102 return p;
1103}
1104
Joe Perchescf3b4292010-05-24 14:33:16 -07001105static noinline_for_stack
1106char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1107 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001108{
1109 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1110
1111 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001112 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001113 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001114 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001115
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001116 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001117}
1118
Joe Perchescf3b4292010-05-24 14:33:16 -07001119static noinline_for_stack
1120char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1121 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001122{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001123 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001124
Joe Perches0159f242010-01-13 20:23:30 -08001125 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001126
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001127 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001128}
1129
Joe Perchescf3b4292010-05-24 14:33:16 -07001130static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001131char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1132 struct printf_spec spec, const char *fmt)
1133{
1134 bool have_p = false, have_s = false, have_f = false, have_c = false;
1135 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1136 sizeof(":12345") + sizeof("/123456789") +
1137 sizeof("%1234567890")];
1138 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1139 const u8 *addr = (const u8 *) &sa->sin6_addr;
1140 char fmt6[2] = { fmt[0], '6' };
1141 u8 off = 0;
1142
1143 fmt++;
1144 while (isalpha(*++fmt)) {
1145 switch (*fmt) {
1146 case 'p':
1147 have_p = true;
1148 break;
1149 case 'f':
1150 have_f = true;
1151 break;
1152 case 's':
1153 have_s = true;
1154 break;
1155 case 'c':
1156 have_c = true;
1157 break;
1158 }
1159 }
1160
1161 if (have_p || have_s || have_f) {
1162 *p = '[';
1163 off = 1;
1164 }
1165
1166 if (fmt6[0] == 'I' && have_c)
1167 p = ip6_compressed_string(ip6_addr + off, addr);
1168 else
1169 p = ip6_string(ip6_addr + off, addr, fmt6);
1170
1171 if (have_p || have_s || have_f)
1172 *p++ = ']';
1173
1174 if (have_p) {
1175 *p++ = ':';
1176 p = number(p, pend, ntohs(sa->sin6_port), spec);
1177 }
1178 if (have_f) {
1179 *p++ = '/';
1180 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1181 IPV6_FLOWINFO_MASK), spec);
1182 }
1183 if (have_s) {
1184 *p++ = '%';
1185 p = number(p, pend, sa->sin6_scope_id, spec);
1186 }
1187 *p = '\0';
1188
1189 return string(buf, end, ip6_addr, spec);
1190}
1191
1192static noinline_for_stack
1193char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1194 struct printf_spec spec, const char *fmt)
1195{
1196 bool have_p = false;
1197 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1198 char *pend = ip4_addr + sizeof(ip4_addr);
1199 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1200 char fmt4[3] = { fmt[0], '4', 0 };
1201
1202 fmt++;
1203 while (isalpha(*++fmt)) {
1204 switch (*fmt) {
1205 case 'p':
1206 have_p = true;
1207 break;
1208 case 'h':
1209 case 'l':
1210 case 'n':
1211 case 'b':
1212 fmt4[2] = *fmt;
1213 break;
1214 }
1215 }
1216
1217 p = ip4_string(ip4_addr, addr, fmt4);
1218 if (have_p) {
1219 *p++ = ':';
1220 p = number(p, pend, ntohs(sa->sin_port), spec);
1221 }
1222 *p = '\0';
1223
1224 return string(buf, end, ip4_addr, spec);
1225}
1226
1227static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001228char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1229 const char *fmt)
1230{
1231 bool found = true;
1232 int count = 1;
1233 unsigned int flags = 0;
1234 int len;
1235
1236 if (spec.field_width == 0)
1237 return buf; /* nothing to print */
1238
1239 if (ZERO_OR_NULL_PTR(addr))
1240 return string(buf, end, NULL, spec); /* NULL pointer */
1241
1242
1243 do {
1244 switch (fmt[count++]) {
1245 case 'a':
1246 flags |= ESCAPE_ANY;
1247 break;
1248 case 'c':
1249 flags |= ESCAPE_SPECIAL;
1250 break;
1251 case 'h':
1252 flags |= ESCAPE_HEX;
1253 break;
1254 case 'n':
1255 flags |= ESCAPE_NULL;
1256 break;
1257 case 'o':
1258 flags |= ESCAPE_OCTAL;
1259 break;
1260 case 'p':
1261 flags |= ESCAPE_NP;
1262 break;
1263 case 's':
1264 flags |= ESCAPE_SPACE;
1265 break;
1266 default:
1267 found = false;
1268 break;
1269 }
1270 } while (found);
1271
1272 if (!flags)
1273 flags = ESCAPE_ANY_NP;
1274
1275 len = spec.field_width < 0 ? 1 : spec.field_width;
1276
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001277 /*
1278 * string_escape_mem() writes as many characters as it can to
1279 * the given buffer, and returns the total size of the output
1280 * had the buffer been big enough.
1281 */
1282 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001283
1284 return buf;
1285}
1286
1287static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001288char *uuid_string(char *buf, char *end, const u8 *addr,
1289 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001290{
1291 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1292 char *p = uuid;
1293 int i;
1294 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1295 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1296 const u8 *index = be;
1297 bool uc = false;
1298
1299 switch (*(++fmt)) {
1300 case 'L':
1301 uc = true; /* fall-through */
1302 case 'l':
1303 index = le;
1304 break;
1305 case 'B':
1306 uc = true;
1307 break;
1308 }
1309
1310 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001311 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001312 switch (i) {
1313 case 3:
1314 case 5:
1315 case 7:
1316 case 9:
1317 *p++ = '-';
1318 break;
1319 }
1320 }
1321
1322 *p = 0;
1323
1324 if (uc) {
1325 p = uuid;
1326 do {
1327 *p = toupper(*p);
1328 } while (*(++p));
1329 }
1330
1331 return string(buf, end, uuid, spec);
1332}
1333
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001334static
1335char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1336 struct printf_spec spec)
1337{
1338 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1339 if (spec.field_width == -1)
1340 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1341 spec.base = 16;
1342
1343 return number(buf, end, *(const netdev_features_t *)addr, spec);
1344}
1345
Joe Perchesaaf07622014-01-23 15:54:17 -08001346static noinline_for_stack
1347char *address_val(char *buf, char *end, const void *addr,
1348 struct printf_spec spec, const char *fmt)
1349{
1350 unsigned long long num;
1351
1352 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1353 spec.base = 16;
1354
1355 switch (fmt[1]) {
1356 case 'd':
1357 num = *(const dma_addr_t *)addr;
1358 spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1359 break;
1360 case 'p':
1361 default:
1362 num = *(const phys_addr_t *)addr;
1363 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1364 break;
1365 }
1366
1367 return number(buf, end, num, spec);
1368}
1369
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001370static noinline_for_stack
1371char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1372 const char *fmt)
1373{
1374 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1375 return string(buf, end, NULL, spec);
1376
1377 switch (fmt[1]) {
1378 case 'r':
1379 return number(buf, end, clk_get_rate(clk), spec);
1380
1381 case 'n':
1382 default:
1383#ifdef CONFIG_COMMON_CLK
1384 return string(buf, end, __clk_get_name(clk), spec);
1385#else
1386 spec.base = 16;
1387 spec.field_width = sizeof(unsigned long) * 2 + 2;
1388 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1389 return number(buf, end, (unsigned long)clk, spec);
1390#endif
1391 }
1392}
1393
Ingo Molnar411f05f2011-05-12 23:00:28 +02001394int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001395
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001396/*
1397 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1398 * by an extra set of alphanumeric characters that are extended format
1399 * specifiers.
1400 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001401 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001402 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001403 * - 'F' For symbolic function descriptor pointers with offset
1404 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001405 * - 'S' For symbolic direct pointers with offset
1406 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001407 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001408 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001409 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1410 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001411 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1412 * width which must be explicitly specified either as part of the
1413 * format string '%32b[l]' or through '%*b[l]', [l] selects
1414 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001415 * - 'M' For a 6-byte MAC address, it prints the address in the
1416 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001417 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001418 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001419 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001420 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001421 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1422 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1423 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001424 * [S][pfs]
1425 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1426 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001427 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1428 * IPv6 omits the colons (01020304...0f)
1429 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001430 * [S][pfs]
1431 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1432 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1433 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1434 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001435 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001436 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1437 * of the following flags (see string_escape_mem() for the
1438 * details):
1439 * a - ESCAPE_ANY
1440 * c - ESCAPE_SPECIAL
1441 * h - ESCAPE_HEX
1442 * n - ESCAPE_NULL
1443 * o - ESCAPE_OCTAL
1444 * p - ESCAPE_NP
1445 * s - ESCAPE_SPACE
1446 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001447 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1448 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1449 * Options for %pU are:
1450 * b big endian lower case hex (default)
1451 * B big endian UPPER case hex
1452 * l little endian lower case hex
1453 * L little endian UPPER case hex
1454 * big endian output byte order is:
1455 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1456 * little endian output byte order is:
1457 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001458 * - 'V' For a struct va_format which contains a format string * and va_list *,
1459 * call vsnprintf(->format, *->va_list).
1460 * Implements a "recursive vsnprintf".
1461 * Do not use this feature without some mechanism to verify the
1462 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001463 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001464 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001465 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1466 * a certain separator (' ' by default):
1467 * C colon
1468 * D dash
1469 * N no separator
1470 * The maximum supported length is 64 bytes of the input. Consider
1471 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001472 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1473 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001474 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1475 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001476 * - 'g' For block_device name (gendisk + partition number)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001477 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1478 * (legacy clock framework) of the clock
1479 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1480 * (legacy clock framework) of the clock
1481 * - 'Cr' For a clock, it prints the current rate of the clock
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001482 *
1483 * ** Please update also Documentation/printk-formats.txt when making changes **
Joe Perches9ac6e442009-12-14 18:01:09 -08001484 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001485 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1486 * function pointers are really function descriptors, which contain a
1487 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001488 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001489static noinline_for_stack
1490char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1491 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001492{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001493 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001494
Kees Cook9f36e2c2011-03-22 16:34:22 -07001495 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001496 /*
1497 * Print (null) with the same width as a pointer so it makes
1498 * tabular output look nice.
1499 */
1500 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001501 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001502 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001503 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001504
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001505 switch (*fmt) {
1506 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001507 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001508 ptr = dereference_function_descriptor(ptr);
1509 /* Fallthrough */
1510 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001511 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001512 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001513 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001514 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001515 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001516 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001517 case 'h':
1518 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001519 case 'b':
1520 switch (fmt[1]) {
1521 case 'l':
1522 return bitmap_list_string(buf, end, ptr, spec, fmt);
1523 default:
1524 return bitmap_string(buf, end, ptr, spec, fmt);
1525 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001526 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1527 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001528 /* [mM]F (FDDI) */
1529 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001530 return mac_address_string(buf, end, ptr, spec, fmt);
1531 case 'I': /* Formatted IP supported
1532 * 4: 1.2.3.4
1533 * 6: 0001:0203:...:0708
1534 * 6c: 1::708 or 1::1.2.3.4
1535 */
1536 case 'i': /* Contiguous:
1537 * 4: 001.002.003.004
1538 * 6: 000102...0f
1539 */
1540 switch (fmt[1]) {
1541 case '6':
1542 return ip6_addr_string(buf, end, ptr, spec, fmt);
1543 case '4':
1544 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001545 case 'S': {
1546 const union {
1547 struct sockaddr raw;
1548 struct sockaddr_in v4;
1549 struct sockaddr_in6 v6;
1550 } *sa = ptr;
1551
1552 switch (sa->raw.sa_family) {
1553 case AF_INET:
1554 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1555 case AF_INET6:
1556 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1557 default:
1558 return string(buf, end, "(invalid address)", spec);
1559 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001560 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001561 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001562 case 'E':
1563 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001564 case 'U':
1565 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001566 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001567 {
1568 va_list va;
1569
1570 va_copy(va, *((struct va_format *)ptr)->va);
1571 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1572 ((struct va_format *)ptr)->fmt, va);
1573 va_end(va);
1574 return buf;
1575 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001576 case 'K':
1577 /*
1578 * %pK cannot be used in IRQ context because its test
1579 * for CAP_SYSLOG would be meaningless.
1580 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001581 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1582 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001583 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001584 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001585 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001586 }
Ryan Mallon312b4e22013-11-12 15:08:51 -08001587
1588 switch (kptr_restrict) {
1589 case 0:
1590 /* Always print %pK values */
1591 break;
1592 case 1: {
1593 /*
1594 * Only print the real pointer value if the current
1595 * process has CAP_SYSLOG and is running with the
1596 * same credentials it started with. This is because
1597 * access to files is checked at open() time, but %pK
1598 * checks permission at read() time. We don't want to
1599 * leak pointer values if a binary opens a file using
1600 * %pK and then elevates privileges before reading it.
1601 */
1602 const struct cred *cred = current_cred();
1603
1604 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1605 !uid_eq(cred->euid, cred->uid) ||
1606 !gid_eq(cred->egid, cred->gid))
1607 ptr = NULL;
1608 break;
1609 }
1610 case 2:
1611 default:
1612 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -07001613 ptr = NULL;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001614 break;
1615 }
Joe Perches26297602011-03-22 16:34:19 -07001616 break;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001617
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001618 case 'N':
1619 switch (fmt[1]) {
1620 case 'F':
1621 return netdev_feature_string(buf, end, ptr, spec);
1622 }
1623 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001624 case 'a':
Joe Perchesaaf07622014-01-23 15:54:17 -08001625 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001626 case 'd':
1627 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001628 case 'C':
1629 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001630 case 'D':
1631 return dentry_name(buf, end,
1632 ((const struct file *)ptr)->f_path.dentry,
1633 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001634#ifdef CONFIG_BLOCK
1635 case 'g':
1636 return bdev_name(buf, end, ptr, spec, fmt);
1637#endif
1638
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001639 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001640 spec.flags |= SMALL;
1641 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001642 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001643 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001644 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001645 spec.base = 16;
1646
1647 return number(buf, end, (unsigned long) ptr, spec);
1648}
1649
1650/*
1651 * Helper function to decode printf style format.
1652 * Each call decode a token from the format and return the
1653 * number of characters read (or likely the delta where it wants
1654 * to go on the next call).
1655 * The decoded token is returned through the parameters
1656 *
1657 * 'h', 'l', or 'L' for integer fields
1658 * 'z' support added 23/7/1999 S.H.
1659 * 'z' changed to 'Z' --davidm 1/25/99
1660 * 't' added for ptrdiff_t
1661 *
1662 * @fmt: the format string
1663 * @type of the token returned
1664 * @flags: various flags such as +, -, # tokens..
1665 * @field_width: overwritten width
1666 * @base: base of the number (octal, hex, ...)
1667 * @precision: precision of a number
1668 * @qualifier: qualifier of a number (long, size_t, ...)
1669 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001670static noinline_for_stack
1671int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001672{
1673 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001674 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001675
1676 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001677 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001678 if (spec->field_width < 0) {
1679 spec->field_width = -spec->field_width;
1680 spec->flags |= LEFT;
1681 }
1682 spec->type = FORMAT_TYPE_NONE;
1683 goto precision;
1684 }
1685
1686 /* we finished early by reading the precision */
1687 if (spec->type == FORMAT_TYPE_PRECISION) {
1688 if (spec->precision < 0)
1689 spec->precision = 0;
1690
1691 spec->type = FORMAT_TYPE_NONE;
1692 goto qualifier;
1693 }
1694
1695 /* By default */
1696 spec->type = FORMAT_TYPE_NONE;
1697
1698 for (; *fmt ; ++fmt) {
1699 if (*fmt == '%')
1700 break;
1701 }
1702
1703 /* Return the current non-format string */
1704 if (fmt != start || !*fmt)
1705 return fmt - start;
1706
1707 /* Process flags */
1708 spec->flags = 0;
1709
1710 while (1) { /* this also skips first '%' */
1711 bool found = true;
1712
1713 ++fmt;
1714
1715 switch (*fmt) {
1716 case '-': spec->flags |= LEFT; break;
1717 case '+': spec->flags |= PLUS; break;
1718 case ' ': spec->flags |= SPACE; break;
1719 case '#': spec->flags |= SPECIAL; break;
1720 case '0': spec->flags |= ZEROPAD; break;
1721 default: found = false;
1722 }
1723
1724 if (!found)
1725 break;
1726 }
1727
1728 /* get field width */
1729 spec->field_width = -1;
1730
1731 if (isdigit(*fmt))
1732 spec->field_width = skip_atoi(&fmt);
1733 else if (*fmt == '*') {
1734 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001735 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001736 return ++fmt - start;
1737 }
1738
1739precision:
1740 /* get the precision */
1741 spec->precision = -1;
1742 if (*fmt == '.') {
1743 ++fmt;
1744 if (isdigit(*fmt)) {
1745 spec->precision = skip_atoi(&fmt);
1746 if (spec->precision < 0)
1747 spec->precision = 0;
1748 } else if (*fmt == '*') {
1749 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001750 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001751 return ++fmt - start;
1752 }
1753 }
1754
1755qualifier:
1756 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001757 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001758 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1759 _tolower(*fmt) == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001760 qualifier = *fmt++;
1761 if (unlikely(qualifier == *fmt)) {
1762 if (qualifier == 'l') {
1763 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08001764 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001765 } else if (qualifier == 'h') {
1766 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08001767 ++fmt;
1768 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001769 }
1770 }
1771
1772 /* default base */
1773 spec->base = 10;
1774 switch (*fmt) {
1775 case 'c':
1776 spec->type = FORMAT_TYPE_CHAR;
1777 return ++fmt - start;
1778
1779 case 's':
1780 spec->type = FORMAT_TYPE_STR;
1781 return ++fmt - start;
1782
1783 case 'p':
1784 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001785 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001786
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001787 case '%':
1788 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1789 return ++fmt - start;
1790
1791 /* integer number formats - set up the flags and "break" */
1792 case 'o':
1793 spec->base = 8;
1794 break;
1795
1796 case 'x':
1797 spec->flags |= SMALL;
1798
1799 case 'X':
1800 spec->base = 16;
1801 break;
1802
1803 case 'd':
1804 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001805 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001806 case 'u':
1807 break;
1808
Ryan Mallon708d96f2014-04-03 14:48:37 -07001809 case 'n':
1810 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001811 * Since %n poses a greater security risk than
1812 * utility, treat it as any other invalid or
1813 * unsupported format specifier.
Ryan Mallon708d96f2014-04-03 14:48:37 -07001814 */
Ryan Mallon708d96f2014-04-03 14:48:37 -07001815 /* Fall-through */
1816
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001817 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001818 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001819 spec->type = FORMAT_TYPE_INVALID;
1820 return fmt - start;
1821 }
1822
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001823 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001824 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001825 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001826 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1827 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001828 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001829 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001830 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001831 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001832 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001833 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1834 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001835 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001836 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1837 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001838 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001839 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1840 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001841 }
1842
1843 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001844}
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846/**
1847 * vsnprintf - Format a string and place it in a buffer
1848 * @buf: The buffer to place the result into
1849 * @size: The size of the buffer, including the trailing null space
1850 * @fmt: The format string to use
1851 * @args: Arguments for the format string
1852 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08001853 * This function generally follows C99 vsnprintf, but has some
1854 * extensions and a few limitations:
1855 *
1856 * %n is unsupported
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001857 * %p* is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07001858 *
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001859 * See pointer() or Documentation/printk-formats.txt for more
1860 * extensive description.
1861 *
1862 * ** Please update the documentation in both places when making changes **
Andrew Morton80f548e2012-07-30 14:40:25 -07001863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * The return value is the number of characters which would
1865 * be generated for the given input, excluding the trailing
1866 * '\0', as per ISO C99. If you want to have the exact
1867 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001868 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * return is greater than or equal to @size, the resulting
1870 * string is truncated.
1871 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001872 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 */
1874int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001877 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001878 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001880 /* Reject out-of-range values early. Large positive sizes are
1881 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08001882 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001886 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001888 /* Make sure end is always >= buf */
1889 if (end < buf) {
1890 end = ((void *)-1);
1891 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001894 while (*fmt) {
1895 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001896 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001897
1898 fmt += read;
1899
1900 switch (spec.type) {
1901 case FORMAT_TYPE_NONE: {
1902 int copy = read;
1903 if (str < end) {
1904 if (copy > end - str)
1905 copy = end - str;
1906 memcpy(str, old_fmt, copy);
1907 }
1908 str += read;
1909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
Vegard Nossumed681a92009-03-14 12:08:50 +01001912 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001913 spec.field_width = va_arg(args, int);
1914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001916 case FORMAT_TYPE_PRECISION:
1917 spec.precision = va_arg(args, int);
1918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
André Goddard Rosad4be1512009-12-14 18:00:59 -08001920 case FORMAT_TYPE_CHAR: {
1921 char c;
1922
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001923 if (!(spec.flags & LEFT)) {
1924 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001925 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 *str = ' ';
1927 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001930 }
1931 c = (unsigned char) va_arg(args, int);
1932 if (str < end)
1933 *str = c;
1934 ++str;
1935 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001936 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001937 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001939 }
1940 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001943 case FORMAT_TYPE_STR:
1944 str = string(str, end, va_arg(args, char *), spec);
1945 break;
1946
1947 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001948 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001949 spec);
1950 while (isalnum(*fmt))
1951 fmt++;
1952 break;
1953
1954 case FORMAT_TYPE_PERCENT_CHAR:
1955 if (str < end)
1956 *str = '%';
1957 ++str;
1958 break;
1959
1960 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001961 /*
1962 * Presumably the arguments passed gcc's type
1963 * checking, but there is no safe or sane way
1964 * for us to continue parsing the format and
1965 * fetching from the va_list; the remaining
1966 * specifiers and arguments would be out of
1967 * sync.
1968 */
1969 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001970
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001971 default:
1972 switch (spec.type) {
1973 case FORMAT_TYPE_LONG_LONG:
1974 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001976 case FORMAT_TYPE_ULONG:
1977 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001979 case FORMAT_TYPE_LONG:
1980 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001982 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001983 if (spec.flags & SIGN)
1984 num = va_arg(args, ssize_t);
1985 else
1986 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001987 break;
1988 case FORMAT_TYPE_PTRDIFF:
1989 num = va_arg(args, ptrdiff_t);
1990 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001991 case FORMAT_TYPE_UBYTE:
1992 num = (unsigned char) va_arg(args, int);
1993 break;
1994 case FORMAT_TYPE_BYTE:
1995 num = (signed char) va_arg(args, int);
1996 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001997 case FORMAT_TYPE_USHORT:
1998 num = (unsigned short) va_arg(args, int);
1999 break;
2000 case FORMAT_TYPE_SHORT:
2001 num = (short) va_arg(args, int);
2002 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002003 case FORMAT_TYPE_INT:
2004 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002007 num = va_arg(args, unsigned int);
2008 }
2009
2010 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002013
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002014out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002015 if (size > 0) {
2016 if (str < end)
2017 *str = '\0';
2018 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002019 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002020 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002021
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002022 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026EXPORT_SYMBOL(vsnprintf);
2027
2028/**
2029 * vscnprintf - Format a string and place it in a buffer
2030 * @buf: The buffer to place the result into
2031 * @size: The size of the buffer, including the trailing null space
2032 * @fmt: The format string to use
2033 * @args: Arguments for the format string
2034 *
2035 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002036 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 * returns 0.
2038 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002039 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002040 *
2041 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
2043int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2044{
2045 int i;
2046
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002047 i = vsnprintf(buf, size, fmt, args);
2048
Anton Arapovb921c692011-01-12 16:59:49 -08002049 if (likely(i < size))
2050 return i;
2051 if (size != 0)
2052 return size - 1;
2053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055EXPORT_SYMBOL(vscnprintf);
2056
2057/**
2058 * snprintf - Format a string and place it in a buffer
2059 * @buf: The buffer to place the result into
2060 * @size: The size of the buffer, including the trailing null space
2061 * @fmt: The format string to use
2062 * @...: Arguments for the format string
2063 *
2064 * The return value is the number of characters which would be
2065 * generated for the given input, excluding the trailing null,
2066 * as per ISO C99. If the return is greater than or equal to
2067 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002068 *
2069 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002071int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 va_list args;
2074 int i;
2075
2076 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002077 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return i;
2081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082EXPORT_SYMBOL(snprintf);
2083
2084/**
2085 * scnprintf - Format a string and place it in a buffer
2086 * @buf: The buffer to place the result into
2087 * @size: The size of the buffer, including the trailing null space
2088 * @fmt: The format string to use
2089 * @...: Arguments for the format string
2090 *
2091 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002092 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 */
2094
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002095int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 va_list args;
2098 int i;
2099
2100 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002101 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002103
Anton Arapovb921c692011-01-12 16:59:49 -08002104 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106EXPORT_SYMBOL(scnprintf);
2107
2108/**
2109 * vsprintf - Format a string and place it in a buffer
2110 * @buf: The buffer to place the result into
2111 * @fmt: The format string to use
2112 * @args: Arguments for the format string
2113 *
2114 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002115 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 * buffer overflows.
2117 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002118 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002119 *
2120 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 */
2122int vsprintf(char *buf, const char *fmt, va_list args)
2123{
2124 return vsnprintf(buf, INT_MAX, fmt, args);
2125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126EXPORT_SYMBOL(vsprintf);
2127
2128/**
2129 * sprintf - Format a string and place it in a buffer
2130 * @buf: The buffer to place the result into
2131 * @fmt: The format string to use
2132 * @...: Arguments for the format string
2133 *
2134 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002135 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002137 *
2138 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002140int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 va_list args;
2143 int i;
2144
2145 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002146 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return i;
2150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151EXPORT_SYMBOL(sprintf);
2152
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002153#ifdef CONFIG_BINARY_PRINTF
2154/*
2155 * bprintf service:
2156 * vbin_printf() - VA arguments to binary data
2157 * bstr_printf() - Binary data to text string
2158 */
2159
2160/**
2161 * vbin_printf - Parse a format string and place args' binary value in a buffer
2162 * @bin_buf: The buffer to place args' binary value
2163 * @size: The size of the buffer(by words(32bits), not characters)
2164 * @fmt: The format string to use
2165 * @args: Arguments for the format string
2166 *
2167 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002168 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002169 *
2170 * The return value is the number of words(32bits) which would be generated for
2171 * the given input.
2172 *
2173 * NOTE:
2174 * If the return value is greater than @size, the resulting bin_buf is NOT
2175 * valid for bstr_printf().
2176 */
2177int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2178{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002179 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002180 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002181
2182 str = (char *)bin_buf;
2183 end = (char *)(bin_buf + size);
2184
2185#define save_arg(type) \
2186do { \
2187 if (sizeof(type) == 8) { \
2188 unsigned long long value; \
2189 str = PTR_ALIGN(str, sizeof(u32)); \
2190 value = va_arg(args, unsigned long long); \
2191 if (str + sizeof(type) <= end) { \
2192 *(u32 *)str = *(u32 *)&value; \
2193 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2194 } \
2195 } else { \
2196 unsigned long value; \
2197 str = PTR_ALIGN(str, sizeof(type)); \
2198 value = va_arg(args, int); \
2199 if (str + sizeof(type) <= end) \
2200 *(typeof(type) *)str = (type)value; \
2201 } \
2202 str += sizeof(type); \
2203} while (0)
2204
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002205 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002206 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002207
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002208 fmt += read;
2209
2210 switch (spec.type) {
2211 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002212 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002213 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002214 case FORMAT_TYPE_INVALID:
2215 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002216
Vegard Nossumed681a92009-03-14 12:08:50 +01002217 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002218 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002219 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002220 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002221
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002222 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002223 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002224 break;
2225
2226 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002227 const char *save_str = va_arg(args, char *);
2228 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002229
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002230 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2231 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002232 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002233 len = strlen(save_str) + 1;
2234 if (str + len < end)
2235 memcpy(str, save_str, len);
2236 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002237 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002238 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002239
2240 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002241 save_arg(void *);
2242 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002243 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002244 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002245 break;
2246
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002247 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002248 switch (spec.type) {
2249
2250 case FORMAT_TYPE_LONG_LONG:
2251 save_arg(long long);
2252 break;
2253 case FORMAT_TYPE_ULONG:
2254 case FORMAT_TYPE_LONG:
2255 save_arg(unsigned long);
2256 break;
2257 case FORMAT_TYPE_SIZE_T:
2258 save_arg(size_t);
2259 break;
2260 case FORMAT_TYPE_PTRDIFF:
2261 save_arg(ptrdiff_t);
2262 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002263 case FORMAT_TYPE_UBYTE:
2264 case FORMAT_TYPE_BYTE:
2265 save_arg(char);
2266 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002267 case FORMAT_TYPE_USHORT:
2268 case FORMAT_TYPE_SHORT:
2269 save_arg(short);
2270 break;
2271 default:
2272 save_arg(int);
2273 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002274 }
2275 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002276
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002277out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002278 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002279#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002280}
2281EXPORT_SYMBOL_GPL(vbin_printf);
2282
2283/**
2284 * bstr_printf - Format a string from binary arguments and place it in a buffer
2285 * @buf: The buffer to place the result into
2286 * @size: The size of the buffer, including the trailing null space
2287 * @fmt: The format string to use
2288 * @bin_buf: Binary arguments for the format string
2289 *
2290 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2291 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2292 * a binary buffer that generated by vbin_printf.
2293 *
2294 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002295 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002296 *
2297 * The return value is the number of characters which would
2298 * be generated for the given input, excluding the trailing
2299 * '\0', as per ISO C99. If you want to have the exact
2300 * number of characters written into @buf as return value
2301 * (not including the trailing '\0'), use vscnprintf(). If the
2302 * return is greater than or equal to @size, the resulting
2303 * string is truncated.
2304 */
2305int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2306{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002307 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002308 char *str, *end;
2309 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002310
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002311 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002312 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002313
2314 str = buf;
2315 end = buf + size;
2316
2317#define get_arg(type) \
2318({ \
2319 typeof(type) value; \
2320 if (sizeof(type) == 8) { \
2321 args = PTR_ALIGN(args, sizeof(u32)); \
2322 *(u32 *)&value = *(u32 *)args; \
2323 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2324 } else { \
2325 args = PTR_ALIGN(args, sizeof(type)); \
2326 value = *(typeof(type) *)args; \
2327 } \
2328 args += sizeof(type); \
2329 value; \
2330})
2331
2332 /* Make sure end is always >= buf */
2333 if (end < buf) {
2334 end = ((void *)-1);
2335 size = end - buf;
2336 }
2337
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002338 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002339 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002340 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002341
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002342 fmt += read;
2343
2344 switch (spec.type) {
2345 case FORMAT_TYPE_NONE: {
2346 int copy = read;
2347 if (str < end) {
2348 if (copy > end - str)
2349 copy = end - str;
2350 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002351 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002352 str += read;
2353 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002354 }
2355
Vegard Nossumed681a92009-03-14 12:08:50 +01002356 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002357 spec.field_width = get_arg(int);
2358 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002359
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002360 case FORMAT_TYPE_PRECISION:
2361 spec.precision = get_arg(int);
2362 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002363
André Goddard Rosad4be1512009-12-14 18:00:59 -08002364 case FORMAT_TYPE_CHAR: {
2365 char c;
2366
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002367 if (!(spec.flags & LEFT)) {
2368 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002369 if (str < end)
2370 *str = ' ';
2371 ++str;
2372 }
2373 }
2374 c = (unsigned char) get_arg(char);
2375 if (str < end)
2376 *str = c;
2377 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002378 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002379 if (str < end)
2380 *str = ' ';
2381 ++str;
2382 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002383 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002384 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002385
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002386 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002387 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002388 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002389 str = string(str, end, (char *)str_arg, spec);
2390 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002391 }
2392
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002393 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002394 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002395 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002396 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002397 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002398
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002399 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002400 if (str < end)
2401 *str = '%';
2402 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002403 break;
2404
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002405 case FORMAT_TYPE_INVALID:
2406 goto out;
2407
André Goddard Rosad4be1512009-12-14 18:00:59 -08002408 default: {
2409 unsigned long long num;
2410
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002411 switch (spec.type) {
2412
2413 case FORMAT_TYPE_LONG_LONG:
2414 num = get_arg(long long);
2415 break;
2416 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002417 case FORMAT_TYPE_LONG:
2418 num = get_arg(unsigned long);
2419 break;
2420 case FORMAT_TYPE_SIZE_T:
2421 num = get_arg(size_t);
2422 break;
2423 case FORMAT_TYPE_PTRDIFF:
2424 num = get_arg(ptrdiff_t);
2425 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002426 case FORMAT_TYPE_UBYTE:
2427 num = get_arg(unsigned char);
2428 break;
2429 case FORMAT_TYPE_BYTE:
2430 num = get_arg(signed char);
2431 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002432 case FORMAT_TYPE_USHORT:
2433 num = get_arg(unsigned short);
2434 break;
2435 case FORMAT_TYPE_SHORT:
2436 num = get_arg(short);
2437 break;
2438 case FORMAT_TYPE_UINT:
2439 num = get_arg(unsigned int);
2440 break;
2441 default:
2442 num = get_arg(int);
2443 }
2444
2445 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002446 } /* default: */
2447 } /* switch(spec.type) */
2448 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002449
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002450out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002451 if (size > 0) {
2452 if (str < end)
2453 *str = '\0';
2454 else
2455 end[-1] = '\0';
2456 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002457
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002458#undef get_arg
2459
2460 /* the trailing null byte doesn't count towards the total */
2461 return str - buf;
2462}
2463EXPORT_SYMBOL_GPL(bstr_printf);
2464
2465/**
2466 * bprintf - Parse a format string and place args' binary value in a buffer
2467 * @bin_buf: The buffer to place args' binary value
2468 * @size: The size of the buffer(by words(32bits), not characters)
2469 * @fmt: The format string to use
2470 * @...: Arguments for the format string
2471 *
2472 * The function returns the number of words(u32) written
2473 * into @bin_buf.
2474 */
2475int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2476{
2477 va_list args;
2478 int ret;
2479
2480 va_start(args, fmt);
2481 ret = vbin_printf(bin_buf, size, fmt, args);
2482 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002483
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002484 return ret;
2485}
2486EXPORT_SYMBOL_GPL(bprintf);
2487
2488#endif /* CONFIG_BINARY_PRINTF */
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490/**
2491 * vsscanf - Unformat a buffer into a list of arguments
2492 * @buf: input buffer
2493 * @fmt: format of buffer
2494 * @args: arguments
2495 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002496int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 const char *str = buf;
2499 char *next;
2500 char digit;
2501 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002502 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002503 unsigned int base;
2504 union {
2505 long long s;
2506 unsigned long long u;
2507 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002508 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002509 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Jan Beulichda990752012-10-04 17:13:24 -07002511 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /* skip any white space in format */
2513 /* white space in format matchs any amount of
2514 * white space, including none, in the input.
2515 */
2516 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002517 fmt = skip_spaces(++fmt);
2518 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520
2521 /* anything that is not a conversion must match exactly */
2522 if (*fmt != '%' && *fmt) {
2523 if (*fmt++ != *str++)
2524 break;
2525 continue;
2526 }
2527
2528 if (!*fmt)
2529 break;
2530 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /* skip this conversion.
2533 * advance both strings to next white space
2534 */
2535 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002536 if (!*str)
2537 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002538 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 fmt++;
2540 while (!isspace(*str) && *str)
2541 str++;
2542 continue;
2543 }
2544
2545 /* get field width */
2546 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002547 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002549 if (field_width <= 0)
2550 break;
2551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 /* get conversion qualifier */
2554 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002555 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2556 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 qualifier = *fmt++;
2558 if (unlikely(qualifier == *fmt)) {
2559 if (qualifier == 'h') {
2560 qualifier = 'H';
2561 fmt++;
2562 } else if (qualifier == 'l') {
2563 qualifier = 'L';
2564 fmt++;
2565 }
2566 }
2567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Jan Beulichda990752012-10-04 17:13:24 -07002569 if (!*fmt)
2570 break;
2571
2572 if (*fmt == 'n') {
2573 /* return number of characters read so far */
2574 *va_arg(args, int *) = str - buf;
2575 ++fmt;
2576 continue;
2577 }
2578
2579 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 break;
2581
André Goddard Rosad4be1512009-12-14 18:00:59 -08002582 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002583 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002584
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002585 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 case 'c':
2587 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002588 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (field_width == -1)
2590 field_width = 1;
2591 do {
2592 *s++ = *str++;
2593 } while (--field_width > 0 && *str);
2594 num++;
2595 }
2596 continue;
2597 case 's':
2598 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002599 char *s = (char *)va_arg(args, char *);
2600 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002601 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002603 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002606 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 *s = '\0';
2609 num++;
2610 }
2611 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 case 'o':
2613 base = 8;
2614 break;
2615 case 'x':
2616 case 'X':
2617 base = 16;
2618 break;
2619 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002620 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002622 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 case 'u':
2624 break;
2625 case '%':
2626 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002627 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 return num;
2629 continue;
2630 default:
2631 /* invalid format; stop here */
2632 return num;
2633 }
2634
2635 /* have some sort of integer conversion.
2636 * first, skip white space in buffer.
2637 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002638 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 digit = *str;
2641 if (is_sign && digit == '-')
2642 digit = *(str + 1);
2643
2644 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002645 || (base == 16 && !isxdigit(digit))
2646 || (base == 10 && !isdigit(digit))
2647 || (base == 8 && (!isdigit(digit) || digit > '7'))
2648 || (base == 0 && !isdigit(digit)))
2649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Jan Beulich53809752012-12-17 16:01:31 -08002651 if (is_sign)
2652 val.s = qualifier != 'L' ?
2653 simple_strtol(str, &next, base) :
2654 simple_strtoll(str, &next, base);
2655 else
2656 val.u = qualifier != 'L' ?
2657 simple_strtoul(str, &next, base) :
2658 simple_strtoull(str, &next, base);
2659
2660 if (field_width > 0 && next - str > field_width) {
2661 if (base == 0)
2662 _parse_integer_fixup_radix(str, &base);
2663 while (next - str > field_width) {
2664 if (is_sign)
2665 val.s = div_s64(val.s, base);
2666 else
2667 val.u = div_u64(val.u, base);
2668 --next;
2669 }
2670 }
2671
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002672 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002674 if (is_sign)
2675 *va_arg(args, signed char *) = val.s;
2676 else
2677 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 break;
2679 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002680 if (is_sign)
2681 *va_arg(args, short *) = val.s;
2682 else
2683 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 break;
2685 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002686 if (is_sign)
2687 *va_arg(args, long *) = val.s;
2688 else
2689 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 break;
2691 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002692 if (is_sign)
2693 *va_arg(args, long long *) = val.s;
2694 else
2695 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 break;
2697 case 'Z':
2698 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002699 *va_arg(args, size_t *) = val.u;
2700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 default:
Jan Beulich53809752012-12-17 16:01:31 -08002702 if (is_sign)
2703 *va_arg(args, int *) = val.s;
2704 else
2705 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 break;
2707 }
2708 num++;
2709
2710 if (!next)
2711 break;
2712 str = next;
2713 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return num;
2716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717EXPORT_SYMBOL(vsscanf);
2718
2719/**
2720 * sscanf - Unformat a buffer into a list of arguments
2721 * @buf: input buffer
2722 * @fmt: formatting of buffer
2723 * @...: resulting arguments
2724 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002725int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 va_list args;
2728 int i;
2729
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002730 va_start(args, fmt);
2731 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return i;
2735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736EXPORT_SYMBOL(sscanf);