blob: 8dc5cf85cef474e703cb95f624f8070efa906465 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070020#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070021#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050022#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ctype.h>
26#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070027#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080028#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070029#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110030#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040031#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080032#include <linux/cred.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070033#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020034#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000035#include <net/addrconf.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040036#ifdef CONFIG_BLOCK
37#include <linux/blkdev.h>
38#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070040#include "../mm/internal.h" /* For the trace_print_flags arrays */
41
Tim Schmielau4e57b682005-10-30 15:03:48 -080042#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050043#include <asm/sections.h> /* for dereference_function_descriptor() */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070044#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Andy Shevchenko71dca952014-10-13 15:55:18 -070046#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070047#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * simple_strtoull - convert a string to an unsigned long long
51 * @cp: The start of the string
52 * @endp: A pointer to the end of the parsed string will be placed here
53 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080054 *
55 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
Harvey Harrison22d27052008-10-16 13:40:35 -070057unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070059 unsigned long long result;
60 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070062 cp = _parse_integer_fixup_radix(cp, &base);
63 rv = _parse_integer(cp, base, &result);
64 /* FIXME */
65 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (endp)
68 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return result;
71}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072EXPORT_SYMBOL(simple_strtoull);
73
74/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080075 * simple_strtoul - convert a string to an unsigned long
76 * @cp: The start of the string
77 * @endp: A pointer to the end of the parsed string will be placed here
78 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080079 *
80 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080081 */
82unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
83{
84 return simple_strtoull(cp, endp, base);
85}
86EXPORT_SYMBOL(simple_strtoul);
87
88/**
89 * simple_strtol - convert a string to a signed long
90 * @cp: The start of the string
91 * @endp: A pointer to the end of the parsed string will be placed here
92 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080093 *
94 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080095 */
96long simple_strtol(const char *cp, char **endp, unsigned int base)
97{
98 if (*cp == '-')
99 return -simple_strtoul(cp + 1, endp, base);
100
101 return simple_strtoul(cp, endp, base);
102}
103EXPORT_SYMBOL(simple_strtol);
104
105/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * simple_strtoll - convert a string to a signed long long
107 * @cp: The start of the string
108 * @endp: A pointer to the end of the parsed string will be placed here
109 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800110 *
111 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700113long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800115 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700116 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800117
Harvey Harrison22d27052008-10-16 13:40:35 -0700118 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400120EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Joe Perchescf3b4292010-05-24 14:33:16 -0700122static noinline_for_stack
123int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800125 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800127 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800129 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return i;
132}
133
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700134/*
135 * Decimal conversion is by far the most typical, and is used for
136 * /proc and /sys data. This directly impacts e.g. top performance
137 * with many processes running. We optimize it for speed by emitting
138 * two characters at a time, using a 200 byte lookup table. This
139 * roughly halves the number of multiplications compared to computing
140 * the digits one at a time. Implementation strongly inspired by the
141 * previous version, which in turn used ideas described at
142 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
143 * from the author, Douglas W. Jones).
144 *
145 * It turns out there is precisely one 26 bit fixed-point
146 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
147 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
148 * range happens to be somewhat larger (x <= 1073741898), but that's
149 * irrelevant for our purpose.
150 *
151 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
152 * need a 32x32->64 bit multiply, so we simply use the same constant.
153 *
154 * For dividing a number in the range [100, 10^4-1] by 100, there are
155 * several options. The simplest is (x * 0x147b) >> 19, which is valid
156 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700157 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700158
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700159static const u16 decpair[100] = {
160#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
161 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
162 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
163 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
164 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
165 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
166 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
167 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
168 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
169 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
170 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
171#undef _
172};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700173
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700174/*
175 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700176 * immediately jump to out_r where two 0s would be written but only
177 * one of them accounted for in buf. This is needed by ip4_string
178 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700179*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700180static noinline_for_stack
181char *put_dec_trunc8(char *buf, unsigned r)
182{
183 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700184
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700185 /* 1 <= r < 10^8 */
186 if (r < 100)
187 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700188
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700189 /* 100 <= r < 10^8 */
190 q = (r * (u64)0x28f5c29) >> 32;
191 *((u16 *)buf) = decpair[r - 100*q];
192 buf += 2;
193
194 /* 1 <= q < 10^6 */
195 if (q < 100)
196 goto out_q;
197
198 /* 100 <= q < 10^6 */
199 r = (q * (u64)0x28f5c29) >> 32;
200 *((u16 *)buf) = decpair[q - 100*r];
201 buf += 2;
202
203 /* 1 <= r < 10^4 */
204 if (r < 100)
205 goto out_r;
206
207 /* 100 <= r < 10^4 */
208 q = (r * 0x147b) >> 19;
209 *((u16 *)buf) = decpair[r - 100*q];
210 buf += 2;
211out_q:
212 /* 1 <= q < 100 */
213 r = q;
214out_r:
215 /* 1 <= r < 100 */
216 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700217 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700218 return buf;
219}
220
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700221#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
222static noinline_for_stack
223char *put_dec_full8(char *buf, unsigned r)
224{
225 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700226
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700227 /* 0 <= r < 10^8 */
228 q = (r * (u64)0x28f5c29) >> 32;
229 *((u16 *)buf) = decpair[r - 100*q];
230 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700231
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700232 /* 0 <= q < 10^6 */
233 r = (q * (u64)0x28f5c29) >> 32;
234 *((u16 *)buf) = decpair[q - 100*r];
235 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700236
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700237 /* 0 <= r < 10^4 */
238 q = (r * 0x147b) >> 19;
239 *((u16 *)buf) = decpair[r - 100*q];
240 buf += 2;
241
242 /* 0 <= q < 100 */
243 *((u16 *)buf) = decpair[q];
244 buf += 2;
245 return buf;
246}
247
248static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700249char *put_dec(char *buf, unsigned long long n)
250{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700251 if (n >= 100*1000*1000)
252 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
253 /* 1 <= n <= 1.6e11 */
254 if (n >= 100*1000*1000)
255 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
256 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700257 return put_dec_trunc8(buf, n);
258}
259
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700260#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700261
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700262static void
263put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700264{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700265 unsigned q;
266
267 /* 0 <= r < 10^4 */
268 q = (r * 0x147b) >> 19;
269 *((u16 *)buf) = decpair[r - 100*q];
270 buf += 2;
271 /* 0 <= q < 100 */
272 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700273}
274
275/*
276 * Call put_dec_full4 on x % 10000, return x / 10000.
277 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
278 * holds for all x < 1,128,869,999. The largest value this
279 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700280 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700281 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700282static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700283unsigned put_dec_helper4(char *buf, unsigned x)
284{
285 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
286
287 put_dec_full4(buf, x - q * 10000);
288 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700289}
290
291/* Based on code by Douglas W. Jones found at
292 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
293 * (with permission from the author).
294 * Performs no 64-bit division and hence should be fast on 32-bit machines.
295 */
296static
297char *put_dec(char *buf, unsigned long long n)
298{
299 uint32_t d3, d2, d1, q, h;
300
301 if (n < 100*1000*1000)
302 return put_dec_trunc8(buf, n);
303
304 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
305 h = (n >> 32);
306 d2 = (h ) & 0xffff;
307 d3 = (h >> 16); /* implicit "& 0xffff" */
308
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700309 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
310 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700311 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700312 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700313
George Spelvin23591722012-10-04 17:12:29 -0700314 q += 7671 * d3 + 9496 * d2 + 6 * d1;
315 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700316
George Spelvin23591722012-10-04 17:12:29 -0700317 q += 4749 * d3 + 42 * d2;
318 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700319
George Spelvin23591722012-10-04 17:12:29 -0700320 q += 281 * d3;
321 buf += 12;
322 if (q)
323 buf = put_dec_trunc8(buf, q);
324 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700325 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800326
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700327 return buf;
328}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700329
330#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700331
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700332/*
333 * Convert passed number to decimal string.
334 * Returns the length of string. On buffer overflow, returns 0.
335 *
336 * If speed is not important, use snprintf(). It's easy to read the code.
337 */
338int num_to_str(char *buf, int size, unsigned long long num)
339{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700340 /* put_dec requires 2-byte alignment of the buffer. */
341 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700342 int idx, len;
343
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700344 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
345 if (num <= 9) {
346 tmp[0] = '0' + num;
347 len = 1;
348 } else {
349 len = put_dec(tmp, num) - tmp;
350 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700351
352 if (len > size)
353 return 0;
354 for (idx = 0; idx < len; ++idx)
355 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700356 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700357}
358
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700359#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700360#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define PLUS 4 /* show plus */
362#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700363#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700364#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
365#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100367enum format_type {
368 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100369 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100370 FORMAT_TYPE_PRECISION,
371 FORMAT_TYPE_CHAR,
372 FORMAT_TYPE_STR,
373 FORMAT_TYPE_PTR,
374 FORMAT_TYPE_PERCENT_CHAR,
375 FORMAT_TYPE_INVALID,
376 FORMAT_TYPE_LONG_LONG,
377 FORMAT_TYPE_ULONG,
378 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800379 FORMAT_TYPE_UBYTE,
380 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100381 FORMAT_TYPE_USHORT,
382 FORMAT_TYPE_SHORT,
383 FORMAT_TYPE_UINT,
384 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100385 FORMAT_TYPE_SIZE_T,
386 FORMAT_TYPE_PTRDIFF
387};
388
389struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800390 unsigned int type:8; /* format_type enum */
391 signed int field_width:24; /* width of output field */
392 unsigned int flags:8; /* flags to number() */
393 unsigned int base:8; /* number base, 8, 10 or 16 only */
394 signed int precision:16; /* # of digits/chars */
395} __packed;
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800396#define FIELD_WIDTH_MAX ((1 << 23) - 1)
397#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100398
Joe Perchescf3b4292010-05-24 14:33:16 -0700399static noinline_for_stack
400char *number(char *buf, char *end, unsigned long long num,
401 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700403 /* put_dec requires 2-byte alignment of the buffer. */
404 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100405 char sign;
406 char locase;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100407 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700409 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800410 int field_width = spec.field_width;
411 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800413 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
414
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100415 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
416 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100417 locase = (spec.flags & SMALL);
418 if (spec.flags & LEFT)
419 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 sign = 0;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100421 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800422 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800424 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800425 field_width--;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100426 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800428 field_width--;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100429 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800431 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700434 if (need_pfx) {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100435 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800436 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700437 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800438 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700440
441 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700443 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700444 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100445 else if (spec.base != 10) { /* 8 or 16 */
446 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800448
449 if (spec.base == 16)
450 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700451 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700452 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700453 num >>= shift;
454 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700455 } else { /* base 10 */
456 i = put_dec(tmp, num) - tmp;
457 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700458
459 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800460 if (i > precision)
461 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700462 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800463 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700464 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800465 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700466 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *buf = ' ';
468 ++buf;
469 }
470 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700471 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700473 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *buf = sign;
475 ++buf;
476 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700477 /* "0x" / "0" prefix */
478 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700479 if (spec.base == 16 || !is_zero) {
480 if (buf < end)
481 *buf = '0';
482 ++buf;
483 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100484 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700485 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100486 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 ++buf;
488 }
489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* zero or space padding */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100491 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700492 char c = ' ' + (spec.flags & ZEROPAD);
493 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800494 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700495 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 *buf = c;
497 ++buf;
498 }
499 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700500 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800501 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700502 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *buf = '0';
504 ++buf;
505 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700506 /* actual digits of result */
507 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700508 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buf = tmp[i];
510 ++buf;
511 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700512 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800513 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700514 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *buf = ' ';
516 ++buf;
517 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return buf;
520}
521
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800522static noinline_for_stack
523char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
524{
525 struct printf_spec spec;
526
527 spec.type = FORMAT_TYPE_PTR;
528 spec.field_width = 2 + 2 * size; /* 0x + hex */
529 spec.flags = SPECIAL | SMALL | ZEROPAD;
530 spec.base = 16;
531 spec.precision = -1;
532
533 return number(buf, end, num, spec);
534}
535
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800536static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400537{
538 size_t size;
539 if (buf >= end) /* nowhere to put anything */
540 return;
541 size = end - buf;
542 if (size <= spaces) {
543 memset(buf, ' ', size);
544 return;
545 }
546 if (len) {
547 if (len > size - spaces)
548 len = size - spaces;
549 memmove(buf + spaces, buf, len);
550 }
551 memset(buf, ' ', spaces);
552}
553
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800554/*
555 * Handle field width padding for a string.
556 * @buf: current buffer position
557 * @n: length of string
558 * @end: end of output buffer
559 * @spec: for field width and flags
560 * Returns: new buffer position after padding.
561 */
562static noinline_for_stack
563char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
564{
565 unsigned spaces;
566
567 if (likely(n >= spec.field_width))
568 return buf;
569 /* we want to pad the sucker */
570 spaces = spec.field_width - n;
571 if (!(spec.flags & LEFT)) {
572 move_right(buf - n, end, n, spaces);
573 return buf + spaces;
574 }
575 while (spaces--) {
576 if (buf < end)
577 *buf = ' ';
578 ++buf;
579 }
580 return buf;
581}
582
Al Viro4b6ccca2013-09-03 12:00:44 -0400583static noinline_for_stack
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800584char *string(char *buf, char *end, const char *s, struct printf_spec spec)
585{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800586 int len = 0;
587 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800588
589 if ((unsigned long)s < PAGE_SIZE)
590 s = "(null)";
591
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800592 while (lim--) {
593 char c = *s++;
594 if (!c)
595 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800596 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800597 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800598 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800599 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800600 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800601 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800602}
603
604static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400605char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
606 const char *fmt)
607{
608 const char *array[4], *s;
609 const struct dentry *p;
610 int depth;
611 int i, n;
612
613 switch (fmt[1]) {
614 case '2': case '3': case '4':
615 depth = fmt[1] - '0';
616 break;
617 default:
618 depth = 1;
619 }
620
621 rcu_read_lock();
622 for (i = 0; i < depth; i++, d = p) {
Mark Rutland6aa7de02017-10-23 14:07:29 -0700623 p = READ_ONCE(d->d_parent);
624 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400625 if (p == d) {
626 if (i)
627 array[i] = "";
628 i++;
629 break;
630 }
631 }
632 s = array[--i];
633 for (n = 0; n != spec.precision; n++, buf++) {
634 char c = *s++;
635 if (!c) {
636 if (!i)
637 break;
638 c = '/';
639 s = array[--i];
640 }
641 if (buf < end)
642 *buf = c;
643 }
644 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800645 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400646}
647
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400648#ifdef CONFIG_BLOCK
649static noinline_for_stack
650char *bdev_name(char *buf, char *end, struct block_device *bdev,
651 struct printf_spec spec, const char *fmt)
652{
653 struct gendisk *hd = bdev->bd_disk;
654
655 buf = string(buf, end, hd->disk_name, spec);
656 if (bdev->bd_part->partno) {
657 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
658 if (buf < end)
659 *buf = 'p';
660 buf++;
661 }
662 buf = number(buf, end, bdev->bd_part->partno, spec);
663 }
664 return buf;
665}
666#endif
667
Joe Perchescf3b4292010-05-24 14:33:16 -0700668static noinline_for_stack
669char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800670 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700671{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800672 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700673#ifdef CONFIG_KALLSYMS
674 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800675#endif
676
677 if (fmt[1] == 'R')
678 ptr = __builtin_extract_return_addr(ptr);
679 value = (unsigned long)ptr;
680
681#ifdef CONFIG_KALLSYMS
682 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900683 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800684 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200685 sprint_symbol(sym, value);
686 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700687 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800688
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100689 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700690#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800691 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700692#endif
693}
694
Joe Perchescf3b4292010-05-24 14:33:16 -0700695static noinline_for_stack
696char *resource_string(char *buf, char *end, struct resource *res,
697 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100698{
699#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600700#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100701#endif
702
703#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600704#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100705#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700706 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100707 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700708 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100709 .precision = -1,
710 .flags = SPECIAL | SMALL | ZEROPAD,
711 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700712 static const struct printf_spec mem_spec = {
713 .base = 16,
714 .field_width = MEM_RSRC_PRINTK_SIZE,
715 .precision = -1,
716 .flags = SPECIAL | SMALL | ZEROPAD,
717 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700718 static const struct printf_spec bus_spec = {
719 .base = 16,
720 .field_width = 2,
721 .precision = -1,
722 .flags = SMALL | ZEROPAD,
723 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700724 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600725 .base = 10,
726 .precision = -1,
727 .flags = 0,
728 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700729 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600730 .field_width = -1,
731 .precision = 10,
732 .flags = LEFT,
733 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700734 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600735 .base = 16,
736 .precision = -1,
737 .flags = SPECIAL | SMALL,
738 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600739
740 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
741 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
742#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
743#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700744#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600745#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
746 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
747 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
748
Linus Torvalds332d2e72008-10-20 15:07:34 +1100749 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600750 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700751 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100752
753 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700754 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600755 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700756 specp = &io_spec;
757 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600758 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700759 specp = &mem_spec;
760 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600761 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700762 specp = &dec_spec;
763 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600764 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700765 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700766 } else if (res->flags & IORESOURCE_BUS) {
767 p = string(p, pend, "bus ", str_spec);
768 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700769 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600770 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700771 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600772 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600773 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700774 if (decode && res->flags & IORESOURCE_UNSET) {
775 p = string(p, pend, "size ", str_spec);
776 p = number(p, pend, resource_size(res), *specp);
777 } else {
778 p = number(p, pend, res->start, *specp);
779 if (res->start != res->end) {
780 *p++ = '-';
781 p = number(p, pend, res->end, *specp);
782 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600783 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600784 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600785 if (res->flags & IORESOURCE_MEM_64)
786 p = string(p, pend, " 64bit", str_spec);
787 if (res->flags & IORESOURCE_PREFETCH)
788 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700789 if (res->flags & IORESOURCE_WINDOW)
790 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600791 if (res->flags & IORESOURCE_DISABLED)
792 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600793 } else {
794 p = string(p, pend, " flags ", str_spec);
795 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600796 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100797 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600798 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100799
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100800 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100801}
802
Joe Perchescf3b4292010-05-24 14:33:16 -0700803static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700804char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
805 const char *fmt)
806{
Steven Rostedt360603a2013-05-28 15:47:39 -0400807 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -0700808 negative value, fallback to the default */
809 char separator;
810
811 if (spec.field_width == 0)
812 /* nothing to print */
813 return buf;
814
815 if (ZERO_OR_NULL_PTR(addr))
816 /* NULL pointer */
817 return string(buf, end, NULL, spec);
818
819 switch (fmt[1]) {
820 case 'C':
821 separator = ':';
822 break;
823 case 'D':
824 separator = '-';
825 break;
826 case 'N':
827 separator = 0;
828 break;
829 default:
830 separator = ' ';
831 break;
832 }
833
834 if (spec.field_width > 0)
835 len = min_t(int, spec.field_width, 64);
836
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700837 for (i = 0; i < len; ++i) {
838 if (buf < end)
839 *buf = hex_asc_hi(addr[i]);
840 ++buf;
841 if (buf < end)
842 *buf = hex_asc_lo(addr[i]);
843 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -0700844
Rasmus Villemoes9c98f232015-04-15 16:17:23 -0700845 if (separator && i != len - 1) {
846 if (buf < end)
847 *buf = separator;
848 ++buf;
849 }
Andy Shevchenko31550a12012-07-30 14:40:27 -0700850 }
851
852 return buf;
853}
854
855static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -0800856char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
857 struct printf_spec spec, const char *fmt)
858{
859 const int CHUNKSZ = 32;
860 int nr_bits = max_t(int, spec.field_width, 0);
861 int i, chunksz;
862 bool first = true;
863
864 /* reused to print numbers */
865 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
866
867 chunksz = nr_bits & (CHUNKSZ - 1);
868 if (chunksz == 0)
869 chunksz = CHUNKSZ;
870
871 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
872 for (; i >= 0; i -= CHUNKSZ) {
873 u32 chunkmask, val;
874 int word, bit;
875
876 chunkmask = ((1ULL << chunksz) - 1);
877 word = i / BITS_PER_LONG;
878 bit = i % BITS_PER_LONG;
879 val = (bitmap[word] >> bit) & chunkmask;
880
881 if (!first) {
882 if (buf < end)
883 *buf = ',';
884 buf++;
885 }
886 first = false;
887
888 spec.field_width = DIV_ROUND_UP(chunksz, 4);
889 buf = number(buf, end, val, spec);
890
891 chunksz = CHUNKSZ;
892 }
893 return buf;
894}
895
896static noinline_for_stack
897char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
898 struct printf_spec spec, const char *fmt)
899{
900 int nr_bits = max_t(int, spec.field_width, 0);
901 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
902 int cur, rbot, rtop;
903 bool first = true;
904
905 /* reused to print numbers */
906 spec = (struct printf_spec){ .base = 10 };
907
908 rbot = cur = find_first_bit(bitmap, nr_bits);
909 while (cur < nr_bits) {
910 rtop = cur;
911 cur = find_next_bit(bitmap, nr_bits, cur + 1);
912 if (cur < nr_bits && cur <= rtop + 1)
913 continue;
914
915 if (!first) {
916 if (buf < end)
917 *buf = ',';
918 buf++;
919 }
920 first = false;
921
922 buf = number(buf, end, rbot, spec);
923 if (rbot < rtop) {
924 if (buf < end)
925 *buf = '-';
926 buf++;
927
928 buf = number(buf, end, rtop, spec);
929 }
930
931 rbot = cur;
932 }
933 return buf;
934}
935
936static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700937char *mac_address_string(char *buf, char *end, u8 *addr,
938 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700939{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000940 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700941 char *p = mac_addr;
942 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000943 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700944 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000945
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700946 switch (fmt[1]) {
947 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000948 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700949 break;
950
951 case 'R':
952 reversed = true;
953 /* fall through */
954
955 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000956 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700957 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000958 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700959
960 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700961 if (reversed)
962 p = hex_byte_pack(p, addr[5 - i]);
963 else
964 p = hex_byte_pack(p, addr[i]);
965
Joe Perches8a27f7c2009-08-17 12:29:44 +0000966 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000967 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700968 }
969 *p = '\0';
970
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +0100971 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700972}
973
Joe Perchescf3b4292010-05-24 14:33:16 -0700974static noinline_for_stack
975char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700976{
Harvey Harrison689afa72008-10-28 16:04:44 -0700977 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800978 bool leading_zeros = (fmt[0] == 'i');
979 int index;
980 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700981
Joe Perches0159f242010-01-13 20:23:30 -0800982 switch (fmt[2]) {
983 case 'h':
984#ifdef __BIG_ENDIAN
985 index = 0;
986 step = 1;
987#else
988 index = 3;
989 step = -1;
990#endif
991 break;
992 case 'l':
993 index = 3;
994 step = -1;
995 break;
996 case 'n':
997 case 'b':
998 default:
999 index = 0;
1000 step = 1;
1001 break;
1002 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001003 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001004 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001005 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001006 if (leading_zeros) {
1007 if (digits < 3)
1008 *p++ = '0';
1009 if (digits < 2)
1010 *p++ = '0';
1011 }
1012 /* reverse the digits in the quad */
1013 while (digits--)
1014 *p++ = temp[digits];
1015 if (i < 3)
1016 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001017 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001018 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001019 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001020
Joe Perches8a27f7c2009-08-17 12:29:44 +00001021 return p;
1022}
1023
Joe Perchescf3b4292010-05-24 14:33:16 -07001024static noinline_for_stack
1025char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001026{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001027 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001028 unsigned char zerolength[8];
1029 int longest = 1;
1030 int colonpos = -1;
1031 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001032 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001033 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001034 bool useIPv4;
1035 struct in6_addr in6;
1036
1037 memcpy(&in6, addr, sizeof(struct in6_addr));
1038
1039 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001040
1041 memset(zerolength, 0, sizeof(zerolength));
1042
1043 if (useIPv4)
1044 range = 6;
1045 else
1046 range = 8;
1047
1048 /* find position of longest 0 run */
1049 for (i = 0; i < range; i++) {
1050 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001051 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001052 break;
1053 zerolength[i]++;
1054 }
1055 }
1056 for (i = 0; i < range; i++) {
1057 if (zerolength[i] > longest) {
1058 longest = zerolength[i];
1059 colonpos = i;
1060 }
1061 }
Joe Perches29cf5192011-06-09 11:23:37 -07001062 if (longest == 1) /* don't compress a single 0 */
1063 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001064
1065 /* emit address */
1066 for (i = 0; i < range; i++) {
1067 if (i == colonpos) {
1068 if (needcolon || i == 0)
1069 *p++ = ':';
1070 *p++ = ':';
1071 needcolon = false;
1072 i += longest - 1;
1073 continue;
1074 }
1075 if (needcolon) {
1076 *p++ = ':';
1077 needcolon = false;
1078 }
1079 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001080 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001081 hi = word >> 8;
1082 lo = word & 0xff;
1083 if (hi) {
1084 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001085 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001086 else
1087 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001088 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001089 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001090 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001091 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001092 else
1093 *p++ = hex_asc_lo(lo);
1094 needcolon = true;
1095 }
1096
1097 if (useIPv4) {
1098 if (needcolon)
1099 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001100 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001101 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001102 *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_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001109{
1110 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001111
Harvey Harrison689afa72008-10-28 16:04:44 -07001112 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001113 p = hex_byte_pack(p, *addr++);
1114 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001115 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001116 *p++ = ':';
1117 }
1118 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001119
Joe Perches8a27f7c2009-08-17 12:29:44 +00001120 return p;
1121}
1122
Joe Perchescf3b4292010-05-24 14:33:16 -07001123static noinline_for_stack
1124char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1125 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001126{
1127 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1128
1129 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001130 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001131 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001132 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001133
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001134 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001135}
1136
Joe Perchescf3b4292010-05-24 14:33:16 -07001137static noinline_for_stack
1138char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1139 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001140{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001141 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001142
Joe Perches0159f242010-01-13 20:23:30 -08001143 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001144
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001145 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001146}
1147
Joe Perchescf3b4292010-05-24 14:33:16 -07001148static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001149char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1150 struct printf_spec spec, const char *fmt)
1151{
1152 bool have_p = false, have_s = false, have_f = false, have_c = false;
1153 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1154 sizeof(":12345") + sizeof("/123456789") +
1155 sizeof("%1234567890")];
1156 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1157 const u8 *addr = (const u8 *) &sa->sin6_addr;
1158 char fmt6[2] = { fmt[0], '6' };
1159 u8 off = 0;
1160
1161 fmt++;
1162 while (isalpha(*++fmt)) {
1163 switch (*fmt) {
1164 case 'p':
1165 have_p = true;
1166 break;
1167 case 'f':
1168 have_f = true;
1169 break;
1170 case 's':
1171 have_s = true;
1172 break;
1173 case 'c':
1174 have_c = true;
1175 break;
1176 }
1177 }
1178
1179 if (have_p || have_s || have_f) {
1180 *p = '[';
1181 off = 1;
1182 }
1183
1184 if (fmt6[0] == 'I' && have_c)
1185 p = ip6_compressed_string(ip6_addr + off, addr);
1186 else
1187 p = ip6_string(ip6_addr + off, addr, fmt6);
1188
1189 if (have_p || have_s || have_f)
1190 *p++ = ']';
1191
1192 if (have_p) {
1193 *p++ = ':';
1194 p = number(p, pend, ntohs(sa->sin6_port), spec);
1195 }
1196 if (have_f) {
1197 *p++ = '/';
1198 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1199 IPV6_FLOWINFO_MASK), spec);
1200 }
1201 if (have_s) {
1202 *p++ = '%';
1203 p = number(p, pend, sa->sin6_scope_id, spec);
1204 }
1205 *p = '\0';
1206
1207 return string(buf, end, ip6_addr, spec);
1208}
1209
1210static noinline_for_stack
1211char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1212 struct printf_spec spec, const char *fmt)
1213{
1214 bool have_p = false;
1215 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1216 char *pend = ip4_addr + sizeof(ip4_addr);
1217 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1218 char fmt4[3] = { fmt[0], '4', 0 };
1219
1220 fmt++;
1221 while (isalpha(*++fmt)) {
1222 switch (*fmt) {
1223 case 'p':
1224 have_p = true;
1225 break;
1226 case 'h':
1227 case 'l':
1228 case 'n':
1229 case 'b':
1230 fmt4[2] = *fmt;
1231 break;
1232 }
1233 }
1234
1235 p = ip4_string(ip4_addr, addr, fmt4);
1236 if (have_p) {
1237 *p++ = ':';
1238 p = number(p, pend, ntohs(sa->sin_port), spec);
1239 }
1240 *p = '\0';
1241
1242 return string(buf, end, ip4_addr, spec);
1243}
1244
1245static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001246char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1247 const char *fmt)
1248{
1249 bool found = true;
1250 int count = 1;
1251 unsigned int flags = 0;
1252 int len;
1253
1254 if (spec.field_width == 0)
1255 return buf; /* nothing to print */
1256
1257 if (ZERO_OR_NULL_PTR(addr))
1258 return string(buf, end, NULL, spec); /* NULL pointer */
1259
1260
1261 do {
1262 switch (fmt[count++]) {
1263 case 'a':
1264 flags |= ESCAPE_ANY;
1265 break;
1266 case 'c':
1267 flags |= ESCAPE_SPECIAL;
1268 break;
1269 case 'h':
1270 flags |= ESCAPE_HEX;
1271 break;
1272 case 'n':
1273 flags |= ESCAPE_NULL;
1274 break;
1275 case 'o':
1276 flags |= ESCAPE_OCTAL;
1277 break;
1278 case 'p':
1279 flags |= ESCAPE_NP;
1280 break;
1281 case 's':
1282 flags |= ESCAPE_SPACE;
1283 break;
1284 default:
1285 found = false;
1286 break;
1287 }
1288 } while (found);
1289
1290 if (!flags)
1291 flags = ESCAPE_ANY_NP;
1292
1293 len = spec.field_width < 0 ? 1 : spec.field_width;
1294
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001295 /*
1296 * string_escape_mem() writes as many characters as it can to
1297 * the given buffer, and returns the total size of the output
1298 * had the buffer been big enough.
1299 */
1300 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001301
1302 return buf;
1303}
1304
1305static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001306char *uuid_string(char *buf, char *end, const u8 *addr,
1307 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001308{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001309 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001310 char *p = uuid;
1311 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001312 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001313 bool uc = false;
1314
1315 switch (*(++fmt)) {
1316 case 'L':
1317 uc = true; /* fall-through */
1318 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001319 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001320 break;
1321 case 'B':
1322 uc = true;
1323 break;
1324 }
1325
1326 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001327 if (uc)
1328 p = hex_byte_pack_upper(p, addr[index[i]]);
1329 else
1330 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001331 switch (i) {
1332 case 3:
1333 case 5:
1334 case 7:
1335 case 9:
1336 *p++ = '-';
1337 break;
1338 }
1339 }
1340
1341 *p = 0;
1342
Joe Perches9ac6e442009-12-14 18:01:09 -08001343 return string(buf, end, uuid, spec);
1344}
1345
Tobin C. Harding57e73442017-11-23 10:56:39 +11001346int kptr_restrict __read_mostly;
1347
1348static noinline_for_stack
1349char *restricted_pointer(char *buf, char *end, const void *ptr,
1350 struct printf_spec spec)
1351{
1352 spec.base = 16;
1353 spec.flags |= SMALL;
1354 if (spec.field_width == -1) {
1355 spec.field_width = 2 * sizeof(ptr);
1356 spec.flags |= ZEROPAD;
1357 }
1358
1359 switch (kptr_restrict) {
1360 case 0:
1361 /* Always print %pK values */
1362 break;
1363 case 1: {
1364 const struct cred *cred;
1365
1366 /*
1367 * kptr_restrict==1 cannot be used in IRQ context
1368 * because its test for CAP_SYSLOG would be meaningless.
1369 */
1370 if (in_irq() || in_serving_softirq() || in_nmi())
1371 return string(buf, end, "pK-error", spec);
1372
1373 /*
1374 * Only print the real pointer value if the current
1375 * process has CAP_SYSLOG and is running with the
1376 * same credentials it started with. This is because
1377 * access to files is checked at open() time, but %pK
1378 * checks permission at read() time. We don't want to
1379 * leak pointer values if a binary opens a file using
1380 * %pK and then elevates privileges before reading it.
1381 */
1382 cred = current_cred();
1383 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1384 !uid_eq(cred->euid, cred->uid) ||
1385 !gid_eq(cred->egid, cred->gid))
1386 ptr = NULL;
1387 break;
1388 }
1389 case 2:
1390 default:
1391 /* Always print 0's for %pK */
1392 ptr = NULL;
1393 break;
1394 }
1395
1396 return number(buf, end, (unsigned long)ptr, spec);
1397}
1398
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001399static noinline_for_stack
1400char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001401{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001402 unsigned long long num;
1403 int size;
1404
1405 switch (fmt[1]) {
1406 case 'F':
1407 num = *(const netdev_features_t *)addr;
1408 size = sizeof(netdev_features_t);
1409 break;
1410 default:
1411 num = (unsigned long)addr;
1412 size = sizeof(unsigned long);
1413 break;
1414 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001415
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001416 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001417}
1418
Joe Perchesaaf07622014-01-23 15:54:17 -08001419static noinline_for_stack
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001420char *address_val(char *buf, char *end, const void *addr, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001421{
1422 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001423 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001424
1425 switch (fmt[1]) {
1426 case 'd':
1427 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001428 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001429 break;
1430 case 'p':
1431 default:
1432 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001433 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001434 break;
1435 }
1436
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001437 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001438}
1439
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001440static noinline_for_stack
1441char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1442 const char *fmt)
1443{
1444 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1445 return string(buf, end, NULL, spec);
1446
1447 switch (fmt[1]) {
1448 case 'r':
1449 return number(buf, end, clk_get_rate(clk), spec);
1450
1451 case 'n':
1452 default:
1453#ifdef CONFIG_COMMON_CLK
1454 return string(buf, end, __clk_get_name(clk), spec);
1455#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001456 return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001457#endif
1458 }
1459}
1460
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001461static
1462char *format_flags(char *buf, char *end, unsigned long flags,
1463 const struct trace_print_flags *names)
1464{
1465 unsigned long mask;
1466 const struct printf_spec strspec = {
1467 .field_width = -1,
1468 .precision = -1,
1469 };
1470 const struct printf_spec numspec = {
1471 .flags = SPECIAL|SMALL,
1472 .field_width = -1,
1473 .precision = -1,
1474 .base = 16,
1475 };
1476
1477 for ( ; flags && names->name; names++) {
1478 mask = names->mask;
1479 if ((flags & mask) != mask)
1480 continue;
1481
1482 buf = string(buf, end, names->name, strspec);
1483
1484 flags &= ~mask;
1485 if (flags) {
1486 if (buf < end)
1487 *buf = '|';
1488 buf++;
1489 }
1490 }
1491
1492 if (flags)
1493 buf = number(buf, end, flags, numspec);
1494
1495 return buf;
1496}
1497
1498static noinline_for_stack
1499char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1500{
1501 unsigned long flags;
1502 const struct trace_print_flags *names;
1503
1504 switch (fmt[1]) {
1505 case 'p':
1506 flags = *(unsigned long *)flags_ptr;
1507 /* Remove zone id */
1508 flags &= (1UL << NR_PAGEFLAGS) - 1;
1509 names = pageflag_names;
1510 break;
1511 case 'v':
1512 flags = *(unsigned long *)flags_ptr;
1513 names = vmaflag_names;
1514 break;
1515 case 'g':
1516 flags = *(gfp_t *)flags_ptr;
1517 names = gfpflag_names;
1518 break;
1519 default:
1520 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1521 return buf;
1522 }
1523
1524 return format_flags(buf, end, flags, names);
1525}
1526
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001527static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1528{
1529 for ( ; np && depth; depth--)
1530 np = np->parent;
1531
1532 return kbasename(np->full_name);
1533}
1534
1535static noinline_for_stack
1536char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1537{
1538 int depth;
1539 const struct device_node *parent = np->parent;
1540 static const struct printf_spec strspec = {
1541 .field_width = -1,
1542 .precision = -1,
1543 };
1544
1545 /* special case for root node */
1546 if (!parent)
1547 return string(buf, end, "/", strspec);
1548
1549 for (depth = 0; parent->parent; depth++)
1550 parent = parent->parent;
1551
1552 for ( ; depth >= 0; depth--) {
1553 buf = string(buf, end, "/", strspec);
1554 buf = string(buf, end, device_node_name_for_depth(np, depth),
1555 strspec);
1556 }
1557 return buf;
1558}
1559
1560static noinline_for_stack
1561char *device_node_string(char *buf, char *end, struct device_node *dn,
1562 struct printf_spec spec, const char *fmt)
1563{
1564 char tbuf[sizeof("xxxx") + 1];
1565 const char *p;
1566 int ret;
1567 char *buf_start = buf;
1568 struct property *prop;
1569 bool has_mult, pass;
1570 static const struct printf_spec num_spec = {
1571 .flags = SMALL,
1572 .field_width = -1,
1573 .precision = -1,
1574 .base = 10,
1575 };
1576
1577 struct printf_spec str_spec = spec;
1578 str_spec.field_width = -1;
1579
1580 if (!IS_ENABLED(CONFIG_OF))
1581 return string(buf, end, "(!OF)", spec);
1582
1583 if ((unsigned long)dn < PAGE_SIZE)
1584 return string(buf, end, "(null)", spec);
1585
1586 /* simple case without anything any more format specifiers */
1587 fmt++;
1588 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1589 fmt = "f";
1590
1591 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1592 if (pass) {
1593 if (buf < end)
1594 *buf = ':';
1595 buf++;
1596 }
1597
1598 switch (*fmt) {
1599 case 'f': /* full_name */
1600 buf = device_node_gen_full_name(dn, buf, end);
1601 break;
1602 case 'n': /* name */
1603 buf = string(buf, end, dn->name, str_spec);
1604 break;
1605 case 'p': /* phandle */
1606 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1607 break;
1608 case 'P': /* path-spec */
1609 p = kbasename(of_node_full_name(dn));
1610 if (!p[1])
1611 p = "/";
1612 buf = string(buf, end, p, str_spec);
1613 break;
1614 case 'F': /* flags */
1615 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1616 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1617 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1618 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1619 tbuf[4] = 0;
1620 buf = string(buf, end, tbuf, str_spec);
1621 break;
1622 case 'c': /* major compatible string */
1623 ret = of_property_read_string(dn, "compatible", &p);
1624 if (!ret)
1625 buf = string(buf, end, p, str_spec);
1626 break;
1627 case 'C': /* full compatible string */
1628 has_mult = false;
1629 of_property_for_each_string(dn, "compatible", prop, p) {
1630 if (has_mult)
1631 buf = string(buf, end, ",", str_spec);
1632 buf = string(buf, end, "\"", str_spec);
1633 buf = string(buf, end, p, str_spec);
1634 buf = string(buf, end, "\"", str_spec);
1635
1636 has_mult = true;
1637 }
1638 break;
1639 default:
1640 break;
1641 }
1642 }
1643
1644 return widen_string(buf, buf - buf_start, end, spec);
1645}
1646
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001647/*
1648 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1649 * by an extra set of alphanumeric characters that are extended format
1650 * specifiers.
1651 *
Joe Perches0b523762017-05-08 15:55:36 -07001652 * Please update scripts/checkpatch.pl when adding/removing conversion
1653 * characters. (Search for "check for vsprintf extension").
1654 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001655 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001656 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001657 * - 'F' For symbolic function descriptor pointers with offset
1658 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001659 * - 'S' For symbolic direct pointers with offset
1660 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -08001661 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001662 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001663 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1664 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001665 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1666 * width which must be explicitly specified either as part of the
1667 * format string '%32b[l]' or through '%*b[l]', [l] selects
1668 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001669 * - 'M' For a 6-byte MAC address, it prints the address in the
1670 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001671 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001672 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001673 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001674 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001675 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1676 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1677 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001678 * [S][pfs]
1679 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1680 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001681 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1682 * IPv6 omits the colons (01020304...0f)
1683 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001684 * [S][pfs]
1685 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1686 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1687 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1688 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001689 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001690 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1691 * of the following flags (see string_escape_mem() for the
1692 * details):
1693 * a - ESCAPE_ANY
1694 * c - ESCAPE_SPECIAL
1695 * h - ESCAPE_HEX
1696 * n - ESCAPE_NULL
1697 * o - ESCAPE_OCTAL
1698 * p - ESCAPE_NP
1699 * s - ESCAPE_SPACE
1700 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001701 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1702 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1703 * Options for %pU are:
1704 * b big endian lower case hex (default)
1705 * B big endian UPPER case hex
1706 * l little endian lower case hex
1707 * L little endian UPPER case hex
1708 * big endian output byte order is:
1709 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1710 * little endian output byte order is:
1711 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001712 * - 'V' For a struct va_format which contains a format string * and va_list *,
1713 * call vsnprintf(->format, *->va_list).
1714 * Implements a "recursive vsnprintf".
1715 * Do not use this feature without some mechanism to verify the
1716 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001717 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001718 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001719 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1720 * a certain separator (' ' by default):
1721 * C colon
1722 * D dash
1723 * N no separator
1724 * The maximum supported length is 64 bytes of the input. Consider
1725 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001726 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1727 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001728 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1729 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001730 * - 'g' For block_device name (gendisk + partition number)
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001731 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1732 * (legacy clock framework) of the clock
1733 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1734 * (legacy clock framework) of the clock
1735 * - 'Cr' For a clock, it prints the current rate of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001736 * - 'G' For flags to be printed as a collection of symbolic strings that would
1737 * construct the specific value. Supported flags given by option:
1738 * p page flags (see struct page) given as pointer to unsigned long
1739 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1740 * v vma flags (VM_*) given as pointer to unsigned long
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001741 * - 'O' For a kobject based struct. Must be one of the following:
1742 * - 'OF[fnpPcCF]' For a device tree object
1743 * Without any optional arguments prints the full_name
1744 * f device node full_name
1745 * n device node name
1746 * p device node phandle
1747 * P device node path spec (name + @unit)
1748 * F device node flags
1749 * c major compatible string
1750 * C full compatible string
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08001751 *
1752 * ** Please update also Documentation/printk-formats.txt when making changes **
Joe Perches9ac6e442009-12-14 18:01:09 -08001753 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001754 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1755 * function pointers are really function descriptors, which contain a
1756 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001757 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001758static noinline_for_stack
1759char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1760 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001761{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001762 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001763
Kees Cook9f36e2c2011-03-22 16:34:22 -07001764 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001765 /*
1766 * Print (null) with the same width as a pointer so it makes
1767 * tabular output look nice.
1768 */
1769 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001770 spec.field_width = default_width;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001771 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001772 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001773
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001774 switch (*fmt) {
1775 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001776 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001777 ptr = dereference_function_descriptor(ptr);
1778 /* Fallthrough */
1779 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001780 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001781 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001782 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001783 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001784 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001785 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001786 case 'h':
1787 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08001788 case 'b':
1789 switch (fmt[1]) {
1790 case 'l':
1791 return bitmap_list_string(buf, end, ptr, spec, fmt);
1792 default:
1793 return bitmap_string(buf, end, ptr, spec, fmt);
1794 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001795 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1796 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001797 /* [mM]F (FDDI) */
1798 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001799 return mac_address_string(buf, end, ptr, spec, fmt);
1800 case 'I': /* Formatted IP supported
1801 * 4: 1.2.3.4
1802 * 6: 0001:0203:...:0708
1803 * 6c: 1::708 or 1::1.2.3.4
1804 */
1805 case 'i': /* Contiguous:
1806 * 4: 001.002.003.004
1807 * 6: 000102...0f
1808 */
1809 switch (fmt[1]) {
1810 case '6':
1811 return ip6_addr_string(buf, end, ptr, spec, fmt);
1812 case '4':
1813 return ip4_addr_string(buf, end, ptr, spec, fmt);
Daniel Borkmann10679642013-06-28 19:49:39 +02001814 case 'S': {
1815 const union {
1816 struct sockaddr raw;
1817 struct sockaddr_in v4;
1818 struct sockaddr_in6 v6;
1819 } *sa = ptr;
1820
1821 switch (sa->raw.sa_family) {
1822 case AF_INET:
1823 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1824 case AF_INET6:
1825 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1826 default:
1827 return string(buf, end, "(invalid address)", spec);
1828 }}
Joe Perches8a27f7c2009-08-17 12:29:44 +00001829 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001830 break;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001831 case 'E':
1832 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08001833 case 'U':
1834 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001835 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001836 {
1837 va_list va;
1838
1839 va_copy(va, *((struct va_format *)ptr)->va);
1840 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1841 ((struct va_format *)ptr)->fmt, va);
1842 va_end(va);
1843 return buf;
1844 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001845 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11001846 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001847 case 'N':
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001848 return netdev_bits(buf, end, ptr, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001849 case 'a':
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001850 return address_val(buf, end, ptr, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001851 case 'd':
1852 return dentry_name(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001853 case 'C':
1854 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04001855 case 'D':
1856 return dentry_name(buf, end,
1857 ((const struct file *)ptr)->f_path.dentry,
1858 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001859#ifdef CONFIG_BLOCK
1860 case 'g':
1861 return bdev_name(buf, end, ptr, spec, fmt);
1862#endif
1863
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001864 case 'G':
1865 return flags_string(buf, end, ptr, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001866 case 'O':
1867 switch (fmt[1]) {
1868 case 'F':
1869 return device_node_string(buf, end, ptr, spec, fmt + 1);
1870 }
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001871 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001872 spec.flags |= SMALL;
1873 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001874 spec.field_width = default_width;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001875 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001876 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001877 spec.base = 16;
1878
1879 return number(buf, end, (unsigned long) ptr, spec);
1880}
1881
1882/*
1883 * Helper function to decode printf style format.
1884 * Each call decode a token from the format and return the
1885 * number of characters read (or likely the delta where it wants
1886 * to go on the next call).
1887 * The decoded token is returned through the parameters
1888 *
1889 * 'h', 'l', or 'L' for integer fields
1890 * 'z' support added 23/7/1999 S.H.
1891 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001892 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001893 * 't' added for ptrdiff_t
1894 *
1895 * @fmt: the format string
1896 * @type of the token returned
1897 * @flags: various flags such as +, -, # tokens..
1898 * @field_width: overwritten width
1899 * @base: base of the number (octal, hex, ...)
1900 * @precision: precision of a number
1901 * @qualifier: qualifier of a number (long, size_t, ...)
1902 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001903static noinline_for_stack
1904int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001905{
1906 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001907 char qualifier;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001908
1909 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001910 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001911 if (spec->field_width < 0) {
1912 spec->field_width = -spec->field_width;
1913 spec->flags |= LEFT;
1914 }
1915 spec->type = FORMAT_TYPE_NONE;
1916 goto precision;
1917 }
1918
1919 /* we finished early by reading the precision */
1920 if (spec->type == FORMAT_TYPE_PRECISION) {
1921 if (spec->precision < 0)
1922 spec->precision = 0;
1923
1924 spec->type = FORMAT_TYPE_NONE;
1925 goto qualifier;
1926 }
1927
1928 /* By default */
1929 spec->type = FORMAT_TYPE_NONE;
1930
1931 for (; *fmt ; ++fmt) {
1932 if (*fmt == '%')
1933 break;
1934 }
1935
1936 /* Return the current non-format string */
1937 if (fmt != start || !*fmt)
1938 return fmt - start;
1939
1940 /* Process flags */
1941 spec->flags = 0;
1942
1943 while (1) { /* this also skips first '%' */
1944 bool found = true;
1945
1946 ++fmt;
1947
1948 switch (*fmt) {
1949 case '-': spec->flags |= LEFT; break;
1950 case '+': spec->flags |= PLUS; break;
1951 case ' ': spec->flags |= SPACE; break;
1952 case '#': spec->flags |= SPECIAL; break;
1953 case '0': spec->flags |= ZEROPAD; break;
1954 default: found = false;
1955 }
1956
1957 if (!found)
1958 break;
1959 }
1960
1961 /* get field width */
1962 spec->field_width = -1;
1963
1964 if (isdigit(*fmt))
1965 spec->field_width = skip_atoi(&fmt);
1966 else if (*fmt == '*') {
1967 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001968 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001969 return ++fmt - start;
1970 }
1971
1972precision:
1973 /* get the precision */
1974 spec->precision = -1;
1975 if (*fmt == '.') {
1976 ++fmt;
1977 if (isdigit(*fmt)) {
1978 spec->precision = skip_atoi(&fmt);
1979 if (spec->precision < 0)
1980 spec->precision = 0;
1981 } else if (*fmt == '*') {
1982 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001983 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01001984 return ++fmt - start;
1985 }
1986 }
1987
1988qualifier:
1989 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001990 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001991 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001992 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001993 qualifier = *fmt++;
1994 if (unlikely(qualifier == *fmt)) {
1995 if (qualifier == 'l') {
1996 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08001997 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08001998 } else if (qualifier == 'h') {
1999 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002000 ++fmt;
2001 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002002 }
2003 }
2004
2005 /* default base */
2006 spec->base = 10;
2007 switch (*fmt) {
2008 case 'c':
2009 spec->type = FORMAT_TYPE_CHAR;
2010 return ++fmt - start;
2011
2012 case 's':
2013 spec->type = FORMAT_TYPE_STR;
2014 return ++fmt - start;
2015
2016 case 'p':
2017 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002018 return ++fmt - start;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002019
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002020 case '%':
2021 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2022 return ++fmt - start;
2023
2024 /* integer number formats - set up the flags and "break" */
2025 case 'o':
2026 spec->base = 8;
2027 break;
2028
2029 case 'x':
2030 spec->flags |= SMALL;
2031
2032 case 'X':
2033 spec->base = 16;
2034 break;
2035
2036 case 'd':
2037 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002038 spec->flags |= SIGN;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002039 case 'u':
2040 break;
2041
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002042 case 'n':
2043 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002044 * Since %n poses a greater security risk than
2045 * utility, treat it as any other invalid or
2046 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002047 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002048 /* Fall-through */
2049
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002050 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002051 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002052 spec->type = FORMAT_TYPE_INVALID;
2053 return fmt - start;
2054 }
2055
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002056 if (qualifier == 'L')
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002057 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002058 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002059 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2060 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002061 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002062 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002063 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002064 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002065 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002066 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2067 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002068 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002069 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2070 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002071 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002072 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2073 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002074 }
2075
2076 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002077}
2078
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002079static void
2080set_field_width(struct printf_spec *spec, int width)
2081{
2082 spec->field_width = width;
2083 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2084 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2085 }
2086}
2087
2088static void
2089set_precision(struct printf_spec *spec, int prec)
2090{
2091 spec->precision = prec;
2092 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2093 spec->precision = clamp(prec, 0, PRECISION_MAX);
2094 }
2095}
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097/**
2098 * vsnprintf - Format a string and place it in a buffer
2099 * @buf: The buffer to place the result into
2100 * @size: The size of the buffer, including the trailing null space
2101 * @fmt: The format string to use
2102 * @args: Arguments for the format string
2103 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002104 * This function generally follows C99 vsnprintf, but has some
2105 * extensions and a few limitations:
2106 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002107 * - ``%n`` is unsupported
2108 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002109 *
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002110 * See pointer() or Documentation/printk-formats.txt for more
2111 * extensive description.
2112 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002113 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 * The return value is the number of characters which would
2116 * be generated for the given input, excluding the trailing
2117 * '\0', as per ISO C99. If you want to have the exact
2118 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002119 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 * return is greater than or equal to @size, the resulting
2121 * string is truncated.
2122 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002123 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 */
2125int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002128 char *str, *end;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002129 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002131 /* Reject out-of-range values early. Large positive sizes are
2132 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002133 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002137 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002139 /* Make sure end is always >= buf */
2140 if (end < buf) {
2141 end = ((void *)-1);
2142 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002145 while (*fmt) {
2146 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002147 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002148
2149 fmt += read;
2150
2151 switch (spec.type) {
2152 case FORMAT_TYPE_NONE: {
2153 int copy = read;
2154 if (str < end) {
2155 if (copy > end - str)
2156 copy = end - str;
2157 memcpy(str, old_fmt, copy);
2158 }
2159 str += read;
2160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162
Vegard Nossumed681a92009-03-14 12:08:50 +01002163 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002164 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002167 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002168 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
André Goddard Rosad4be1512009-12-14 18:00:59 -08002171 case FORMAT_TYPE_CHAR: {
2172 char c;
2173
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002174 if (!(spec.flags & LEFT)) {
2175 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002176 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 *str = ' ';
2178 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002181 }
2182 c = (unsigned char) va_arg(args, int);
2183 if (str < end)
2184 *str = c;
2185 ++str;
2186 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002187 if (str < end)
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002188 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002190 }
2191 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002194 case FORMAT_TYPE_STR:
2195 str = string(str, end, va_arg(args, char *), spec);
2196 break;
2197
2198 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002199 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002200 spec);
2201 while (isalnum(*fmt))
2202 fmt++;
2203 break;
2204
2205 case FORMAT_TYPE_PERCENT_CHAR:
2206 if (str < end)
2207 *str = '%';
2208 ++str;
2209 break;
2210
2211 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002212 /*
2213 * Presumably the arguments passed gcc's type
2214 * checking, but there is no safe or sane way
2215 * for us to continue parsing the format and
2216 * fetching from the va_list; the remaining
2217 * specifiers and arguments would be out of
2218 * sync.
2219 */
2220 goto out;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002221
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002222 default:
2223 switch (spec.type) {
2224 case FORMAT_TYPE_LONG_LONG:
2225 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002227 case FORMAT_TYPE_ULONG:
2228 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002230 case FORMAT_TYPE_LONG:
2231 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002233 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002234 if (spec.flags & SIGN)
2235 num = va_arg(args, ssize_t);
2236 else
2237 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002238 break;
2239 case FORMAT_TYPE_PTRDIFF:
2240 num = va_arg(args, ptrdiff_t);
2241 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002242 case FORMAT_TYPE_UBYTE:
2243 num = (unsigned char) va_arg(args, int);
2244 break;
2245 case FORMAT_TYPE_BYTE:
2246 num = (signed char) va_arg(args, int);
2247 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002248 case FORMAT_TYPE_USHORT:
2249 num = (unsigned short) va_arg(args, int);
2250 break;
2251 case FORMAT_TYPE_SHORT:
2252 num = (short) va_arg(args, int);
2253 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002254 case FORMAT_TYPE_INT:
2255 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 default:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002258 num = va_arg(args, unsigned int);
2259 }
2260
2261 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002264
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002265out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002266 if (size > 0) {
2267 if (str < end)
2268 *str = '\0';
2269 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002270 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002271 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002272
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002273 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return str-buf;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277EXPORT_SYMBOL(vsnprintf);
2278
2279/**
2280 * vscnprintf - Format a string and place it in a buffer
2281 * @buf: The buffer to place the result into
2282 * @size: The size of the buffer, including the trailing null space
2283 * @fmt: The format string to use
2284 * @args: Arguments for the format string
2285 *
2286 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002287 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 * returns 0.
2289 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002290 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002291 *
2292 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
2294int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2295{
2296 int i;
2297
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002298 i = vsnprintf(buf, size, fmt, args);
2299
Anton Arapovb921c692011-01-12 16:59:49 -08002300 if (likely(i < size))
2301 return i;
2302 if (size != 0)
2303 return size - 1;
2304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306EXPORT_SYMBOL(vscnprintf);
2307
2308/**
2309 * snprintf - Format a string and place it in a buffer
2310 * @buf: The buffer to place the result into
2311 * @size: The size of the buffer, including the trailing null space
2312 * @fmt: The format string to use
2313 * @...: Arguments for the format string
2314 *
2315 * The return value is the number of characters which would be
2316 * generated for the given input, excluding the trailing null,
2317 * as per ISO C99. If the return is greater than or equal to
2318 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002319 *
2320 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002322int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 va_list args;
2325 int i;
2326
2327 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002328 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return i;
2332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333EXPORT_SYMBOL(snprintf);
2334
2335/**
2336 * scnprintf - Format a string and place it in a buffer
2337 * @buf: The buffer to place the result into
2338 * @size: The size of the buffer, including the trailing null space
2339 * @fmt: The format string to use
2340 * @...: Arguments for the format string
2341 *
2342 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002343 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 */
2345
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002346int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348 va_list args;
2349 int i;
2350
2351 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002352 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002354
Anton Arapovb921c692011-01-12 16:59:49 -08002355 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357EXPORT_SYMBOL(scnprintf);
2358
2359/**
2360 * vsprintf - Format a string and place it in a buffer
2361 * @buf: The buffer to place the result into
2362 * @fmt: The format string to use
2363 * @args: Arguments for the format string
2364 *
2365 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002366 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 * buffer overflows.
2368 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002369 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002370 *
2371 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
2373int vsprintf(char *buf, const char *fmt, va_list args)
2374{
2375 return vsnprintf(buf, INT_MAX, fmt, args);
2376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377EXPORT_SYMBOL(vsprintf);
2378
2379/**
2380 * sprintf - Format a string and place it in a buffer
2381 * @buf: The buffer to place the result into
2382 * @fmt: The format string to use
2383 * @...: Arguments for the format string
2384 *
2385 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002386 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002388 *
2389 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002391int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 va_list args;
2394 int i;
2395
2396 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002397 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return i;
2401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402EXPORT_SYMBOL(sprintf);
2403
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002404#ifdef CONFIG_BINARY_PRINTF
2405/*
2406 * bprintf service:
2407 * vbin_printf() - VA arguments to binary data
2408 * bstr_printf() - Binary data to text string
2409 */
2410
2411/**
2412 * vbin_printf - Parse a format string and place args' binary value in a buffer
2413 * @bin_buf: The buffer to place args' binary value
2414 * @size: The size of the buffer(by words(32bits), not characters)
2415 * @fmt: The format string to use
2416 * @args: Arguments for the format string
2417 *
2418 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002419 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002420 *
2421 * The return value is the number of words(32bits) which would be generated for
2422 * the given input.
2423 *
2424 * NOTE:
2425 * If the return value is greater than @size, the resulting bin_buf is NOT
2426 * valid for bstr_printf().
2427 */
2428int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2429{
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002430 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002431 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002432
2433 str = (char *)bin_buf;
2434 end = (char *)(bin_buf + size);
2435
2436#define save_arg(type) \
2437do { \
2438 if (sizeof(type) == 8) { \
2439 unsigned long long value; \
2440 str = PTR_ALIGN(str, sizeof(u32)); \
2441 value = va_arg(args, unsigned long long); \
2442 if (str + sizeof(type) <= end) { \
2443 *(u32 *)str = *(u32 *)&value; \
2444 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2445 } \
2446 } else { \
2447 unsigned long value; \
2448 str = PTR_ALIGN(str, sizeof(type)); \
2449 value = va_arg(args, int); \
2450 if (str + sizeof(type) <= end) \
2451 *(typeof(type) *)str = (type)value; \
2452 } \
2453 str += sizeof(type); \
2454} while (0)
2455
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002456 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002457 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002458
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002459 fmt += read;
2460
2461 switch (spec.type) {
2462 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002463 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002464 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002465 case FORMAT_TYPE_INVALID:
2466 goto out;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002467
Vegard Nossumed681a92009-03-14 12:08:50 +01002468 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002469 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002470 save_arg(int);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002471 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002472
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002473 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002474 save_arg(char);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002475 break;
2476
2477 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002478 const char *save_str = va_arg(args, char *);
2479 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002480
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002481 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2482 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002483 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002484 len = strlen(save_str) + 1;
2485 if (str + len < end)
2486 memcpy(str, save_str, len);
2487 str += len;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002488 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002489 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002490
2491 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002492 save_arg(void *);
2493 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002494 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002495 fmt++;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002496 break;
2497
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002498 default:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002499 switch (spec.type) {
2500
2501 case FORMAT_TYPE_LONG_LONG:
2502 save_arg(long long);
2503 break;
2504 case FORMAT_TYPE_ULONG:
2505 case FORMAT_TYPE_LONG:
2506 save_arg(unsigned long);
2507 break;
2508 case FORMAT_TYPE_SIZE_T:
2509 save_arg(size_t);
2510 break;
2511 case FORMAT_TYPE_PTRDIFF:
2512 save_arg(ptrdiff_t);
2513 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002514 case FORMAT_TYPE_UBYTE:
2515 case FORMAT_TYPE_BYTE:
2516 save_arg(char);
2517 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002518 case FORMAT_TYPE_USHORT:
2519 case FORMAT_TYPE_SHORT:
2520 save_arg(short);
2521 break;
2522 default:
2523 save_arg(int);
2524 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002525 }
2526 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002527
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002528out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002529 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002530#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002531}
2532EXPORT_SYMBOL_GPL(vbin_printf);
2533
2534/**
2535 * bstr_printf - Format a string from binary arguments and place it in a buffer
2536 * @buf: The buffer to place the result into
2537 * @size: The size of the buffer, including the trailing null space
2538 * @fmt: The format string to use
2539 * @bin_buf: Binary arguments for the format string
2540 *
2541 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2542 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2543 * a binary buffer that generated by vbin_printf.
2544 *
2545 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002546 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002547 *
2548 * The return value is the number of characters which would
2549 * be generated for the given input, excluding the trailing
2550 * '\0', as per ISO C99. If you want to have the exact
2551 * number of characters written into @buf as return value
2552 * (not including the trailing '\0'), use vscnprintf(). If the
2553 * return is greater than or equal to @size, the resulting
2554 * string is truncated.
2555 */
2556int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2557{
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002558 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002559 char *str, *end;
2560 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002561
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002562 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002563 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002564
2565 str = buf;
2566 end = buf + size;
2567
2568#define get_arg(type) \
2569({ \
2570 typeof(type) value; \
2571 if (sizeof(type) == 8) { \
2572 args = PTR_ALIGN(args, sizeof(u32)); \
2573 *(u32 *)&value = *(u32 *)args; \
2574 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2575 } else { \
2576 args = PTR_ALIGN(args, sizeof(type)); \
2577 value = *(typeof(type) *)args; \
2578 } \
2579 args += sizeof(type); \
2580 value; \
2581})
2582
2583 /* Make sure end is always >= buf */
2584 if (end < buf) {
2585 end = ((void *)-1);
2586 size = end - buf;
2587 }
2588
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002589 while (*fmt) {
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002590 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002591 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002592
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002593 fmt += read;
2594
2595 switch (spec.type) {
2596 case FORMAT_TYPE_NONE: {
2597 int copy = read;
2598 if (str < end) {
2599 if (copy > end - str)
2600 copy = end - str;
2601 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002602 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002603 str += read;
2604 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002605 }
2606
Vegard Nossumed681a92009-03-14 12:08:50 +01002607 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002608 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002609 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002610
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002611 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002612 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002613 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002614
André Goddard Rosad4be1512009-12-14 18:00:59 -08002615 case FORMAT_TYPE_CHAR: {
2616 char c;
2617
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002618 if (!(spec.flags & LEFT)) {
2619 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002620 if (str < end)
2621 *str = ' ';
2622 ++str;
2623 }
2624 }
2625 c = (unsigned char) get_arg(char);
2626 if (str < end)
2627 *str = c;
2628 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002629 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002630 if (str < end)
2631 *str = ' ';
2632 ++str;
2633 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002634 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002635 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002636
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002637 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002638 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002639 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002640 str = string(str, end, (char *)str_arg, spec);
2641 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002642 }
2643
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002644 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002645 str = pointer(fmt, str, end, get_arg(void *), spec);
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002646 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002647 fmt++;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002648 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002649
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002650 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002651 if (str < end)
2652 *str = '%';
2653 ++str;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002654 break;
2655
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002656 case FORMAT_TYPE_INVALID:
2657 goto out;
2658
André Goddard Rosad4be1512009-12-14 18:00:59 -08002659 default: {
2660 unsigned long long num;
2661
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002662 switch (spec.type) {
2663
2664 case FORMAT_TYPE_LONG_LONG:
2665 num = get_arg(long long);
2666 break;
2667 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002668 case FORMAT_TYPE_LONG:
2669 num = get_arg(unsigned long);
2670 break;
2671 case FORMAT_TYPE_SIZE_T:
2672 num = get_arg(size_t);
2673 break;
2674 case FORMAT_TYPE_PTRDIFF:
2675 num = get_arg(ptrdiff_t);
2676 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002677 case FORMAT_TYPE_UBYTE:
2678 num = get_arg(unsigned char);
2679 break;
2680 case FORMAT_TYPE_BYTE:
2681 num = get_arg(signed char);
2682 break;
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002683 case FORMAT_TYPE_USHORT:
2684 num = get_arg(unsigned short);
2685 break;
2686 case FORMAT_TYPE_SHORT:
2687 num = get_arg(short);
2688 break;
2689 case FORMAT_TYPE_UINT:
2690 num = get_arg(unsigned int);
2691 break;
2692 default:
2693 num = get_arg(int);
2694 }
2695
2696 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002697 } /* default: */
2698 } /* switch(spec.type) */
2699 } /* while(*fmt) */
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002700
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002701out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002702 if (size > 0) {
2703 if (str < end)
2704 *str = '\0';
2705 else
2706 end[-1] = '\0';
2707 }
Frederic Weisbeckerfef20d9c2009-03-06 17:21:50 +01002708
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002709#undef get_arg
2710
2711 /* the trailing null byte doesn't count towards the total */
2712 return str - buf;
2713}
2714EXPORT_SYMBOL_GPL(bstr_printf);
2715
2716/**
2717 * bprintf - Parse a format string and place args' binary value in a buffer
2718 * @bin_buf: The buffer to place args' binary value
2719 * @size: The size of the buffer(by words(32bits), not characters)
2720 * @fmt: The format string to use
2721 * @...: Arguments for the format string
2722 *
2723 * The function returns the number of words(u32) written
2724 * into @bin_buf.
2725 */
2726int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2727{
2728 va_list args;
2729 int ret;
2730
2731 va_start(args, fmt);
2732 ret = vbin_printf(bin_buf, size, fmt, args);
2733 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002734
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002735 return ret;
2736}
2737EXPORT_SYMBOL_GPL(bprintf);
2738
2739#endif /* CONFIG_BINARY_PRINTF */
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741/**
2742 * vsscanf - Unformat a buffer into a list of arguments
2743 * @buf: input buffer
2744 * @fmt: format of buffer
2745 * @args: arguments
2746 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002747int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 const char *str = buf;
2750 char *next;
2751 char digit;
2752 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002753 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002754 unsigned int base;
2755 union {
2756 long long s;
2757 unsigned long long u;
2758 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002759 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002760 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Jan Beulichda990752012-10-04 17:13:24 -07002762 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 /* skip any white space in format */
2764 /* white space in format matchs any amount of
2765 * white space, including none, in the input.
2766 */
2767 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002768 fmt = skip_spaces(++fmt);
2769 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771
2772 /* anything that is not a conversion must match exactly */
2773 if (*fmt != '%' && *fmt) {
2774 if (*fmt++ != *str++)
2775 break;
2776 continue;
2777 }
2778
2779 if (!*fmt)
2780 break;
2781 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /* skip this conversion.
2784 * advance both strings to next white space
2785 */
2786 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002787 if (!*str)
2788 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07002789 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2790 /* '%*[' not yet supported, invalid format */
2791 if (*fmt == '[')
2792 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07002794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 while (!isspace(*str) && *str)
2796 str++;
2797 continue;
2798 }
2799
2800 /* get field width */
2801 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002802 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002804 if (field_width <= 0)
2805 break;
2806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 /* get conversion qualifier */
2809 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002810 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002811 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 qualifier = *fmt++;
2813 if (unlikely(qualifier == *fmt)) {
2814 if (qualifier == 'h') {
2815 qualifier = 'H';
2816 fmt++;
2817 } else if (qualifier == 'l') {
2818 qualifier = 'L';
2819 fmt++;
2820 }
2821 }
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Jan Beulichda990752012-10-04 17:13:24 -07002824 if (!*fmt)
2825 break;
2826
2827 if (*fmt == 'n') {
2828 /* return number of characters read so far */
2829 *va_arg(args, int *) = str - buf;
2830 ++fmt;
2831 continue;
2832 }
2833
2834 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 break;
2836
André Goddard Rosad4be1512009-12-14 18:00:59 -08002837 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002838 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002839
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002840 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 case 'c':
2842 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002843 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (field_width == -1)
2845 field_width = 1;
2846 do {
2847 *s++ = *str++;
2848 } while (--field_width > 0 && *str);
2849 num++;
2850 }
2851 continue;
2852 case 's':
2853 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002854 char *s = (char *)va_arg(args, char *);
2855 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002856 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002858 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002861 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 *s = '\0';
2864 num++;
2865 }
2866 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07002867 /*
2868 * Warning: This implementation of the '[' conversion specifier
2869 * deviates from its glibc counterpart in the following ways:
2870 * (1) It does NOT support ranges i.e. '-' is NOT a special
2871 * character
2872 * (2) It cannot match the closing bracket ']' itself
2873 * (3) A field width is required
2874 * (4) '%*[' (discard matching input) is currently not supported
2875 *
2876 * Example usage:
2877 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
2878 * buf1, buf2, buf3);
2879 * if (ret < 3)
2880 * // etc..
2881 */
2882 case '[':
2883 {
2884 char *s = (char *)va_arg(args, char *);
2885 DECLARE_BITMAP(set, 256) = {0};
2886 unsigned int len = 0;
2887 bool negate = (*fmt == '^');
2888
2889 /* field width is required */
2890 if (field_width == -1)
2891 return num;
2892
2893 if (negate)
2894 ++fmt;
2895
2896 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
2897 set_bit((u8)*fmt, set);
2898
2899 /* no ']' or no character set found */
2900 if (!*fmt || !len)
2901 return num;
2902 ++fmt;
2903
2904 if (negate) {
2905 bitmap_complement(set, set, 256);
2906 /* exclude null '\0' byte */
2907 clear_bit(0, set);
2908 }
2909
2910 /* match must be non-empty */
2911 if (!test_bit((u8)*str, set))
2912 return num;
2913
2914 while (test_bit((u8)*str, set) && field_width--)
2915 *s++ = *str++;
2916 *s = '\0';
2917 ++num;
2918 }
2919 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 case 'o':
2921 base = 8;
2922 break;
2923 case 'x':
2924 case 'X':
2925 base = 16;
2926 break;
2927 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002928 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07002930 is_sign = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 case 'u':
2932 break;
2933 case '%':
2934 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002935 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return num;
2937 continue;
2938 default:
2939 /* invalid format; stop here */
2940 return num;
2941 }
2942
2943 /* have some sort of integer conversion.
2944 * first, skip white space in buffer.
2945 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002946 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 digit = *str;
2949 if (is_sign && digit == '-')
2950 digit = *(str + 1);
2951
2952 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002953 || (base == 16 && !isxdigit(digit))
2954 || (base == 10 && !isdigit(digit))
2955 || (base == 8 && (!isdigit(digit) || digit > '7'))
2956 || (base == 0 && !isdigit(digit)))
2957 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Jan Beulich53809752012-12-17 16:01:31 -08002959 if (is_sign)
2960 val.s = qualifier != 'L' ?
2961 simple_strtol(str, &next, base) :
2962 simple_strtoll(str, &next, base);
2963 else
2964 val.u = qualifier != 'L' ?
2965 simple_strtoul(str, &next, base) :
2966 simple_strtoull(str, &next, base);
2967
2968 if (field_width > 0 && next - str > field_width) {
2969 if (base == 0)
2970 _parse_integer_fixup_radix(str, &base);
2971 while (next - str > field_width) {
2972 if (is_sign)
2973 val.s = div_s64(val.s, base);
2974 else
2975 val.u = div_u64(val.u, base);
2976 --next;
2977 }
2978 }
2979
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002980 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002982 if (is_sign)
2983 *va_arg(args, signed char *) = val.s;
2984 else
2985 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 break;
2987 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002988 if (is_sign)
2989 *va_arg(args, short *) = val.s;
2990 else
2991 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 break;
2993 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002994 if (is_sign)
2995 *va_arg(args, long *) = val.s;
2996 else
2997 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 break;
2999 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003000 if (is_sign)
3001 *va_arg(args, long long *) = val.s;
3002 else
3003 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003006 *va_arg(args, size_t *) = val.u;
3007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 default:
Jan Beulich53809752012-12-17 16:01:31 -08003009 if (is_sign)
3010 *va_arg(args, int *) = val.s;
3011 else
3012 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 break;
3014 }
3015 num++;
3016
3017 if (!next)
3018 break;
3019 str = next;
3020 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return num;
3023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024EXPORT_SYMBOL(vsscanf);
3025
3026/**
3027 * sscanf - Unformat a buffer into a list of arguments
3028 * @buf: input buffer
3029 * @fmt: formatting of buffer
3030 * @...: resulting arguments
3031 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003032int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
3034 va_list args;
3035 int i;
3036
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003037 va_start(args, fmt);
3038 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return i;
3042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043EXPORT_SYMBOL(sscanf);