blob: 8ca29bc0d7865a700ea3e8dc07e7a7510572fc1e [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>
Rasmus Villemoesef27ac12019-03-07 16:27:03 -080020#include <linux/build_bug.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070021#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070022#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050023#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/ctype.h>
27#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070028#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080029#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070030#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110031#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040032#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080033#include <linux/cred.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020034#include <linux/rtc.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070035#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020036#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000037#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110038#include <linux/siphash.h>
39#include <linux/compiler.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040040#ifdef CONFIG_BLOCK
41#include <linux/blkdev.h>
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070044#include "../mm/internal.h" /* For the trace_print_flags arrays */
45
Tim Schmielau4e57b682005-10-30 15:03:48 -080046#include <asm/page.h> /* for PAGE_SIZE */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070047#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Shevchenko71dca952014-10-13 15:55:18 -070049#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070050#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * simple_strtoull - convert a string to an unsigned long long
54 * @cp: The start of the string
55 * @endp: A pointer to the end of the parsed string will be placed here
56 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080057 *
58 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Harvey Harrison22d27052008-10-16 13:40:35 -070060unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070062 unsigned long long result;
63 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070065 cp = _parse_integer_fixup_radix(cp, &base);
66 rv = _parse_integer(cp, base, &result);
67 /* FIXME */
68 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (endp)
71 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return result;
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075EXPORT_SYMBOL(simple_strtoull);
76
77/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080078 * simple_strtoul - convert a string to an unsigned long
79 * @cp: The start of the string
80 * @endp: A pointer to the end of the parsed string will be placed here
81 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080082 *
83 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080084 */
85unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
86{
87 return simple_strtoull(cp, endp, base);
88}
89EXPORT_SYMBOL(simple_strtoul);
90
91/**
92 * simple_strtol - convert a string to a signed long
93 * @cp: The start of the string
94 * @endp: A pointer to the end of the parsed string will be placed here
95 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080096 *
97 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080098 */
99long simple_strtol(const char *cp, char **endp, unsigned int base)
100{
101 if (*cp == '-')
102 return -simple_strtoul(cp + 1, endp, base);
103
104 return simple_strtoul(cp, endp, base);
105}
106EXPORT_SYMBOL(simple_strtol);
107
108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800113 *
114 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700116long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800118 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700119 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800120
Harvey Harrison22d27052008-10-16 13:40:35 -0700121 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400123EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Joe Perchescf3b4292010-05-24 14:33:16 -0700125static noinline_for_stack
126int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800128 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800130 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800132 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return i;
135}
136
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700137/*
138 * Decimal conversion is by far the most typical, and is used for
139 * /proc and /sys data. This directly impacts e.g. top performance
140 * with many processes running. We optimize it for speed by emitting
141 * two characters at a time, using a 200 byte lookup table. This
142 * roughly halves the number of multiplications compared to computing
143 * the digits one at a time. Implementation strongly inspired by the
144 * previous version, which in turn used ideas described at
145 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
146 * from the author, Douglas W. Jones).
147 *
148 * It turns out there is precisely one 26 bit fixed-point
149 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
150 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
151 * range happens to be somewhat larger (x <= 1073741898), but that's
152 * irrelevant for our purpose.
153 *
154 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
155 * need a 32x32->64 bit multiply, so we simply use the same constant.
156 *
157 * For dividing a number in the range [100, 10^4-1] by 100, there are
158 * several options. The simplest is (x * 0x147b) >> 19, which is valid
159 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700160 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700161
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700162static const u16 decpair[100] = {
163#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
164 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
165 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
166 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
167 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
168 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
169 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
170 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
171 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
172 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
173 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
174#undef _
175};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700176
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700177/*
178 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700179 * immediately jump to out_r where two 0s would be written but only
180 * one of them accounted for in buf. This is needed by ip4_string
181 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700182*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700183static noinline_for_stack
184char *put_dec_trunc8(char *buf, unsigned r)
185{
186 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700187
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700188 /* 1 <= r < 10^8 */
189 if (r < 100)
190 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700191
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700192 /* 100 <= r < 10^8 */
193 q = (r * (u64)0x28f5c29) >> 32;
194 *((u16 *)buf) = decpair[r - 100*q];
195 buf += 2;
196
197 /* 1 <= q < 10^6 */
198 if (q < 100)
199 goto out_q;
200
201 /* 100 <= q < 10^6 */
202 r = (q * (u64)0x28f5c29) >> 32;
203 *((u16 *)buf) = decpair[q - 100*r];
204 buf += 2;
205
206 /* 1 <= r < 10^4 */
207 if (r < 100)
208 goto out_r;
209
210 /* 100 <= r < 10^4 */
211 q = (r * 0x147b) >> 19;
212 *((u16 *)buf) = decpair[r - 100*q];
213 buf += 2;
214out_q:
215 /* 1 <= q < 100 */
216 r = q;
217out_r:
218 /* 1 <= r < 100 */
219 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700220 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700221 return buf;
222}
223
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700224#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
225static noinline_for_stack
226char *put_dec_full8(char *buf, unsigned r)
227{
228 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700229
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700230 /* 0 <= r < 10^8 */
231 q = (r * (u64)0x28f5c29) >> 32;
232 *((u16 *)buf) = decpair[r - 100*q];
233 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700234
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700235 /* 0 <= q < 10^6 */
236 r = (q * (u64)0x28f5c29) >> 32;
237 *((u16 *)buf) = decpair[q - 100*r];
238 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700239
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700240 /* 0 <= r < 10^4 */
241 q = (r * 0x147b) >> 19;
242 *((u16 *)buf) = decpair[r - 100*q];
243 buf += 2;
244
245 /* 0 <= q < 100 */
246 *((u16 *)buf) = decpair[q];
247 buf += 2;
248 return buf;
249}
250
251static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700252char *put_dec(char *buf, unsigned long long n)
253{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700254 if (n >= 100*1000*1000)
255 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
256 /* 1 <= n <= 1.6e11 */
257 if (n >= 100*1000*1000)
258 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
259 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700260 return put_dec_trunc8(buf, n);
261}
262
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700263#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700264
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700265static void
266put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700267{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700268 unsigned q;
269
270 /* 0 <= r < 10^4 */
271 q = (r * 0x147b) >> 19;
272 *((u16 *)buf) = decpair[r - 100*q];
273 buf += 2;
274 /* 0 <= q < 100 */
275 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700276}
277
278/*
279 * Call put_dec_full4 on x % 10000, return x / 10000.
280 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
281 * holds for all x < 1,128,869,999. The largest value this
282 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700283 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700284 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700285static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700286unsigned put_dec_helper4(char *buf, unsigned x)
287{
288 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
289
290 put_dec_full4(buf, x - q * 10000);
291 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700292}
293
294/* Based on code by Douglas W. Jones found at
295 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
296 * (with permission from the author).
297 * Performs no 64-bit division and hence should be fast on 32-bit machines.
298 */
299static
300char *put_dec(char *buf, unsigned long long n)
301{
302 uint32_t d3, d2, d1, q, h;
303
304 if (n < 100*1000*1000)
305 return put_dec_trunc8(buf, n);
306
307 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
308 h = (n >> 32);
309 d2 = (h ) & 0xffff;
310 d3 = (h >> 16); /* implicit "& 0xffff" */
311
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700312 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
313 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700314 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700315 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700316
George Spelvin23591722012-10-04 17:12:29 -0700317 q += 7671 * d3 + 9496 * d2 + 6 * d1;
318 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700319
George Spelvin23591722012-10-04 17:12:29 -0700320 q += 4749 * d3 + 42 * d2;
321 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700322
George Spelvin23591722012-10-04 17:12:29 -0700323 q += 281 * d3;
324 buf += 12;
325 if (q)
326 buf = put_dec_trunc8(buf, q);
327 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700328 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800329
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700330 return buf;
331}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700332
333#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700334
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700335/*
336 * Convert passed number to decimal string.
337 * Returns the length of string. On buffer overflow, returns 0.
338 *
339 * If speed is not important, use snprintf(). It's easy to read the code.
340 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700341int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700342{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700343 /* put_dec requires 2-byte alignment of the buffer. */
344 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700345 int idx, len;
346
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700347 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
348 if (num <= 9) {
349 tmp[0] = '0' + num;
350 len = 1;
351 } else {
352 len = put_dec(tmp, num) - tmp;
353 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700354
Andrei Vagind1be35c2018-04-10 16:31:16 -0700355 if (len > size || width > size)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700356 return 0;
Andrei Vagind1be35c2018-04-10 16:31:16 -0700357
358 if (width > len) {
359 width = width - len;
360 for (idx = 0; idx < width; idx++)
361 buf[idx] = ' ';
362 } else {
363 width = 0;
364 }
365
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700366 for (idx = 0; idx < len; ++idx)
Andrei Vagind1be35c2018-04-10 16:31:16 -0700367 buf[idx + width] = tmp[len - idx - 1];
368
369 return len + width;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700370}
371
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700372#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700373#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#define PLUS 4 /* show plus */
375#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700376#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700377#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
378#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100380enum format_type {
381 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100382 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100383 FORMAT_TYPE_PRECISION,
384 FORMAT_TYPE_CHAR,
385 FORMAT_TYPE_STR,
386 FORMAT_TYPE_PTR,
387 FORMAT_TYPE_PERCENT_CHAR,
388 FORMAT_TYPE_INVALID,
389 FORMAT_TYPE_LONG_LONG,
390 FORMAT_TYPE_ULONG,
391 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800392 FORMAT_TYPE_UBYTE,
393 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100394 FORMAT_TYPE_USHORT,
395 FORMAT_TYPE_SHORT,
396 FORMAT_TYPE_UINT,
397 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100398 FORMAT_TYPE_SIZE_T,
399 FORMAT_TYPE_PTRDIFF
400};
401
402struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800403 unsigned int type:8; /* format_type enum */
404 signed int field_width:24; /* width of output field */
405 unsigned int flags:8; /* flags to number() */
406 unsigned int base:8; /* number base, 8, 10 or 16 only */
407 signed int precision:16; /* # of digits/chars */
408} __packed;
Rasmus Villemoesef27ac12019-03-07 16:27:03 -0800409static_assert(sizeof(struct printf_spec) == 8);
410
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800411#define FIELD_WIDTH_MAX ((1 << 23) - 1)
412#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100413
Joe Perchescf3b4292010-05-24 14:33:16 -0700414static noinline_for_stack
415char *number(char *buf, char *end, unsigned long long num,
416 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700418 /* put_dec requires 2-byte alignment of the buffer. */
419 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100420 char sign;
421 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100422 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700424 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800425 int field_width = spec.field_width;
426 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100428 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
429 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100430 locase = (spec.flags & SMALL);
431 if (spec.flags & LEFT)
432 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100434 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800435 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800437 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800438 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100439 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800441 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100442 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800444 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100448 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800449 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700450 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800451 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700453
454 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700456 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700457 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100458 else if (spec.base != 10) { /* 8 or 16 */
459 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800461
462 if (spec.base == 16)
463 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700464 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700465 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700466 num >>= shift;
467 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700468 } else { /* base 10 */
469 i = put_dec(tmp, num) - tmp;
470 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700471
472 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800473 if (i > precision)
474 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700475 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800476 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700477 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800478 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700479 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *buf = ' ';
481 ++buf;
482 }
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700486 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *buf = sign;
488 ++buf;
489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* "0x" / "0" prefix */
491 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700492 if (spec.base == 16 || !is_zero) {
493 if (buf < end)
494 *buf = '0';
495 ++buf;
496 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100497 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700498 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100499 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ++buf;
501 }
502 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700503 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100504 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700505 char c = ' ' + (spec.flags & ZEROPAD);
506 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800507 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700508 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buf = c;
510 ++buf;
511 }
512 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700513 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800514 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700515 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *buf = '0';
517 ++buf;
518 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700519 /* actual digits of result */
520 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700521 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *buf = tmp[i];
523 ++buf;
524 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700525 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800526 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700527 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *buf = ' ';
529 ++buf;
530 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return buf;
533}
534
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800535static noinline_for_stack
536char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
537{
538 struct printf_spec spec;
539
540 spec.type = FORMAT_TYPE_PTR;
541 spec.field_width = 2 + 2 * size; /* 0x + hex */
542 spec.flags = SPECIAL | SMALL | ZEROPAD;
543 spec.base = 16;
544 spec.precision = -1;
545
546 return number(buf, end, num, spec);
547}
548
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800549static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400550{
551 size_t size;
552 if (buf >= end) /* nowhere to put anything */
553 return;
554 size = end - buf;
555 if (size <= spaces) {
556 memset(buf, ' ', size);
557 return;
558 }
559 if (len) {
560 if (len > size - spaces)
561 len = size - spaces;
562 memmove(buf + spaces, buf, len);
563 }
564 memset(buf, ' ', spaces);
565}
566
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800567/*
568 * Handle field width padding for a string.
569 * @buf: current buffer position
570 * @n: length of string
571 * @end: end of output buffer
572 * @spec: for field width and flags
573 * Returns: new buffer position after padding.
574 */
575static noinline_for_stack
576char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
577{
578 unsigned spaces;
579
580 if (likely(n >= spec.field_width))
581 return buf;
582 /* we want to pad the sucker */
583 spaces = spec.field_width - n;
584 if (!(spec.flags & LEFT)) {
585 move_right(buf - n, end, n, spaces);
586 return buf + spaces;
587 }
588 while (spaces--) {
589 if (buf < end)
590 *buf = ' ';
591 ++buf;
592 }
593 return buf;
594}
595
Petr Mladekd529ac42019-04-17 13:53:43 +0200596/* Handle string from a well known address. */
597static char *string_nocheck(char *buf, char *end, const char *s,
598 struct printf_spec spec)
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800599{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800600 int len = 0;
601 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800602
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800603 while (lim--) {
604 char c = *s++;
605 if (!c)
606 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800607 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800608 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800609 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800610 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800611 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800612 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800613}
614
615static noinline_for_stack
Petr Mladekd529ac42019-04-17 13:53:43 +0200616char *string(char *buf, char *end, const char *s,
617 struct printf_spec spec)
618{
619 if ((unsigned long)s < PAGE_SIZE)
620 s = "(null)";
621
622 return string_nocheck(buf, end, s, spec);
623}
624
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200625char *pointer_string(char *buf, char *end, const void *ptr,
626 struct printf_spec spec)
627{
628 spec.base = 16;
629 spec.flags |= SMALL;
630 if (spec.field_width == -1) {
631 spec.field_width = 2 * sizeof(ptr);
632 spec.flags |= ZEROPAD;
633 }
634
635 return number(buf, end, (unsigned long int)ptr, spec);
636}
637
638/* Make pointers available for printing early in the boot sequence. */
639static int debug_boot_weak_hash __ro_after_init;
640
641static int __init debug_boot_weak_hash_enable(char *str)
642{
643 debug_boot_weak_hash = 1;
644 pr_info("debug_boot_weak_hash enabled\n");
645 return 0;
646}
647early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
648
649static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
650static siphash_key_t ptr_key __read_mostly;
651
652static void enable_ptr_key_workfn(struct work_struct *work)
653{
654 get_random_bytes(&ptr_key, sizeof(ptr_key));
655 /* Needs to run from preemptible context */
656 static_branch_disable(&not_filled_random_ptr_key);
657}
658
659static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
660
661static void fill_random_ptr_key(struct random_ready_callback *unused)
662{
663 /* This may be in an interrupt handler. */
664 queue_work(system_unbound_wq, &enable_ptr_key_work);
665}
666
667static struct random_ready_callback random_ready = {
668 .func = fill_random_ptr_key
669};
670
671static int __init initialize_ptr_random(void)
672{
673 int key_size = sizeof(ptr_key);
674 int ret;
675
676 /* Use hw RNG if available. */
677 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
678 static_branch_disable(&not_filled_random_ptr_key);
679 return 0;
680 }
681
682 ret = add_random_ready_callback(&random_ready);
683 if (!ret) {
684 return 0;
685 } else if (ret == -EALREADY) {
686 /* This is in preemptible context */
687 enable_ptr_key_workfn(&enable_ptr_key_work);
688 return 0;
689 }
690
691 return ret;
692}
693early_initcall(initialize_ptr_random);
694
695/* Maps a pointer to a 32 bit unique identifier. */
696static char *ptr_to_id(char *buf, char *end, const void *ptr,
697 struct printf_spec spec)
698{
699 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
700 unsigned long hashval;
701
702 /* When debugging early boot use non-cryptographically secure hash. */
703 if (unlikely(debug_boot_weak_hash)) {
704 hashval = hash_long((unsigned long)ptr, 32);
705 return pointer_string(buf, end, (const void *)hashval, spec);
706 }
707
708 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
709 spec.field_width = 2 * sizeof(ptr);
710 /* string length must be less than default_width */
Petr Mladekd529ac42019-04-17 13:53:43 +0200711 return string_nocheck(buf, end, str, spec);
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200712 }
713
714#ifdef CONFIG_64BIT
715 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
716 /*
717 * Mask off the first 32 bits, this makes explicit that we have
718 * modified the address (and 32 bits is plenty for a unique ID).
719 */
720 hashval = hashval & 0xffffffff;
721#else
722 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
723#endif
724 return pointer_string(buf, end, (const void *)hashval, spec);
725}
726
Petr Mladek6eea2422019-04-17 13:53:41 +0200727int kptr_restrict __read_mostly;
728
729static noinline_for_stack
730char *restricted_pointer(char *buf, char *end, const void *ptr,
731 struct printf_spec spec)
732{
733 switch (kptr_restrict) {
734 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200735 /* Handle as %p, hash and do _not_ leak addresses. */
736 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200737 case 1: {
738 const struct cred *cred;
739
740 /*
741 * kptr_restrict==1 cannot be used in IRQ context
742 * because its test for CAP_SYSLOG would be meaningless.
743 */
744 if (in_irq() || in_serving_softirq() || in_nmi()) {
745 if (spec.field_width == -1)
746 spec.field_width = 2 * sizeof(ptr);
Petr Mladekd529ac42019-04-17 13:53:43 +0200747 return string_nocheck(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200748 }
749
750 /*
751 * Only print the real pointer value if the current
752 * process has CAP_SYSLOG and is running with the
753 * same credentials it started with. This is because
754 * access to files is checked at open() time, but %pK
755 * checks permission at read() time. We don't want to
756 * leak pointer values if a binary opens a file using
757 * %pK and then elevates privileges before reading it.
758 */
759 cred = current_cred();
760 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
761 !uid_eq(cred->euid, cred->uid) ||
762 !gid_eq(cred->egid, cred->gid))
763 ptr = NULL;
764 break;
765 }
766 case 2:
767 default:
768 /* Always print 0's for %pK */
769 ptr = NULL;
770 break;
771 }
772
773 return pointer_string(buf, end, ptr, spec);
774}
775
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200776static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400777char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
778 const char *fmt)
779{
780 const char *array[4], *s;
781 const struct dentry *p;
782 int depth;
783 int i, n;
784
785 switch (fmt[1]) {
786 case '2': case '3': case '4':
787 depth = fmt[1] - '0';
788 break;
789 default:
790 depth = 1;
791 }
792
793 rcu_read_lock();
794 for (i = 0; i < depth; i++, d = p) {
Mark Rutland6aa7de02017-10-23 14:07:29 -0700795 p = READ_ONCE(d->d_parent);
796 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400797 if (p == d) {
798 if (i)
799 array[i] = "";
800 i++;
801 break;
802 }
803 }
804 s = array[--i];
805 for (n = 0; n != spec.precision; n++, buf++) {
806 char c = *s++;
807 if (!c) {
808 if (!i)
809 break;
810 c = '/';
811 s = array[--i];
812 }
813 if (buf < end)
814 *buf = c;
815 }
816 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800817 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400818}
819
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400820#ifdef CONFIG_BLOCK
821static noinline_for_stack
822char *bdev_name(char *buf, char *end, struct block_device *bdev,
823 struct printf_spec spec, const char *fmt)
824{
825 struct gendisk *hd = bdev->bd_disk;
826
827 buf = string(buf, end, hd->disk_name, spec);
828 if (bdev->bd_part->partno) {
829 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
830 if (buf < end)
831 *buf = 'p';
832 buf++;
833 }
834 buf = number(buf, end, bdev->bd_part->partno, spec);
835 }
836 return buf;
837}
838#endif
839
Joe Perchescf3b4292010-05-24 14:33:16 -0700840static noinline_for_stack
841char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800842 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700843{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800844 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700845#ifdef CONFIG_KALLSYMS
846 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800847#endif
848
849 if (fmt[1] == 'R')
850 ptr = __builtin_extract_return_addr(ptr);
851 value = (unsigned long)ptr;
852
853#ifdef CONFIG_KALLSYMS
854 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900855 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800856 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200857 sprint_symbol(sym, value);
858 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700859 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800860
Petr Mladekd529ac42019-04-17 13:53:43 +0200861 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700862#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800863 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700864#endif
865}
866
Andy Shevchenkoabd4fe622018-02-16 23:07:05 +0200867static const struct printf_spec default_str_spec = {
868 .field_width = -1,
869 .precision = -1,
870};
871
Andy Shevchenko54433972018-02-16 23:07:06 +0200872static const struct printf_spec default_flag_spec = {
873 .base = 16,
874 .precision = -1,
875 .flags = SPECIAL | SMALL,
876};
877
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200878static const struct printf_spec default_dec_spec = {
879 .base = 10,
880 .precision = -1,
881};
882
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200883static const struct printf_spec default_dec02_spec = {
884 .base = 10,
885 .field_width = 2,
886 .precision = -1,
887 .flags = ZEROPAD,
888};
889
890static const struct printf_spec default_dec04_spec = {
891 .base = 10,
892 .field_width = 4,
893 .precision = -1,
894 .flags = ZEROPAD,
895};
896
Joe Perchescf3b4292010-05-24 14:33:16 -0700897static noinline_for_stack
898char *resource_string(char *buf, char *end, struct resource *res,
899 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100900{
901#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600902#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100903#endif
904
905#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600906#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100907#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700908 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100909 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700910 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100911 .precision = -1,
912 .flags = SPECIAL | SMALL | ZEROPAD,
913 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700914 static const struct printf_spec mem_spec = {
915 .base = 16,
916 .field_width = MEM_RSRC_PRINTK_SIZE,
917 .precision = -1,
918 .flags = SPECIAL | SMALL | ZEROPAD,
919 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700920 static const struct printf_spec bus_spec = {
921 .base = 16,
922 .field_width = 2,
923 .precision = -1,
924 .flags = SMALL | ZEROPAD,
925 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700926 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600927 .field_width = -1,
928 .precision = 10,
929 .flags = LEFT,
930 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600931
932 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
933 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
934#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
935#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700936#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600937#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
938 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
939 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
940
Linus Torvalds332d2e72008-10-20 15:07:34 +1100941 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600942 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700943 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100944
945 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700946 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +0200947 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700948 specp = &io_spec;
949 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +0200950 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700951 specp = &mem_spec;
952 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +0200953 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200954 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700955 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +0200956 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200957 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700958 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +0200959 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700960 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700961 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +0200962 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700963 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600964 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600965 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700966 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +0200967 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -0700968 p = number(p, pend, resource_size(res), *specp);
969 } else {
970 p = number(p, pend, res->start, *specp);
971 if (res->start != res->end) {
972 *p++ = '-';
973 p = number(p, pend, res->end, *specp);
974 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600975 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600976 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600977 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +0200978 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600979 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +0200980 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700981 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +0200982 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600983 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +0200984 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600985 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +0200986 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +0200987 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600988 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100989 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600990 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100991
Petr Mladekd529ac42019-04-17 13:53:43 +0200992 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100993}
994
Joe Perchescf3b4292010-05-24 14:33:16 -0700995static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700996char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
997 const char *fmt)
998{
Steven Rostedt360603a2013-05-28 15:47:39 -0400999 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001000 negative value, fallback to the default */
1001 char separator;
1002
1003 if (spec.field_width == 0)
1004 /* nothing to print */
1005 return buf;
1006
1007 if (ZERO_OR_NULL_PTR(addr))
1008 /* NULL pointer */
1009 return string(buf, end, NULL, spec);
1010
1011 switch (fmt[1]) {
1012 case 'C':
1013 separator = ':';
1014 break;
1015 case 'D':
1016 separator = '-';
1017 break;
1018 case 'N':
1019 separator = 0;
1020 break;
1021 default:
1022 separator = ' ';
1023 break;
1024 }
1025
1026 if (spec.field_width > 0)
1027 len = min_t(int, spec.field_width, 64);
1028
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001029 for (i = 0; i < len; ++i) {
1030 if (buf < end)
1031 *buf = hex_asc_hi(addr[i]);
1032 ++buf;
1033 if (buf < end)
1034 *buf = hex_asc_lo(addr[i]);
1035 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001036
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001037 if (separator && i != len - 1) {
1038 if (buf < end)
1039 *buf = separator;
1040 ++buf;
1041 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001042 }
1043
1044 return buf;
1045}
1046
1047static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001048char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1049 struct printf_spec spec, const char *fmt)
1050{
1051 const int CHUNKSZ = 32;
1052 int nr_bits = max_t(int, spec.field_width, 0);
1053 int i, chunksz;
1054 bool first = true;
1055
1056 /* reused to print numbers */
1057 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1058
1059 chunksz = nr_bits & (CHUNKSZ - 1);
1060 if (chunksz == 0)
1061 chunksz = CHUNKSZ;
1062
1063 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1064 for (; i >= 0; i -= CHUNKSZ) {
1065 u32 chunkmask, val;
1066 int word, bit;
1067
1068 chunkmask = ((1ULL << chunksz) - 1);
1069 word = i / BITS_PER_LONG;
1070 bit = i % BITS_PER_LONG;
1071 val = (bitmap[word] >> bit) & chunkmask;
1072
1073 if (!first) {
1074 if (buf < end)
1075 *buf = ',';
1076 buf++;
1077 }
1078 first = false;
1079
1080 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1081 buf = number(buf, end, val, spec);
1082
1083 chunksz = CHUNKSZ;
1084 }
1085 return buf;
1086}
1087
1088static noinline_for_stack
1089char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1090 struct printf_spec spec, const char *fmt)
1091{
1092 int nr_bits = max_t(int, spec.field_width, 0);
1093 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1094 int cur, rbot, rtop;
1095 bool first = true;
1096
Tejun Heodbc760b2015-02-13 14:36:53 -08001097 rbot = cur = find_first_bit(bitmap, nr_bits);
1098 while (cur < nr_bits) {
1099 rtop = cur;
1100 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1101 if (cur < nr_bits && cur <= rtop + 1)
1102 continue;
1103
1104 if (!first) {
1105 if (buf < end)
1106 *buf = ',';
1107 buf++;
1108 }
1109 first = false;
1110
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001111 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001112 if (rbot < rtop) {
1113 if (buf < end)
1114 *buf = '-';
1115 buf++;
1116
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001117 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001118 }
1119
1120 rbot = cur;
1121 }
1122 return buf;
1123}
1124
1125static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001126char *mac_address_string(char *buf, char *end, u8 *addr,
1127 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001128{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001129 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001130 char *p = mac_addr;
1131 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001132 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001133 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001134
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001135 switch (fmt[1]) {
1136 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001137 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001138 break;
1139
1140 case 'R':
1141 reversed = true;
1142 /* fall through */
1143
1144 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001145 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001146 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001147 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001148
1149 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001150 if (reversed)
1151 p = hex_byte_pack(p, addr[5 - i]);
1152 else
1153 p = hex_byte_pack(p, addr[i]);
1154
Joe Perches8a27f7c2009-08-17 12:29:44 +00001155 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001156 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001157 }
1158 *p = '\0';
1159
Petr Mladekd529ac42019-04-17 13:53:43 +02001160 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001161}
1162
Joe Perchescf3b4292010-05-24 14:33:16 -07001163static noinline_for_stack
1164char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001165{
Harvey Harrison689afa72008-10-28 16:04:44 -07001166 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001167 bool leading_zeros = (fmt[0] == 'i');
1168 int index;
1169 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001170
Joe Perches0159f242010-01-13 20:23:30 -08001171 switch (fmt[2]) {
1172 case 'h':
1173#ifdef __BIG_ENDIAN
1174 index = 0;
1175 step = 1;
1176#else
1177 index = 3;
1178 step = -1;
1179#endif
1180 break;
1181 case 'l':
1182 index = 3;
1183 step = -1;
1184 break;
1185 case 'n':
1186 case 'b':
1187 default:
1188 index = 0;
1189 step = 1;
1190 break;
1191 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001192 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001193 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001194 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001195 if (leading_zeros) {
1196 if (digits < 3)
1197 *p++ = '0';
1198 if (digits < 2)
1199 *p++ = '0';
1200 }
1201 /* reverse the digits in the quad */
1202 while (digits--)
1203 *p++ = temp[digits];
1204 if (i < 3)
1205 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001206 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001207 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001208 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001209
Joe Perches8a27f7c2009-08-17 12:29:44 +00001210 return p;
1211}
1212
Joe Perchescf3b4292010-05-24 14:33:16 -07001213static noinline_for_stack
1214char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001215{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001216 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001217 unsigned char zerolength[8];
1218 int longest = 1;
1219 int colonpos = -1;
1220 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001221 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001222 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001223 bool useIPv4;
1224 struct in6_addr in6;
1225
1226 memcpy(&in6, addr, sizeof(struct in6_addr));
1227
1228 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001229
1230 memset(zerolength, 0, sizeof(zerolength));
1231
1232 if (useIPv4)
1233 range = 6;
1234 else
1235 range = 8;
1236
1237 /* find position of longest 0 run */
1238 for (i = 0; i < range; i++) {
1239 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001240 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001241 break;
1242 zerolength[i]++;
1243 }
1244 }
1245 for (i = 0; i < range; i++) {
1246 if (zerolength[i] > longest) {
1247 longest = zerolength[i];
1248 colonpos = i;
1249 }
1250 }
Joe Perches29cf5192011-06-09 11:23:37 -07001251 if (longest == 1) /* don't compress a single 0 */
1252 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001253
1254 /* emit address */
1255 for (i = 0; i < range; i++) {
1256 if (i == colonpos) {
1257 if (needcolon || i == 0)
1258 *p++ = ':';
1259 *p++ = ':';
1260 needcolon = false;
1261 i += longest - 1;
1262 continue;
1263 }
1264 if (needcolon) {
1265 *p++ = ':';
1266 needcolon = false;
1267 }
1268 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001269 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001270 hi = word >> 8;
1271 lo = word & 0xff;
1272 if (hi) {
1273 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001274 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001275 else
1276 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001277 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001278 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001279 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001280 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001281 else
1282 *p++ = hex_asc_lo(lo);
1283 needcolon = true;
1284 }
1285
1286 if (useIPv4) {
1287 if (needcolon)
1288 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001289 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001290 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001291 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001292
Joe Perches8a27f7c2009-08-17 12:29:44 +00001293 return p;
1294}
1295
Joe Perchescf3b4292010-05-24 14:33:16 -07001296static noinline_for_stack
1297char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001298{
1299 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001300
Harvey Harrison689afa72008-10-28 16:04:44 -07001301 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001302 p = hex_byte_pack(p, *addr++);
1303 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001304 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001305 *p++ = ':';
1306 }
1307 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001308
Joe Perches8a27f7c2009-08-17 12:29:44 +00001309 return p;
1310}
1311
Joe Perchescf3b4292010-05-24 14:33:16 -07001312static noinline_for_stack
1313char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1314 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001315{
1316 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1317
1318 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001319 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001320 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001321 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001322
Petr Mladekd529ac42019-04-17 13:53:43 +02001323 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001324}
1325
Joe Perchescf3b4292010-05-24 14:33:16 -07001326static noinline_for_stack
1327char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1328 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001329{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001330 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001331
Joe Perches0159f242010-01-13 20:23:30 -08001332 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001333
Petr Mladekd529ac42019-04-17 13:53:43 +02001334 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001335}
1336
Joe Perchescf3b4292010-05-24 14:33:16 -07001337static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001338char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1339 struct printf_spec spec, const char *fmt)
1340{
1341 bool have_p = false, have_s = false, have_f = false, have_c = false;
1342 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1343 sizeof(":12345") + sizeof("/123456789") +
1344 sizeof("%1234567890")];
1345 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1346 const u8 *addr = (const u8 *) &sa->sin6_addr;
1347 char fmt6[2] = { fmt[0], '6' };
1348 u8 off = 0;
1349
1350 fmt++;
1351 while (isalpha(*++fmt)) {
1352 switch (*fmt) {
1353 case 'p':
1354 have_p = true;
1355 break;
1356 case 'f':
1357 have_f = true;
1358 break;
1359 case 's':
1360 have_s = true;
1361 break;
1362 case 'c':
1363 have_c = true;
1364 break;
1365 }
1366 }
1367
1368 if (have_p || have_s || have_f) {
1369 *p = '[';
1370 off = 1;
1371 }
1372
1373 if (fmt6[0] == 'I' && have_c)
1374 p = ip6_compressed_string(ip6_addr + off, addr);
1375 else
1376 p = ip6_string(ip6_addr + off, addr, fmt6);
1377
1378 if (have_p || have_s || have_f)
1379 *p++ = ']';
1380
1381 if (have_p) {
1382 *p++ = ':';
1383 p = number(p, pend, ntohs(sa->sin6_port), spec);
1384 }
1385 if (have_f) {
1386 *p++ = '/';
1387 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1388 IPV6_FLOWINFO_MASK), spec);
1389 }
1390 if (have_s) {
1391 *p++ = '%';
1392 p = number(p, pend, sa->sin6_scope_id, spec);
1393 }
1394 *p = '\0';
1395
Petr Mladekd529ac42019-04-17 13:53:43 +02001396 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001397}
1398
1399static noinline_for_stack
1400char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1401 struct printf_spec spec, const char *fmt)
1402{
1403 bool have_p = false;
1404 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1405 char *pend = ip4_addr + sizeof(ip4_addr);
1406 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1407 char fmt4[3] = { fmt[0], '4', 0 };
1408
1409 fmt++;
1410 while (isalpha(*++fmt)) {
1411 switch (*fmt) {
1412 case 'p':
1413 have_p = true;
1414 break;
1415 case 'h':
1416 case 'l':
1417 case 'n':
1418 case 'b':
1419 fmt4[2] = *fmt;
1420 break;
1421 }
1422 }
1423
1424 p = ip4_string(ip4_addr, addr, fmt4);
1425 if (have_p) {
1426 *p++ = ':';
1427 p = number(p, pend, ntohs(sa->sin_port), spec);
1428 }
1429 *p = '\0';
1430
Petr Mladekd529ac42019-04-17 13:53:43 +02001431 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001432}
1433
1434static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001435char *ip_addr_string(char *buf, char *end, const void *ptr,
1436 struct printf_spec spec, const char *fmt)
1437{
1438 switch (fmt[1]) {
1439 case '6':
1440 return ip6_addr_string(buf, end, ptr, spec, fmt);
1441 case '4':
1442 return ip4_addr_string(buf, end, ptr, spec, fmt);
1443 case 'S': {
1444 const union {
1445 struct sockaddr raw;
1446 struct sockaddr_in v4;
1447 struct sockaddr_in6 v6;
1448 } *sa = ptr;
1449
1450 switch (sa->raw.sa_family) {
1451 case AF_INET:
1452 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1453 case AF_INET6:
1454 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1455 default:
1456 return string_nocheck(buf, end, "(invalid address)", spec);
1457 }}
1458 }
1459
1460 return ptr_to_id(buf, end, ptr, spec);
1461}
1462
1463static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001464char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1465 const char *fmt)
1466{
1467 bool found = true;
1468 int count = 1;
1469 unsigned int flags = 0;
1470 int len;
1471
1472 if (spec.field_width == 0)
1473 return buf; /* nothing to print */
1474
1475 if (ZERO_OR_NULL_PTR(addr))
1476 return string(buf, end, NULL, spec); /* NULL pointer */
1477
1478
1479 do {
1480 switch (fmt[count++]) {
1481 case 'a':
1482 flags |= ESCAPE_ANY;
1483 break;
1484 case 'c':
1485 flags |= ESCAPE_SPECIAL;
1486 break;
1487 case 'h':
1488 flags |= ESCAPE_HEX;
1489 break;
1490 case 'n':
1491 flags |= ESCAPE_NULL;
1492 break;
1493 case 'o':
1494 flags |= ESCAPE_OCTAL;
1495 break;
1496 case 'p':
1497 flags |= ESCAPE_NP;
1498 break;
1499 case 's':
1500 flags |= ESCAPE_SPACE;
1501 break;
1502 default:
1503 found = false;
1504 break;
1505 }
1506 } while (found);
1507
1508 if (!flags)
1509 flags = ESCAPE_ANY_NP;
1510
1511 len = spec.field_width < 0 ? 1 : spec.field_width;
1512
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001513 /*
1514 * string_escape_mem() writes as many characters as it can to
1515 * the given buffer, and returns the total size of the output
1516 * had the buffer been big enough.
1517 */
1518 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001519
1520 return buf;
1521}
1522
1523static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001524char *uuid_string(char *buf, char *end, const u8 *addr,
1525 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001526{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001527 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001528 char *p = uuid;
1529 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001530 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001531 bool uc = false;
1532
1533 switch (*(++fmt)) {
1534 case 'L':
1535 uc = true; /* fall-through */
1536 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001537 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001538 break;
1539 case 'B':
1540 uc = true;
1541 break;
1542 }
1543
1544 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001545 if (uc)
1546 p = hex_byte_pack_upper(p, addr[index[i]]);
1547 else
1548 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001549 switch (i) {
1550 case 3:
1551 case 5:
1552 case 7:
1553 case 9:
1554 *p++ = '-';
1555 break;
1556 }
1557 }
1558
1559 *p = 0;
1560
Petr Mladekd529ac42019-04-17 13:53:43 +02001561 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001562}
1563
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001564static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001565char *netdev_bits(char *buf, char *end, const void *addr,
1566 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001567{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001568 unsigned long long num;
1569 int size;
1570
1571 switch (fmt[1]) {
1572 case 'F':
1573 num = *(const netdev_features_t *)addr;
1574 size = sizeof(netdev_features_t);
1575 break;
1576 default:
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001577 return ptr_to_id(buf, end, addr, spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001578 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001579
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001580 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001581}
1582
Joe Perchesaaf07622014-01-23 15:54:17 -08001583static noinline_for_stack
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001584char *address_val(char *buf, char *end, const void *addr, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001585{
1586 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001587 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001588
1589 switch (fmt[1]) {
1590 case 'd':
1591 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001592 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001593 break;
1594 case 'p':
1595 default:
1596 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001597 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001598 break;
1599 }
1600
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001601 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001602}
1603
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001604static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001605char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1606{
1607 int year = tm->tm_year + (r ? 0 : 1900);
1608 int mon = tm->tm_mon + (r ? 0 : 1);
1609
1610 buf = number(buf, end, year, default_dec04_spec);
1611 if (buf < end)
1612 *buf = '-';
1613 buf++;
1614
1615 buf = number(buf, end, mon, default_dec02_spec);
1616 if (buf < end)
1617 *buf = '-';
1618 buf++;
1619
1620 return number(buf, end, tm->tm_mday, default_dec02_spec);
1621}
1622
1623static noinline_for_stack
1624char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1625{
1626 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1627 if (buf < end)
1628 *buf = ':';
1629 buf++;
1630
1631 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1632 if (buf < end)
1633 *buf = ':';
1634 buf++;
1635
1636 return number(buf, end, tm->tm_sec, default_dec02_spec);
1637}
1638
1639static noinline_for_stack
1640char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt)
1641{
1642 bool have_t = true, have_d = true;
1643 bool raw = false;
1644 int count = 2;
1645
1646 switch (fmt[count]) {
1647 case 'd':
1648 have_t = false;
1649 count++;
1650 break;
1651 case 't':
1652 have_d = false;
1653 count++;
1654 break;
1655 }
1656
1657 raw = fmt[count] == 'r';
1658
1659 if (have_d)
1660 buf = date_str(buf, end, tm, raw);
1661 if (have_d && have_t) {
1662 /* Respect ISO 8601 */
1663 if (buf < end)
1664 *buf = 'T';
1665 buf++;
1666 }
1667 if (have_t)
1668 buf = time_str(buf, end, tm, raw);
1669
1670 return buf;
1671}
1672
1673static noinline_for_stack
1674char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1675 const char *fmt)
1676{
1677 switch (fmt[1]) {
1678 case 'R':
1679 return rtc_str(buf, end, (const struct rtc_time *)ptr, fmt);
1680 default:
1681 return ptr_to_id(buf, end, ptr, spec);
1682 }
1683}
1684
1685static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001686char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1687 const char *fmt)
1688{
1689 if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1690 return string(buf, end, NULL, spec);
1691
1692 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001693 case 'n':
1694 default:
1695#ifdef CONFIG_COMMON_CLK
1696 return string(buf, end, __clk_get_name(clk), spec);
1697#else
Geert Uytterhoevenec12bc22018-10-11 10:42:48 +02001698 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001699#endif
1700 }
1701}
1702
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001703static
1704char *format_flags(char *buf, char *end, unsigned long flags,
1705 const struct trace_print_flags *names)
1706{
1707 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001708
1709 for ( ; flags && names->name; names++) {
1710 mask = names->mask;
1711 if ((flags & mask) != mask)
1712 continue;
1713
Andy Shevchenkoabd4fe622018-02-16 23:07:05 +02001714 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001715
1716 flags &= ~mask;
1717 if (flags) {
1718 if (buf < end)
1719 *buf = '|';
1720 buf++;
1721 }
1722 }
1723
1724 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001725 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001726
1727 return buf;
1728}
1729
1730static noinline_for_stack
1731char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1732{
1733 unsigned long flags;
1734 const struct trace_print_flags *names;
1735
1736 switch (fmt[1]) {
1737 case 'p':
1738 flags = *(unsigned long *)flags_ptr;
1739 /* Remove zone id */
1740 flags &= (1UL << NR_PAGEFLAGS) - 1;
1741 names = pageflag_names;
1742 break;
1743 case 'v':
1744 flags = *(unsigned long *)flags_ptr;
1745 names = vmaflag_names;
1746 break;
1747 case 'g':
1748 flags = *(gfp_t *)flags_ptr;
1749 names = gfpflag_names;
1750 break;
1751 default:
1752 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1753 return buf;
1754 }
1755
1756 return format_flags(buf, end, flags, names);
1757}
1758
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001759static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1760{
1761 for ( ; np && depth; depth--)
1762 np = np->parent;
1763
1764 return kbasename(np->full_name);
1765}
1766
1767static noinline_for_stack
1768char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1769{
1770 int depth;
1771 const struct device_node *parent = np->parent;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001772
1773 /* special case for root node */
1774 if (!parent)
Petr Mladekd529ac42019-04-17 13:53:43 +02001775 return string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001776
1777 for (depth = 0; parent->parent; depth++)
1778 parent = parent->parent;
1779
1780 for ( ; depth >= 0; depth--) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001781 buf = string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001782 buf = string(buf, end, device_node_name_for_depth(np, depth),
Andy Shevchenkoabd4fe622018-02-16 23:07:05 +02001783 default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001784 }
1785 return buf;
1786}
1787
1788static noinline_for_stack
1789char *device_node_string(char *buf, char *end, struct device_node *dn,
1790 struct printf_spec spec, const char *fmt)
1791{
1792 char tbuf[sizeof("xxxx") + 1];
1793 const char *p;
1794 int ret;
1795 char *buf_start = buf;
1796 struct property *prop;
1797 bool has_mult, pass;
1798 static const struct printf_spec num_spec = {
1799 .flags = SMALL,
1800 .field_width = -1,
1801 .precision = -1,
1802 .base = 10,
1803 };
1804
1805 struct printf_spec str_spec = spec;
1806 str_spec.field_width = -1;
1807
1808 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekd529ac42019-04-17 13:53:43 +02001809 return string_nocheck(buf, end, "(!OF)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001810
1811 if ((unsigned long)dn < PAGE_SIZE)
Petr Mladekd529ac42019-04-17 13:53:43 +02001812 return string_nocheck(buf, end, "(null)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001813
1814 /* simple case without anything any more format specifiers */
1815 fmt++;
1816 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1817 fmt = "f";
1818
1819 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001820 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001821 if (pass) {
1822 if (buf < end)
1823 *buf = ':';
1824 buf++;
1825 }
1826
1827 switch (*fmt) {
1828 case 'f': /* full_name */
1829 buf = device_node_gen_full_name(dn, buf, end);
1830 break;
1831 case 'n': /* name */
Rob Herring6d0a70a2018-08-27 08:13:56 -05001832 p = kbasename(of_node_full_name(dn));
1833 precision = str_spec.precision;
1834 str_spec.precision = strchrnul(p, '@') - p;
1835 buf = string(buf, end, p, str_spec);
1836 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001837 break;
1838 case 'p': /* phandle */
1839 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1840 break;
1841 case 'P': /* path-spec */
1842 p = kbasename(of_node_full_name(dn));
1843 if (!p[1])
1844 p = "/";
1845 buf = string(buf, end, p, str_spec);
1846 break;
1847 case 'F': /* flags */
1848 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1849 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1850 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1851 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1852 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02001853 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001854 break;
1855 case 'c': /* major compatible string */
1856 ret = of_property_read_string(dn, "compatible", &p);
1857 if (!ret)
1858 buf = string(buf, end, p, str_spec);
1859 break;
1860 case 'C': /* full compatible string */
1861 has_mult = false;
1862 of_property_for_each_string(dn, "compatible", prop, p) {
1863 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02001864 buf = string_nocheck(buf, end, ",", str_spec);
1865 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001866 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02001867 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001868
1869 has_mult = true;
1870 }
1871 break;
1872 default:
1873 break;
1874 }
1875 }
1876
1877 return widen_string(buf, buf - buf_start, end, spec);
1878}
1879
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001880/*
1881 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1882 * by an extra set of alphanumeric characters that are extended format
1883 * specifiers.
1884 *
Joe Perches0b523762017-05-08 15:55:36 -07001885 * Please update scripts/checkpatch.pl when adding/removing conversion
1886 * characters. (Search for "check for vsprintf extension").
1887 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001888 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001889 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09001890 * - 'S' For symbolic direct pointers (or function descriptors) with offset
1891 * - 's' For symbolic direct pointers (or function descriptors) without offset
1892 * - 'F' Same as 'S'
1893 * - 'f' Same as 's'
Joe Perchesb0d33c22012-12-12 10:18:50 -08001894 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001895 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001896 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1897 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08001898 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1899 * width which must be explicitly specified either as part of the
1900 * format string '%32b[l]' or through '%*b[l]', [l] selects
1901 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001902 * - 'M' For a 6-byte MAC address, it prints the address in the
1903 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001904 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001905 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001906 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001907 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001908 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1909 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1910 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02001911 * [S][pfs]
1912 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1913 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00001914 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1915 * IPv6 omits the colons (01020304...0f)
1916 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02001917 * [S][pfs]
1918 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1919 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1920 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1921 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001922 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07001923 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1924 * of the following flags (see string_escape_mem() for the
1925 * details):
1926 * a - ESCAPE_ANY
1927 * c - ESCAPE_SPECIAL
1928 * h - ESCAPE_HEX
1929 * n - ESCAPE_NULL
1930 * o - ESCAPE_OCTAL
1931 * p - ESCAPE_NP
1932 * s - ESCAPE_SPACE
1933 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08001934 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1935 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1936 * Options for %pU are:
1937 * b big endian lower case hex (default)
1938 * B big endian UPPER case hex
1939 * l little endian lower case hex
1940 * L little endian UPPER case hex
1941 * big endian output byte order is:
1942 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1943 * little endian output byte order is:
1944 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001945 * - 'V' For a struct va_format which contains a format string * and va_list *,
1946 * call vsnprintf(->format, *->va_list).
1947 * Implements a "recursive vsnprintf".
1948 * Do not use this feature without some mechanism to verify the
1949 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001950 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001951 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001952 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1953 * a certain separator (' ' by default):
1954 * C colon
1955 * D dash
1956 * N no separator
1957 * The maximum supported length is 64 bytes of the input. Consider
1958 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08001959 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1960 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08001961 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1962 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04001963 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001964 * - 't[R][dt][r]' For time and date as represented:
1965 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001966 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1967 * (legacy clock framework) of the clock
1968 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1969 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001970 * - 'G' For flags to be printed as a collection of symbolic strings that would
1971 * construct the specific value. Supported flags given by option:
1972 * p page flags (see struct page) given as pointer to unsigned long
1973 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1974 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02001975 * - 'OF[fnpPcCF]' For a device tree object
1976 * Without any optional arguments prints the full_name
1977 * f device node full_name
1978 * n device node name
1979 * p device node phandle
1980 * P device node path spec (name + @unit)
1981 * F device node flags
1982 * c major compatible string
1983 * C full compatible string
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11001984 * - 'x' For printing the address. Equivalent to "%lx".
1985 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11001986 * ** When making changes please also update:
1987 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08001988 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11001989 * Note: The default behaviour (unadorned %p) is to hash the address,
1990 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001991 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001992static noinline_for_stack
1993char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1994 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001995{
Rasmus Villemoes80c9eb42015-11-06 16:30:26 -08001996 const int default_width = 2 * sizeof(void *);
Grant Likely725fe002012-05-31 16:26:08 -07001997
Adam Borowski3a129cc2018-02-04 18:45:21 +01001998 if (!ptr && *fmt != 'K' && *fmt != 'x') {
Joe Perches5e057982010-10-26 14:22:50 -07001999 /*
2000 * Print (null) with the same width as a pointer so it makes
2001 * tabular output look nice.
2002 */
2003 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07002004 spec.field_width = default_width;
Petr Mladekd529ac42019-04-17 13:53:43 +02002005 return string_nocheck(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07002006 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08002007
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002008 switch (*fmt) {
2009 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02002010 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002011 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002012 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002013 ptr = dereference_symbol_descriptor(ptr);
2014 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002015 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002016 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002017 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002018 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002019 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002020 case 'h':
2021 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002022 case 'b':
2023 switch (fmt[1]) {
2024 case 'l':
2025 return bitmap_list_string(buf, end, ptr, spec, fmt);
2026 default:
2027 return bitmap_string(buf, end, ptr, spec, fmt);
2028 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002029 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2030 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002031 /* [mM]F (FDDI) */
2032 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002033 return mac_address_string(buf, end, ptr, spec, fmt);
2034 case 'I': /* Formatted IP supported
2035 * 4: 1.2.3.4
2036 * 6: 0001:0203:...:0708
2037 * 6c: 1::708 or 1::1.2.3.4
2038 */
2039 case 'i': /* Contiguous:
2040 * 4: 001.002.003.004
2041 * 6: 000102...0f
2042 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002043 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002044 case 'E':
2045 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002046 case 'U':
2047 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002048 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00002049 {
2050 va_list va;
2051
2052 va_copy(va, *((struct va_format *)ptr)->va);
2053 buf += vsnprintf(buf, end > buf ? end - buf : 0,
2054 ((struct va_format *)ptr)->fmt, va);
2055 va_end(va);
2056 return buf;
2057 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002058 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002059 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002060 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002061 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002062 case 'a':
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08002063 return address_val(buf, end, ptr, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002064 case 'd':
2065 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002066 case 't':
2067 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002068 case 'C':
2069 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002070 case 'D':
2071 return dentry_name(buf, end,
2072 ((const struct file *)ptr)->f_path.dentry,
2073 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002074#ifdef CONFIG_BLOCK
2075 case 'g':
2076 return bdev_name(buf, end, ptr, spec, fmt);
2077#endif
2078
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002079 case 'G':
2080 return flags_string(buf, end, ptr, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002081 case 'O':
2082 switch (fmt[1]) {
2083 case 'F':
2084 return device_node_string(buf, end, ptr, spec, fmt + 1);
2085 }
Bart Van Assche554ec502018-08-06 15:34:21 -07002086 break;
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002087 case 'x':
2088 return pointer_string(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002089 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002090
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002091 /* default is to _not_ leak addresses, hash before printing */
2092 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002093}
2094
2095/*
2096 * Helper function to decode printf style format.
2097 * Each call decode a token from the format and return the
2098 * number of characters read (or likely the delta where it wants
2099 * to go on the next call).
2100 * The decoded token is returned through the parameters
2101 *
2102 * 'h', 'l', or 'L' for integer fields
2103 * 'z' support added 23/7/1999 S.H.
2104 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002105 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002106 * 't' added for ptrdiff_t
2107 *
2108 * @fmt: the format string
2109 * @type of the token returned
2110 * @flags: various flags such as +, -, # tokens..
2111 * @field_width: overwritten width
2112 * @base: base of the number (octal, hex, ...)
2113 * @precision: precision of a number
2114 * @qualifier: qualifier of a number (long, size_t, ...)
2115 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002116static noinline_for_stack
2117int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002118{
2119 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002120 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002121
2122 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002123 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002124 if (spec->field_width < 0) {
2125 spec->field_width = -spec->field_width;
2126 spec->flags |= LEFT;
2127 }
2128 spec->type = FORMAT_TYPE_NONE;
2129 goto precision;
2130 }
2131
2132 /* we finished early by reading the precision */
2133 if (spec->type == FORMAT_TYPE_PRECISION) {
2134 if (spec->precision < 0)
2135 spec->precision = 0;
2136
2137 spec->type = FORMAT_TYPE_NONE;
2138 goto qualifier;
2139 }
2140
2141 /* By default */
2142 spec->type = FORMAT_TYPE_NONE;
2143
2144 for (; *fmt ; ++fmt) {
2145 if (*fmt == '%')
2146 break;
2147 }
2148
2149 /* Return the current non-format string */
2150 if (fmt != start || !*fmt)
2151 return fmt - start;
2152
2153 /* Process flags */
2154 spec->flags = 0;
2155
2156 while (1) { /* this also skips first '%' */
2157 bool found = true;
2158
2159 ++fmt;
2160
2161 switch (*fmt) {
2162 case '-': spec->flags |= LEFT; break;
2163 case '+': spec->flags |= PLUS; break;
2164 case ' ': spec->flags |= SPACE; break;
2165 case '#': spec->flags |= SPECIAL; break;
2166 case '0': spec->flags |= ZEROPAD; break;
2167 default: found = false;
2168 }
2169
2170 if (!found)
2171 break;
2172 }
2173
2174 /* get field width */
2175 spec->field_width = -1;
2176
2177 if (isdigit(*fmt))
2178 spec->field_width = skip_atoi(&fmt);
2179 else if (*fmt == '*') {
2180 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002181 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002182 return ++fmt - start;
2183 }
2184
2185precision:
2186 /* get the precision */
2187 spec->precision = -1;
2188 if (*fmt == '.') {
2189 ++fmt;
2190 if (isdigit(*fmt)) {
2191 spec->precision = skip_atoi(&fmt);
2192 if (spec->precision < 0)
2193 spec->precision = 0;
2194 } else if (*fmt == '*') {
2195 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002196 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002197 return ++fmt - start;
2198 }
2199 }
2200
2201qualifier:
2202 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002203 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002204 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002205 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002206 qualifier = *fmt++;
2207 if (unlikely(qualifier == *fmt)) {
2208 if (qualifier == 'l') {
2209 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002210 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002211 } else if (qualifier == 'h') {
2212 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002213 ++fmt;
2214 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002215 }
2216 }
2217
2218 /* default base */
2219 spec->base = 10;
2220 switch (*fmt) {
2221 case 'c':
2222 spec->type = FORMAT_TYPE_CHAR;
2223 return ++fmt - start;
2224
2225 case 's':
2226 spec->type = FORMAT_TYPE_STR;
2227 return ++fmt - start;
2228
2229 case 'p':
2230 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002231 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002232
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002233 case '%':
2234 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2235 return ++fmt - start;
2236
2237 /* integer number formats - set up the flags and "break" */
2238 case 'o':
2239 spec->base = 8;
2240 break;
2241
2242 case 'x':
2243 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002244 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002245
2246 case 'X':
2247 spec->base = 16;
2248 break;
2249
2250 case 'd':
2251 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002252 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002253 case 'u':
2254 break;
2255
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002256 case 'n':
2257 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002258 * Since %n poses a greater security risk than
2259 * utility, treat it as any other invalid or
2260 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002261 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002262 /* Fall-through */
2263
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002264 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002265 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002266 spec->type = FORMAT_TYPE_INVALID;
2267 return fmt - start;
2268 }
2269
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002270 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002271 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002272 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002273 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2274 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002275 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002276 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002277 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002278 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002279 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002280 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2281 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002282 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002283 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2284 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002285 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002286 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2287 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002288 }
2289
2290 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002291}
2292
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002293static void
2294set_field_width(struct printf_spec *spec, int width)
2295{
2296 spec->field_width = width;
2297 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2298 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2299 }
2300}
2301
2302static void
2303set_precision(struct printf_spec *spec, int prec)
2304{
2305 spec->precision = prec;
2306 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2307 spec->precision = clamp(prec, 0, PRECISION_MAX);
2308 }
2309}
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311/**
2312 * vsnprintf - Format a string and place it in a buffer
2313 * @buf: The buffer to place the result into
2314 * @size: The size of the buffer, including the trailing null space
2315 * @fmt: The format string to use
2316 * @args: Arguments for the format string
2317 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002318 * This function generally follows C99 vsnprintf, but has some
2319 * extensions and a few limitations:
2320 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002321 * - ``%n`` is unsupported
2322 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002323 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002324 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002325 * extensive description.
2326 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002327 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002328 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 * The return value is the number of characters which would
2330 * be generated for the given input, excluding the trailing
2331 * '\0', as per ISO C99. If you want to have the exact
2332 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002333 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 * return is greater than or equal to @size, the resulting
2335 * string is truncated.
2336 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002337 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 */
2339int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002342 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002343 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002345 /* Reject out-of-range values early. Large positive sizes are
2346 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002347 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002351 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002353 /* Make sure end is always >= buf */
2354 if (end < buf) {
2355 end = ((void *)-1);
2356 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002359 while (*fmt) {
2360 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002361 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002362
2363 fmt += read;
2364
2365 switch (spec.type) {
2366 case FORMAT_TYPE_NONE: {
2367 int copy = read;
2368 if (str < end) {
2369 if (copy > end - str)
2370 copy = end - str;
2371 memcpy(str, old_fmt, copy);
2372 }
2373 str += read;
2374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
Vegard Nossumed681a92009-03-14 12:08:50 +01002377 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002378 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002381 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002382 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
André Goddard Rosad4be1512009-12-14 18:00:59 -08002385 case FORMAT_TYPE_CHAR: {
2386 char c;
2387
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002388 if (!(spec.flags & LEFT)) {
2389 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002390 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 *str = ' ';
2392 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002395 }
2396 c = (unsigned char) va_arg(args, int);
2397 if (str < end)
2398 *str = c;
2399 ++str;
2400 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002401 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002402 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002404 }
2405 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002408 case FORMAT_TYPE_STR:
2409 str = string(str, end, va_arg(args, char *), spec);
2410 break;
2411
2412 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002413 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002414 spec);
2415 while (isalnum(*fmt))
2416 fmt++;
2417 break;
2418
2419 case FORMAT_TYPE_PERCENT_CHAR:
2420 if (str < end)
2421 *str = '%';
2422 ++str;
2423 break;
2424
2425 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002426 /*
2427 * Presumably the arguments passed gcc's type
2428 * checking, but there is no safe or sane way
2429 * for us to continue parsing the format and
2430 * fetching from the va_list; the remaining
2431 * specifiers and arguments would be out of
2432 * sync.
2433 */
2434 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002435
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002436 default:
2437 switch (spec.type) {
2438 case FORMAT_TYPE_LONG_LONG:
2439 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002441 case FORMAT_TYPE_ULONG:
2442 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002444 case FORMAT_TYPE_LONG:
2445 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002447 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002448 if (spec.flags & SIGN)
2449 num = va_arg(args, ssize_t);
2450 else
2451 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002452 break;
2453 case FORMAT_TYPE_PTRDIFF:
2454 num = va_arg(args, ptrdiff_t);
2455 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002456 case FORMAT_TYPE_UBYTE:
2457 num = (unsigned char) va_arg(args, int);
2458 break;
2459 case FORMAT_TYPE_BYTE:
2460 num = (signed char) va_arg(args, int);
2461 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002462 case FORMAT_TYPE_USHORT:
2463 num = (unsigned short) va_arg(args, int);
2464 break;
2465 case FORMAT_TYPE_SHORT:
2466 num = (short) va_arg(args, int);
2467 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002468 case FORMAT_TYPE_INT:
2469 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002472 num = va_arg(args, unsigned int);
2473 }
2474
2475 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002478
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002479out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002480 if (size > 0) {
2481 if (str < end)
2482 *str = '\0';
2483 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002484 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002485 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002486
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002487 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491EXPORT_SYMBOL(vsnprintf);
2492
2493/**
2494 * vscnprintf - Format a string and place it in a buffer
2495 * @buf: The buffer to place the result into
2496 * @size: The size of the buffer, including the trailing null space
2497 * @fmt: The format string to use
2498 * @args: Arguments for the format string
2499 *
2500 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002501 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 * returns 0.
2503 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002504 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002505 *
2506 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 */
2508int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2509{
2510 int i;
2511
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002512 i = vsnprintf(buf, size, fmt, args);
2513
Anton Arapovb921c692011-01-12 16:59:49 -08002514 if (likely(i < size))
2515 return i;
2516 if (size != 0)
2517 return size - 1;
2518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520EXPORT_SYMBOL(vscnprintf);
2521
2522/**
2523 * snprintf - Format a string and place it in a buffer
2524 * @buf: The buffer to place the result into
2525 * @size: The size of the buffer, including the trailing null space
2526 * @fmt: The format string to use
2527 * @...: Arguments for the format string
2528 *
2529 * The return value is the number of characters which would be
2530 * generated for the given input, excluding the trailing null,
2531 * as per ISO C99. If the return is greater than or equal to
2532 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002533 *
2534 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002536int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
2538 va_list args;
2539 int i;
2540
2541 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002542 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return i;
2546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547EXPORT_SYMBOL(snprintf);
2548
2549/**
2550 * scnprintf - Format a string and place it in a buffer
2551 * @buf: The buffer to place the result into
2552 * @size: The size of the buffer, including the trailing null space
2553 * @fmt: The format string to use
2554 * @...: Arguments for the format string
2555 *
2556 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002557 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 */
2559
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002560int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 va_list args;
2563 int i;
2564
2565 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002566 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002568
Anton Arapovb921c692011-01-12 16:59:49 -08002569 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571EXPORT_SYMBOL(scnprintf);
2572
2573/**
2574 * vsprintf - Format a string and place it in a buffer
2575 * @buf: The buffer to place the result into
2576 * @fmt: The format string to use
2577 * @args: Arguments for the format string
2578 *
2579 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002580 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 * buffer overflows.
2582 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002583 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002584 *
2585 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 */
2587int vsprintf(char *buf, const char *fmt, va_list args)
2588{
2589 return vsnprintf(buf, INT_MAX, fmt, args);
2590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591EXPORT_SYMBOL(vsprintf);
2592
2593/**
2594 * sprintf - Format a string and place it in a buffer
2595 * @buf: The buffer to place the result into
2596 * @fmt: The format string to use
2597 * @...: Arguments for the format string
2598 *
2599 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002600 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002602 *
2603 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002605int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 va_list args;
2608 int i;
2609
2610 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002611 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return i;
2615}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616EXPORT_SYMBOL(sprintf);
2617
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002618#ifdef CONFIG_BINARY_PRINTF
2619/*
2620 * bprintf service:
2621 * vbin_printf() - VA arguments to binary data
2622 * bstr_printf() - Binary data to text string
2623 */
2624
2625/**
2626 * vbin_printf - Parse a format string and place args' binary value in a buffer
2627 * @bin_buf: The buffer to place args' binary value
2628 * @size: The size of the buffer(by words(32bits), not characters)
2629 * @fmt: The format string to use
2630 * @args: Arguments for the format string
2631 *
2632 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002633 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002634 *
2635 * The return value is the number of words(32bits) which would be generated for
2636 * the given input.
2637 *
2638 * NOTE:
2639 * If the return value is greater than @size, the resulting bin_buf is NOT
2640 * valid for bstr_printf().
2641 */
2642int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2643{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002644 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002645 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002646 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002647
2648 str = (char *)bin_buf;
2649 end = (char *)(bin_buf + size);
2650
2651#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002652({ \
2653 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002654 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002655 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002656 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002657 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002658 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002659 *(u32 *)str = *(u32 *)&val8; \
2660 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002661 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002662 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002663 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002664 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002665 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002666 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002667 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002668 *(typeof(type) *)str = (type)(long)val4; \
2669 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002670 } \
2671 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002672 value; \
2673})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002674
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002675 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002676 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002677
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002678 fmt += read;
2679
2680 switch (spec.type) {
2681 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002682 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002683 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002684 case FORMAT_TYPE_INVALID:
2685 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002686
Vegard Nossumed681a92009-03-14 12:08:50 +01002687 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002688 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002689 width = (int)save_arg(int);
2690 /* Pointers may require the width */
2691 if (*fmt == 'p')
2692 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002693 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002694
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002695 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002696 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002697 break;
2698
2699 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002700 const char *save_str = va_arg(args, char *);
2701 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002702
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002703 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
2704 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08002705 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08002706 len = strlen(save_str) + 1;
2707 if (str + len < end)
2708 memcpy(str, save_str, len);
2709 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002710 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002711 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002712
2713 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002714 /* Dereferenced pointers must be done now */
2715 switch (*fmt) {
2716 /* Dereference of functions is still OK */
2717 case 'S':
2718 case 's':
2719 case 'F':
2720 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002721 case 'x':
2722 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002723 save_arg(void *);
2724 break;
2725 default:
2726 if (!isalnum(*fmt)) {
2727 save_arg(void *);
2728 break;
2729 }
2730 str = pointer(fmt, str, end, va_arg(args, void *),
2731 spec);
2732 if (str + 1 < end)
2733 *str++ = '\0';
2734 else
2735 end[-1] = '\0'; /* Must be nul terminated */
2736 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002737 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002738 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002739 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002740 break;
2741
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002742 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002743 switch (spec.type) {
2744
2745 case FORMAT_TYPE_LONG_LONG:
2746 save_arg(long long);
2747 break;
2748 case FORMAT_TYPE_ULONG:
2749 case FORMAT_TYPE_LONG:
2750 save_arg(unsigned long);
2751 break;
2752 case FORMAT_TYPE_SIZE_T:
2753 save_arg(size_t);
2754 break;
2755 case FORMAT_TYPE_PTRDIFF:
2756 save_arg(ptrdiff_t);
2757 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002758 case FORMAT_TYPE_UBYTE:
2759 case FORMAT_TYPE_BYTE:
2760 save_arg(char);
2761 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002762 case FORMAT_TYPE_USHORT:
2763 case FORMAT_TYPE_SHORT:
2764 save_arg(short);
2765 break;
2766 default:
2767 save_arg(int);
2768 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002769 }
2770 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002771
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002772out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002773 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002774#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002775}
2776EXPORT_SYMBOL_GPL(vbin_printf);
2777
2778/**
2779 * bstr_printf - Format a string from binary arguments and place it in a buffer
2780 * @buf: The buffer to place the result into
2781 * @size: The size of the buffer, including the trailing null space
2782 * @fmt: The format string to use
2783 * @bin_buf: Binary arguments for the format string
2784 *
2785 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2786 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2787 * a binary buffer that generated by vbin_printf.
2788 *
2789 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002790 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002791 *
2792 * The return value is the number of characters which would
2793 * be generated for the given input, excluding the trailing
2794 * '\0', as per ISO C99. If you want to have the exact
2795 * number of characters written into @buf as return value
2796 * (not including the trailing '\0'), use vscnprintf(). If the
2797 * return is greater than or equal to @size, the resulting
2798 * string is truncated.
2799 */
2800int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2801{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002802 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002803 char *str, *end;
2804 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002805
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002806 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002807 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002808
2809 str = buf;
2810 end = buf + size;
2811
2812#define get_arg(type) \
2813({ \
2814 typeof(type) value; \
2815 if (sizeof(type) == 8) { \
2816 args = PTR_ALIGN(args, sizeof(u32)); \
2817 *(u32 *)&value = *(u32 *)args; \
2818 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2819 } else { \
2820 args = PTR_ALIGN(args, sizeof(type)); \
2821 value = *(typeof(type) *)args; \
2822 } \
2823 args += sizeof(type); \
2824 value; \
2825})
2826
2827 /* Make sure end is always >= buf */
2828 if (end < buf) {
2829 end = ((void *)-1);
2830 size = end - buf;
2831 }
2832
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002833 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002834 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002835 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002836
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002837 fmt += read;
2838
2839 switch (spec.type) {
2840 case FORMAT_TYPE_NONE: {
2841 int copy = read;
2842 if (str < end) {
2843 if (copy > end - str)
2844 copy = end - str;
2845 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002846 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002847 str += read;
2848 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002849 }
2850
Vegard Nossumed681a92009-03-14 12:08:50 +01002851 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002852 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002853 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002854
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002855 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002856 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002857 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002858
André Goddard Rosad4be1512009-12-14 18:00:59 -08002859 case FORMAT_TYPE_CHAR: {
2860 char c;
2861
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002862 if (!(spec.flags & LEFT)) {
2863 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002864 if (str < end)
2865 *str = ' ';
2866 ++str;
2867 }
2868 }
2869 c = (unsigned char) get_arg(char);
2870 if (str < end)
2871 *str = c;
2872 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002873 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002874 if (str < end)
2875 *str = ' ';
2876 ++str;
2877 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002878 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002879 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002880
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002881 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002882 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002883 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002884 str = string(str, end, (char *)str_arg, spec);
2885 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002886 }
2887
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002888 case FORMAT_TYPE_PTR: {
2889 bool process = false;
2890 int copy, len;
2891 /* Non function dereferences were already done */
2892 switch (*fmt) {
2893 case 'S':
2894 case 's':
2895 case 'F':
2896 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002897 case 'x':
2898 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002899 process = true;
2900 break;
2901 default:
2902 if (!isalnum(*fmt)) {
2903 process = true;
2904 break;
2905 }
2906 /* Pointer dereference was already processed */
2907 if (str < end) {
2908 len = copy = strlen(args);
2909 if (copy > end - str)
2910 copy = end - str;
2911 memcpy(str, args, copy);
2912 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04002913 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002914 }
2915 }
2916 if (process)
2917 str = pointer(fmt, str, end, get_arg(void *), spec);
2918
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002919 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002920 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002921 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002922 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002923
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002924 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002925 if (str < end)
2926 *str = '%';
2927 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002928 break;
2929
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002930 case FORMAT_TYPE_INVALID:
2931 goto out;
2932
André Goddard Rosad4be1512009-12-14 18:00:59 -08002933 default: {
2934 unsigned long long num;
2935
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002936 switch (spec.type) {
2937
2938 case FORMAT_TYPE_LONG_LONG:
2939 num = get_arg(long long);
2940 break;
2941 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002942 case FORMAT_TYPE_LONG:
2943 num = get_arg(unsigned long);
2944 break;
2945 case FORMAT_TYPE_SIZE_T:
2946 num = get_arg(size_t);
2947 break;
2948 case FORMAT_TYPE_PTRDIFF:
2949 num = get_arg(ptrdiff_t);
2950 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002951 case FORMAT_TYPE_UBYTE:
2952 num = get_arg(unsigned char);
2953 break;
2954 case FORMAT_TYPE_BYTE:
2955 num = get_arg(signed char);
2956 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002957 case FORMAT_TYPE_USHORT:
2958 num = get_arg(unsigned short);
2959 break;
2960 case FORMAT_TYPE_SHORT:
2961 num = get_arg(short);
2962 break;
2963 case FORMAT_TYPE_UINT:
2964 num = get_arg(unsigned int);
2965 break;
2966 default:
2967 num = get_arg(int);
2968 }
2969
2970 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08002971 } /* default: */
2972 } /* switch(spec.type) */
2973 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002974
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002975out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002976 if (size > 0) {
2977 if (str < end)
2978 *str = '\0';
2979 else
2980 end[-1] = '\0';
2981 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002982
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002983#undef get_arg
2984
2985 /* the trailing null byte doesn't count towards the total */
2986 return str - buf;
2987}
2988EXPORT_SYMBOL_GPL(bstr_printf);
2989
2990/**
2991 * bprintf - Parse a format string and place args' binary value in a buffer
2992 * @bin_buf: The buffer to place args' binary value
2993 * @size: The size of the buffer(by words(32bits), not characters)
2994 * @fmt: The format string to use
2995 * @...: Arguments for the format string
2996 *
2997 * The function returns the number of words(u32) written
2998 * into @bin_buf.
2999 */
3000int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3001{
3002 va_list args;
3003 int ret;
3004
3005 va_start(args, fmt);
3006 ret = vbin_printf(bin_buf, size, fmt, args);
3007 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003008
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003009 return ret;
3010}
3011EXPORT_SYMBOL_GPL(bprintf);
3012
3013#endif /* CONFIG_BINARY_PRINTF */
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015/**
3016 * vsscanf - Unformat a buffer into a list of arguments
3017 * @buf: input buffer
3018 * @fmt: format of buffer
3019 * @args: arguments
3020 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003021int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 const char *str = buf;
3024 char *next;
3025 char digit;
3026 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003027 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003028 unsigned int base;
3029 union {
3030 long long s;
3031 unsigned long long u;
3032 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003033 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003034 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Jan Beulichda990752012-10-04 17:13:24 -07003036 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 /* skip any white space in format */
3038 /* white space in format matchs any amount of
3039 * white space, including none, in the input.
3040 */
3041 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003042 fmt = skip_spaces(++fmt);
3043 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
3045
3046 /* anything that is not a conversion must match exactly */
3047 if (*fmt != '%' && *fmt) {
3048 if (*fmt++ != *str++)
3049 break;
3050 continue;
3051 }
3052
3053 if (!*fmt)
3054 break;
3055 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 /* skip this conversion.
3058 * advance both strings to next white space
3059 */
3060 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003061 if (!*str)
3062 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003063 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3064 /* '%*[' not yet supported, invalid format */
3065 if (*fmt == '[')
3066 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 while (!isspace(*str) && *str)
3070 str++;
3071 continue;
3072 }
3073
3074 /* get field width */
3075 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003076 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003078 if (field_width <= 0)
3079 break;
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 /* get conversion qualifier */
3083 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003084 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003085 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 qualifier = *fmt++;
3087 if (unlikely(qualifier == *fmt)) {
3088 if (qualifier == 'h') {
3089 qualifier = 'H';
3090 fmt++;
3091 } else if (qualifier == 'l') {
3092 qualifier = 'L';
3093 fmt++;
3094 }
3095 }
3096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Jan Beulichda990752012-10-04 17:13:24 -07003098 if (!*fmt)
3099 break;
3100
3101 if (*fmt == 'n') {
3102 /* return number of characters read so far */
3103 *va_arg(args, int *) = str - buf;
3104 ++fmt;
3105 continue;
3106 }
3107
3108 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 break;
3110
André Goddard Rosad4be1512009-12-14 18:00:59 -08003111 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003112 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003113
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003114 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 case 'c':
3116 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003117 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (field_width == -1)
3119 field_width = 1;
3120 do {
3121 *s++ = *str++;
3122 } while (--field_width > 0 && *str);
3123 num++;
3124 }
3125 continue;
3126 case 's':
3127 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003128 char *s = (char *)va_arg(args, char *);
3129 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003130 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003132 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003135 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 *s = '\0';
3138 num++;
3139 }
3140 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003141 /*
3142 * Warning: This implementation of the '[' conversion specifier
3143 * deviates from its glibc counterpart in the following ways:
3144 * (1) It does NOT support ranges i.e. '-' is NOT a special
3145 * character
3146 * (2) It cannot match the closing bracket ']' itself
3147 * (3) A field width is required
3148 * (4) '%*[' (discard matching input) is currently not supported
3149 *
3150 * Example usage:
3151 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3152 * buf1, buf2, buf3);
3153 * if (ret < 3)
3154 * // etc..
3155 */
3156 case '[':
3157 {
3158 char *s = (char *)va_arg(args, char *);
3159 DECLARE_BITMAP(set, 256) = {0};
3160 unsigned int len = 0;
3161 bool negate = (*fmt == '^');
3162
3163 /* field width is required */
3164 if (field_width == -1)
3165 return num;
3166
3167 if (negate)
3168 ++fmt;
3169
3170 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3171 set_bit((u8)*fmt, set);
3172
3173 /* no ']' or no character set found */
3174 if (!*fmt || !len)
3175 return num;
3176 ++fmt;
3177
3178 if (negate) {
3179 bitmap_complement(set, set, 256);
3180 /* exclude null '\0' byte */
3181 clear_bit(0, set);
3182 }
3183
3184 /* match must be non-empty */
3185 if (!test_bit((u8)*str, set))
3186 return num;
3187
3188 while (test_bit((u8)*str, set) && field_width--)
3189 *s++ = *str++;
3190 *s = '\0';
3191 ++num;
3192 }
3193 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 case 'o':
3195 base = 8;
3196 break;
3197 case 'x':
3198 case 'X':
3199 base = 16;
3200 break;
3201 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003202 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003203 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003205 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003206 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 case 'u':
3208 break;
3209 case '%':
3210 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003211 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return num;
3213 continue;
3214 default:
3215 /* invalid format; stop here */
3216 return num;
3217 }
3218
3219 /* have some sort of integer conversion.
3220 * first, skip white space in buffer.
3221 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003222 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 digit = *str;
3225 if (is_sign && digit == '-')
3226 digit = *(str + 1);
3227
3228 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003229 || (base == 16 && !isxdigit(digit))
3230 || (base == 10 && !isdigit(digit))
3231 || (base == 8 && (!isdigit(digit) || digit > '7'))
3232 || (base == 0 && !isdigit(digit)))
3233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Jan Beulich53809752012-12-17 16:01:31 -08003235 if (is_sign)
3236 val.s = qualifier != 'L' ?
3237 simple_strtol(str, &next, base) :
3238 simple_strtoll(str, &next, base);
3239 else
3240 val.u = qualifier != 'L' ?
3241 simple_strtoul(str, &next, base) :
3242 simple_strtoull(str, &next, base);
3243
3244 if (field_width > 0 && next - str > field_width) {
3245 if (base == 0)
3246 _parse_integer_fixup_radix(str, &base);
3247 while (next - str > field_width) {
3248 if (is_sign)
3249 val.s = div_s64(val.s, base);
3250 else
3251 val.u = div_u64(val.u, base);
3252 --next;
3253 }
3254 }
3255
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003256 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003258 if (is_sign)
3259 *va_arg(args, signed char *) = val.s;
3260 else
3261 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 break;
3263 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003264 if (is_sign)
3265 *va_arg(args, short *) = val.s;
3266 else
3267 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 break;
3269 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003270 if (is_sign)
3271 *va_arg(args, long *) = val.s;
3272 else
3273 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 break;
3275 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003276 if (is_sign)
3277 *va_arg(args, long long *) = val.s;
3278 else
3279 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003282 *va_arg(args, size_t *) = val.u;
3283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 default:
Jan Beulich53809752012-12-17 16:01:31 -08003285 if (is_sign)
3286 *va_arg(args, int *) = val.s;
3287 else
3288 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 break;
3290 }
3291 num++;
3292
3293 if (!next)
3294 break;
3295 str = next;
3296 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return num;
3299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300EXPORT_SYMBOL(vsscanf);
3301
3302/**
3303 * sscanf - Unformat a buffer into a list of arguments
3304 * @buf: input buffer
3305 * @fmt: formatting of buffer
3306 * @...: resulting arguments
3307 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003308int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 va_list args;
3311 int i;
3312
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003313 va_start(args, fmt);
3314 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return i;
3318}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319EXPORT_SYMBOL(sscanf);