blob: 8056dff6de642d2db005d185b60b64e29dd0d73a [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;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800404 int field_width = spec.field_width;
405 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800407 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
408
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100409 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
410 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100411 locase = (spec.flags & SMALL);
412 if (spec.flags & LEFT)
413 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100415 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800416 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800418 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800419 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100420 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800422 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100423 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800425 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700428 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100429 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800430 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700431 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800432 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700434
435 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700437 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700438 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100439 else if (spec.base != 10) { /* 8 or 16 */
440 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700441 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800442
443 if (spec.base == 16)
444 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700445 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700446 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 num >>= shift;
448 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700449 } else { /* base 10 */
450 i = put_dec(tmp, num) - tmp;
451 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700452
453 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800454 if (i > precision)
455 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700456 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800457 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700458 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800459 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700460 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *buf = ' ';
462 ++buf;
463 }
464 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700465 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700467 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 *buf = sign;
469 ++buf;
470 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700471 /* "0x" / "0" prefix */
472 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700473 if (spec.base == 16 || !is_zero) {
474 if (buf < end)
475 *buf = '0';
476 ++buf;
477 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100478 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700479 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100480 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 ++buf;
482 }
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100485 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700486 char c = ' ' + (spec.flags & ZEROPAD);
487 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800488 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700489 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *buf = c;
491 ++buf;
492 }
493 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700494 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800495 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700496 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *buf = '0';
498 ++buf;
499 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700500 /* actual digits of result */
501 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700502 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *buf = tmp[i];
504 ++buf;
505 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700506 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800507 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700508 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buf = ' ';
510 ++buf;
511 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return buf;
514}
515
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800516static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400517{
518 size_t size;
519 if (buf >= end) /* nowhere to put anything */
520 return;
521 size = end - buf;
522 if (size <= spaces) {
523 memset(buf, ' ', size);
524 return;
525 }
526 if (len) {
527 if (len > size - spaces)
528 len = size - spaces;
529 memmove(buf + spaces, buf, len);
530 }
531 memset(buf, ' ', spaces);
532}
533
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800534/*
535 * Handle field width padding for a string.
536 * @buf: current buffer position
537 * @n: length of string
538 * @end: end of output buffer
539 * @spec: for field width and flags
540 * Returns: new buffer position after padding.
541 */
542static noinline_for_stack
543char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
544{
545 unsigned spaces;
546
547 if (likely(n >= spec.field_width))
548 return buf;
549 /* we want to pad the sucker */
550 spaces = spec.field_width - n;
551 if (!(spec.flags & LEFT)) {
552 move_right(buf - n, end, n, spaces);
553 return buf + spaces;
554 }
555 while (spaces--) {
556 if (buf < end)
557 *buf = ' ';
558 ++buf;
559 }
560 return buf;
561}
562
Al Viro4b6ccca2013-09-03 12:00:44 -0400563static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800564char *string(char *buf, char *end, const char *s, struct printf_spec spec)
565{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800566 int len = 0;
567 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800568
569 if ((unsigned long)s < PAGE_SIZE)
570 s = "(null)";
571
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800572 while (lim--) {
573 char c = *s++;
574 if (!c)
575 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800576 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800577 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800578 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800579 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800580 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800581 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800582}
583
584static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400585char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
586 const char *fmt)
587{
588 const char *array[4], *s;
589 const struct dentry *p;
590 int depth;
591 int i, n;
592
593 switch (fmt[1]) {
594 case '2': case '3': case '4':
595 depth = fmt[1] - '0';
596 break;
597 default:
598 depth = 1;
599 }
600
601 rcu_read_lock();
602 for (i = 0; i < depth; i++, d = p) {
603 p = ACCESS_ONCE(d->d_parent);
604 array[i] = ACCESS_ONCE(d->d_name.name);
605 if (p == d) {
606 if (i)
607 array[i] = "";
608 i++;
609 break;
610 }
611 }
612 s = array[--i];
613 for (n = 0; n != spec.precision; n++, buf++) {
614 char c = *s++;
615 if (!c) {
616 if (!i)
617 break;
618 c = '/';
619 s = array[--i];
620 }
621 if (buf < end)
622 *buf = c;
623 }
624 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800625 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400626}
627
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400628#ifdef CONFIG_BLOCK
629static noinline_for_stack
630char *bdev_name(char *buf, char *end, struct block_device *bdev,
631 struct printf_spec spec, const char *fmt)
632{
633 struct gendisk *hd = bdev->bd_disk;
634
635 buf = string(buf, end, hd->disk_name, spec);
636 if (bdev->bd_part->partno) {
637 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
638 if (buf < end)
639 *buf = 'p';
640 buf++;
641 }
642 buf = number(buf, end, bdev->bd_part->partno, spec);
643 }
644 return buf;
645}
646#endif
647
Joe Perchescf3b4292010-05-24 14:33:16 -0700648static noinline_for_stack
649char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800650 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700651{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800652 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700653#ifdef CONFIG_KALLSYMS
654 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800655#endif
656
657 if (fmt[1] == 'R')
658 ptr = __builtin_extract_return_addr(ptr);
659 value = (unsigned long)ptr;
660
661#ifdef CONFIG_KALLSYMS
662 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900663 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800664 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200665 sprint_symbol(sym, value);
666 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700667 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800668
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100669 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700670#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800671 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100672 spec.flags |= SPECIAL | SMALL | ZEROPAD;
673 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800674
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100675 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700676#endif
677}
678
Joe Perchescf3b4292010-05-24 14:33:16 -0700679static noinline_for_stack
680char *resource_string(char *buf, char *end, struct resource *res,
681 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100682{
683#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600684#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100685#endif
686
687#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600688#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100689#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700690 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100691 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700692 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100693 .precision = -1,
694 .flags = SPECIAL | SMALL | ZEROPAD,
695 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700696 static const struct printf_spec mem_spec = {
697 .base = 16,
698 .field_width = MEM_RSRC_PRINTK_SIZE,
699 .precision = -1,
700 .flags = SPECIAL | SMALL | ZEROPAD,
701 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700702 static const struct printf_spec bus_spec = {
703 .base = 16,
704 .field_width = 2,
705 .precision = -1,
706 .flags = SMALL | ZEROPAD,
707 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700708 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600709 .base = 10,
710 .precision = -1,
711 .flags = 0,
712 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700713 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600714 .field_width = -1,
715 .precision = 10,
716 .flags = LEFT,
717 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700718 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600719 .base = 16,
720 .precision = -1,
721 .flags = SPECIAL | SMALL,
722 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600723
724 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
725 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
726#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
727#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700728#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600729#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
730 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
731 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
732
Linus Torvalds332d2e72008-10-20 15:07:34 +1100733 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600734 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700735 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100736
737 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700738 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600739 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700740 specp = &io_spec;
741 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600742 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700743 specp = &mem_spec;
744 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600745 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700746 specp = &dec_spec;
747 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600748 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700749 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700750 } else if (res->flags & IORESOURCE_BUS) {
751 p = string(p, pend, "bus ", str_spec);
752 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700753 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600754 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700755 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600756 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600757 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700758 if (decode && res->flags & IORESOURCE_UNSET) {
759 p = string(p, pend, "size ", str_spec);
760 p = number(p, pend, resource_size(res), *specp);
761 } else {
762 p = number(p, pend, res->start, *specp);
763 if (res->start != res->end) {
764 *p++ = '-';
765 p = number(p, pend, res->end, *specp);
766 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600767 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600768 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600769 if (res->flags & IORESOURCE_MEM_64)
770 p = string(p, pend, " 64bit", str_spec);
771 if (res->flags & IORESOURCE_PREFETCH)
772 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700773 if (res->flags & IORESOURCE_WINDOW)
774 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600775 if (res->flags & IORESOURCE_DISABLED)
776 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600777 } else {
778 p = string(p, pend, " flags ", str_spec);
779 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600780 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100781 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600782 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100783
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100784 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100785}
786
Joe Perchescf3b4292010-05-24 14:33:16 -0700787static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700788char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
789 const char *fmt)
790{
Steven Rostedt360603a2013-05-28 15:47:39 -0400791 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700792 negative value, fallback to the default */
793 char separator;
794
795 if (spec.field_width == 0)
796 /* nothing to print */
797 return buf;
798
799 if (ZERO_OR_NULL_PTR(addr))
800 /* NULL pointer */
801 return string(buf, end, NULL, spec);
802
803 switch (fmt[1]) {
804 case 'C':
805 separator = ':';
806 break;
807 case 'D':
808 separator = '-';
809 break;
810 case 'N':
811 separator = 0;
812 break;
813 default:
814 separator = ' ';
815 break;
816 }
817
818 if (spec.field_width > 0)
819 len = min_t(int, spec.field_width, 64);
820
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700821 for (i = 0; i < len; ++i) {
822 if (buf < end)
823 *buf = hex_asc_hi(addr[i]);
824 ++buf;
825 if (buf < end)
826 *buf = hex_asc_lo(addr[i]);
827 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700828
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700829 if (separator && i != len - 1) {
830 if (buf < end)
831 *buf = separator;
832 ++buf;
833 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700834 }
835
836 return buf;
837}
838
839static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800840char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
841 struct printf_spec spec, const char *fmt)
842{
843 const int CHUNKSZ = 32;
844 int nr_bits = max_t(int, spec.field_width, 0);
845 int i, chunksz;
846 bool first = true;
847
848 /* reused to print numbers */
849 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
850
851 chunksz = nr_bits & (CHUNKSZ - 1);
852 if (chunksz == 0)
853 chunksz = CHUNKSZ;
854
855 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
856 for (; i >= 0; i -= CHUNKSZ) {
857 u32 chunkmask, val;
858 int word, bit;
859
860 chunkmask = ((1ULL << chunksz) - 1);
861 word = i / BITS_PER_LONG;
862 bit = i % BITS_PER_LONG;
863 val = (bitmap[word] >> bit) & chunkmask;
864
865 if (!first) {
866 if (buf < end)
867 *buf = ',';
868 buf++;
869 }
870 first = false;
871
872 spec.field_width = DIV_ROUND_UP(chunksz, 4);
873 buf = number(buf, end, val, spec);
874
875 chunksz = CHUNKSZ;
876 }
877 return buf;
878}
879
880static noinline_for_stack
881char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
882 struct printf_spec spec, const char *fmt)
883{
884 int nr_bits = max_t(int, spec.field_width, 0);
885 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
886 int cur, rbot, rtop;
887 bool first = true;
888
889 /* reused to print numbers */
890 spec = (struct printf_spec){ .base = 10 };
891
892 rbot = cur = find_first_bit(bitmap, nr_bits);
893 while (cur < nr_bits) {
894 rtop = cur;
895 cur = find_next_bit(bitmap, nr_bits, cur + 1);
896 if (cur < nr_bits && cur <= rtop + 1)
897 continue;
898
899 if (!first) {
900 if (buf < end)
901 *buf = ',';
902 buf++;
903 }
904 first = false;
905
906 buf = number(buf, end, rbot, spec);
907 if (rbot < rtop) {
908 if (buf < end)
909 *buf = '-';
910 buf++;
911
912 buf = number(buf, end, rtop, spec);
913 }
914
915 rbot = cur;
916 }
917 return buf;
918}
919
920static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700921char *mac_address_string(char *buf, char *end, u8 *addr,
922 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700923{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000924 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700925 char *p = mac_addr;
926 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000927 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700928 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000929
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700930 switch (fmt[1]) {
931 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000932 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700933 break;
934
935 case 'R':
936 reversed = true;
937 /* fall through */
938
939 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000940 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700941 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000942 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700943
944 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700945 if (reversed)
946 p = hex_byte_pack(p, addr[5 - i]);
947 else
948 p = hex_byte_pack(p, addr[i]);
949
Joe Perches8a27f7c2009-08-17 12:29:44 +0000950 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000951 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700952 }
953 *p = '\0';
954
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100955 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700956}
957
Joe Perchescf3b4292010-05-24 14:33:16 -0700958static noinline_for_stack
959char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700960{
Harvey Harrison689afa72008-10-28 16:04:44 -0700961 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800962 bool leading_zeros = (fmt[0] == 'i');
963 int index;
964 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700965
Joe Perches0159f242010-01-13 20:23:30 -0800966 switch (fmt[2]) {
967 case 'h':
968#ifdef __BIG_ENDIAN
969 index = 0;
970 step = 1;
971#else
972 index = 3;
973 step = -1;
974#endif
975 break;
976 case 'l':
977 index = 3;
978 step = -1;
979 break;
980 case 'n':
981 case 'b':
982 default:
983 index = 0;
984 step = 1;
985 break;
986 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000987 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700988 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700989 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000990 if (leading_zeros) {
991 if (digits < 3)
992 *p++ = '0';
993 if (digits < 2)
994 *p++ = '0';
995 }
996 /* reverse the digits in the quad */
997 while (digits--)
998 *p++ = temp[digits];
999 if (i < 3)
1000 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001001 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001002 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001003 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001004
Joe Perches8a27f7c2009-08-17 12:29:44 +00001005 return p;
1006}
1007
Joe Perchescf3b4292010-05-24 14:33:16 -07001008static noinline_for_stack
1009char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001010{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001011 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001012 unsigned char zerolength[8];
1013 int longest = 1;
1014 int colonpos = -1;
1015 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001016 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001017 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001018 bool useIPv4;
1019 struct in6_addr in6;
1020
1021 memcpy(&in6, addr, sizeof(struct in6_addr));
1022
1023 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001024
1025 memset(zerolength, 0, sizeof(zerolength));
1026
1027 if (useIPv4)
1028 range = 6;
1029 else
1030 range = 8;
1031
1032 /* find position of longest 0 run */
1033 for (i = 0; i < range; i++) {
1034 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001035 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001036 break;
1037 zerolength[i]++;
1038 }
1039 }
1040 for (i = 0; i < range; i++) {
1041 if (zerolength[i] > longest) {
1042 longest = zerolength[i];
1043 colonpos = i;
1044 }
1045 }
Joe Perches29cf5192011-06-09 11:23:37 -07001046 if (longest == 1) /* don't compress a single 0 */
1047 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001048
1049 /* emit address */
1050 for (i = 0; i < range; i++) {
1051 if (i == colonpos) {
1052 if (needcolon || i == 0)
1053 *p++ = ':';
1054 *p++ = ':';
1055 needcolon = false;
1056 i += longest - 1;
1057 continue;
1058 }
1059 if (needcolon) {
1060 *p++ = ':';
1061 needcolon = false;
1062 }
1063 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001064 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001065 hi = word >> 8;
1066 lo = word & 0xff;
1067 if (hi) {
1068 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001069 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001070 else
1071 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001072 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001073 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001074 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001075 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001076 else
1077 *p++ = hex_asc_lo(lo);
1078 needcolon = true;
1079 }
1080
1081 if (useIPv4) {
1082 if (needcolon)
1083 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001084 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001085 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001086 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001087
Joe Perches8a27f7c2009-08-17 12:29:44 +00001088 return p;
1089}
1090
Joe Perchescf3b4292010-05-24 14:33:16 -07001091static noinline_for_stack
1092char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001093{
1094 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001095
Harvey Harrison689afa72008-10-28 16:04:44 -07001096 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001097 p = hex_byte_pack(p, *addr++);
1098 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001099 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001100 *p++ = ':';
1101 }
1102 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001103
Joe Perches8a27f7c2009-08-17 12:29:44 +00001104 return p;
1105}
1106
Joe Perchescf3b4292010-05-24 14:33:16 -07001107static noinline_for_stack
1108char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1109 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001110{
1111 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1112
1113 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001114 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001115 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001116 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001117
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001118 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001119}
1120
Joe Perchescf3b4292010-05-24 14:33:16 -07001121static noinline_for_stack
1122char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1123 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001124{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001125 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001126
Joe Perches0159f242010-01-13 20:23:30 -08001127 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001128
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001129 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001130}
1131
Joe Perchescf3b4292010-05-24 14:33:16 -07001132static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001133char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1134 struct printf_spec spec, const char *fmt)
1135{
1136 bool have_p = false, have_s = false, have_f = false, have_c = false;
1137 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1138 sizeof(":12345") + sizeof("/123456789") +
1139 sizeof("%1234567890")];
1140 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1141 const u8 *addr = (const u8 *) &sa->sin6_addr;
1142 char fmt6[2] = { fmt[0], '6' };
1143 u8 off = 0;
1144
1145 fmt++;
1146 while (isalpha(*++fmt)) {
1147 switch (*fmt) {
1148 case 'p':
1149 have_p = true;
1150 break;
1151 case 'f':
1152 have_f = true;
1153 break;
1154 case 's':
1155 have_s = true;
1156 break;
1157 case 'c':
1158 have_c = true;
1159 break;
1160 }
1161 }
1162
1163 if (have_p || have_s || have_f) {
1164 *p = '[';
1165 off = 1;
1166 }
1167
1168 if (fmt6[0] == 'I' && have_c)
1169 p = ip6_compressed_string(ip6_addr + off, addr);
1170 else
1171 p = ip6_string(ip6_addr + off, addr, fmt6);
1172
1173 if (have_p || have_s || have_f)
1174 *p++ = ']';
1175
1176 if (have_p) {
1177 *p++ = ':';
1178 p = number(p, pend, ntohs(sa->sin6_port), spec);
1179 }
1180 if (have_f) {
1181 *p++ = '/';
1182 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1183 IPV6_FLOWINFO_MASK), spec);
1184 }
1185 if (have_s) {
1186 *p++ = '%';
1187 p = number(p, pend, sa->sin6_scope_id, spec);
1188 }
1189 *p = '\0';
1190
1191 return string(buf, end, ip6_addr, spec);
1192}
1193
1194static noinline_for_stack
1195char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1196 struct printf_spec spec, const char *fmt)
1197{
1198 bool have_p = false;
1199 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1200 char *pend = ip4_addr + sizeof(ip4_addr);
1201 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1202 char fmt4[3] = { fmt[0], '4', 0 };
1203
1204 fmt++;
1205 while (isalpha(*++fmt)) {
1206 switch (*fmt) {
1207 case 'p':
1208 have_p = true;
1209 break;
1210 case 'h':
1211 case 'l':
1212 case 'n':
1213 case 'b':
1214 fmt4[2] = *fmt;
1215 break;
1216 }
1217 }
1218
1219 p = ip4_string(ip4_addr, addr, fmt4);
1220 if (have_p) {
1221 *p++ = ':';
1222 p = number(p, pend, ntohs(sa->sin_port), spec);
1223 }
1224 *p = '\0';
1225
1226 return string(buf, end, ip4_addr, spec);
1227}
1228
1229static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001230char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1231 const char *fmt)
1232{
1233 bool found = true;
1234 int count = 1;
1235 unsigned int flags = 0;
1236 int len;
1237
1238 if (spec.field_width == 0)
1239 return buf; /* nothing to print */
1240
1241 if (ZERO_OR_NULL_PTR(addr))
1242 return string(buf, end, NULL, spec); /* NULL pointer */
1243
1244
1245 do {
1246 switch (fmt[count++]) {
1247 case 'a':
1248 flags |= ESCAPE_ANY;
1249 break;
1250 case 'c':
1251 flags |= ESCAPE_SPECIAL;
1252 break;
1253 case 'h':
1254 flags |= ESCAPE_HEX;
1255 break;
1256 case 'n':
1257 flags |= ESCAPE_NULL;
1258 break;
1259 case 'o':
1260 flags |= ESCAPE_OCTAL;
1261 break;
1262 case 'p':
1263 flags |= ESCAPE_NP;
1264 break;
1265 case 's':
1266 flags |= ESCAPE_SPACE;
1267 break;
1268 default:
1269 found = false;
1270 break;
1271 }
1272 } while (found);
1273
1274 if (!flags)
1275 flags = ESCAPE_ANY_NP;
1276
1277 len = spec.field_width < 0 ? 1 : spec.field_width;
1278
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001279 /*
1280 * string_escape_mem() writes as many characters as it can to
1281 * the given buffer, and returns the total size of the output
1282 * had the buffer been big enough.
1283 */
1284 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001285
1286 return buf;
1287}
1288
1289static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001290char *uuid_string(char *buf, char *end, const u8 *addr,
1291 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001292{
1293 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1294 char *p = uuid;
1295 int i;
1296 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1297 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1298 const u8 *index = be;
1299 bool uc = false;
1300
1301 switch (*(++fmt)) {
1302 case 'L':
1303 uc = true; /* fall-through */
1304 case 'l':
1305 index = le;
1306 break;
1307 case 'B':
1308 uc = true;
1309 break;
1310 }
1311
1312 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001313 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001314 switch (i) {
1315 case 3:
1316 case 5:
1317 case 7:
1318 case 9:
1319 *p++ = '-';
1320 break;
1321 }
1322 }
1323
1324 *p = 0;
1325
1326 if (uc) {
1327 p = uuid;
1328 do {
1329 *p = toupper(*p);
1330 } while (*(++p));
1331 }
1332
1333 return string(buf, end, uuid, spec);
1334}
1335
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001336static
1337char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1338 struct printf_spec spec)
1339{
1340 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1341 if (spec.field_width == -1)
1342 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1343 spec.base = 16;
1344
1345 return number(buf, end, *(const netdev_features_t *)addr, spec);
1346}
1347
Joe Perchesaaf07622014-01-23 15:54:17 -08001348static noinline_for_stack
1349char *address_val(char *buf, char *end, const void *addr,
1350 struct printf_spec spec, const char *fmt)
1351{
1352 unsigned long long num;
1353
1354 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1355 spec.base = 16;
1356
1357 switch (fmt[1]) {
1358 case 'd':
1359 num = *(const dma_addr_t *)addr;
1360 spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1361 break;
1362 case 'p':
1363 default:
1364 num = *(const phys_addr_t *)addr;
1365 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1366 break;
1367 }
1368
1369 return number(buf, end, num, spec);
1370}
1371
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001372static noinline_for_stack
1373char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1374 const char *fmt)
1375{
1376 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1377 return string(buf, end, NULL, spec);
1378
1379 switch (fmt[1]) {
1380 case 'r':
1381 return number(buf, end, clk_get_rate(clk), spec);
1382
1383 case 'n':
1384 default:
1385#ifdef CONFIG_COMMON_CLK
1386 return string(buf, end, __clk_get_name(clk), spec);
1387#else
1388 spec.base = 16;
1389 spec.field_width = sizeof(unsigned long) * 2 + 2;
1390 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1391 return number(buf, end, (unsigned long)clk, spec);
1392#endif
1393 }
1394}
1395
Ingo Molnar411f05f2011-05-12 23:00:28 +02001396int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001397
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001398/*
1399 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1400 * by an extra set of alphanumeric characters that are extended format
1401 * specifiers.
1402 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001403 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001404 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001405 * - 'F' For symbolic function descriptor pointers with offset
1406 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001407 * - 'S' For symbolic direct pointers with offset
1408 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001409 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001410 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001411 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1412 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001413 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1414 * width which must be explicitly specified either as part of the
1415 * format string '%32b[l]' or through '%*b[l]', [l] selects
1416 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001417 * - 'M' For a 6-byte MAC address, it prints the address in the
1418 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001419 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001420 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001421 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001422 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001423 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1424 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1425 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001426 * [S][pfs]
1427 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1428 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001429 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1430 * IPv6 omits the colons (01020304...0f)
1431 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001432 * [S][pfs]
1433 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1434 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1435 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1436 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001437 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001438 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1439 * of the following flags (see string_escape_mem() for the
1440 * details):
1441 * a - ESCAPE_ANY
1442 * c - ESCAPE_SPECIAL
1443 * h - ESCAPE_HEX
1444 * n - ESCAPE_NULL
1445 * o - ESCAPE_OCTAL
1446 * p - ESCAPE_NP
1447 * s - ESCAPE_SPACE
1448 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001449 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1450 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1451 * Options for %pU are:
1452 * b big endian lower case hex (default)
1453 * B big endian UPPER case hex
1454 * l little endian lower case hex
1455 * L little endian UPPER case hex
1456 * big endian output byte order is:
1457 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1458 * little endian output byte order is:
1459 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001460 * - 'V' For a struct va_format which contains a format string * and va_list *,
1461 * call vsnprintf(->format, *->va_list).
1462 * Implements a "recursive vsnprintf".
1463 * Do not use this feature without some mechanism to verify the
1464 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001465 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001466 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001467 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1468 * a certain separator (' ' by default):
1469 * C colon
1470 * D dash
1471 * N no separator
1472 * The maximum supported length is 64 bytes of the input. Consider
1473 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001474 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1475 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001476 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1477 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001478 * - 'g' For block_device name (gendisk + partition number)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001479 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1480 * (legacy clock framework) of the clock
1481 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1482 * (legacy clock framework) of the clock
1483 * - 'Cr' For a clock, it prints the current rate of the clock
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001484 *
1485 * ** Please update also Documentation/printk-formats.txt when making changes **
Joe Perches9ac6e442009-12-14 18:01:09 -08001486 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001487 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1488 * function pointers are really function descriptors, which contain a
1489 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001490 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001491static noinline_for_stack
1492char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1493 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001494{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001495 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001496
Kees Cook9f36e2c2011-03-22 16:34:22 -07001497 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001498 /*
1499 * Print (null) with the same width as a pointer so it makes
1500 * tabular output look nice.
1501 */
1502 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001503 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001504 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001505 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001506
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001507 switch (*fmt) {
1508 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001509 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001510 ptr = dereference_function_descriptor(ptr);
1511 /* Fallthrough */
1512 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001513 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001514 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001515 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001516 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001517 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001518 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001519 case 'h':
1520 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001521 case 'b':
1522 switch (fmt[1]) {
1523 case 'l':
1524 return bitmap_list_string(buf, end, ptr, spec, fmt);
1525 default:
1526 return bitmap_string(buf, end, ptr, spec, fmt);
1527 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001528 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1529 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001530 /* [mM]F (FDDI) */
1531 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001532 return mac_address_string(buf, end, ptr, spec, fmt);
1533 case 'I': /* Formatted IP supported
1534 * 4: 1.2.3.4
1535 * 6: 0001:0203:...:0708
1536 * 6c: 1::708 or 1::1.2.3.4
1537 */
1538 case 'i': /* Contiguous:
1539 * 4: 001.002.003.004
1540 * 6: 000102...0f
1541 */
1542 switch (fmt[1]) {
1543 case '6':
1544 return ip6_addr_string(buf, end, ptr, spec, fmt);
1545 case '4':
1546 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001547 case 'S': {
1548 const union {
1549 struct sockaddr raw;
1550 struct sockaddr_in v4;
1551 struct sockaddr_in6 v6;
1552 } *sa = ptr;
1553
1554 switch (sa->raw.sa_family) {
1555 case AF_INET:
1556 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1557 case AF_INET6:
1558 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1559 default:
1560 return string(buf, end, "(invalid address)", spec);
1561 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001562 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001563 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001564 case 'E':
1565 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001566 case 'U':
1567 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001568 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001569 {
1570 va_list va;
1571
1572 va_copy(va, *((struct va_format *)ptr)->va);
1573 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1574 ((struct va_format *)ptr)->fmt, va);
1575 va_end(va);
1576 return buf;
1577 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001578 case 'K':
1579 /*
1580 * %pK cannot be used in IRQ context because its test
1581 * for CAP_SYSLOG would be meaningless.
1582 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001583 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1584 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001585 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001586 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001587 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001588 }
Ryan Mallon312b4e22013-11-12 15:08:51 -08001589
1590 switch (kptr_restrict) {
1591 case 0:
1592 /* Always print %pK values */
1593 break;
1594 case 1: {
1595 /*
1596 * Only print the real pointer value if the current
1597 * process has CAP_SYSLOG and is running with the
1598 * same credentials it started with. This is because
1599 * access to files is checked at open() time, but %pK
1600 * checks permission at read() time. We don't want to
1601 * leak pointer values if a binary opens a file using
1602 * %pK and then elevates privileges before reading it.
1603 */
1604 const struct cred *cred = current_cred();
1605
1606 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1607 !uid_eq(cred->euid, cred->uid) ||
1608 !gid_eq(cred->egid, cred->gid))
1609 ptr = NULL;
1610 break;
1611 }
1612 case 2:
1613 default:
1614 /* Always print 0's for %pK */
Joe Perches26297602011-03-22 16:34:19 -07001615 ptr = NULL;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001616 break;
1617 }
Joe Perches26297602011-03-22 16:34:19 -07001618 break;
Ryan Mallon312b4e22013-11-12 15:08:51 -08001619
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001620 case 'N':
1621 switch (fmt[1]) {
1622 case 'F':
1623 return netdev_feature_string(buf, end, ptr, spec);
1624 }
1625 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001626 case 'a':
Joe Perchesaaf07622014-01-23 15:54:17 -08001627 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001628 case 'd':
1629 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001630 case 'C':
1631 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001632 case 'D':
1633 return dentry_name(buf, end,
1634 ((const struct file *)ptr)->f_path.dentry,
1635 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001636#ifdef CONFIG_BLOCK
1637 case 'g':
1638 return bdev_name(buf, end, ptr, spec, fmt);
1639#endif
1640
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001641 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001642 spec.flags |= SMALL;
1643 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001644 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001645 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001646 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001647 spec.base = 16;
1648
1649 return number(buf, end, (unsigned long) ptr, spec);
1650}
1651
1652/*
1653 * Helper function to decode printf style format.
1654 * Each call decode a token from the format and return the
1655 * number of characters read (or likely the delta where it wants
1656 * to go on the next call).
1657 * The decoded token is returned through the parameters
1658 *
1659 * 'h', 'l', or 'L' for integer fields
1660 * 'z' support added 23/7/1999 S.H.
1661 * 'z' changed to 'Z' --davidm 1/25/99
1662 * 't' added for ptrdiff_t
1663 *
1664 * @fmt: the format string
1665 * @type of the token returned
1666 * @flags: various flags such as +, -, # tokens..
1667 * @field_width: overwritten width
1668 * @base: base of the number (octal, hex, ...)
1669 * @precision: precision of a number
1670 * @qualifier: qualifier of a number (long, size_t, ...)
1671 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001672static noinline_for_stack
1673int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001674{
1675 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001676 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001677
1678 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001679 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001680 if (spec->field_width < 0) {
1681 spec->field_width = -spec->field_width;
1682 spec->flags |= LEFT;
1683 }
1684 spec->type = FORMAT_TYPE_NONE;
1685 goto precision;
1686 }
1687
1688 /* we finished early by reading the precision */
1689 if (spec->type == FORMAT_TYPE_PRECISION) {
1690 if (spec->precision < 0)
1691 spec->precision = 0;
1692
1693 spec->type = FORMAT_TYPE_NONE;
1694 goto qualifier;
1695 }
1696
1697 /* By default */
1698 spec->type = FORMAT_TYPE_NONE;
1699
1700 for (; *fmt ; ++fmt) {
1701 if (*fmt == '%')
1702 break;
1703 }
1704
1705 /* Return the current non-format string */
1706 if (fmt != start || !*fmt)
1707 return fmt - start;
1708
1709 /* Process flags */
1710 spec->flags = 0;
1711
1712 while (1) { /* this also skips first '%' */
1713 bool found = true;
1714
1715 ++fmt;
1716
1717 switch (*fmt) {
1718 case '-': spec->flags |= LEFT; break;
1719 case '+': spec->flags |= PLUS; break;
1720 case ' ': spec->flags |= SPACE; break;
1721 case '#': spec->flags |= SPECIAL; break;
1722 case '0': spec->flags |= ZEROPAD; break;
1723 default: found = false;
1724 }
1725
1726 if (!found)
1727 break;
1728 }
1729
1730 /* get field width */
1731 spec->field_width = -1;
1732
1733 if (isdigit(*fmt))
1734 spec->field_width = skip_atoi(&fmt);
1735 else if (*fmt == '*') {
1736 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001737 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001738 return ++fmt - start;
1739 }
1740
1741precision:
1742 /* get the precision */
1743 spec->precision = -1;
1744 if (*fmt == '.') {
1745 ++fmt;
1746 if (isdigit(*fmt)) {
1747 spec->precision = skip_atoi(&fmt);
1748 if (spec->precision < 0)
1749 spec->precision = 0;
1750 } else if (*fmt == '*') {
1751 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001752 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001753 return ++fmt - start;
1754 }
1755 }
1756
1757qualifier:
1758 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001759 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001760 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1761 _tolower(*fmt) == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001762 qualifier = *fmt++;
1763 if (unlikely(qualifier == *fmt)) {
1764 if (qualifier == 'l') {
1765 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08001766 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001767 } else if (qualifier == 'h') {
1768 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08001769 ++fmt;
1770 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001771 }
1772 }
1773
1774 /* default base */
1775 spec->base = 10;
1776 switch (*fmt) {
1777 case 'c':
1778 spec->type = FORMAT_TYPE_CHAR;
1779 return ++fmt - start;
1780
1781 case 's':
1782 spec->type = FORMAT_TYPE_STR;
1783 return ++fmt - start;
1784
1785 case 'p':
1786 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001787 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001788
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001789 case '%':
1790 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1791 return ++fmt - start;
1792
1793 /* integer number formats - set up the flags and "break" */
1794 case 'o':
1795 spec->base = 8;
1796 break;
1797
1798 case 'x':
1799 spec->flags |= SMALL;
1800
1801 case 'X':
1802 spec->base = 16;
1803 break;
1804
1805 case 'd':
1806 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001807 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001808 case 'u':
1809 break;
1810
Ryan Mallon708d96f2014-04-03 14:48:37 -07001811 case 'n':
1812 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001813 * Since %n poses a greater security risk than
1814 * utility, treat it as any other invalid or
1815 * unsupported format specifier.
Ryan Mallon708d96f2014-04-03 14:48:37 -07001816 */
Ryan Mallon708d96f2014-04-03 14:48:37 -07001817 /* Fall-through */
1818
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001819 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001820 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001821 spec->type = FORMAT_TYPE_INVALID;
1822 return fmt - start;
1823 }
1824
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001825 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001826 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001827 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001828 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1829 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001830 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001831 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001832 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001833 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001834 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001835 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1836 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001837 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001838 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1839 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001840 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07001841 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
1842 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001843 }
1844
1845 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/**
1849 * vsnprintf - Format a string and place it in a buffer
1850 * @buf: The buffer to place the result into
1851 * @size: The size of the buffer, including the trailing null space
1852 * @fmt: The format string to use
1853 * @args: Arguments for the format string
1854 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08001855 * This function generally follows C99 vsnprintf, but has some
1856 * extensions and a few limitations:
1857 *
1858 * %n is unsupported
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001859 * %p* is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07001860 *
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001861 * See pointer() or Documentation/printk-formats.txt for more
1862 * extensive description.
1863 *
1864 * ** Please update the documentation in both places when making changes **
Andrew Morton80f548e2012-07-30 14:40:25 -07001865 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * The return value is the number of characters which would
1867 * be generated for the given input, excluding the trailing
1868 * '\0', as per ISO C99. If you want to have the exact
1869 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001870 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * return is greater than or equal to @size, the resulting
1872 * string is truncated.
1873 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001874 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
1876int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001879 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001880 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001882 /* Reject out-of-range values early. Large positive sizes are
1883 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08001884 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001888 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001890 /* Make sure end is always >= buf */
1891 if (end < buf) {
1892 end = ((void *)-1);
1893 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001896 while (*fmt) {
1897 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001898 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001899
1900 fmt += read;
1901
1902 switch (spec.type) {
1903 case FORMAT_TYPE_NONE: {
1904 int copy = read;
1905 if (str < end) {
1906 if (copy > end - str)
1907 copy = end - str;
1908 memcpy(str, old_fmt, copy);
1909 }
1910 str += read;
1911 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
Vegard Nossumed681a92009-03-14 12:08:50 +01001914 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001915 spec.field_width = va_arg(args, int);
1916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001918 case FORMAT_TYPE_PRECISION:
1919 spec.precision = va_arg(args, int);
1920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
André Goddard Rosad4be1512009-12-14 18:00:59 -08001922 case FORMAT_TYPE_CHAR: {
1923 char c;
1924
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001925 if (!(spec.flags & LEFT)) {
1926 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001927 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 *str = ' ';
1929 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001932 }
1933 c = (unsigned char) va_arg(args, int);
1934 if (str < end)
1935 *str = c;
1936 ++str;
1937 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001938 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001939 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001941 }
1942 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001945 case FORMAT_TYPE_STR:
1946 str = string(str, end, va_arg(args, char *), spec);
1947 break;
1948
1949 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08001950 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001951 spec);
1952 while (isalnum(*fmt))
1953 fmt++;
1954 break;
1955
1956 case FORMAT_TYPE_PERCENT_CHAR:
1957 if (str < end)
1958 *str = '%';
1959 ++str;
1960 break;
1961
1962 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08001963 /*
1964 * Presumably the arguments passed gcc's type
1965 * checking, but there is no safe or sane way
1966 * for us to continue parsing the format and
1967 * fetching from the va_list; the remaining
1968 * specifiers and arguments would be out of
1969 * sync.
1970 */
1971 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001972
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001973 default:
1974 switch (spec.type) {
1975 case FORMAT_TYPE_LONG_LONG:
1976 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001978 case FORMAT_TYPE_ULONG:
1979 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001981 case FORMAT_TYPE_LONG:
1982 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001984 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001985 if (spec.flags & SIGN)
1986 num = va_arg(args, ssize_t);
1987 else
1988 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001989 break;
1990 case FORMAT_TYPE_PTRDIFF:
1991 num = va_arg(args, ptrdiff_t);
1992 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001993 case FORMAT_TYPE_UBYTE:
1994 num = (unsigned char) va_arg(args, int);
1995 break;
1996 case FORMAT_TYPE_BYTE:
1997 num = (signed char) va_arg(args, int);
1998 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001999 case FORMAT_TYPE_USHORT:
2000 num = (unsigned short) va_arg(args, int);
2001 break;
2002 case FORMAT_TYPE_SHORT:
2003 num = (short) va_arg(args, int);
2004 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002005 case FORMAT_TYPE_INT:
2006 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002009 num = va_arg(args, unsigned int);
2010 }
2011
2012 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002015
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002016out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002017 if (size > 0) {
2018 if (str < end)
2019 *str = '\0';
2020 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002021 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002022 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002023
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002024 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028EXPORT_SYMBOL(vsnprintf);
2029
2030/**
2031 * vscnprintf - Format a string and place it in a buffer
2032 * @buf: The buffer to place the result into
2033 * @size: The size of the buffer, including the trailing null space
2034 * @fmt: The format string to use
2035 * @args: Arguments for the format string
2036 *
2037 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002038 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * returns 0.
2040 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002041 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002042 *
2043 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 */
2045int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2046{
2047 int i;
2048
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002049 i = vsnprintf(buf, size, fmt, args);
2050
Anton Arapovb921c692011-01-12 16:59:49 -08002051 if (likely(i < size))
2052 return i;
2053 if (size != 0)
2054 return size - 1;
2055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057EXPORT_SYMBOL(vscnprintf);
2058
2059/**
2060 * snprintf - Format a string and place it in a buffer
2061 * @buf: The buffer to place the result into
2062 * @size: The size of the buffer, including the trailing null space
2063 * @fmt: The format string to use
2064 * @...: Arguments for the format string
2065 *
2066 * The return value is the number of characters which would be
2067 * generated for the given input, excluding the trailing null,
2068 * as per ISO C99. If the return is greater than or equal to
2069 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002070 *
2071 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002073int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 va_list args;
2076 int i;
2077
2078 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002079 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return i;
2083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084EXPORT_SYMBOL(snprintf);
2085
2086/**
2087 * scnprintf - Format a string and place it in a buffer
2088 * @buf: The buffer to place the result into
2089 * @size: The size of the buffer, including the trailing null space
2090 * @fmt: The format string to use
2091 * @...: Arguments for the format string
2092 *
2093 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002094 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
2096
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002097int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 va_list args;
2100 int i;
2101
2102 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002103 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002105
Anton Arapovb921c692011-01-12 16:59:49 -08002106 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108EXPORT_SYMBOL(scnprintf);
2109
2110/**
2111 * vsprintf - Format a string and place it in a buffer
2112 * @buf: The buffer to place the result into
2113 * @fmt: The format string to use
2114 * @args: Arguments for the format string
2115 *
2116 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002117 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * buffer overflows.
2119 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002120 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002121 *
2122 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
2124int vsprintf(char *buf, const char *fmt, va_list args)
2125{
2126 return vsnprintf(buf, INT_MAX, fmt, args);
2127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128EXPORT_SYMBOL(vsprintf);
2129
2130/**
2131 * sprintf - Format a string and place it in a buffer
2132 * @buf: The buffer to place the result into
2133 * @fmt: The format string to use
2134 * @...: Arguments for the format string
2135 *
2136 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002137 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002139 *
2140 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002142int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
2144 va_list args;
2145 int i;
2146
2147 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002148 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return i;
2152}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153EXPORT_SYMBOL(sprintf);
2154
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002155#ifdef CONFIG_BINARY_PRINTF
2156/*
2157 * bprintf service:
2158 * vbin_printf() - VA arguments to binary data
2159 * bstr_printf() - Binary data to text string
2160 */
2161
2162/**
2163 * vbin_printf - Parse a format string and place args' binary value in a buffer
2164 * @bin_buf: The buffer to place args' binary value
2165 * @size: The size of the buffer(by words(32bits), not characters)
2166 * @fmt: The format string to use
2167 * @args: Arguments for the format string
2168 *
2169 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002170 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002171 *
2172 * The return value is the number of words(32bits) which would be generated for
2173 * the given input.
2174 *
2175 * NOTE:
2176 * If the return value is greater than @size, the resulting bin_buf is NOT
2177 * valid for bstr_printf().
2178 */
2179int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2180{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002181 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002182 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002183
2184 str = (char *)bin_buf;
2185 end = (char *)(bin_buf + size);
2186
2187#define save_arg(type) \
2188do { \
2189 if (sizeof(type) == 8) { \
2190 unsigned long long value; \
2191 str = PTR_ALIGN(str, sizeof(u32)); \
2192 value = va_arg(args, unsigned long long); \
2193 if (str + sizeof(type) <= end) { \
2194 *(u32 *)str = *(u32 *)&value; \
2195 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2196 } \
2197 } else { \
2198 unsigned long value; \
2199 str = PTR_ALIGN(str, sizeof(type)); \
2200 value = va_arg(args, int); \
2201 if (str + sizeof(type) <= end) \
2202 *(typeof(type) *)str = (type)value; \
2203 } \
2204 str += sizeof(type); \
2205} while (0)
2206
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002207 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002208 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002209
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002210 fmt += read;
2211
2212 switch (spec.type) {
2213 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002214 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002215 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002216 case FORMAT_TYPE_INVALID:
2217 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002218
Vegard Nossumed681a92009-03-14 12:08:50 +01002219 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002220 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002221 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002222 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002223
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002224 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002225 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002226 break;
2227
2228 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002229 const char *save_str = va_arg(args, char *);
2230 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002231
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002232 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2233 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002234 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002235 len = strlen(save_str) + 1;
2236 if (str + len < end)
2237 memcpy(str, save_str, len);
2238 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002239 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002240 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002241
2242 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002243 save_arg(void *);
2244 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002245 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002246 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002247 break;
2248
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002249 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002250 switch (spec.type) {
2251
2252 case FORMAT_TYPE_LONG_LONG:
2253 save_arg(long long);
2254 break;
2255 case FORMAT_TYPE_ULONG:
2256 case FORMAT_TYPE_LONG:
2257 save_arg(unsigned long);
2258 break;
2259 case FORMAT_TYPE_SIZE_T:
2260 save_arg(size_t);
2261 break;
2262 case FORMAT_TYPE_PTRDIFF:
2263 save_arg(ptrdiff_t);
2264 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002265 case FORMAT_TYPE_UBYTE:
2266 case FORMAT_TYPE_BYTE:
2267 save_arg(char);
2268 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002269 case FORMAT_TYPE_USHORT:
2270 case FORMAT_TYPE_SHORT:
2271 save_arg(short);
2272 break;
2273 default:
2274 save_arg(int);
2275 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002276 }
2277 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002278
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002279out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002280 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002281#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002282}
2283EXPORT_SYMBOL_GPL(vbin_printf);
2284
2285/**
2286 * bstr_printf - Format a string from binary arguments and place it in a buffer
2287 * @buf: The buffer to place the result into
2288 * @size: The size of the buffer, including the trailing null space
2289 * @fmt: The format string to use
2290 * @bin_buf: Binary arguments for the format string
2291 *
2292 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2293 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2294 * a binary buffer that generated by vbin_printf.
2295 *
2296 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002297 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002298 *
2299 * The return value is the number of characters which would
2300 * be generated for the given input, excluding the trailing
2301 * '\0', as per ISO C99. If you want to have the exact
2302 * number of characters written into @buf as return value
2303 * (not including the trailing '\0'), use vscnprintf(). If the
2304 * return is greater than or equal to @size, the resulting
2305 * string is truncated.
2306 */
2307int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2308{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002309 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002310 char *str, *end;
2311 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002312
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002313 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002314 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002315
2316 str = buf;
2317 end = buf + size;
2318
2319#define get_arg(type) \
2320({ \
2321 typeof(type) value; \
2322 if (sizeof(type) == 8) { \
2323 args = PTR_ALIGN(args, sizeof(u32)); \
2324 *(u32 *)&value = *(u32 *)args; \
2325 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2326 } else { \
2327 args = PTR_ALIGN(args, sizeof(type)); \
2328 value = *(typeof(type) *)args; \
2329 } \
2330 args += sizeof(type); \
2331 value; \
2332})
2333
2334 /* Make sure end is always >= buf */
2335 if (end < buf) {
2336 end = ((void *)-1);
2337 size = end - buf;
2338 }
2339
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002340 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002341 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002342 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002343
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002344 fmt += read;
2345
2346 switch (spec.type) {
2347 case FORMAT_TYPE_NONE: {
2348 int copy = read;
2349 if (str < end) {
2350 if (copy > end - str)
2351 copy = end - str;
2352 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002353 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002354 str += read;
2355 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002356 }
2357
Vegard Nossumed681a92009-03-14 12:08:50 +01002358 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002359 spec.field_width = get_arg(int);
2360 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002361
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002362 case FORMAT_TYPE_PRECISION:
2363 spec.precision = get_arg(int);
2364 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002365
André Goddard Rosad4be1512009-12-14 18:00:59 -08002366 case FORMAT_TYPE_CHAR: {
2367 char c;
2368
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002369 if (!(spec.flags & LEFT)) {
2370 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002371 if (str < end)
2372 *str = ' ';
2373 ++str;
2374 }
2375 }
2376 c = (unsigned char) get_arg(char);
2377 if (str < end)
2378 *str = c;
2379 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002380 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002381 if (str < end)
2382 *str = ' ';
2383 ++str;
2384 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002385 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002386 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002387
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002388 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002389 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002390 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002391 str = string(str, end, (char *)str_arg, spec);
2392 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002393 }
2394
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002395 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002396 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002397 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002398 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002399 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002400
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002401 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002402 if (str < end)
2403 *str = '%';
2404 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002405 break;
2406
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002407 case FORMAT_TYPE_INVALID:
2408 goto out;
2409
André Goddard Rosad4be1512009-12-14 18:00:59 -08002410 default: {
2411 unsigned long long num;
2412
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002413 switch (spec.type) {
2414
2415 case FORMAT_TYPE_LONG_LONG:
2416 num = get_arg(long long);
2417 break;
2418 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002419 case FORMAT_TYPE_LONG:
2420 num = get_arg(unsigned long);
2421 break;
2422 case FORMAT_TYPE_SIZE_T:
2423 num = get_arg(size_t);
2424 break;
2425 case FORMAT_TYPE_PTRDIFF:
2426 num = get_arg(ptrdiff_t);
2427 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002428 case FORMAT_TYPE_UBYTE:
2429 num = get_arg(unsigned char);
2430 break;
2431 case FORMAT_TYPE_BYTE:
2432 num = get_arg(signed char);
2433 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002434 case FORMAT_TYPE_USHORT:
2435 num = get_arg(unsigned short);
2436 break;
2437 case FORMAT_TYPE_SHORT:
2438 num = get_arg(short);
2439 break;
2440 case FORMAT_TYPE_UINT:
2441 num = get_arg(unsigned int);
2442 break;
2443 default:
2444 num = get_arg(int);
2445 }
2446
2447 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002448 } /* default: */
2449 } /* switch(spec.type) */
2450 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002451
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002452out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002453 if (size > 0) {
2454 if (str < end)
2455 *str = '\0';
2456 else
2457 end[-1] = '\0';
2458 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002459
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002460#undef get_arg
2461
2462 /* the trailing null byte doesn't count towards the total */
2463 return str - buf;
2464}
2465EXPORT_SYMBOL_GPL(bstr_printf);
2466
2467/**
2468 * bprintf - Parse a format string and place args' binary value in a buffer
2469 * @bin_buf: The buffer to place args' binary value
2470 * @size: The size of the buffer(by words(32bits), not characters)
2471 * @fmt: The format string to use
2472 * @...: Arguments for the format string
2473 *
2474 * The function returns the number of words(u32) written
2475 * into @bin_buf.
2476 */
2477int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2478{
2479 va_list args;
2480 int ret;
2481
2482 va_start(args, fmt);
2483 ret = vbin_printf(bin_buf, size, fmt, args);
2484 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002485
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002486 return ret;
2487}
2488EXPORT_SYMBOL_GPL(bprintf);
2489
2490#endif /* CONFIG_BINARY_PRINTF */
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492/**
2493 * vsscanf - Unformat a buffer into a list of arguments
2494 * @buf: input buffer
2495 * @fmt: format of buffer
2496 * @args: arguments
2497 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002498int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
2500 const char *str = buf;
2501 char *next;
2502 char digit;
2503 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002504 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002505 unsigned int base;
2506 union {
2507 long long s;
2508 unsigned long long u;
2509 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002510 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002511 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Jan Beulichda990752012-10-04 17:13:24 -07002513 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /* skip any white space in format */
2515 /* white space in format matchs any amount of
2516 * white space, including none, in the input.
2517 */
2518 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002519 fmt = skip_spaces(++fmt);
2520 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522
2523 /* anything that is not a conversion must match exactly */
2524 if (*fmt != '%' && *fmt) {
2525 if (*fmt++ != *str++)
2526 break;
2527 continue;
2528 }
2529
2530 if (!*fmt)
2531 break;
2532 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /* skip this conversion.
2535 * advance both strings to next white space
2536 */
2537 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002538 if (!*str)
2539 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002540 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 fmt++;
2542 while (!isspace(*str) && *str)
2543 str++;
2544 continue;
2545 }
2546
2547 /* get field width */
2548 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002549 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002551 if (field_width <= 0)
2552 break;
2553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /* get conversion qualifier */
2556 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002557 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2558 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 qualifier = *fmt++;
2560 if (unlikely(qualifier == *fmt)) {
2561 if (qualifier == 'h') {
2562 qualifier = 'H';
2563 fmt++;
2564 } else if (qualifier == 'l') {
2565 qualifier = 'L';
2566 fmt++;
2567 }
2568 }
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Jan Beulichda990752012-10-04 17:13:24 -07002571 if (!*fmt)
2572 break;
2573
2574 if (*fmt == 'n') {
2575 /* return number of characters read so far */
2576 *va_arg(args, int *) = str - buf;
2577 ++fmt;
2578 continue;
2579 }
2580
2581 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 break;
2583
André Goddard Rosad4be1512009-12-14 18:00:59 -08002584 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002585 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002586
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002587 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 case 'c':
2589 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002590 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (field_width == -1)
2592 field_width = 1;
2593 do {
2594 *s++ = *str++;
2595 } while (--field_width > 0 && *str);
2596 num++;
2597 }
2598 continue;
2599 case 's':
2600 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002601 char *s = (char *)va_arg(args, char *);
2602 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002603 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002605 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002608 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 *s = '\0';
2611 num++;
2612 }
2613 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 case 'o':
2615 base = 8;
2616 break;
2617 case 'x':
2618 case 'X':
2619 base = 16;
2620 break;
2621 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002622 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002624 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 case 'u':
2626 break;
2627 case '%':
2628 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002629 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return num;
2631 continue;
2632 default:
2633 /* invalid format; stop here */
2634 return num;
2635 }
2636
2637 /* have some sort of integer conversion.
2638 * first, skip white space in buffer.
2639 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002640 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 digit = *str;
2643 if (is_sign && digit == '-')
2644 digit = *(str + 1);
2645
2646 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002647 || (base == 16 && !isxdigit(digit))
2648 || (base == 10 && !isdigit(digit))
2649 || (base == 8 && (!isdigit(digit) || digit > '7'))
2650 || (base == 0 && !isdigit(digit)))
2651 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Jan Beulich53809752012-12-17 16:01:31 -08002653 if (is_sign)
2654 val.s = qualifier != 'L' ?
2655 simple_strtol(str, &next, base) :
2656 simple_strtoll(str, &next, base);
2657 else
2658 val.u = qualifier != 'L' ?
2659 simple_strtoul(str, &next, base) :
2660 simple_strtoull(str, &next, base);
2661
2662 if (field_width > 0 && next - str > field_width) {
2663 if (base == 0)
2664 _parse_integer_fixup_radix(str, &base);
2665 while (next - str > field_width) {
2666 if (is_sign)
2667 val.s = div_s64(val.s, base);
2668 else
2669 val.u = div_u64(val.u, base);
2670 --next;
2671 }
2672 }
2673
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002674 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002676 if (is_sign)
2677 *va_arg(args, signed char *) = val.s;
2678 else
2679 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 break;
2681 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002682 if (is_sign)
2683 *va_arg(args, short *) = val.s;
2684 else
2685 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 break;
2687 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002688 if (is_sign)
2689 *va_arg(args, long *) = val.s;
2690 else
2691 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 break;
2693 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002694 if (is_sign)
2695 *va_arg(args, long long *) = val.s;
2696 else
2697 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 break;
2699 case 'Z':
2700 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002701 *va_arg(args, size_t *) = val.u;
2702 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 default:
Jan Beulich53809752012-12-17 16:01:31 -08002704 if (is_sign)
2705 *va_arg(args, int *) = val.s;
2706 else
2707 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 break;
2709 }
2710 num++;
2711
2712 if (!next)
2713 break;
2714 str = next;
2715 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return num;
2718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719EXPORT_SYMBOL(vsscanf);
2720
2721/**
2722 * sscanf - Unformat a buffer into a list of arguments
2723 * @buf: input buffer
2724 * @fmt: formatting of buffer
2725 * @...: resulting arguments
2726 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002727int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 va_list args;
2730 int i;
2731
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002732 va_start(args, fmt);
2733 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return i;
2737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738EXPORT_SYMBOL(sscanf);