blob: 2f003cfe340ec55d0a8e96309b02fd6326b1af1e [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
Petr Mladekc8c3b582019-04-17 13:53:50 +0200615/* Be careful: error messages must fit into the given buffer. */
616static char *error_string(char *buf, char *end, const char *s,
617 struct printf_spec spec)
618{
619 /*
620 * Hard limit to avoid a completely insane messages. It actually
621 * works pretty well because most error messages are in
622 * the many pointer format modifiers.
623 */
624 if (spec.precision == -1)
625 spec.precision = 2 * sizeof(void *);
626
627 return string_nocheck(buf, end, s, spec);
628}
629
Petr Mladek3e5903e2019-04-17 13:53:48 +0200630/*
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200631 * Do not call any complex external code here. Nested printk()/vsprintf()
632 * might cause infinite loops. Failures might break printk() and would
633 * be hard to debug.
Petr Mladek3e5903e2019-04-17 13:53:48 +0200634 */
635static const char *check_pointer_msg(const void *ptr)
636{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200637 if (!ptr)
638 return "(null)";
639
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200640 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
Petr Mladek3e5903e2019-04-17 13:53:48 +0200641 return "(efault)";
642
643 return NULL;
644}
645
646static int check_pointer(char **buf, char *end, const void *ptr,
647 struct printf_spec spec)
648{
649 const char *err_msg;
650
651 err_msg = check_pointer_msg(ptr);
652 if (err_msg) {
Petr Mladekc8c3b582019-04-17 13:53:50 +0200653 *buf = error_string(*buf, end, err_msg, spec);
Petr Mladek3e5903e2019-04-17 13:53:48 +0200654 return -EFAULT;
655 }
656
657 return 0;
658}
659
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800660static noinline_for_stack
Petr Mladekd529ac42019-04-17 13:53:43 +0200661char *string(char *buf, char *end, const char *s,
662 struct printf_spec spec)
663{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200664 if (check_pointer(&buf, end, s, spec))
665 return buf;
Petr Mladekd529ac42019-04-17 13:53:43 +0200666
667 return string_nocheck(buf, end, s, spec);
668}
669
YueHaibingce9d3ec2019-04-27 00:46:30 +0800670static char *pointer_string(char *buf, char *end,
671 const void *ptr,
672 struct printf_spec spec)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200673{
674 spec.base = 16;
675 spec.flags |= SMALL;
676 if (spec.field_width == -1) {
677 spec.field_width = 2 * sizeof(ptr);
678 spec.flags |= ZEROPAD;
679 }
680
681 return number(buf, end, (unsigned long int)ptr, spec);
682}
683
684/* Make pointers available for printing early in the boot sequence. */
685static int debug_boot_weak_hash __ro_after_init;
686
687static int __init debug_boot_weak_hash_enable(char *str)
688{
689 debug_boot_weak_hash = 1;
690 pr_info("debug_boot_weak_hash enabled\n");
691 return 0;
692}
693early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
694
695static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
696static siphash_key_t ptr_key __read_mostly;
697
698static void enable_ptr_key_workfn(struct work_struct *work)
699{
700 get_random_bytes(&ptr_key, sizeof(ptr_key));
701 /* Needs to run from preemptible context */
702 static_branch_disable(&not_filled_random_ptr_key);
703}
704
705static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
706
707static void fill_random_ptr_key(struct random_ready_callback *unused)
708{
709 /* This may be in an interrupt handler. */
710 queue_work(system_unbound_wq, &enable_ptr_key_work);
711}
712
713static struct random_ready_callback random_ready = {
714 .func = fill_random_ptr_key
715};
716
717static int __init initialize_ptr_random(void)
718{
719 int key_size = sizeof(ptr_key);
720 int ret;
721
722 /* Use hw RNG if available. */
723 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
724 static_branch_disable(&not_filled_random_ptr_key);
725 return 0;
726 }
727
728 ret = add_random_ready_callback(&random_ready);
729 if (!ret) {
730 return 0;
731 } else if (ret == -EALREADY) {
732 /* This is in preemptible context */
733 enable_ptr_key_workfn(&enable_ptr_key_work);
734 return 0;
735 }
736
737 return ret;
738}
739early_initcall(initialize_ptr_random);
740
741/* Maps a pointer to a 32 bit unique identifier. */
742static char *ptr_to_id(char *buf, char *end, const void *ptr,
743 struct printf_spec spec)
744{
745 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
746 unsigned long hashval;
747
748 /* When debugging early boot use non-cryptographically secure hash. */
749 if (unlikely(debug_boot_weak_hash)) {
750 hashval = hash_long((unsigned long)ptr, 32);
751 return pointer_string(buf, end, (const void *)hashval, spec);
752 }
753
754 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
755 spec.field_width = 2 * sizeof(ptr);
756 /* string length must be less than default_width */
Petr Mladekc8c3b582019-04-17 13:53:50 +0200757 return error_string(buf, end, str, spec);
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200758 }
759
760#ifdef CONFIG_64BIT
761 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
762 /*
763 * Mask off the first 32 bits, this makes explicit that we have
764 * modified the address (and 32 bits is plenty for a unique ID).
765 */
766 hashval = hashval & 0xffffffff;
767#else
768 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
769#endif
770 return pointer_string(buf, end, (const void *)hashval, spec);
771}
772
Petr Mladek6eea2422019-04-17 13:53:41 +0200773int kptr_restrict __read_mostly;
774
775static noinline_for_stack
776char *restricted_pointer(char *buf, char *end, const void *ptr,
777 struct printf_spec spec)
778{
779 switch (kptr_restrict) {
780 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200781 /* Handle as %p, hash and do _not_ leak addresses. */
782 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200783 case 1: {
784 const struct cred *cred;
785
786 /*
787 * kptr_restrict==1 cannot be used in IRQ context
788 * because its test for CAP_SYSLOG would be meaningless.
789 */
790 if (in_irq() || in_serving_softirq() || in_nmi()) {
791 if (spec.field_width == -1)
792 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200793 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200794 }
795
796 /*
797 * Only print the real pointer value if the current
798 * process has CAP_SYSLOG and is running with the
799 * same credentials it started with. This is because
800 * access to files is checked at open() time, but %pK
801 * checks permission at read() time. We don't want to
802 * leak pointer values if a binary opens a file using
803 * %pK and then elevates privileges before reading it.
804 */
805 cred = current_cred();
806 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
807 !uid_eq(cred->euid, cred->uid) ||
808 !gid_eq(cred->egid, cred->gid))
809 ptr = NULL;
810 break;
811 }
812 case 2:
813 default:
814 /* Always print 0's for %pK */
815 ptr = NULL;
816 break;
817 }
818
819 return pointer_string(buf, end, ptr, spec);
820}
821
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200822static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400823char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
824 const char *fmt)
825{
826 const char *array[4], *s;
827 const struct dentry *p;
828 int depth;
829 int i, n;
830
831 switch (fmt[1]) {
832 case '2': case '3': case '4':
833 depth = fmt[1] - '0';
834 break;
835 default:
836 depth = 1;
837 }
838
839 rcu_read_lock();
840 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200841 if (check_pointer(&buf, end, d, spec)) {
842 rcu_read_unlock();
843 return buf;
844 }
845
Mark Rutland6aa7de02017-10-23 14:07:29 -0700846 p = READ_ONCE(d->d_parent);
847 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400848 if (p == d) {
849 if (i)
850 array[i] = "";
851 i++;
852 break;
853 }
854 }
855 s = array[--i];
856 for (n = 0; n != spec.precision; n++, buf++) {
857 char c = *s++;
858 if (!c) {
859 if (!i)
860 break;
861 c = '/';
862 s = array[--i];
863 }
864 if (buf < end)
865 *buf = c;
866 }
867 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800868 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400869}
870
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400871#ifdef CONFIG_BLOCK
872static noinline_for_stack
873char *bdev_name(char *buf, char *end, struct block_device *bdev,
874 struct printf_spec spec, const char *fmt)
875{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200876 struct gendisk *hd;
877
878 if (check_pointer(&buf, end, bdev, spec))
879 return buf;
880
881 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400882 buf = string(buf, end, hd->disk_name, spec);
883 if (bdev->bd_part->partno) {
884 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
885 if (buf < end)
886 *buf = 'p';
887 buf++;
888 }
889 buf = number(buf, end, bdev->bd_part->partno, spec);
890 }
891 return buf;
892}
893#endif
894
Joe Perchescf3b4292010-05-24 14:33:16 -0700895static noinline_for_stack
896char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800897 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700898{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800899 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700900#ifdef CONFIG_KALLSYMS
901 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800902#endif
903
904 if (fmt[1] == 'R')
905 ptr = __builtin_extract_return_addr(ptr);
906 value = (unsigned long)ptr;
907
908#ifdef CONFIG_KALLSYMS
909 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900910 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800911 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200912 sprint_symbol(sym, value);
913 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700914 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800915
Petr Mladekd529ac42019-04-17 13:53:43 +0200916 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700917#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800918 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700919#endif
920}
921
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200922static const struct printf_spec default_str_spec = {
923 .field_width = -1,
924 .precision = -1,
925};
926
Andy Shevchenko54433972018-02-16 23:07:06 +0200927static const struct printf_spec default_flag_spec = {
928 .base = 16,
929 .precision = -1,
930 .flags = SPECIAL | SMALL,
931};
932
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200933static const struct printf_spec default_dec_spec = {
934 .base = 10,
935 .precision = -1,
936};
937
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200938static const struct printf_spec default_dec02_spec = {
939 .base = 10,
940 .field_width = 2,
941 .precision = -1,
942 .flags = ZEROPAD,
943};
944
945static const struct printf_spec default_dec04_spec = {
946 .base = 10,
947 .field_width = 4,
948 .precision = -1,
949 .flags = ZEROPAD,
950};
951
Joe Perchescf3b4292010-05-24 14:33:16 -0700952static noinline_for_stack
953char *resource_string(char *buf, char *end, struct resource *res,
954 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100955{
956#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600957#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100958#endif
959
960#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600961#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100962#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700963 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100964 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700965 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100966 .precision = -1,
967 .flags = SPECIAL | SMALL | ZEROPAD,
968 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700969 static const struct printf_spec mem_spec = {
970 .base = 16,
971 .field_width = MEM_RSRC_PRINTK_SIZE,
972 .precision = -1,
973 .flags = SPECIAL | SMALL | ZEROPAD,
974 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700975 static const struct printf_spec bus_spec = {
976 .base = 16,
977 .field_width = 2,
978 .precision = -1,
979 .flags = SMALL | ZEROPAD,
980 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700981 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600982 .field_width = -1,
983 .precision = 10,
984 .flags = LEFT,
985 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600986
987 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
988 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
989#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
990#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700991#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600992#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
993 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
994 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
995
Linus Torvalds332d2e72008-10-20 15:07:34 +1100996 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600997 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700998 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100999
Petr Mladek3e5903e2019-04-17 13:53:48 +02001000 if (check_pointer(&buf, end, res, spec))
1001 return buf;
1002
Linus Torvalds332d2e72008-10-20 15:07:34 +11001003 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001004 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001005 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001006 specp = &io_spec;
1007 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001008 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001009 specp = &mem_spec;
1010 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001011 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001012 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001013 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001014 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001015 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001016 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001017 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001018 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001019 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001020 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001021 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001022 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001023 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001024 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001025 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001026 p = number(p, pend, resource_size(res), *specp);
1027 } else {
1028 p = number(p, pend, res->start, *specp);
1029 if (res->start != res->end) {
1030 *p++ = '-';
1031 p = number(p, pend, res->end, *specp);
1032 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001033 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001034 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001035 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001036 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001037 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001038 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001039 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001040 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001041 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001042 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001043 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001044 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001045 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001046 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001047 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001048 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001049
Petr Mladekd529ac42019-04-17 13:53:43 +02001050 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001051}
1052
Joe Perchescf3b4292010-05-24 14:33:16 -07001053static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001054char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1055 const char *fmt)
1056{
Steven Rostedt360603a2013-05-28 15:47:39 -04001057 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001058 negative value, fallback to the default */
1059 char separator;
1060
1061 if (spec.field_width == 0)
1062 /* nothing to print */
1063 return buf;
1064
Petr Mladek3e5903e2019-04-17 13:53:48 +02001065 if (check_pointer(&buf, end, addr, spec))
1066 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001067
1068 switch (fmt[1]) {
1069 case 'C':
1070 separator = ':';
1071 break;
1072 case 'D':
1073 separator = '-';
1074 break;
1075 case 'N':
1076 separator = 0;
1077 break;
1078 default:
1079 separator = ' ';
1080 break;
1081 }
1082
1083 if (spec.field_width > 0)
1084 len = min_t(int, spec.field_width, 64);
1085
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001086 for (i = 0; i < len; ++i) {
1087 if (buf < end)
1088 *buf = hex_asc_hi(addr[i]);
1089 ++buf;
1090 if (buf < end)
1091 *buf = hex_asc_lo(addr[i]);
1092 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001093
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001094 if (separator && i != len - 1) {
1095 if (buf < end)
1096 *buf = separator;
1097 ++buf;
1098 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001099 }
1100
1101 return buf;
1102}
1103
1104static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001105char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1106 struct printf_spec spec, const char *fmt)
1107{
1108 const int CHUNKSZ = 32;
1109 int nr_bits = max_t(int, spec.field_width, 0);
1110 int i, chunksz;
1111 bool first = true;
1112
Petr Mladek3e5903e2019-04-17 13:53:48 +02001113 if (check_pointer(&buf, end, bitmap, spec))
1114 return buf;
1115
Tejun Heodbc760b2015-02-13 14:36:53 -08001116 /* reused to print numbers */
1117 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1118
1119 chunksz = nr_bits & (CHUNKSZ - 1);
1120 if (chunksz == 0)
1121 chunksz = CHUNKSZ;
1122
1123 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1124 for (; i >= 0; i -= CHUNKSZ) {
1125 u32 chunkmask, val;
1126 int word, bit;
1127
1128 chunkmask = ((1ULL << chunksz) - 1);
1129 word = i / BITS_PER_LONG;
1130 bit = i % BITS_PER_LONG;
1131 val = (bitmap[word] >> bit) & chunkmask;
1132
1133 if (!first) {
1134 if (buf < end)
1135 *buf = ',';
1136 buf++;
1137 }
1138 first = false;
1139
1140 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1141 buf = number(buf, end, val, spec);
1142
1143 chunksz = CHUNKSZ;
1144 }
1145 return buf;
1146}
1147
1148static noinline_for_stack
1149char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1150 struct printf_spec spec, const char *fmt)
1151{
1152 int nr_bits = max_t(int, spec.field_width, 0);
1153 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1154 int cur, rbot, rtop;
1155 bool first = true;
1156
Petr Mladek3e5903e2019-04-17 13:53:48 +02001157 if (check_pointer(&buf, end, bitmap, spec))
1158 return buf;
1159
Tejun Heodbc760b2015-02-13 14:36:53 -08001160 rbot = cur = find_first_bit(bitmap, nr_bits);
1161 while (cur < nr_bits) {
1162 rtop = cur;
1163 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1164 if (cur < nr_bits && cur <= rtop + 1)
1165 continue;
1166
1167 if (!first) {
1168 if (buf < end)
1169 *buf = ',';
1170 buf++;
1171 }
1172 first = false;
1173
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001174 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001175 if (rbot < rtop) {
1176 if (buf < end)
1177 *buf = '-';
1178 buf++;
1179
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001180 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001181 }
1182
1183 rbot = cur;
1184 }
1185 return buf;
1186}
1187
1188static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001189char *mac_address_string(char *buf, char *end, u8 *addr,
1190 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001191{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001192 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001193 char *p = mac_addr;
1194 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001195 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001196 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001197
Petr Mladek3e5903e2019-04-17 13:53:48 +02001198 if (check_pointer(&buf, end, addr, spec))
1199 return buf;
1200
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001201 switch (fmt[1]) {
1202 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001203 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001204 break;
1205
1206 case 'R':
1207 reversed = true;
1208 /* fall through */
1209
1210 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001211 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001212 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001213 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001214
1215 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001216 if (reversed)
1217 p = hex_byte_pack(p, addr[5 - i]);
1218 else
1219 p = hex_byte_pack(p, addr[i]);
1220
Joe Perches8a27f7c2009-08-17 12:29:44 +00001221 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001222 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001223 }
1224 *p = '\0';
1225
Petr Mladekd529ac42019-04-17 13:53:43 +02001226 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001227}
1228
Joe Perchescf3b4292010-05-24 14:33:16 -07001229static noinline_for_stack
1230char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001231{
Harvey Harrison689afa72008-10-28 16:04:44 -07001232 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001233 bool leading_zeros = (fmt[0] == 'i');
1234 int index;
1235 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001236
Joe Perches0159f242010-01-13 20:23:30 -08001237 switch (fmt[2]) {
1238 case 'h':
1239#ifdef __BIG_ENDIAN
1240 index = 0;
1241 step = 1;
1242#else
1243 index = 3;
1244 step = -1;
1245#endif
1246 break;
1247 case 'l':
1248 index = 3;
1249 step = -1;
1250 break;
1251 case 'n':
1252 case 'b':
1253 default:
1254 index = 0;
1255 step = 1;
1256 break;
1257 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001258 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001259 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001260 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001261 if (leading_zeros) {
1262 if (digits < 3)
1263 *p++ = '0';
1264 if (digits < 2)
1265 *p++ = '0';
1266 }
1267 /* reverse the digits in the quad */
1268 while (digits--)
1269 *p++ = temp[digits];
1270 if (i < 3)
1271 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001272 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001273 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001274 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001275
Joe Perches8a27f7c2009-08-17 12:29:44 +00001276 return p;
1277}
1278
Joe Perchescf3b4292010-05-24 14:33:16 -07001279static noinline_for_stack
1280char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001281{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001282 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001283 unsigned char zerolength[8];
1284 int longest = 1;
1285 int colonpos = -1;
1286 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001287 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001288 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001289 bool useIPv4;
1290 struct in6_addr in6;
1291
1292 memcpy(&in6, addr, sizeof(struct in6_addr));
1293
1294 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001295
1296 memset(zerolength, 0, sizeof(zerolength));
1297
1298 if (useIPv4)
1299 range = 6;
1300 else
1301 range = 8;
1302
1303 /* find position of longest 0 run */
1304 for (i = 0; i < range; i++) {
1305 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001306 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001307 break;
1308 zerolength[i]++;
1309 }
1310 }
1311 for (i = 0; i < range; i++) {
1312 if (zerolength[i] > longest) {
1313 longest = zerolength[i];
1314 colonpos = i;
1315 }
1316 }
Joe Perches29cf5192011-06-09 11:23:37 -07001317 if (longest == 1) /* don't compress a single 0 */
1318 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001319
1320 /* emit address */
1321 for (i = 0; i < range; i++) {
1322 if (i == colonpos) {
1323 if (needcolon || i == 0)
1324 *p++ = ':';
1325 *p++ = ':';
1326 needcolon = false;
1327 i += longest - 1;
1328 continue;
1329 }
1330 if (needcolon) {
1331 *p++ = ':';
1332 needcolon = false;
1333 }
1334 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001335 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001336 hi = word >> 8;
1337 lo = word & 0xff;
1338 if (hi) {
1339 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001340 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001341 else
1342 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001343 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001344 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001345 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001346 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001347 else
1348 *p++ = hex_asc_lo(lo);
1349 needcolon = true;
1350 }
1351
1352 if (useIPv4) {
1353 if (needcolon)
1354 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001355 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001356 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001357 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001358
Joe Perches8a27f7c2009-08-17 12:29:44 +00001359 return p;
1360}
1361
Joe Perchescf3b4292010-05-24 14:33:16 -07001362static noinline_for_stack
1363char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001364{
1365 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001366
Harvey Harrison689afa72008-10-28 16:04:44 -07001367 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001368 p = hex_byte_pack(p, *addr++);
1369 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001370 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001371 *p++ = ':';
1372 }
1373 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001374
Joe Perches8a27f7c2009-08-17 12:29:44 +00001375 return p;
1376}
1377
Joe Perchescf3b4292010-05-24 14:33:16 -07001378static noinline_for_stack
1379char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1380 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001381{
1382 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1383
1384 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001385 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001386 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001387 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001388
Petr Mladekd529ac42019-04-17 13:53:43 +02001389 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001390}
1391
Joe Perchescf3b4292010-05-24 14:33:16 -07001392static noinline_for_stack
1393char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1394 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001395{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001396 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001397
Joe Perches0159f242010-01-13 20:23:30 -08001398 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001399
Petr Mladekd529ac42019-04-17 13:53:43 +02001400 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001401}
1402
Joe Perchescf3b4292010-05-24 14:33:16 -07001403static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001404char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1405 struct printf_spec spec, const char *fmt)
1406{
1407 bool have_p = false, have_s = false, have_f = false, have_c = false;
1408 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1409 sizeof(":12345") + sizeof("/123456789") +
1410 sizeof("%1234567890")];
1411 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1412 const u8 *addr = (const u8 *) &sa->sin6_addr;
1413 char fmt6[2] = { fmt[0], '6' };
1414 u8 off = 0;
1415
1416 fmt++;
1417 while (isalpha(*++fmt)) {
1418 switch (*fmt) {
1419 case 'p':
1420 have_p = true;
1421 break;
1422 case 'f':
1423 have_f = true;
1424 break;
1425 case 's':
1426 have_s = true;
1427 break;
1428 case 'c':
1429 have_c = true;
1430 break;
1431 }
1432 }
1433
1434 if (have_p || have_s || have_f) {
1435 *p = '[';
1436 off = 1;
1437 }
1438
1439 if (fmt6[0] == 'I' && have_c)
1440 p = ip6_compressed_string(ip6_addr + off, addr);
1441 else
1442 p = ip6_string(ip6_addr + off, addr, fmt6);
1443
1444 if (have_p || have_s || have_f)
1445 *p++ = ']';
1446
1447 if (have_p) {
1448 *p++ = ':';
1449 p = number(p, pend, ntohs(sa->sin6_port), spec);
1450 }
1451 if (have_f) {
1452 *p++ = '/';
1453 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1454 IPV6_FLOWINFO_MASK), spec);
1455 }
1456 if (have_s) {
1457 *p++ = '%';
1458 p = number(p, pend, sa->sin6_scope_id, spec);
1459 }
1460 *p = '\0';
1461
Petr Mladekd529ac42019-04-17 13:53:43 +02001462 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001463}
1464
1465static noinline_for_stack
1466char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1467 struct printf_spec spec, const char *fmt)
1468{
1469 bool have_p = false;
1470 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1471 char *pend = ip4_addr + sizeof(ip4_addr);
1472 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1473 char fmt4[3] = { fmt[0], '4', 0 };
1474
1475 fmt++;
1476 while (isalpha(*++fmt)) {
1477 switch (*fmt) {
1478 case 'p':
1479 have_p = true;
1480 break;
1481 case 'h':
1482 case 'l':
1483 case 'n':
1484 case 'b':
1485 fmt4[2] = *fmt;
1486 break;
1487 }
1488 }
1489
1490 p = ip4_string(ip4_addr, addr, fmt4);
1491 if (have_p) {
1492 *p++ = ':';
1493 p = number(p, pend, ntohs(sa->sin_port), spec);
1494 }
1495 *p = '\0';
1496
Petr Mladekd529ac42019-04-17 13:53:43 +02001497 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001498}
1499
1500static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001501char *ip_addr_string(char *buf, char *end, const void *ptr,
1502 struct printf_spec spec, const char *fmt)
1503{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001504 char *err_fmt_msg;
1505
Petr Mladek3e5903e2019-04-17 13:53:48 +02001506 if (check_pointer(&buf, end, ptr, spec))
1507 return buf;
1508
Petr Mladekf00cc102019-04-17 13:53:44 +02001509 switch (fmt[1]) {
1510 case '6':
1511 return ip6_addr_string(buf, end, ptr, spec, fmt);
1512 case '4':
1513 return ip4_addr_string(buf, end, ptr, spec, fmt);
1514 case 'S': {
1515 const union {
1516 struct sockaddr raw;
1517 struct sockaddr_in v4;
1518 struct sockaddr_in6 v6;
1519 } *sa = ptr;
1520
1521 switch (sa->raw.sa_family) {
1522 case AF_INET:
1523 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1524 case AF_INET6:
1525 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1526 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001527 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001528 }}
1529 }
1530
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001531 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001532 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001533}
1534
1535static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001536char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1537 const char *fmt)
1538{
1539 bool found = true;
1540 int count = 1;
1541 unsigned int flags = 0;
1542 int len;
1543
1544 if (spec.field_width == 0)
1545 return buf; /* nothing to print */
1546
Petr Mladek3e5903e2019-04-17 13:53:48 +02001547 if (check_pointer(&buf, end, addr, spec))
1548 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001549
1550 do {
1551 switch (fmt[count++]) {
1552 case 'a':
1553 flags |= ESCAPE_ANY;
1554 break;
1555 case 'c':
1556 flags |= ESCAPE_SPECIAL;
1557 break;
1558 case 'h':
1559 flags |= ESCAPE_HEX;
1560 break;
1561 case 'n':
1562 flags |= ESCAPE_NULL;
1563 break;
1564 case 'o':
1565 flags |= ESCAPE_OCTAL;
1566 break;
1567 case 'p':
1568 flags |= ESCAPE_NP;
1569 break;
1570 case 's':
1571 flags |= ESCAPE_SPACE;
1572 break;
1573 default:
1574 found = false;
1575 break;
1576 }
1577 } while (found);
1578
1579 if (!flags)
1580 flags = ESCAPE_ANY_NP;
1581
1582 len = spec.field_width < 0 ? 1 : spec.field_width;
1583
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001584 /*
1585 * string_escape_mem() writes as many characters as it can to
1586 * the given buffer, and returns the total size of the output
1587 * had the buffer been big enough.
1588 */
1589 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001590
1591 return buf;
1592}
1593
Petr Mladek3e5903e2019-04-17 13:53:48 +02001594static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1595 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001596{
1597 va_list va;
1598
Petr Mladek3e5903e2019-04-17 13:53:48 +02001599 if (check_pointer(&buf, end, va_fmt, spec))
1600 return buf;
1601
Petr Mladek45c3e932019-04-17 13:53:45 +02001602 va_copy(va, *va_fmt->va);
1603 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1604 va_end(va);
1605
1606 return buf;
1607}
1608
Andy Shevchenko71dca952014-10-13 15:55:18 -07001609static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001610char *uuid_string(char *buf, char *end, const u8 *addr,
1611 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001612{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001613 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001614 char *p = uuid;
1615 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001616 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001617 bool uc = false;
1618
Petr Mladek3e5903e2019-04-17 13:53:48 +02001619 if (check_pointer(&buf, end, addr, spec))
1620 return buf;
1621
Joe Perches9ac6e442009-12-14 18:01:09 -08001622 switch (*(++fmt)) {
1623 case 'L':
1624 uc = true; /* fall-through */
1625 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001626 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001627 break;
1628 case 'B':
1629 uc = true;
1630 break;
1631 }
1632
1633 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001634 if (uc)
1635 p = hex_byte_pack_upper(p, addr[index[i]]);
1636 else
1637 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001638 switch (i) {
1639 case 3:
1640 case 5:
1641 case 7:
1642 case 9:
1643 *p++ = '-';
1644 break;
1645 }
1646 }
1647
1648 *p = 0;
1649
Petr Mladekd529ac42019-04-17 13:53:43 +02001650 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001651}
1652
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001653static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001654char *netdev_bits(char *buf, char *end, const void *addr,
1655 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001656{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001657 unsigned long long num;
1658 int size;
1659
Petr Mladek3e5903e2019-04-17 13:53:48 +02001660 if (check_pointer(&buf, end, addr, spec))
1661 return buf;
1662
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001663 switch (fmt[1]) {
1664 case 'F':
1665 num = *(const netdev_features_t *)addr;
1666 size = sizeof(netdev_features_t);
1667 break;
1668 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001669 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001670 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001671
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001672 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001673}
1674
Joe Perchesaaf07622014-01-23 15:54:17 -08001675static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001676char *address_val(char *buf, char *end, const void *addr,
1677 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001678{
1679 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001680 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001681
Petr Mladek3e5903e2019-04-17 13:53:48 +02001682 if (check_pointer(&buf, end, addr, spec))
1683 return buf;
1684
Joe Perchesaaf07622014-01-23 15:54:17 -08001685 switch (fmt[1]) {
1686 case 'd':
1687 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001688 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001689 break;
1690 case 'p':
1691 default:
1692 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001693 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001694 break;
1695 }
1696
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001697 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001698}
1699
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001700static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001701char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1702{
1703 int year = tm->tm_year + (r ? 0 : 1900);
1704 int mon = tm->tm_mon + (r ? 0 : 1);
1705
1706 buf = number(buf, end, year, default_dec04_spec);
1707 if (buf < end)
1708 *buf = '-';
1709 buf++;
1710
1711 buf = number(buf, end, mon, default_dec02_spec);
1712 if (buf < end)
1713 *buf = '-';
1714 buf++;
1715
1716 return number(buf, end, tm->tm_mday, default_dec02_spec);
1717}
1718
1719static noinline_for_stack
1720char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1721{
1722 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1723 if (buf < end)
1724 *buf = ':';
1725 buf++;
1726
1727 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1728 if (buf < end)
1729 *buf = ':';
1730 buf++;
1731
1732 return number(buf, end, tm->tm_sec, default_dec02_spec);
1733}
1734
1735static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001736char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1737 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001738{
1739 bool have_t = true, have_d = true;
1740 bool raw = false;
1741 int count = 2;
1742
Petr Mladek3e5903e2019-04-17 13:53:48 +02001743 if (check_pointer(&buf, end, tm, spec))
1744 return buf;
1745
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001746 switch (fmt[count]) {
1747 case 'd':
1748 have_t = false;
1749 count++;
1750 break;
1751 case 't':
1752 have_d = false;
1753 count++;
1754 break;
1755 }
1756
1757 raw = fmt[count] == 'r';
1758
1759 if (have_d)
1760 buf = date_str(buf, end, tm, raw);
1761 if (have_d && have_t) {
1762 /* Respect ISO 8601 */
1763 if (buf < end)
1764 *buf = 'T';
1765 buf++;
1766 }
1767 if (have_t)
1768 buf = time_str(buf, end, tm, raw);
1769
1770 return buf;
1771}
1772
1773static noinline_for_stack
1774char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1775 const char *fmt)
1776{
1777 switch (fmt[1]) {
1778 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001779 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001780 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001781 return error_string(buf, end, "(%ptR?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001782 }
1783}
1784
1785static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001786char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1787 const char *fmt)
1788{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001789 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001790 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001791
Petr Mladek3e5903e2019-04-17 13:53:48 +02001792 if (check_pointer(&buf, end, clk, spec))
1793 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001794
1795 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001796 case 'n':
1797 default:
1798#ifdef CONFIG_COMMON_CLK
1799 return string(buf, end, __clk_get_name(clk), spec);
1800#else
Petr Mladekc8c3b582019-04-17 13:53:50 +02001801 return error_string(buf, end, "(%pC?)", spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001802#endif
1803 }
1804}
1805
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001806static
1807char *format_flags(char *buf, char *end, unsigned long flags,
1808 const struct trace_print_flags *names)
1809{
1810 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001811
1812 for ( ; flags && names->name; names++) {
1813 mask = names->mask;
1814 if ((flags & mask) != mask)
1815 continue;
1816
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001817 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001818
1819 flags &= ~mask;
1820 if (flags) {
1821 if (buf < end)
1822 *buf = '|';
1823 buf++;
1824 }
1825 }
1826
1827 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001828 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001829
1830 return buf;
1831}
1832
1833static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001834char *flags_string(char *buf, char *end, void *flags_ptr,
1835 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001836{
1837 unsigned long flags;
1838 const struct trace_print_flags *names;
1839
Petr Mladek3e5903e2019-04-17 13:53:48 +02001840 if (check_pointer(&buf, end, flags_ptr, spec))
1841 return buf;
1842
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001843 switch (fmt[1]) {
1844 case 'p':
1845 flags = *(unsigned long *)flags_ptr;
1846 /* Remove zone id */
1847 flags &= (1UL << NR_PAGEFLAGS) - 1;
1848 names = pageflag_names;
1849 break;
1850 case 'v':
1851 flags = *(unsigned long *)flags_ptr;
1852 names = vmaflag_names;
1853 break;
1854 case 'g':
1855 flags = *(gfp_t *)flags_ptr;
1856 names = gfpflag_names;
1857 break;
1858 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001859 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001860 }
1861
1862 return format_flags(buf, end, flags, names);
1863}
1864
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001865static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1866{
1867 for ( ; np && depth; depth--)
1868 np = np->parent;
1869
1870 return kbasename(np->full_name);
1871}
1872
1873static noinline_for_stack
1874char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1875{
1876 int depth;
1877 const struct device_node *parent = np->parent;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001878
1879 /* special case for root node */
1880 if (!parent)
Petr Mladekd529ac42019-04-17 13:53:43 +02001881 return string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001882
1883 for (depth = 0; parent->parent; depth++)
1884 parent = parent->parent;
1885
1886 for ( ; depth >= 0; depth--) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001887 buf = string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001888 buf = string(buf, end, device_node_name_for_depth(np, depth),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001889 default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001890 }
1891 return buf;
1892}
1893
1894static noinline_for_stack
1895char *device_node_string(char *buf, char *end, struct device_node *dn,
1896 struct printf_spec spec, const char *fmt)
1897{
1898 char tbuf[sizeof("xxxx") + 1];
1899 const char *p;
1900 int ret;
1901 char *buf_start = buf;
1902 struct property *prop;
1903 bool has_mult, pass;
1904 static const struct printf_spec num_spec = {
1905 .flags = SMALL,
1906 .field_width = -1,
1907 .precision = -1,
1908 .base = 10,
1909 };
1910
1911 struct printf_spec str_spec = spec;
1912 str_spec.field_width = -1;
1913
1914 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001915 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001916
Petr Mladek3e5903e2019-04-17 13:53:48 +02001917 if (check_pointer(&buf, end, dn, spec))
1918 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001919
1920 /* simple case without anything any more format specifiers */
1921 fmt++;
1922 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1923 fmt = "f";
1924
1925 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001926 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001927 if (pass) {
1928 if (buf < end)
1929 *buf = ':';
1930 buf++;
1931 }
1932
1933 switch (*fmt) {
1934 case 'f': /* full_name */
1935 buf = device_node_gen_full_name(dn, buf, end);
1936 break;
1937 case 'n': /* name */
Rob Herring6d0a70a2018-08-27 08:13:56 -05001938 p = kbasename(of_node_full_name(dn));
1939 precision = str_spec.precision;
1940 str_spec.precision = strchrnul(p, '@') - p;
1941 buf = string(buf, end, p, str_spec);
1942 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001943 break;
1944 case 'p': /* phandle */
1945 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1946 break;
1947 case 'P': /* path-spec */
1948 p = kbasename(of_node_full_name(dn));
1949 if (!p[1])
1950 p = "/";
1951 buf = string(buf, end, p, str_spec);
1952 break;
1953 case 'F': /* flags */
1954 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1955 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1956 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1957 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1958 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02001959 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001960 break;
1961 case 'c': /* major compatible string */
1962 ret = of_property_read_string(dn, "compatible", &p);
1963 if (!ret)
1964 buf = string(buf, end, p, str_spec);
1965 break;
1966 case 'C': /* full compatible string */
1967 has_mult = false;
1968 of_property_for_each_string(dn, "compatible", prop, p) {
1969 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02001970 buf = string_nocheck(buf, end, ",", str_spec);
1971 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001972 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02001973 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001974
1975 has_mult = true;
1976 }
1977 break;
1978 default:
1979 break;
1980 }
1981 }
1982
1983 return widen_string(buf, buf - buf_start, end, spec);
1984}
1985
Petr Mladek798cc272019-04-17 13:53:46 +02001986static char *kobject_string(char *buf, char *end, void *ptr,
1987 struct printf_spec spec, const char *fmt)
1988{
1989 switch (fmt[1]) {
1990 case 'F':
1991 return device_node_string(buf, end, ptr, spec, fmt + 1);
1992 }
1993
Petr Mladekc8c3b582019-04-17 13:53:50 +02001994 return error_string(buf, end, "(%pO?)", spec);
Petr Mladek798cc272019-04-17 13:53:46 +02001995}
1996
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001997/*
1998 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1999 * by an extra set of alphanumeric characters that are extended format
2000 * specifiers.
2001 *
Joe Perches0b523762017-05-08 15:55:36 -07002002 * Please update scripts/checkpatch.pl when adding/removing conversion
2003 * characters. (Search for "check for vsprintf extension").
2004 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002005 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002006 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002007 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2008 * - 's' For symbolic direct pointers (or function descriptors) without offset
2009 * - 'F' Same as 'S'
2010 * - 'f' Same as 's'
Joe Perchesb0d33c22012-12-12 10:18:50 -08002011 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002012 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002013 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2014 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002015 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2016 * width which must be explicitly specified either as part of the
2017 * format string '%32b[l]' or through '%*b[l]', [l] selects
2018 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002019 * - 'M' For a 6-byte MAC address, it prints the address in the
2020 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002021 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002022 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002023 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002024 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002025 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2026 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2027 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002028 * [S][pfs]
2029 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2030 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002031 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2032 * IPv6 omits the colons (01020304...0f)
2033 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002034 * [S][pfs]
2035 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2036 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2037 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2038 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07002039 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002040 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2041 * of the following flags (see string_escape_mem() for the
2042 * details):
2043 * a - ESCAPE_ANY
2044 * c - ESCAPE_SPECIAL
2045 * h - ESCAPE_HEX
2046 * n - ESCAPE_NULL
2047 * o - ESCAPE_OCTAL
2048 * p - ESCAPE_NP
2049 * s - ESCAPE_SPACE
2050 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002051 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2052 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2053 * Options for %pU are:
2054 * b big endian lower case hex (default)
2055 * B big endian UPPER case hex
2056 * l little endian lower case hex
2057 * L little endian UPPER case hex
2058 * big endian output byte order is:
2059 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2060 * little endian output byte order is:
2061 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002062 * - 'V' For a struct va_format which contains a format string * and va_list *,
2063 * call vsnprintf(->format, *->va_list).
2064 * Implements a "recursive vsnprintf".
2065 * Do not use this feature without some mechanism to verify the
2066 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002067 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002068 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002069 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2070 * a certain separator (' ' by default):
2071 * C colon
2072 * D dash
2073 * N no separator
2074 * The maximum supported length is 64 bytes of the input. Consider
2075 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002076 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2077 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002078 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2079 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002080 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002081 * - 't[R][dt][r]' For time and date as represented:
2082 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002083 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2084 * (legacy clock framework) of the clock
2085 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2086 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002087 * - 'G' For flags to be printed as a collection of symbolic strings that would
2088 * construct the specific value. Supported flags given by option:
2089 * p page flags (see struct page) given as pointer to unsigned long
2090 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2091 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002092 * - 'OF[fnpPcCF]' For a device tree object
2093 * Without any optional arguments prints the full_name
2094 * f device node full_name
2095 * n device node name
2096 * p device node phandle
2097 * P device node path spec (name + @unit)
2098 * F device node flags
2099 * c major compatible string
2100 * C full compatible string
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002101 * - 'x' For printing the address. Equivalent to "%lx".
2102 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002103 * ** When making changes please also update:
2104 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002105 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002106 * Note: The default behaviour (unadorned %p) is to hash the address,
2107 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002108 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002109static noinline_for_stack
2110char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2111 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002112{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002113 switch (*fmt) {
2114 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02002115 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002116 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002117 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002118 ptr = dereference_symbol_descriptor(ptr);
2119 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002120 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002121 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002122 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002123 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002124 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002125 case 'h':
2126 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002127 case 'b':
2128 switch (fmt[1]) {
2129 case 'l':
2130 return bitmap_list_string(buf, end, ptr, spec, fmt);
2131 default:
2132 return bitmap_string(buf, end, ptr, spec, fmt);
2133 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002134 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2135 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002136 /* [mM]F (FDDI) */
2137 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002138 return mac_address_string(buf, end, ptr, spec, fmt);
2139 case 'I': /* Formatted IP supported
2140 * 4: 1.2.3.4
2141 * 6: 0001:0203:...:0708
2142 * 6c: 1::708 or 1::1.2.3.4
2143 */
2144 case 'i': /* Contiguous:
2145 * 4: 001.002.003.004
2146 * 6: 000102...0f
2147 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002148 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002149 case 'E':
2150 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002151 case 'U':
2152 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002153 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002154 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002155 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002156 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002157 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002158 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002159 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002160 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002161 case 'd':
2162 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002163 case 't':
2164 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002165 case 'C':
2166 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002167 case 'D':
2168 return dentry_name(buf, end,
2169 ((const struct file *)ptr)->f_path.dentry,
2170 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002171#ifdef CONFIG_BLOCK
2172 case 'g':
2173 return bdev_name(buf, end, ptr, spec, fmt);
2174#endif
2175
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002176 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002177 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002178 case 'O':
Petr Mladek798cc272019-04-17 13:53:46 +02002179 return kobject_string(buf, end, ptr, spec, fmt);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002180 case 'x':
2181 return pointer_string(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002182 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002183
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002184 /* default is to _not_ leak addresses, hash before printing */
2185 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002186}
2187
2188/*
2189 * Helper function to decode printf style format.
2190 * Each call decode a token from the format and return the
2191 * number of characters read (or likely the delta where it wants
2192 * to go on the next call).
2193 * The decoded token is returned through the parameters
2194 *
2195 * 'h', 'l', or 'L' for integer fields
2196 * 'z' support added 23/7/1999 S.H.
2197 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002198 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002199 * 't' added for ptrdiff_t
2200 *
2201 * @fmt: the format string
2202 * @type of the token returned
2203 * @flags: various flags such as +, -, # tokens..
2204 * @field_width: overwritten width
2205 * @base: base of the number (octal, hex, ...)
2206 * @precision: precision of a number
2207 * @qualifier: qualifier of a number (long, size_t, ...)
2208 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002209static noinline_for_stack
2210int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002211{
2212 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002213 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002214
2215 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002216 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002217 if (spec->field_width < 0) {
2218 spec->field_width = -spec->field_width;
2219 spec->flags |= LEFT;
2220 }
2221 spec->type = FORMAT_TYPE_NONE;
2222 goto precision;
2223 }
2224
2225 /* we finished early by reading the precision */
2226 if (spec->type == FORMAT_TYPE_PRECISION) {
2227 if (spec->precision < 0)
2228 spec->precision = 0;
2229
2230 spec->type = FORMAT_TYPE_NONE;
2231 goto qualifier;
2232 }
2233
2234 /* By default */
2235 spec->type = FORMAT_TYPE_NONE;
2236
2237 for (; *fmt ; ++fmt) {
2238 if (*fmt == '%')
2239 break;
2240 }
2241
2242 /* Return the current non-format string */
2243 if (fmt != start || !*fmt)
2244 return fmt - start;
2245
2246 /* Process flags */
2247 spec->flags = 0;
2248
2249 while (1) { /* this also skips first '%' */
2250 bool found = true;
2251
2252 ++fmt;
2253
2254 switch (*fmt) {
2255 case '-': spec->flags |= LEFT; break;
2256 case '+': spec->flags |= PLUS; break;
2257 case ' ': spec->flags |= SPACE; break;
2258 case '#': spec->flags |= SPECIAL; break;
2259 case '0': spec->flags |= ZEROPAD; break;
2260 default: found = false;
2261 }
2262
2263 if (!found)
2264 break;
2265 }
2266
2267 /* get field width */
2268 spec->field_width = -1;
2269
2270 if (isdigit(*fmt))
2271 spec->field_width = skip_atoi(&fmt);
2272 else if (*fmt == '*') {
2273 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002274 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002275 return ++fmt - start;
2276 }
2277
2278precision:
2279 /* get the precision */
2280 spec->precision = -1;
2281 if (*fmt == '.') {
2282 ++fmt;
2283 if (isdigit(*fmt)) {
2284 spec->precision = skip_atoi(&fmt);
2285 if (spec->precision < 0)
2286 spec->precision = 0;
2287 } else if (*fmt == '*') {
2288 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002289 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002290 return ++fmt - start;
2291 }
2292 }
2293
2294qualifier:
2295 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002296 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002297 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002298 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002299 qualifier = *fmt++;
2300 if (unlikely(qualifier == *fmt)) {
2301 if (qualifier == 'l') {
2302 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002303 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002304 } else if (qualifier == 'h') {
2305 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002306 ++fmt;
2307 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002308 }
2309 }
2310
2311 /* default base */
2312 spec->base = 10;
2313 switch (*fmt) {
2314 case 'c':
2315 spec->type = FORMAT_TYPE_CHAR;
2316 return ++fmt - start;
2317
2318 case 's':
2319 spec->type = FORMAT_TYPE_STR;
2320 return ++fmt - start;
2321
2322 case 'p':
2323 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002324 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002325
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002326 case '%':
2327 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2328 return ++fmt - start;
2329
2330 /* integer number formats - set up the flags and "break" */
2331 case 'o':
2332 spec->base = 8;
2333 break;
2334
2335 case 'x':
2336 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002337 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002338
2339 case 'X':
2340 spec->base = 16;
2341 break;
2342
2343 case 'd':
2344 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002345 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002346 case 'u':
2347 break;
2348
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002349 case 'n':
2350 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002351 * Since %n poses a greater security risk than
2352 * utility, treat it as any other invalid or
2353 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002354 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002355 /* Fall-through */
2356
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002357 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002358 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002359 spec->type = FORMAT_TYPE_INVALID;
2360 return fmt - start;
2361 }
2362
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002363 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002364 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002365 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002366 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2367 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002368 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002369 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002370 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002371 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002372 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002373 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2374 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002375 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002376 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2377 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002378 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002379 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2380 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002381 }
2382
2383 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002384}
2385
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002386static void
2387set_field_width(struct printf_spec *spec, int width)
2388{
2389 spec->field_width = width;
2390 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2391 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2392 }
2393}
2394
2395static void
2396set_precision(struct printf_spec *spec, int prec)
2397{
2398 spec->precision = prec;
2399 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2400 spec->precision = clamp(prec, 0, PRECISION_MAX);
2401 }
2402}
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404/**
2405 * vsnprintf - Format a string and place it in a buffer
2406 * @buf: The buffer to place the result into
2407 * @size: The size of the buffer, including the trailing null space
2408 * @fmt: The format string to use
2409 * @args: Arguments for the format string
2410 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002411 * This function generally follows C99 vsnprintf, but has some
2412 * extensions and a few limitations:
2413 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002414 * - ``%n`` is unsupported
2415 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002416 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002417 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002418 * extensive description.
2419 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002420 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002421 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * The return value is the number of characters which would
2423 * be generated for the given input, excluding the trailing
2424 * '\0', as per ISO C99. If you want to have the exact
2425 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002426 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * return is greater than or equal to @size, the resulting
2428 * string is truncated.
2429 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002430 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 */
2432int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002435 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002436 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002438 /* Reject out-of-range values early. Large positive sizes are
2439 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002440 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002444 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002446 /* Make sure end is always >= buf */
2447 if (end < buf) {
2448 end = ((void *)-1);
2449 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002452 while (*fmt) {
2453 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002454 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002455
2456 fmt += read;
2457
2458 switch (spec.type) {
2459 case FORMAT_TYPE_NONE: {
2460 int copy = read;
2461 if (str < end) {
2462 if (copy > end - str)
2463 copy = end - str;
2464 memcpy(str, old_fmt, copy);
2465 }
2466 str += read;
2467 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469
Vegard Nossumed681a92009-03-14 12:08:50 +01002470 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002471 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002474 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002475 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
André Goddard Rosad4be1512009-12-14 18:00:59 -08002478 case FORMAT_TYPE_CHAR: {
2479 char c;
2480
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002481 if (!(spec.flags & LEFT)) {
2482 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002483 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 *str = ' ';
2485 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002488 }
2489 c = (unsigned char) va_arg(args, int);
2490 if (str < end)
2491 *str = c;
2492 ++str;
2493 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002494 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002495 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002497 }
2498 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002501 case FORMAT_TYPE_STR:
2502 str = string(str, end, va_arg(args, char *), spec);
2503 break;
2504
2505 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002506 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002507 spec);
2508 while (isalnum(*fmt))
2509 fmt++;
2510 break;
2511
2512 case FORMAT_TYPE_PERCENT_CHAR:
2513 if (str < end)
2514 *str = '%';
2515 ++str;
2516 break;
2517
2518 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002519 /*
2520 * Presumably the arguments passed gcc's type
2521 * checking, but there is no safe or sane way
2522 * for us to continue parsing the format and
2523 * fetching from the va_list; the remaining
2524 * specifiers and arguments would be out of
2525 * sync.
2526 */
2527 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002528
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002529 default:
2530 switch (spec.type) {
2531 case FORMAT_TYPE_LONG_LONG:
2532 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002534 case FORMAT_TYPE_ULONG:
2535 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002537 case FORMAT_TYPE_LONG:
2538 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002540 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002541 if (spec.flags & SIGN)
2542 num = va_arg(args, ssize_t);
2543 else
2544 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002545 break;
2546 case FORMAT_TYPE_PTRDIFF:
2547 num = va_arg(args, ptrdiff_t);
2548 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002549 case FORMAT_TYPE_UBYTE:
2550 num = (unsigned char) va_arg(args, int);
2551 break;
2552 case FORMAT_TYPE_BYTE:
2553 num = (signed char) va_arg(args, int);
2554 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002555 case FORMAT_TYPE_USHORT:
2556 num = (unsigned short) va_arg(args, int);
2557 break;
2558 case FORMAT_TYPE_SHORT:
2559 num = (short) va_arg(args, int);
2560 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002561 case FORMAT_TYPE_INT:
2562 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002563 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002565 num = va_arg(args, unsigned int);
2566 }
2567
2568 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002571
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002572out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002573 if (size > 0) {
2574 if (str < end)
2575 *str = '\0';
2576 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002577 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002578 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002579
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002580 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584EXPORT_SYMBOL(vsnprintf);
2585
2586/**
2587 * vscnprintf - Format a string and place it in a buffer
2588 * @buf: The buffer to place the result into
2589 * @size: The size of the buffer, including the trailing null space
2590 * @fmt: The format string to use
2591 * @args: Arguments for the format string
2592 *
2593 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002594 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 * returns 0.
2596 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002597 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002598 *
2599 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 */
2601int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2602{
2603 int i;
2604
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002605 i = vsnprintf(buf, size, fmt, args);
2606
Anton Arapovb921c692011-01-12 16:59:49 -08002607 if (likely(i < size))
2608 return i;
2609 if (size != 0)
2610 return size - 1;
2611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613EXPORT_SYMBOL(vscnprintf);
2614
2615/**
2616 * snprintf - Format a string and place it in a buffer
2617 * @buf: The buffer to place the result into
2618 * @size: The size of the buffer, including the trailing null space
2619 * @fmt: The format string to use
2620 * @...: Arguments for the format string
2621 *
2622 * The return value is the number of characters which would be
2623 * generated for the given input, excluding the trailing null,
2624 * as per ISO C99. If the return is greater than or equal to
2625 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002626 *
2627 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002629int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 va_list args;
2632 int i;
2633
2634 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002635 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return i;
2639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640EXPORT_SYMBOL(snprintf);
2641
2642/**
2643 * scnprintf - Format a string and place it in a buffer
2644 * @buf: The buffer to place the result into
2645 * @size: The size of the buffer, including the trailing null space
2646 * @fmt: The format string to use
2647 * @...: Arguments for the format string
2648 *
2649 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002650 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 */
2652
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002653int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
2655 va_list args;
2656 int i;
2657
2658 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002659 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002661
Anton Arapovb921c692011-01-12 16:59:49 -08002662 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664EXPORT_SYMBOL(scnprintf);
2665
2666/**
2667 * vsprintf - Format a string and place it in a buffer
2668 * @buf: The buffer to place the result into
2669 * @fmt: The format string to use
2670 * @args: Arguments for the format string
2671 *
2672 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002673 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 * buffer overflows.
2675 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002676 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002677 *
2678 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 */
2680int vsprintf(char *buf, const char *fmt, va_list args)
2681{
2682 return vsnprintf(buf, INT_MAX, fmt, args);
2683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684EXPORT_SYMBOL(vsprintf);
2685
2686/**
2687 * sprintf - Format a string and place it in a buffer
2688 * @buf: The buffer to place the result into
2689 * @fmt: The format string to use
2690 * @...: Arguments for the format string
2691 *
2692 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002693 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002695 *
2696 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002698int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 va_list args;
2701 int i;
2702
2703 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002704 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return i;
2708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709EXPORT_SYMBOL(sprintf);
2710
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002711#ifdef CONFIG_BINARY_PRINTF
2712/*
2713 * bprintf service:
2714 * vbin_printf() - VA arguments to binary data
2715 * bstr_printf() - Binary data to text string
2716 */
2717
2718/**
2719 * vbin_printf - Parse a format string and place args' binary value in a buffer
2720 * @bin_buf: The buffer to place args' binary value
2721 * @size: The size of the buffer(by words(32bits), not characters)
2722 * @fmt: The format string to use
2723 * @args: Arguments for the format string
2724 *
2725 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002726 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002727 *
2728 * The return value is the number of words(32bits) which would be generated for
2729 * the given input.
2730 *
2731 * NOTE:
2732 * If the return value is greater than @size, the resulting bin_buf is NOT
2733 * valid for bstr_printf().
2734 */
2735int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2736{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002737 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002738 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002739 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002740
2741 str = (char *)bin_buf;
2742 end = (char *)(bin_buf + size);
2743
2744#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002745({ \
2746 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002747 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002748 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002749 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002750 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002751 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002752 *(u32 *)str = *(u32 *)&val8; \
2753 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002754 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002755 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002756 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002757 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002758 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002759 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002760 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002761 *(typeof(type) *)str = (type)(long)val4; \
2762 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002763 } \
2764 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002765 value; \
2766})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002767
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002768 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002769 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002770
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002771 fmt += read;
2772
2773 switch (spec.type) {
2774 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002775 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002776 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002777 case FORMAT_TYPE_INVALID:
2778 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002779
Vegard Nossumed681a92009-03-14 12:08:50 +01002780 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002781 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002782 width = (int)save_arg(int);
2783 /* Pointers may require the width */
2784 if (*fmt == 'p')
2785 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002786 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002787
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002788 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002789 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002790 break;
2791
2792 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002793 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002794 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002795 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002796
Petr Mladek3e5903e2019-04-17 13:53:48 +02002797 err_msg = check_pointer_msg(save_str);
2798 if (err_msg)
2799 save_str = err_msg;
2800
André Goddard Rosa6c356632009-12-14 18:00:56 -08002801 len = strlen(save_str) + 1;
2802 if (str + len < end)
2803 memcpy(str, save_str, len);
2804 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002805 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002806 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002807
2808 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002809 /* Dereferenced pointers must be done now */
2810 switch (*fmt) {
2811 /* Dereference of functions is still OK */
2812 case 'S':
2813 case 's':
2814 case 'F':
2815 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002816 case 'x':
2817 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002818 save_arg(void *);
2819 break;
2820 default:
2821 if (!isalnum(*fmt)) {
2822 save_arg(void *);
2823 break;
2824 }
2825 str = pointer(fmt, str, end, va_arg(args, void *),
2826 spec);
2827 if (str + 1 < end)
2828 *str++ = '\0';
2829 else
2830 end[-1] = '\0'; /* Must be nul terminated */
2831 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002832 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002833 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002834 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002835 break;
2836
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002837 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002838 switch (spec.type) {
2839
2840 case FORMAT_TYPE_LONG_LONG:
2841 save_arg(long long);
2842 break;
2843 case FORMAT_TYPE_ULONG:
2844 case FORMAT_TYPE_LONG:
2845 save_arg(unsigned long);
2846 break;
2847 case FORMAT_TYPE_SIZE_T:
2848 save_arg(size_t);
2849 break;
2850 case FORMAT_TYPE_PTRDIFF:
2851 save_arg(ptrdiff_t);
2852 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002853 case FORMAT_TYPE_UBYTE:
2854 case FORMAT_TYPE_BYTE:
2855 save_arg(char);
2856 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002857 case FORMAT_TYPE_USHORT:
2858 case FORMAT_TYPE_SHORT:
2859 save_arg(short);
2860 break;
2861 default:
2862 save_arg(int);
2863 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002864 }
2865 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002866
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002867out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002868 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002869#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002870}
2871EXPORT_SYMBOL_GPL(vbin_printf);
2872
2873/**
2874 * bstr_printf - Format a string from binary arguments and place it in a buffer
2875 * @buf: The buffer to place the result into
2876 * @size: The size of the buffer, including the trailing null space
2877 * @fmt: The format string to use
2878 * @bin_buf: Binary arguments for the format string
2879 *
2880 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2881 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2882 * a binary buffer that generated by vbin_printf.
2883 *
2884 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002885 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002886 *
2887 * The return value is the number of characters which would
2888 * be generated for the given input, excluding the trailing
2889 * '\0', as per ISO C99. If you want to have the exact
2890 * number of characters written into @buf as return value
2891 * (not including the trailing '\0'), use vscnprintf(). If the
2892 * return is greater than or equal to @size, the resulting
2893 * string is truncated.
2894 */
2895int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2896{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002897 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002898 char *str, *end;
2899 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002900
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002901 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002902 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002903
2904 str = buf;
2905 end = buf + size;
2906
2907#define get_arg(type) \
2908({ \
2909 typeof(type) value; \
2910 if (sizeof(type) == 8) { \
2911 args = PTR_ALIGN(args, sizeof(u32)); \
2912 *(u32 *)&value = *(u32 *)args; \
2913 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2914 } else { \
2915 args = PTR_ALIGN(args, sizeof(type)); \
2916 value = *(typeof(type) *)args; \
2917 } \
2918 args += sizeof(type); \
2919 value; \
2920})
2921
2922 /* Make sure end is always >= buf */
2923 if (end < buf) {
2924 end = ((void *)-1);
2925 size = end - buf;
2926 }
2927
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002928 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002929 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002930 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002931
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002932 fmt += read;
2933
2934 switch (spec.type) {
2935 case FORMAT_TYPE_NONE: {
2936 int copy = read;
2937 if (str < end) {
2938 if (copy > end - str)
2939 copy = end - str;
2940 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002941 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002942 str += read;
2943 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002944 }
2945
Vegard Nossumed681a92009-03-14 12:08:50 +01002946 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002947 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002948 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002949
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002950 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002951 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002952 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002953
André Goddard Rosad4be1512009-12-14 18:00:59 -08002954 case FORMAT_TYPE_CHAR: {
2955 char c;
2956
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002957 if (!(spec.flags & LEFT)) {
2958 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002959 if (str < end)
2960 *str = ' ';
2961 ++str;
2962 }
2963 }
2964 c = (unsigned char) get_arg(char);
2965 if (str < end)
2966 *str = c;
2967 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002968 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002969 if (str < end)
2970 *str = ' ';
2971 ++str;
2972 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002973 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002974 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002975
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002976 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002977 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002978 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002979 str = string(str, end, (char *)str_arg, spec);
2980 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002981 }
2982
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002983 case FORMAT_TYPE_PTR: {
2984 bool process = false;
2985 int copy, len;
2986 /* Non function dereferences were already done */
2987 switch (*fmt) {
2988 case 'S':
2989 case 's':
2990 case 'F':
2991 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002992 case 'x':
2993 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002994 process = true;
2995 break;
2996 default:
2997 if (!isalnum(*fmt)) {
2998 process = true;
2999 break;
3000 }
3001 /* Pointer dereference was already processed */
3002 if (str < end) {
3003 len = copy = strlen(args);
3004 if (copy > end - str)
3005 copy = end - str;
3006 memcpy(str, args, copy);
3007 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003008 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003009 }
3010 }
3011 if (process)
3012 str = pointer(fmt, str, end, get_arg(void *), spec);
3013
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003014 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003015 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003016 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003017 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003018
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003019 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003020 if (str < end)
3021 *str = '%';
3022 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003023 break;
3024
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003025 case FORMAT_TYPE_INVALID:
3026 goto out;
3027
André Goddard Rosad4be1512009-12-14 18:00:59 -08003028 default: {
3029 unsigned long long num;
3030
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003031 switch (spec.type) {
3032
3033 case FORMAT_TYPE_LONG_LONG:
3034 num = get_arg(long long);
3035 break;
3036 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003037 case FORMAT_TYPE_LONG:
3038 num = get_arg(unsigned long);
3039 break;
3040 case FORMAT_TYPE_SIZE_T:
3041 num = get_arg(size_t);
3042 break;
3043 case FORMAT_TYPE_PTRDIFF:
3044 num = get_arg(ptrdiff_t);
3045 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003046 case FORMAT_TYPE_UBYTE:
3047 num = get_arg(unsigned char);
3048 break;
3049 case FORMAT_TYPE_BYTE:
3050 num = get_arg(signed char);
3051 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003052 case FORMAT_TYPE_USHORT:
3053 num = get_arg(unsigned short);
3054 break;
3055 case FORMAT_TYPE_SHORT:
3056 num = get_arg(short);
3057 break;
3058 case FORMAT_TYPE_UINT:
3059 num = get_arg(unsigned int);
3060 break;
3061 default:
3062 num = get_arg(int);
3063 }
3064
3065 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003066 } /* default: */
3067 } /* switch(spec.type) */
3068 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003069
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003070out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003071 if (size > 0) {
3072 if (str < end)
3073 *str = '\0';
3074 else
3075 end[-1] = '\0';
3076 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003077
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003078#undef get_arg
3079
3080 /* the trailing null byte doesn't count towards the total */
3081 return str - buf;
3082}
3083EXPORT_SYMBOL_GPL(bstr_printf);
3084
3085/**
3086 * bprintf - Parse a format string and place args' binary value in a buffer
3087 * @bin_buf: The buffer to place args' binary value
3088 * @size: The size of the buffer(by words(32bits), not characters)
3089 * @fmt: The format string to use
3090 * @...: Arguments for the format string
3091 *
3092 * The function returns the number of words(u32) written
3093 * into @bin_buf.
3094 */
3095int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3096{
3097 va_list args;
3098 int ret;
3099
3100 va_start(args, fmt);
3101 ret = vbin_printf(bin_buf, size, fmt, args);
3102 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003103
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003104 return ret;
3105}
3106EXPORT_SYMBOL_GPL(bprintf);
3107
3108#endif /* CONFIG_BINARY_PRINTF */
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110/**
3111 * vsscanf - Unformat a buffer into a list of arguments
3112 * @buf: input buffer
3113 * @fmt: format of buffer
3114 * @args: arguments
3115 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003116int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
3118 const char *str = buf;
3119 char *next;
3120 char digit;
3121 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003122 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003123 unsigned int base;
3124 union {
3125 long long s;
3126 unsigned long long u;
3127 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003128 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003129 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Jan Beulichda990752012-10-04 17:13:24 -07003131 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 /* skip any white space in format */
3133 /* white space in format matchs any amount of
3134 * white space, including none, in the input.
3135 */
3136 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003137 fmt = skip_spaces(++fmt);
3138 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
3140
3141 /* anything that is not a conversion must match exactly */
3142 if (*fmt != '%' && *fmt) {
3143 if (*fmt++ != *str++)
3144 break;
3145 continue;
3146 }
3147
3148 if (!*fmt)
3149 break;
3150 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 /* skip this conversion.
3153 * advance both strings to next white space
3154 */
3155 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003156 if (!*str)
3157 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003158 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3159 /* '%*[' not yet supported, invalid format */
3160 if (*fmt == '[')
3161 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 while (!isspace(*str) && *str)
3165 str++;
3166 continue;
3167 }
3168
3169 /* get field width */
3170 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003171 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003173 if (field_width <= 0)
3174 break;
3175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 /* get conversion qualifier */
3178 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003179 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003180 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 qualifier = *fmt++;
3182 if (unlikely(qualifier == *fmt)) {
3183 if (qualifier == 'h') {
3184 qualifier = 'H';
3185 fmt++;
3186 } else if (qualifier == 'l') {
3187 qualifier = 'L';
3188 fmt++;
3189 }
3190 }
3191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Jan Beulichda990752012-10-04 17:13:24 -07003193 if (!*fmt)
3194 break;
3195
3196 if (*fmt == 'n') {
3197 /* return number of characters read so far */
3198 *va_arg(args, int *) = str - buf;
3199 ++fmt;
3200 continue;
3201 }
3202
3203 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 break;
3205
André Goddard Rosad4be1512009-12-14 18:00:59 -08003206 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003207 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003208
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003209 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 case 'c':
3211 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003212 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (field_width == -1)
3214 field_width = 1;
3215 do {
3216 *s++ = *str++;
3217 } while (--field_width > 0 && *str);
3218 num++;
3219 }
3220 continue;
3221 case 's':
3222 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003223 char *s = (char *)va_arg(args, char *);
3224 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003225 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003227 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003230 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 *s = '\0';
3233 num++;
3234 }
3235 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003236 /*
3237 * Warning: This implementation of the '[' conversion specifier
3238 * deviates from its glibc counterpart in the following ways:
3239 * (1) It does NOT support ranges i.e. '-' is NOT a special
3240 * character
3241 * (2) It cannot match the closing bracket ']' itself
3242 * (3) A field width is required
3243 * (4) '%*[' (discard matching input) is currently not supported
3244 *
3245 * Example usage:
3246 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3247 * buf1, buf2, buf3);
3248 * if (ret < 3)
3249 * // etc..
3250 */
3251 case '[':
3252 {
3253 char *s = (char *)va_arg(args, char *);
3254 DECLARE_BITMAP(set, 256) = {0};
3255 unsigned int len = 0;
3256 bool negate = (*fmt == '^');
3257
3258 /* field width is required */
3259 if (field_width == -1)
3260 return num;
3261
3262 if (negate)
3263 ++fmt;
3264
3265 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3266 set_bit((u8)*fmt, set);
3267
3268 /* no ']' or no character set found */
3269 if (!*fmt || !len)
3270 return num;
3271 ++fmt;
3272
3273 if (negate) {
3274 bitmap_complement(set, set, 256);
3275 /* exclude null '\0' byte */
3276 clear_bit(0, set);
3277 }
3278
3279 /* match must be non-empty */
3280 if (!test_bit((u8)*str, set))
3281 return num;
3282
3283 while (test_bit((u8)*str, set) && field_width--)
3284 *s++ = *str++;
3285 *s = '\0';
3286 ++num;
3287 }
3288 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 case 'o':
3290 base = 8;
3291 break;
3292 case 'x':
3293 case 'X':
3294 base = 16;
3295 break;
3296 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003297 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003298 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003300 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003301 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 case 'u':
3303 break;
3304 case '%':
3305 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003306 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return num;
3308 continue;
3309 default:
3310 /* invalid format; stop here */
3311 return num;
3312 }
3313
3314 /* have some sort of integer conversion.
3315 * first, skip white space in buffer.
3316 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003317 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319 digit = *str;
3320 if (is_sign && digit == '-')
3321 digit = *(str + 1);
3322
3323 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003324 || (base == 16 && !isxdigit(digit))
3325 || (base == 10 && !isdigit(digit))
3326 || (base == 8 && (!isdigit(digit) || digit > '7'))
3327 || (base == 0 && !isdigit(digit)))
3328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Jan Beulich53809752012-12-17 16:01:31 -08003330 if (is_sign)
3331 val.s = qualifier != 'L' ?
3332 simple_strtol(str, &next, base) :
3333 simple_strtoll(str, &next, base);
3334 else
3335 val.u = qualifier != 'L' ?
3336 simple_strtoul(str, &next, base) :
3337 simple_strtoull(str, &next, base);
3338
3339 if (field_width > 0 && next - str > field_width) {
3340 if (base == 0)
3341 _parse_integer_fixup_radix(str, &base);
3342 while (next - str > field_width) {
3343 if (is_sign)
3344 val.s = div_s64(val.s, base);
3345 else
3346 val.u = div_u64(val.u, base);
3347 --next;
3348 }
3349 }
3350
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003351 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003353 if (is_sign)
3354 *va_arg(args, signed char *) = val.s;
3355 else
3356 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 break;
3358 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003359 if (is_sign)
3360 *va_arg(args, short *) = val.s;
3361 else
3362 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 break;
3364 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003365 if (is_sign)
3366 *va_arg(args, long *) = val.s;
3367 else
3368 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 break;
3370 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003371 if (is_sign)
3372 *va_arg(args, long long *) = val.s;
3373 else
3374 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003377 *va_arg(args, size_t *) = val.u;
3378 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 default:
Jan Beulich53809752012-12-17 16:01:31 -08003380 if (is_sign)
3381 *va_arg(args, int *) = val.s;
3382 else
3383 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 break;
3385 }
3386 num++;
3387
3388 if (!next)
3389 break;
3390 str = next;
3391 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 return num;
3394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395EXPORT_SYMBOL(vsscanf);
3396
3397/**
3398 * sscanf - Unformat a buffer into a list of arguments
3399 * @buf: input buffer
3400 * @fmt: formatting of buffer
3401 * @...: resulting arguments
3402 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003403int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
3405 va_list args;
3406 int i;
3407
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003408 va_start(args, fmt);
3409 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return i;
3413}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414EXPORT_SYMBOL(sscanf);