blob: b54d252b398e0b0239f409c6b73ae03fe3f2768a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/lib/vsprintf.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9/*
10 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 */
12
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080013/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
15 * - changed to provide snprintf and vsnprintf functions
16 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
17 * - scnprintf and vscnprintf
18 */
19
20#include <stdarg.h>
Rasmus Villemoesef27ac12019-03-07 16:27:03 -080021#include <linux/build_bug.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070022#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070023#include <linux/clk-provider.h>
Rasmus Villemoes57f56772019-10-15 21:07:05 +020024#include <linux/errname.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050025#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/ctype.h>
29#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070030#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080031#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070032#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110033#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040034#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080035#include <linux/cred.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020036#include <linux/rtc.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070037#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020038#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000039#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110040#include <linux/siphash.h>
41#include <linux/compiler.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040042#ifdef CONFIG_BLOCK
43#include <linux/blkdev.h>
44#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070046#include "../mm/internal.h" /* For the trace_print_flags arrays */
47
Tim Schmielau4e57b682005-10-30 15:03:48 -080048#include <asm/page.h> /* for PAGE_SIZE */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070049#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andy Shevchenko71dca952014-10-13 15:55:18 -070051#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070052#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * simple_strtoull - convert a string to an unsigned long long
56 * @cp: The start of the string
57 * @endp: A pointer to the end of the parsed string will be placed here
58 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080059 *
60 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
Harvey Harrison22d27052008-10-16 13:40:35 -070062unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070064 unsigned long long result;
65 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070067 cp = _parse_integer_fixup_radix(cp, &base);
68 rv = _parse_integer(cp, base, &result);
69 /* FIXME */
70 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (endp)
73 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return result;
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077EXPORT_SYMBOL(simple_strtoull);
78
79/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080080 * simple_strtoul - convert a string to an unsigned long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080084 *
85 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080086 */
87unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
88{
89 return simple_strtoull(cp, endp, base);
90}
91EXPORT_SYMBOL(simple_strtoul);
92
93/**
94 * simple_strtol - convert a string to a signed long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080098 *
99 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -0800100 */
101long simple_strtol(const char *cp, char **endp, unsigned int base)
102{
103 if (*cp == '-')
104 return -simple_strtoul(cp + 1, endp, base);
105
106 return simple_strtoul(cp, endp, base);
107}
108EXPORT_SYMBOL(simple_strtol);
109
110/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * simple_strtoll - convert a string to a signed long long
112 * @cp: The start of the string
113 * @endp: A pointer to the end of the parsed string will be placed here
114 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800115 *
116 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700118long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800120 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700121 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800122
Harvey Harrison22d27052008-10-16 13:40:35 -0700123 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400125EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Joe Perchescf3b4292010-05-24 14:33:16 -0700127static noinline_for_stack
128int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800130 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800132 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800134 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return i;
137}
138
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700139/*
140 * Decimal conversion is by far the most typical, and is used for
141 * /proc and /sys data. This directly impacts e.g. top performance
142 * with many processes running. We optimize it for speed by emitting
143 * two characters at a time, using a 200 byte lookup table. This
144 * roughly halves the number of multiplications compared to computing
145 * the digits one at a time. Implementation strongly inspired by the
146 * previous version, which in turn used ideas described at
147 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
148 * from the author, Douglas W. Jones).
149 *
150 * It turns out there is precisely one 26 bit fixed-point
151 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
152 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
153 * range happens to be somewhat larger (x <= 1073741898), but that's
154 * irrelevant for our purpose.
155 *
156 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
157 * need a 32x32->64 bit multiply, so we simply use the same constant.
158 *
159 * For dividing a number in the range [100, 10^4-1] by 100, there are
160 * several options. The simplest is (x * 0x147b) >> 19, which is valid
161 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700162 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700163
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700164static const u16 decpair[100] = {
165#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
166 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
167 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
168 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
169 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
170 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
171 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
172 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
173 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
174 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
175 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
176#undef _
177};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700178
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700179/*
180 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700181 * immediately jump to out_r where two 0s would be written but only
182 * one of them accounted for in buf. This is needed by ip4_string
183 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700184*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700185static noinline_for_stack
186char *put_dec_trunc8(char *buf, unsigned r)
187{
188 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700189
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700190 /* 1 <= r < 10^8 */
191 if (r < 100)
192 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700193
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700194 /* 100 <= r < 10^8 */
195 q = (r * (u64)0x28f5c29) >> 32;
196 *((u16 *)buf) = decpair[r - 100*q];
197 buf += 2;
198
199 /* 1 <= q < 10^6 */
200 if (q < 100)
201 goto out_q;
202
203 /* 100 <= q < 10^6 */
204 r = (q * (u64)0x28f5c29) >> 32;
205 *((u16 *)buf) = decpair[q - 100*r];
206 buf += 2;
207
208 /* 1 <= r < 10^4 */
209 if (r < 100)
210 goto out_r;
211
212 /* 100 <= r < 10^4 */
213 q = (r * 0x147b) >> 19;
214 *((u16 *)buf) = decpair[r - 100*q];
215 buf += 2;
216out_q:
217 /* 1 <= q < 100 */
218 r = q;
219out_r:
220 /* 1 <= r < 100 */
221 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700222 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700223 return buf;
224}
225
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700226#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
227static noinline_for_stack
228char *put_dec_full8(char *buf, unsigned r)
229{
230 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700231
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700232 /* 0 <= r < 10^8 */
233 q = (r * (u64)0x28f5c29) >> 32;
234 *((u16 *)buf) = decpair[r - 100*q];
235 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700236
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700237 /* 0 <= q < 10^6 */
238 r = (q * (u64)0x28f5c29) >> 32;
239 *((u16 *)buf) = decpair[q - 100*r];
240 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700241
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700242 /* 0 <= r < 10^4 */
243 q = (r * 0x147b) >> 19;
244 *((u16 *)buf) = decpair[r - 100*q];
245 buf += 2;
246
247 /* 0 <= q < 100 */
248 *((u16 *)buf) = decpair[q];
249 buf += 2;
250 return buf;
251}
252
253static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700254char *put_dec(char *buf, unsigned long long n)
255{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700256 if (n >= 100*1000*1000)
257 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
258 /* 1 <= n <= 1.6e11 */
259 if (n >= 100*1000*1000)
260 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
261 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700262 return put_dec_trunc8(buf, n);
263}
264
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700265#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700266
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700267static void
268put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700269{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700270 unsigned q;
271
272 /* 0 <= r < 10^4 */
273 q = (r * 0x147b) >> 19;
274 *((u16 *)buf) = decpair[r - 100*q];
275 buf += 2;
276 /* 0 <= q < 100 */
277 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700278}
279
280/*
281 * Call put_dec_full4 on x % 10000, return x / 10000.
282 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
283 * holds for all x < 1,128,869,999. The largest value this
284 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700285 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700286 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700287static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700288unsigned put_dec_helper4(char *buf, unsigned x)
289{
290 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
291
292 put_dec_full4(buf, x - q * 10000);
293 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700294}
295
296/* Based on code by Douglas W. Jones found at
297 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
298 * (with permission from the author).
299 * Performs no 64-bit division and hence should be fast on 32-bit machines.
300 */
301static
302char *put_dec(char *buf, unsigned long long n)
303{
304 uint32_t d3, d2, d1, q, h;
305
306 if (n < 100*1000*1000)
307 return put_dec_trunc8(buf, n);
308
309 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
310 h = (n >> 32);
311 d2 = (h ) & 0xffff;
312 d3 = (h >> 16); /* implicit "& 0xffff" */
313
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700314 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
315 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700316 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700317 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700318
George Spelvin23591722012-10-04 17:12:29 -0700319 q += 7671 * d3 + 9496 * d2 + 6 * d1;
320 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700321
George Spelvin23591722012-10-04 17:12:29 -0700322 q += 4749 * d3 + 42 * d2;
323 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700324
George Spelvin23591722012-10-04 17:12:29 -0700325 q += 281 * d3;
326 buf += 12;
327 if (q)
328 buf = put_dec_trunc8(buf, q);
329 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700330 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800331
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700332 return buf;
333}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700334
335#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700336
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700337/*
338 * Convert passed number to decimal string.
339 * Returns the length of string. On buffer overflow, returns 0.
340 *
341 * If speed is not important, use snprintf(). It's easy to read the code.
342 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700343int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700344{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700345 /* put_dec requires 2-byte alignment of the buffer. */
346 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700347 int idx, len;
348
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700349 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
350 if (num <= 9) {
351 tmp[0] = '0' + num;
352 len = 1;
353 } else {
354 len = put_dec(tmp, num) - tmp;
355 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700356
Andrei Vagind1be35c2018-04-10 16:31:16 -0700357 if (len > size || width > size)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700358 return 0;
Andrei Vagind1be35c2018-04-10 16:31:16 -0700359
360 if (width > len) {
361 width = width - len;
362 for (idx = 0; idx < width; idx++)
363 buf[idx] = ' ';
364 } else {
365 width = 0;
366 }
367
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700368 for (idx = 0; idx < len; ++idx)
Andrei Vagind1be35c2018-04-10 16:31:16 -0700369 buf[idx + width] = tmp[len - idx - 1];
370
371 return len + width;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700372}
373
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700374#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700375#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376#define PLUS 4 /* show plus */
377#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700378#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700379#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
380#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100382enum format_type {
383 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100384 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100385 FORMAT_TYPE_PRECISION,
386 FORMAT_TYPE_CHAR,
387 FORMAT_TYPE_STR,
388 FORMAT_TYPE_PTR,
389 FORMAT_TYPE_PERCENT_CHAR,
390 FORMAT_TYPE_INVALID,
391 FORMAT_TYPE_LONG_LONG,
392 FORMAT_TYPE_ULONG,
393 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800394 FORMAT_TYPE_UBYTE,
395 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100396 FORMAT_TYPE_USHORT,
397 FORMAT_TYPE_SHORT,
398 FORMAT_TYPE_UINT,
399 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100400 FORMAT_TYPE_SIZE_T,
401 FORMAT_TYPE_PTRDIFF
402};
403
404struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800405 unsigned int type:8; /* format_type enum */
406 signed int field_width:24; /* width of output field */
407 unsigned int flags:8; /* flags to number() */
408 unsigned int base:8; /* number base, 8, 10 or 16 only */
409 signed int precision:16; /* # of digits/chars */
410} __packed;
Rasmus Villemoesef27ac12019-03-07 16:27:03 -0800411static_assert(sizeof(struct printf_spec) == 8);
412
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800413#define FIELD_WIDTH_MAX ((1 << 23) - 1)
414#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100415
Joe Perchescf3b4292010-05-24 14:33:16 -0700416static noinline_for_stack
417char *number(char *buf, char *end, unsigned long long num,
418 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700420 /* put_dec requires 2-byte alignment of the buffer. */
421 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100422 char sign;
423 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100424 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700426 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800427 int field_width = spec.field_width;
428 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100430 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
431 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100432 locase = (spec.flags & SMALL);
433 if (spec.flags & LEFT)
434 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100436 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800437 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800439 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800440 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100441 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800443 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100444 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800446 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700449 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100450 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800451 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700452 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800453 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700455
456 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700458 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700459 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100460 else if (spec.base != 10) { /* 8 or 16 */
461 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700462 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800463
464 if (spec.base == 16)
465 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700466 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700467 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700468 num >>= shift;
469 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700470 } else { /* base 10 */
471 i = put_dec(tmp, num) - tmp;
472 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700473
474 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800475 if (i > precision)
476 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700477 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800478 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700479 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800480 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700481 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *buf = ' ';
483 ++buf;
484 }
485 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700486 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700488 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 *buf = sign;
490 ++buf;
491 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700492 /* "0x" / "0" prefix */
493 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700494 if (spec.base == 16 || !is_zero) {
495 if (buf < end)
496 *buf = '0';
497 ++buf;
498 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100499 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700500 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100501 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 ++buf;
503 }
504 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700505 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100506 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700507 char c = ' ' + (spec.flags & ZEROPAD);
508 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800509 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700510 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *buf = c;
512 ++buf;
513 }
514 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700515 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800516 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700517 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *buf = '0';
519 ++buf;
520 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700521 /* actual digits of result */
522 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700523 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *buf = tmp[i];
525 ++buf;
526 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700527 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800528 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700529 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *buf = ' ';
531 ++buf;
532 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return buf;
535}
536
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800537static noinline_for_stack
538char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
539{
540 struct printf_spec spec;
541
542 spec.type = FORMAT_TYPE_PTR;
543 spec.field_width = 2 + 2 * size; /* 0x + hex */
544 spec.flags = SPECIAL | SMALL | ZEROPAD;
545 spec.base = 16;
546 spec.precision = -1;
547
548 return number(buf, end, num, spec);
549}
550
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800551static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400552{
553 size_t size;
554 if (buf >= end) /* nowhere to put anything */
555 return;
556 size = end - buf;
557 if (size <= spaces) {
558 memset(buf, ' ', size);
559 return;
560 }
561 if (len) {
562 if (len > size - spaces)
563 len = size - spaces;
564 memmove(buf + spaces, buf, len);
565 }
566 memset(buf, ' ', spaces);
567}
568
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800569/*
570 * Handle field width padding for a string.
571 * @buf: current buffer position
572 * @n: length of string
573 * @end: end of output buffer
574 * @spec: for field width and flags
575 * Returns: new buffer position after padding.
576 */
577static noinline_for_stack
578char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
579{
580 unsigned spaces;
581
582 if (likely(n >= spec.field_width))
583 return buf;
584 /* we want to pad the sucker */
585 spaces = spec.field_width - n;
586 if (!(spec.flags & LEFT)) {
587 move_right(buf - n, end, n, spaces);
588 return buf + spaces;
589 }
590 while (spaces--) {
591 if (buf < end)
592 *buf = ' ';
593 ++buf;
594 }
595 return buf;
596}
597
Petr Mladekd529ac42019-04-17 13:53:43 +0200598/* Handle string from a well known address. */
599static char *string_nocheck(char *buf, char *end, const char *s,
600 struct printf_spec spec)
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800601{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800602 int len = 0;
Youngmin Namb314dd42019-06-10 16:47:07 +0900603 int lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800604
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800605 while (lim--) {
606 char c = *s++;
607 if (!c)
608 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800609 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800610 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800611 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800612 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800613 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800614 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800615}
616
Rasmus Villemoes57f56772019-10-15 21:07:05 +0200617static char *err_ptr(char *buf, char *end, void *ptr,
618 struct printf_spec spec)
619{
620 int err = PTR_ERR(ptr);
621 const char *sym = errname(err);
622
623 if (sym)
624 return string_nocheck(buf, end, sym, spec);
625
626 /*
627 * Somebody passed ERR_PTR(-1234) or some other non-existing
628 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
629 * printing it as its decimal representation.
630 */
631 spec.flags |= SIGN;
632 spec.base = 10;
633 return number(buf, end, err, spec);
634}
635
Petr Mladekc8c3b582019-04-17 13:53:50 +0200636/* Be careful: error messages must fit into the given buffer. */
637static char *error_string(char *buf, char *end, const char *s,
638 struct printf_spec spec)
639{
640 /*
641 * Hard limit to avoid a completely insane messages. It actually
642 * works pretty well because most error messages are in
643 * the many pointer format modifiers.
644 */
645 if (spec.precision == -1)
646 spec.precision = 2 * sizeof(void *);
647
648 return string_nocheck(buf, end, s, spec);
649}
650
Petr Mladek3e5903e2019-04-17 13:53:48 +0200651/*
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200652 * Do not call any complex external code here. Nested printk()/vsprintf()
653 * might cause infinite loops. Failures might break printk() and would
654 * be hard to debug.
Petr Mladek3e5903e2019-04-17 13:53:48 +0200655 */
656static const char *check_pointer_msg(const void *ptr)
657{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200658 if (!ptr)
659 return "(null)";
660
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200661 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
Petr Mladek3e5903e2019-04-17 13:53:48 +0200662 return "(efault)";
663
664 return NULL;
665}
666
667static int check_pointer(char **buf, char *end, const void *ptr,
668 struct printf_spec spec)
669{
670 const char *err_msg;
671
672 err_msg = check_pointer_msg(ptr);
673 if (err_msg) {
Petr Mladekc8c3b582019-04-17 13:53:50 +0200674 *buf = error_string(*buf, end, err_msg, spec);
Petr Mladek3e5903e2019-04-17 13:53:48 +0200675 return -EFAULT;
676 }
677
678 return 0;
679}
680
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800681static noinline_for_stack
Petr Mladekd529ac42019-04-17 13:53:43 +0200682char *string(char *buf, char *end, const char *s,
683 struct printf_spec spec)
684{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200685 if (check_pointer(&buf, end, s, spec))
686 return buf;
Petr Mladekd529ac42019-04-17 13:53:43 +0200687
688 return string_nocheck(buf, end, s, spec);
689}
690
YueHaibingce9d3ec2019-04-27 00:46:30 +0800691static char *pointer_string(char *buf, char *end,
692 const void *ptr,
693 struct printf_spec spec)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200694{
695 spec.base = 16;
696 spec.flags |= SMALL;
697 if (spec.field_width == -1) {
698 spec.field_width = 2 * sizeof(ptr);
699 spec.flags |= ZEROPAD;
700 }
701
702 return number(buf, end, (unsigned long int)ptr, spec);
703}
704
705/* Make pointers available for printing early in the boot sequence. */
706static int debug_boot_weak_hash __ro_after_init;
707
708static int __init debug_boot_weak_hash_enable(char *str)
709{
710 debug_boot_weak_hash = 1;
711 pr_info("debug_boot_weak_hash enabled\n");
712 return 0;
713}
714early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
715
716static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
717static siphash_key_t ptr_key __read_mostly;
718
719static void enable_ptr_key_workfn(struct work_struct *work)
720{
721 get_random_bytes(&ptr_key, sizeof(ptr_key));
722 /* Needs to run from preemptible context */
723 static_branch_disable(&not_filled_random_ptr_key);
724}
725
726static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
727
728static void fill_random_ptr_key(struct random_ready_callback *unused)
729{
730 /* This may be in an interrupt handler. */
731 queue_work(system_unbound_wq, &enable_ptr_key_work);
732}
733
734static struct random_ready_callback random_ready = {
735 .func = fill_random_ptr_key
736};
737
738static int __init initialize_ptr_random(void)
739{
740 int key_size = sizeof(ptr_key);
741 int ret;
742
743 /* Use hw RNG if available. */
744 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
745 static_branch_disable(&not_filled_random_ptr_key);
746 return 0;
747 }
748
749 ret = add_random_ready_callback(&random_ready);
750 if (!ret) {
751 return 0;
752 } else if (ret == -EALREADY) {
753 /* This is in preemptible context */
754 enable_ptr_key_workfn(&enable_ptr_key_work);
755 return 0;
756 }
757
758 return ret;
759}
760early_initcall(initialize_ptr_random);
761
762/* Maps a pointer to a 32 bit unique identifier. */
763static char *ptr_to_id(char *buf, char *end, const void *ptr,
764 struct printf_spec spec)
765{
766 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
767 unsigned long hashval;
768
769 /* When debugging early boot use non-cryptographically secure hash. */
770 if (unlikely(debug_boot_weak_hash)) {
771 hashval = hash_long((unsigned long)ptr, 32);
772 return pointer_string(buf, end, (const void *)hashval, spec);
773 }
774
775 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
776 spec.field_width = 2 * sizeof(ptr);
777 /* string length must be less than default_width */
Petr Mladekc8c3b582019-04-17 13:53:50 +0200778 return error_string(buf, end, str, spec);
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200779 }
780
781#ifdef CONFIG_64BIT
782 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
783 /*
784 * Mask off the first 32 bits, this makes explicit that we have
785 * modified the address (and 32 bits is plenty for a unique ID).
786 */
787 hashval = hashval & 0xffffffff;
788#else
789 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
790#endif
791 return pointer_string(buf, end, (const void *)hashval, spec);
792}
793
Petr Mladek6eea2422019-04-17 13:53:41 +0200794int kptr_restrict __read_mostly;
795
796static noinline_for_stack
797char *restricted_pointer(char *buf, char *end, const void *ptr,
798 struct printf_spec spec)
799{
800 switch (kptr_restrict) {
801 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200802 /* Handle as %p, hash and do _not_ leak addresses. */
803 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200804 case 1: {
805 const struct cred *cred;
806
807 /*
808 * kptr_restrict==1 cannot be used in IRQ context
809 * because its test for CAP_SYSLOG would be meaningless.
810 */
811 if (in_irq() || in_serving_softirq() || in_nmi()) {
812 if (spec.field_width == -1)
813 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200814 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200815 }
816
817 /*
818 * Only print the real pointer value if the current
819 * process has CAP_SYSLOG and is running with the
820 * same credentials it started with. This is because
821 * access to files is checked at open() time, but %pK
822 * checks permission at read() time. We don't want to
823 * leak pointer values if a binary opens a file using
824 * %pK and then elevates privileges before reading it.
825 */
826 cred = current_cred();
827 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
828 !uid_eq(cred->euid, cred->uid) ||
829 !gid_eq(cred->egid, cred->gid))
830 ptr = NULL;
831 break;
832 }
833 case 2:
834 default:
835 /* Always print 0's for %pK */
836 ptr = NULL;
837 break;
838 }
839
840 return pointer_string(buf, end, ptr, spec);
841}
842
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200843static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400844char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
845 const char *fmt)
846{
847 const char *array[4], *s;
848 const struct dentry *p;
849 int depth;
850 int i, n;
851
852 switch (fmt[1]) {
853 case '2': case '3': case '4':
854 depth = fmt[1] - '0';
855 break;
856 default:
857 depth = 1;
858 }
859
860 rcu_read_lock();
861 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200862 if (check_pointer(&buf, end, d, spec)) {
863 rcu_read_unlock();
864 return buf;
865 }
866
Mark Rutland6aa7de02017-10-23 14:07:29 -0700867 p = READ_ONCE(d->d_parent);
868 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400869 if (p == d) {
870 if (i)
871 array[i] = "";
872 i++;
873 break;
874 }
875 }
876 s = array[--i];
877 for (n = 0; n != spec.precision; n++, buf++) {
878 char c = *s++;
879 if (!c) {
880 if (!i)
881 break;
882 c = '/';
883 s = array[--i];
884 }
885 if (buf < end)
886 *buf = c;
887 }
888 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800889 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400890}
891
Jia He36594b32019-08-09 09:24:56 +0800892static noinline_for_stack
893char *file_dentry_name(char *buf, char *end, const struct file *f,
894 struct printf_spec spec, const char *fmt)
895{
896 if (check_pointer(&buf, end, f, spec))
897 return buf;
898
899 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
900}
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400901#ifdef CONFIG_BLOCK
902static noinline_for_stack
903char *bdev_name(char *buf, char *end, struct block_device *bdev,
904 struct printf_spec spec, const char *fmt)
905{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200906 struct gendisk *hd;
907
908 if (check_pointer(&buf, end, bdev, spec))
909 return buf;
910
911 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400912 buf = string(buf, end, hd->disk_name, spec);
913 if (bdev->bd_part->partno) {
914 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
915 if (buf < end)
916 *buf = 'p';
917 buf++;
918 }
919 buf = number(buf, end, bdev->bd_part->partno, spec);
920 }
921 return buf;
922}
923#endif
924
Joe Perchescf3b4292010-05-24 14:33:16 -0700925static noinline_for_stack
926char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800927 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700928{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800929 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700930#ifdef CONFIG_KALLSYMS
931 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800932#endif
933
934 if (fmt[1] == 'R')
935 ptr = __builtin_extract_return_addr(ptr);
936 value = (unsigned long)ptr;
937
938#ifdef CONFIG_KALLSYMS
939 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900940 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800941 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200942 sprint_symbol(sym, value);
943 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700944 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800945
Petr Mladekd529ac42019-04-17 13:53:43 +0200946 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700947#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800948 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700949#endif
950}
951
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200952static const struct printf_spec default_str_spec = {
953 .field_width = -1,
954 .precision = -1,
955};
956
Andy Shevchenko54433972018-02-16 23:07:06 +0200957static const struct printf_spec default_flag_spec = {
958 .base = 16,
959 .precision = -1,
960 .flags = SPECIAL | SMALL,
961};
962
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200963static const struct printf_spec default_dec_spec = {
964 .base = 10,
965 .precision = -1,
966};
967
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200968static const struct printf_spec default_dec02_spec = {
969 .base = 10,
970 .field_width = 2,
971 .precision = -1,
972 .flags = ZEROPAD,
973};
974
975static const struct printf_spec default_dec04_spec = {
976 .base = 10,
977 .field_width = 4,
978 .precision = -1,
979 .flags = ZEROPAD,
980};
981
Joe Perchescf3b4292010-05-24 14:33:16 -0700982static noinline_for_stack
983char *resource_string(char *buf, char *end, struct resource *res,
984 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100985{
986#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600987#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100988#endif
989
990#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600991#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100992#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700993 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100994 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700995 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100996 .precision = -1,
997 .flags = SPECIAL | SMALL | ZEROPAD,
998 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700999 static const struct printf_spec mem_spec = {
1000 .base = 16,
1001 .field_width = MEM_RSRC_PRINTK_SIZE,
1002 .precision = -1,
1003 .flags = SPECIAL | SMALL | ZEROPAD,
1004 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001005 static const struct printf_spec bus_spec = {
1006 .base = 16,
1007 .field_width = 2,
1008 .precision = -1,
1009 .flags = SMALL | ZEROPAD,
1010 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001011 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001012 .field_width = -1,
1013 .precision = 10,
1014 .flags = LEFT,
1015 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001016
1017 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1018 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1019#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1020#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001021#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001022#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1023 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1024 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1025
Linus Torvalds332d2e72008-10-20 15:07:34 +11001026 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001027 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001028 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +11001029
Petr Mladek3e5903e2019-04-17 13:53:48 +02001030 if (check_pointer(&buf, end, res, spec))
1031 return buf;
1032
Linus Torvalds332d2e72008-10-20 15:07:34 +11001033 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001034 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001035 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001036 specp = &io_spec;
1037 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001038 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001039 specp = &mem_spec;
1040 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001041 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001042 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001043 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001044 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001045 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001046 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001047 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001048 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001049 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001050 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001051 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001052 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001053 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001054 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001055 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001056 p = number(p, pend, resource_size(res), *specp);
1057 } else {
1058 p = number(p, pend, res->start, *specp);
1059 if (res->start != res->end) {
1060 *p++ = '-';
1061 p = number(p, pend, res->end, *specp);
1062 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001063 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001064 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001065 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001066 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001067 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001068 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001069 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001070 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001071 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001072 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001073 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001074 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001075 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001076 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001077 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001078 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001079
Petr Mladekd529ac42019-04-17 13:53:43 +02001080 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001081}
1082
Joe Perchescf3b4292010-05-24 14:33:16 -07001083static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001084char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1085 const char *fmt)
1086{
Steven Rostedt360603a2013-05-28 15:47:39 -04001087 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001088 negative value, fallback to the default */
1089 char separator;
1090
1091 if (spec.field_width == 0)
1092 /* nothing to print */
1093 return buf;
1094
Petr Mladek3e5903e2019-04-17 13:53:48 +02001095 if (check_pointer(&buf, end, addr, spec))
1096 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001097
1098 switch (fmt[1]) {
1099 case 'C':
1100 separator = ':';
1101 break;
1102 case 'D':
1103 separator = '-';
1104 break;
1105 case 'N':
1106 separator = 0;
1107 break;
1108 default:
1109 separator = ' ';
1110 break;
1111 }
1112
1113 if (spec.field_width > 0)
1114 len = min_t(int, spec.field_width, 64);
1115
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001116 for (i = 0; i < len; ++i) {
1117 if (buf < end)
1118 *buf = hex_asc_hi(addr[i]);
1119 ++buf;
1120 if (buf < end)
1121 *buf = hex_asc_lo(addr[i]);
1122 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001123
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001124 if (separator && i != len - 1) {
1125 if (buf < end)
1126 *buf = separator;
1127 ++buf;
1128 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001129 }
1130
1131 return buf;
1132}
1133
1134static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001135char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1136 struct printf_spec spec, const char *fmt)
1137{
1138 const int CHUNKSZ = 32;
1139 int nr_bits = max_t(int, spec.field_width, 0);
1140 int i, chunksz;
1141 bool first = true;
1142
Petr Mladek3e5903e2019-04-17 13:53:48 +02001143 if (check_pointer(&buf, end, bitmap, spec))
1144 return buf;
1145
Tejun Heodbc760b2015-02-13 14:36:53 -08001146 /* reused to print numbers */
1147 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1148
1149 chunksz = nr_bits & (CHUNKSZ - 1);
1150 if (chunksz == 0)
1151 chunksz = CHUNKSZ;
1152
1153 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1154 for (; i >= 0; i -= CHUNKSZ) {
1155 u32 chunkmask, val;
1156 int word, bit;
1157
1158 chunkmask = ((1ULL << chunksz) - 1);
1159 word = i / BITS_PER_LONG;
1160 bit = i % BITS_PER_LONG;
1161 val = (bitmap[word] >> bit) & chunkmask;
1162
1163 if (!first) {
1164 if (buf < end)
1165 *buf = ',';
1166 buf++;
1167 }
1168 first = false;
1169
1170 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1171 buf = number(buf, end, val, spec);
1172
1173 chunksz = CHUNKSZ;
1174 }
1175 return buf;
1176}
1177
1178static noinline_for_stack
1179char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1180 struct printf_spec spec, const char *fmt)
1181{
1182 int nr_bits = max_t(int, spec.field_width, 0);
1183 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1184 int cur, rbot, rtop;
1185 bool first = true;
1186
Petr Mladek3e5903e2019-04-17 13:53:48 +02001187 if (check_pointer(&buf, end, bitmap, spec))
1188 return buf;
1189
Tejun Heodbc760b2015-02-13 14:36:53 -08001190 rbot = cur = find_first_bit(bitmap, nr_bits);
1191 while (cur < nr_bits) {
1192 rtop = cur;
1193 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1194 if (cur < nr_bits && cur <= rtop + 1)
1195 continue;
1196
1197 if (!first) {
1198 if (buf < end)
1199 *buf = ',';
1200 buf++;
1201 }
1202 first = false;
1203
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001204 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001205 if (rbot < rtop) {
1206 if (buf < end)
1207 *buf = '-';
1208 buf++;
1209
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001210 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001211 }
1212
1213 rbot = cur;
1214 }
1215 return buf;
1216}
1217
1218static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001219char *mac_address_string(char *buf, char *end, u8 *addr,
1220 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001221{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001222 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001223 char *p = mac_addr;
1224 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001225 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001226 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001227
Petr Mladek3e5903e2019-04-17 13:53:48 +02001228 if (check_pointer(&buf, end, addr, spec))
1229 return buf;
1230
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001231 switch (fmt[1]) {
1232 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001233 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001234 break;
1235
1236 case 'R':
1237 reversed = true;
1238 /* fall through */
1239
1240 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001241 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001242 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001243 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001244
1245 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001246 if (reversed)
1247 p = hex_byte_pack(p, addr[5 - i]);
1248 else
1249 p = hex_byte_pack(p, addr[i]);
1250
Joe Perches8a27f7c2009-08-17 12:29:44 +00001251 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001252 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001253 }
1254 *p = '\0';
1255
Petr Mladekd529ac42019-04-17 13:53:43 +02001256 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001257}
1258
Joe Perchescf3b4292010-05-24 14:33:16 -07001259static noinline_for_stack
1260char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001261{
Harvey Harrison689afa72008-10-28 16:04:44 -07001262 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001263 bool leading_zeros = (fmt[0] == 'i');
1264 int index;
1265 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001266
Joe Perches0159f242010-01-13 20:23:30 -08001267 switch (fmt[2]) {
1268 case 'h':
1269#ifdef __BIG_ENDIAN
1270 index = 0;
1271 step = 1;
1272#else
1273 index = 3;
1274 step = -1;
1275#endif
1276 break;
1277 case 'l':
1278 index = 3;
1279 step = -1;
1280 break;
1281 case 'n':
1282 case 'b':
1283 default:
1284 index = 0;
1285 step = 1;
1286 break;
1287 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001288 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001289 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001290 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001291 if (leading_zeros) {
1292 if (digits < 3)
1293 *p++ = '0';
1294 if (digits < 2)
1295 *p++ = '0';
1296 }
1297 /* reverse the digits in the quad */
1298 while (digits--)
1299 *p++ = temp[digits];
1300 if (i < 3)
1301 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001302 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001303 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001304 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001305
Joe Perches8a27f7c2009-08-17 12:29:44 +00001306 return p;
1307}
1308
Joe Perchescf3b4292010-05-24 14:33:16 -07001309static noinline_for_stack
1310char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001311{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001312 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001313 unsigned char zerolength[8];
1314 int longest = 1;
1315 int colonpos = -1;
1316 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001317 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001318 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001319 bool useIPv4;
1320 struct in6_addr in6;
1321
1322 memcpy(&in6, addr, sizeof(struct in6_addr));
1323
1324 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001325
1326 memset(zerolength, 0, sizeof(zerolength));
1327
1328 if (useIPv4)
1329 range = 6;
1330 else
1331 range = 8;
1332
1333 /* find position of longest 0 run */
1334 for (i = 0; i < range; i++) {
1335 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001336 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001337 break;
1338 zerolength[i]++;
1339 }
1340 }
1341 for (i = 0; i < range; i++) {
1342 if (zerolength[i] > longest) {
1343 longest = zerolength[i];
1344 colonpos = i;
1345 }
1346 }
Joe Perches29cf5192011-06-09 11:23:37 -07001347 if (longest == 1) /* don't compress a single 0 */
1348 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001349
1350 /* emit address */
1351 for (i = 0; i < range; i++) {
1352 if (i == colonpos) {
1353 if (needcolon || i == 0)
1354 *p++ = ':';
1355 *p++ = ':';
1356 needcolon = false;
1357 i += longest - 1;
1358 continue;
1359 }
1360 if (needcolon) {
1361 *p++ = ':';
1362 needcolon = false;
1363 }
1364 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001365 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001366 hi = word >> 8;
1367 lo = word & 0xff;
1368 if (hi) {
1369 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001370 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001371 else
1372 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001373 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001374 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001375 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001376 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001377 else
1378 *p++ = hex_asc_lo(lo);
1379 needcolon = true;
1380 }
1381
1382 if (useIPv4) {
1383 if (needcolon)
1384 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001385 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001386 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001387 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001388
Joe Perches8a27f7c2009-08-17 12:29:44 +00001389 return p;
1390}
1391
Joe Perchescf3b4292010-05-24 14:33:16 -07001392static noinline_for_stack
1393char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001394{
1395 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001396
Harvey Harrison689afa72008-10-28 16:04:44 -07001397 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001398 p = hex_byte_pack(p, *addr++);
1399 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001400 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001401 *p++ = ':';
1402 }
1403 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001404
Joe Perches8a27f7c2009-08-17 12:29:44 +00001405 return p;
1406}
1407
Joe Perchescf3b4292010-05-24 14:33:16 -07001408static noinline_for_stack
1409char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1410 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001411{
1412 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1413
1414 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001415 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001416 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001417 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001418
Petr Mladekd529ac42019-04-17 13:53:43 +02001419 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001420}
1421
Joe Perchescf3b4292010-05-24 14:33:16 -07001422static noinline_for_stack
1423char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1424 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001425{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001426 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001427
Joe Perches0159f242010-01-13 20:23:30 -08001428 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001429
Petr Mladekd529ac42019-04-17 13:53:43 +02001430 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001431}
1432
Joe Perchescf3b4292010-05-24 14:33:16 -07001433static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001434char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1435 struct printf_spec spec, const char *fmt)
1436{
1437 bool have_p = false, have_s = false, have_f = false, have_c = false;
1438 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1439 sizeof(":12345") + sizeof("/123456789") +
1440 sizeof("%1234567890")];
1441 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1442 const u8 *addr = (const u8 *) &sa->sin6_addr;
1443 char fmt6[2] = { fmt[0], '6' };
1444 u8 off = 0;
1445
1446 fmt++;
1447 while (isalpha(*++fmt)) {
1448 switch (*fmt) {
1449 case 'p':
1450 have_p = true;
1451 break;
1452 case 'f':
1453 have_f = true;
1454 break;
1455 case 's':
1456 have_s = true;
1457 break;
1458 case 'c':
1459 have_c = true;
1460 break;
1461 }
1462 }
1463
1464 if (have_p || have_s || have_f) {
1465 *p = '[';
1466 off = 1;
1467 }
1468
1469 if (fmt6[0] == 'I' && have_c)
1470 p = ip6_compressed_string(ip6_addr + off, addr);
1471 else
1472 p = ip6_string(ip6_addr + off, addr, fmt6);
1473
1474 if (have_p || have_s || have_f)
1475 *p++ = ']';
1476
1477 if (have_p) {
1478 *p++ = ':';
1479 p = number(p, pend, ntohs(sa->sin6_port), spec);
1480 }
1481 if (have_f) {
1482 *p++ = '/';
1483 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1484 IPV6_FLOWINFO_MASK), spec);
1485 }
1486 if (have_s) {
1487 *p++ = '%';
1488 p = number(p, pend, sa->sin6_scope_id, spec);
1489 }
1490 *p = '\0';
1491
Petr Mladekd529ac42019-04-17 13:53:43 +02001492 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001493}
1494
1495static noinline_for_stack
1496char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1497 struct printf_spec spec, const char *fmt)
1498{
1499 bool have_p = false;
1500 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1501 char *pend = ip4_addr + sizeof(ip4_addr);
1502 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1503 char fmt4[3] = { fmt[0], '4', 0 };
1504
1505 fmt++;
1506 while (isalpha(*++fmt)) {
1507 switch (*fmt) {
1508 case 'p':
1509 have_p = true;
1510 break;
1511 case 'h':
1512 case 'l':
1513 case 'n':
1514 case 'b':
1515 fmt4[2] = *fmt;
1516 break;
1517 }
1518 }
1519
1520 p = ip4_string(ip4_addr, addr, fmt4);
1521 if (have_p) {
1522 *p++ = ':';
1523 p = number(p, pend, ntohs(sa->sin_port), spec);
1524 }
1525 *p = '\0';
1526
Petr Mladekd529ac42019-04-17 13:53:43 +02001527 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001528}
1529
1530static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001531char *ip_addr_string(char *buf, char *end, const void *ptr,
1532 struct printf_spec spec, const char *fmt)
1533{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001534 char *err_fmt_msg;
1535
Petr Mladek3e5903e2019-04-17 13:53:48 +02001536 if (check_pointer(&buf, end, ptr, spec))
1537 return buf;
1538
Petr Mladekf00cc102019-04-17 13:53:44 +02001539 switch (fmt[1]) {
1540 case '6':
1541 return ip6_addr_string(buf, end, ptr, spec, fmt);
1542 case '4':
1543 return ip4_addr_string(buf, end, ptr, spec, fmt);
1544 case 'S': {
1545 const union {
1546 struct sockaddr raw;
1547 struct sockaddr_in v4;
1548 struct sockaddr_in6 v6;
1549 } *sa = ptr;
1550
1551 switch (sa->raw.sa_family) {
1552 case AF_INET:
1553 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1554 case AF_INET6:
1555 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1556 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001557 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001558 }}
1559 }
1560
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001561 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001562 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001563}
1564
1565static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001566char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1567 const char *fmt)
1568{
1569 bool found = true;
1570 int count = 1;
1571 unsigned int flags = 0;
1572 int len;
1573
1574 if (spec.field_width == 0)
1575 return buf; /* nothing to print */
1576
Petr Mladek3e5903e2019-04-17 13:53:48 +02001577 if (check_pointer(&buf, end, addr, spec))
1578 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001579
1580 do {
1581 switch (fmt[count++]) {
1582 case 'a':
1583 flags |= ESCAPE_ANY;
1584 break;
1585 case 'c':
1586 flags |= ESCAPE_SPECIAL;
1587 break;
1588 case 'h':
1589 flags |= ESCAPE_HEX;
1590 break;
1591 case 'n':
1592 flags |= ESCAPE_NULL;
1593 break;
1594 case 'o':
1595 flags |= ESCAPE_OCTAL;
1596 break;
1597 case 'p':
1598 flags |= ESCAPE_NP;
1599 break;
1600 case 's':
1601 flags |= ESCAPE_SPACE;
1602 break;
1603 default:
1604 found = false;
1605 break;
1606 }
1607 } while (found);
1608
1609 if (!flags)
1610 flags = ESCAPE_ANY_NP;
1611
1612 len = spec.field_width < 0 ? 1 : spec.field_width;
1613
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001614 /*
1615 * string_escape_mem() writes as many characters as it can to
1616 * the given buffer, and returns the total size of the output
1617 * had the buffer been big enough.
1618 */
1619 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001620
1621 return buf;
1622}
1623
Petr Mladek3e5903e2019-04-17 13:53:48 +02001624static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1625 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001626{
1627 va_list va;
1628
Petr Mladek3e5903e2019-04-17 13:53:48 +02001629 if (check_pointer(&buf, end, va_fmt, spec))
1630 return buf;
1631
Petr Mladek45c3e932019-04-17 13:53:45 +02001632 va_copy(va, *va_fmt->va);
1633 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1634 va_end(va);
1635
1636 return buf;
1637}
1638
Andy Shevchenko71dca952014-10-13 15:55:18 -07001639static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001640char *uuid_string(char *buf, char *end, const u8 *addr,
1641 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001642{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001643 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001644 char *p = uuid;
1645 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001646 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001647 bool uc = false;
1648
Petr Mladek3e5903e2019-04-17 13:53:48 +02001649 if (check_pointer(&buf, end, addr, spec))
1650 return buf;
1651
Joe Perches9ac6e442009-12-14 18:01:09 -08001652 switch (*(++fmt)) {
1653 case 'L':
1654 uc = true; /* fall-through */
1655 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001656 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001657 break;
1658 case 'B':
1659 uc = true;
1660 break;
1661 }
1662
1663 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001664 if (uc)
1665 p = hex_byte_pack_upper(p, addr[index[i]]);
1666 else
1667 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001668 switch (i) {
1669 case 3:
1670 case 5:
1671 case 7:
1672 case 9:
1673 *p++ = '-';
1674 break;
1675 }
1676 }
1677
1678 *p = 0;
1679
Petr Mladekd529ac42019-04-17 13:53:43 +02001680 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001681}
1682
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001683static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001684char *netdev_bits(char *buf, char *end, const void *addr,
1685 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001686{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001687 unsigned long long num;
1688 int size;
1689
Petr Mladek3e5903e2019-04-17 13:53:48 +02001690 if (check_pointer(&buf, end, addr, spec))
1691 return buf;
1692
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001693 switch (fmt[1]) {
1694 case 'F':
1695 num = *(const netdev_features_t *)addr;
1696 size = sizeof(netdev_features_t);
1697 break;
1698 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001699 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001700 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001701
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001702 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001703}
1704
Joe Perchesaaf07622014-01-23 15:54:17 -08001705static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001706char *address_val(char *buf, char *end, const void *addr,
1707 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001708{
1709 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001710 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001711
Petr Mladek3e5903e2019-04-17 13:53:48 +02001712 if (check_pointer(&buf, end, addr, spec))
1713 return buf;
1714
Joe Perchesaaf07622014-01-23 15:54:17 -08001715 switch (fmt[1]) {
1716 case 'd':
1717 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001718 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001719 break;
1720 case 'p':
1721 default:
1722 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001723 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001724 break;
1725 }
1726
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001727 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001728}
1729
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001730static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001731char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1732{
1733 int year = tm->tm_year + (r ? 0 : 1900);
1734 int mon = tm->tm_mon + (r ? 0 : 1);
1735
1736 buf = number(buf, end, year, default_dec04_spec);
1737 if (buf < end)
1738 *buf = '-';
1739 buf++;
1740
1741 buf = number(buf, end, mon, default_dec02_spec);
1742 if (buf < end)
1743 *buf = '-';
1744 buf++;
1745
1746 return number(buf, end, tm->tm_mday, default_dec02_spec);
1747}
1748
1749static noinline_for_stack
1750char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1751{
1752 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1753 if (buf < end)
1754 *buf = ':';
1755 buf++;
1756
1757 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1758 if (buf < end)
1759 *buf = ':';
1760 buf++;
1761
1762 return number(buf, end, tm->tm_sec, default_dec02_spec);
1763}
1764
1765static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001766char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1767 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001768{
1769 bool have_t = true, have_d = true;
1770 bool raw = false;
1771 int count = 2;
1772
Petr Mladek3e5903e2019-04-17 13:53:48 +02001773 if (check_pointer(&buf, end, tm, spec))
1774 return buf;
1775
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001776 switch (fmt[count]) {
1777 case 'd':
1778 have_t = false;
1779 count++;
1780 break;
1781 case 't':
1782 have_d = false;
1783 count++;
1784 break;
1785 }
1786
1787 raw = fmt[count] == 'r';
1788
1789 if (have_d)
1790 buf = date_str(buf, end, tm, raw);
1791 if (have_d && have_t) {
1792 /* Respect ISO 8601 */
1793 if (buf < end)
1794 *buf = 'T';
1795 buf++;
1796 }
1797 if (have_t)
1798 buf = time_str(buf, end, tm, raw);
1799
1800 return buf;
1801}
1802
1803static noinline_for_stack
1804char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1805 const char *fmt)
1806{
1807 switch (fmt[1]) {
1808 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001809 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001810 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001811 return error_string(buf, end, "(%ptR?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001812 }
1813}
1814
1815static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001816char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1817 const char *fmt)
1818{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001819 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001820 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001821
Petr Mladek3e5903e2019-04-17 13:53:48 +02001822 if (check_pointer(&buf, end, clk, spec))
1823 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001824
1825 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001826 case 'n':
1827 default:
1828#ifdef CONFIG_COMMON_CLK
1829 return string(buf, end, __clk_get_name(clk), spec);
1830#else
Geert Uytterhoeven4ca96aa2019-07-01 16:00:09 +02001831 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001832#endif
1833 }
1834}
1835
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001836static
1837char *format_flags(char *buf, char *end, unsigned long flags,
1838 const struct trace_print_flags *names)
1839{
1840 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001841
1842 for ( ; flags && names->name; names++) {
1843 mask = names->mask;
1844 if ((flags & mask) != mask)
1845 continue;
1846
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001847 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001848
1849 flags &= ~mask;
1850 if (flags) {
1851 if (buf < end)
1852 *buf = '|';
1853 buf++;
1854 }
1855 }
1856
1857 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001858 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001859
1860 return buf;
1861}
1862
1863static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001864char *flags_string(char *buf, char *end, void *flags_ptr,
1865 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001866{
1867 unsigned long flags;
1868 const struct trace_print_flags *names;
1869
Petr Mladek3e5903e2019-04-17 13:53:48 +02001870 if (check_pointer(&buf, end, flags_ptr, spec))
1871 return buf;
1872
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001873 switch (fmt[1]) {
1874 case 'p':
1875 flags = *(unsigned long *)flags_ptr;
1876 /* Remove zone id */
1877 flags &= (1UL << NR_PAGEFLAGS) - 1;
1878 names = pageflag_names;
1879 break;
1880 case 'v':
1881 flags = *(unsigned long *)flags_ptr;
1882 names = vmaflag_names;
1883 break;
1884 case 'g':
1885 flags = *(gfp_t *)flags_ptr;
1886 names = gfpflag_names;
1887 break;
1888 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001889 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001890 }
1891
1892 return format_flags(buf, end, flags, names);
1893}
1894
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001895static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1896{
1897 for ( ; np && depth; depth--)
1898 np = np->parent;
1899
1900 return kbasename(np->full_name);
1901}
1902
1903static noinline_for_stack
1904char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1905{
1906 int depth;
1907 const struct device_node *parent = np->parent;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001908
1909 /* special case for root node */
1910 if (!parent)
Petr Mladekd529ac42019-04-17 13:53:43 +02001911 return string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001912
1913 for (depth = 0; parent->parent; depth++)
1914 parent = parent->parent;
1915
1916 for ( ; depth >= 0; depth--) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001917 buf = string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001918 buf = string(buf, end, device_node_name_for_depth(np, depth),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001919 default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001920 }
1921 return buf;
1922}
1923
1924static noinline_for_stack
1925char *device_node_string(char *buf, char *end, struct device_node *dn,
1926 struct printf_spec spec, const char *fmt)
1927{
1928 char tbuf[sizeof("xxxx") + 1];
1929 const char *p;
1930 int ret;
1931 char *buf_start = buf;
1932 struct property *prop;
1933 bool has_mult, pass;
1934 static const struct printf_spec num_spec = {
1935 .flags = SMALL,
1936 .field_width = -1,
1937 .precision = -1,
1938 .base = 10,
1939 };
1940
1941 struct printf_spec str_spec = spec;
1942 str_spec.field_width = -1;
1943
1944 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001945 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001946
Petr Mladek3e5903e2019-04-17 13:53:48 +02001947 if (check_pointer(&buf, end, dn, spec))
1948 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001949
1950 /* simple case without anything any more format specifiers */
1951 fmt++;
1952 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1953 fmt = "f";
1954
1955 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001956 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001957 if (pass) {
1958 if (buf < end)
1959 *buf = ':';
1960 buf++;
1961 }
1962
1963 switch (*fmt) {
1964 case 'f': /* full_name */
1965 buf = device_node_gen_full_name(dn, buf, end);
1966 break;
1967 case 'n': /* name */
Rob Herring6d0a70a2018-08-27 08:13:56 -05001968 p = kbasename(of_node_full_name(dn));
1969 precision = str_spec.precision;
1970 str_spec.precision = strchrnul(p, '@') - p;
1971 buf = string(buf, end, p, str_spec);
1972 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001973 break;
1974 case 'p': /* phandle */
1975 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1976 break;
1977 case 'P': /* path-spec */
1978 p = kbasename(of_node_full_name(dn));
1979 if (!p[1])
1980 p = "/";
1981 buf = string(buf, end, p, str_spec);
1982 break;
1983 case 'F': /* flags */
1984 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1985 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1986 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1987 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1988 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02001989 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001990 break;
1991 case 'c': /* major compatible string */
1992 ret = of_property_read_string(dn, "compatible", &p);
1993 if (!ret)
1994 buf = string(buf, end, p, str_spec);
1995 break;
1996 case 'C': /* full compatible string */
1997 has_mult = false;
1998 of_property_for_each_string(dn, "compatible", prop, p) {
1999 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02002000 buf = string_nocheck(buf, end, ",", str_spec);
2001 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002002 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02002003 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002004
2005 has_mult = true;
2006 }
2007 break;
2008 default:
2009 break;
2010 }
2011 }
2012
2013 return widen_string(buf, buf - buf_start, end, spec);
2014}
2015
Petr Mladek798cc272019-04-17 13:53:46 +02002016static char *kobject_string(char *buf, char *end, void *ptr,
2017 struct printf_spec spec, const char *fmt)
2018{
2019 switch (fmt[1]) {
2020 case 'F':
2021 return device_node_string(buf, end, ptr, spec, fmt + 1);
2022 }
2023
Petr Mladekc8c3b582019-04-17 13:53:50 +02002024 return error_string(buf, end, "(%pO?)", spec);
Petr Mladek798cc272019-04-17 13:53:46 +02002025}
2026
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002027/*
2028 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2029 * by an extra set of alphanumeric characters that are extended format
2030 * specifiers.
2031 *
Joe Perches0b523762017-05-08 15:55:36 -07002032 * Please update scripts/checkpatch.pl when adding/removing conversion
2033 * characters. (Search for "check for vsprintf extension").
2034 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002035 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002036 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002037 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2038 * - 's' For symbolic direct pointers (or function descriptors) without offset
2039 * - 'F' Same as 'S'
2040 * - 'f' Same as 's'
Joe Perchesb0d33c22012-12-12 10:18:50 -08002041 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002042 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002043 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2044 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002045 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2046 * width which must be explicitly specified either as part of the
2047 * format string '%32b[l]' or through '%*b[l]', [l] selects
2048 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002049 * - 'M' For a 6-byte MAC address, it prints the address in the
2050 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002051 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002052 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002053 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002054 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002055 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2056 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2057 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002058 * [S][pfs]
2059 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2060 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002061 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2062 * IPv6 omits the colons (01020304...0f)
2063 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002064 * [S][pfs]
2065 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2066 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2067 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2068 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07002069 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002070 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2071 * of the following flags (see string_escape_mem() for the
2072 * details):
2073 * a - ESCAPE_ANY
2074 * c - ESCAPE_SPECIAL
2075 * h - ESCAPE_HEX
2076 * n - ESCAPE_NULL
2077 * o - ESCAPE_OCTAL
2078 * p - ESCAPE_NP
2079 * s - ESCAPE_SPACE
2080 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002081 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2082 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2083 * Options for %pU are:
2084 * b big endian lower case hex (default)
2085 * B big endian UPPER case hex
2086 * l little endian lower case hex
2087 * L little endian UPPER case hex
2088 * big endian output byte order is:
2089 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2090 * little endian output byte order is:
2091 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002092 * - 'V' For a struct va_format which contains a format string * and va_list *,
2093 * call vsnprintf(->format, *->va_list).
2094 * Implements a "recursive vsnprintf".
2095 * Do not use this feature without some mechanism to verify the
2096 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002097 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002098 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002099 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2100 * a certain separator (' ' by default):
2101 * C colon
2102 * D dash
2103 * N no separator
2104 * The maximum supported length is 64 bytes of the input. Consider
2105 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002106 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2107 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002108 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2109 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002110 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002111 * - 't[R][dt][r]' For time and date as represented:
2112 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002113 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2114 * (legacy clock framework) of the clock
2115 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2116 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002117 * - 'G' For flags to be printed as a collection of symbolic strings that would
2118 * construct the specific value. Supported flags given by option:
2119 * p page flags (see struct page) given as pointer to unsigned long
2120 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2121 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002122 * - 'OF[fnpPcCF]' For a device tree object
2123 * Without any optional arguments prints the full_name
2124 * f device node full_name
2125 * n device node name
2126 * p device node phandle
2127 * P device node path spec (name + @unit)
2128 * F device node flags
2129 * c major compatible string
2130 * C full compatible string
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002131 * - 'x' For printing the address. Equivalent to "%lx".
2132 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002133 * ** When making changes please also update:
2134 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002135 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002136 * Note: The default behaviour (unadorned %p) is to hash the address,
2137 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002138 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002139static noinline_for_stack
2140char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2141 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002142{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002143 switch (*fmt) {
2144 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02002145 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002146 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002147 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002148 ptr = dereference_symbol_descriptor(ptr);
2149 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002150 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002151 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002152 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002153 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002154 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002155 case 'h':
2156 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002157 case 'b':
2158 switch (fmt[1]) {
2159 case 'l':
2160 return bitmap_list_string(buf, end, ptr, spec, fmt);
2161 default:
2162 return bitmap_string(buf, end, ptr, spec, fmt);
2163 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002164 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2165 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002166 /* [mM]F (FDDI) */
2167 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002168 return mac_address_string(buf, end, ptr, spec, fmt);
2169 case 'I': /* Formatted IP supported
2170 * 4: 1.2.3.4
2171 * 6: 0001:0203:...:0708
2172 * 6c: 1::708 or 1::1.2.3.4
2173 */
2174 case 'i': /* Contiguous:
2175 * 4: 001.002.003.004
2176 * 6: 000102...0f
2177 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002178 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002179 case 'E':
2180 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002181 case 'U':
2182 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002183 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002184 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002185 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002186 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002187 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002188 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002189 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002190 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002191 case 'd':
2192 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002193 case 't':
2194 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002195 case 'C':
2196 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002197 case 'D':
Jia He36594b32019-08-09 09:24:56 +08002198 return file_dentry_name(buf, end, ptr, spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002199#ifdef CONFIG_BLOCK
2200 case 'g':
2201 return bdev_name(buf, end, ptr, spec, fmt);
2202#endif
2203
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002204 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002205 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002206 case 'O':
Petr Mladek798cc272019-04-17 13:53:46 +02002207 return kobject_string(buf, end, ptr, spec, fmt);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002208 case 'x':
2209 return pointer_string(buf, end, ptr, spec);
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002210 case 'e':
2211 /* %pe with a non-ERR_PTR gets treated as plain %p */
2212 if (!IS_ERR(ptr))
2213 break;
2214 return err_ptr(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002215 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002216
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002217 /* default is to _not_ leak addresses, hash before printing */
2218 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002219}
2220
2221/*
2222 * Helper function to decode printf style format.
2223 * Each call decode a token from the format and return the
2224 * number of characters read (or likely the delta where it wants
2225 * to go on the next call).
2226 * The decoded token is returned through the parameters
2227 *
2228 * 'h', 'l', or 'L' for integer fields
2229 * 'z' support added 23/7/1999 S.H.
2230 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002231 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002232 * 't' added for ptrdiff_t
2233 *
2234 * @fmt: the format string
2235 * @type of the token returned
2236 * @flags: various flags such as +, -, # tokens..
2237 * @field_width: overwritten width
2238 * @base: base of the number (octal, hex, ...)
2239 * @precision: precision of a number
2240 * @qualifier: qualifier of a number (long, size_t, ...)
2241 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002242static noinline_for_stack
2243int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002244{
2245 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002246 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002247
2248 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002249 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002250 if (spec->field_width < 0) {
2251 spec->field_width = -spec->field_width;
2252 spec->flags |= LEFT;
2253 }
2254 spec->type = FORMAT_TYPE_NONE;
2255 goto precision;
2256 }
2257
2258 /* we finished early by reading the precision */
2259 if (spec->type == FORMAT_TYPE_PRECISION) {
2260 if (spec->precision < 0)
2261 spec->precision = 0;
2262
2263 spec->type = FORMAT_TYPE_NONE;
2264 goto qualifier;
2265 }
2266
2267 /* By default */
2268 spec->type = FORMAT_TYPE_NONE;
2269
2270 for (; *fmt ; ++fmt) {
2271 if (*fmt == '%')
2272 break;
2273 }
2274
2275 /* Return the current non-format string */
2276 if (fmt != start || !*fmt)
2277 return fmt - start;
2278
2279 /* Process flags */
2280 spec->flags = 0;
2281
2282 while (1) { /* this also skips first '%' */
2283 bool found = true;
2284
2285 ++fmt;
2286
2287 switch (*fmt) {
2288 case '-': spec->flags |= LEFT; break;
2289 case '+': spec->flags |= PLUS; break;
2290 case ' ': spec->flags |= SPACE; break;
2291 case '#': spec->flags |= SPECIAL; break;
2292 case '0': spec->flags |= ZEROPAD; break;
2293 default: found = false;
2294 }
2295
2296 if (!found)
2297 break;
2298 }
2299
2300 /* get field width */
2301 spec->field_width = -1;
2302
2303 if (isdigit(*fmt))
2304 spec->field_width = skip_atoi(&fmt);
2305 else if (*fmt == '*') {
2306 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002307 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002308 return ++fmt - start;
2309 }
2310
2311precision:
2312 /* get the precision */
2313 spec->precision = -1;
2314 if (*fmt == '.') {
2315 ++fmt;
2316 if (isdigit(*fmt)) {
2317 spec->precision = skip_atoi(&fmt);
2318 if (spec->precision < 0)
2319 spec->precision = 0;
2320 } else if (*fmt == '*') {
2321 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002322 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002323 return ++fmt - start;
2324 }
2325 }
2326
2327qualifier:
2328 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002329 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002330 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002331 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002332 qualifier = *fmt++;
2333 if (unlikely(qualifier == *fmt)) {
2334 if (qualifier == 'l') {
2335 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002336 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002337 } else if (qualifier == 'h') {
2338 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002339 ++fmt;
2340 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002341 }
2342 }
2343
2344 /* default base */
2345 spec->base = 10;
2346 switch (*fmt) {
2347 case 'c':
2348 spec->type = FORMAT_TYPE_CHAR;
2349 return ++fmt - start;
2350
2351 case 's':
2352 spec->type = FORMAT_TYPE_STR;
2353 return ++fmt - start;
2354
2355 case 'p':
2356 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002357 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002358
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002359 case '%':
2360 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2361 return ++fmt - start;
2362
2363 /* integer number formats - set up the flags and "break" */
2364 case 'o':
2365 spec->base = 8;
2366 break;
2367
2368 case 'x':
2369 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002370 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002371
2372 case 'X':
2373 spec->base = 16;
2374 break;
2375
2376 case 'd':
2377 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002378 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002379 case 'u':
2380 break;
2381
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002382 case 'n':
2383 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002384 * Since %n poses a greater security risk than
2385 * utility, treat it as any other invalid or
2386 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002387 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002388 /* Fall-through */
2389
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002390 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002391 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002392 spec->type = FORMAT_TYPE_INVALID;
2393 return fmt - start;
2394 }
2395
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002396 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002397 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002398 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002399 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2400 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002401 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002402 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002403 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002404 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002405 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002406 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2407 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002408 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002409 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2410 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002411 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002412 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2413 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002414 }
2415
2416 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002417}
2418
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002419static void
2420set_field_width(struct printf_spec *spec, int width)
2421{
2422 spec->field_width = width;
2423 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2424 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2425 }
2426}
2427
2428static void
2429set_precision(struct printf_spec *spec, int prec)
2430{
2431 spec->precision = prec;
2432 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2433 spec->precision = clamp(prec, 0, PRECISION_MAX);
2434 }
2435}
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437/**
2438 * vsnprintf - Format a string and place it in a buffer
2439 * @buf: The buffer to place the result into
2440 * @size: The size of the buffer, including the trailing null space
2441 * @fmt: The format string to use
2442 * @args: Arguments for the format string
2443 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002444 * This function generally follows C99 vsnprintf, but has some
2445 * extensions and a few limitations:
2446 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002447 * - ``%n`` is unsupported
2448 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002449 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002450 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002451 * extensive description.
2452 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002453 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002454 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * The return value is the number of characters which would
2456 * be generated for the given input, excluding the trailing
2457 * '\0', as per ISO C99. If you want to have the exact
2458 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002459 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 * return is greater than or equal to @size, the resulting
2461 * string is truncated.
2462 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002463 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 */
2465int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2466{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002468 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002469 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002471 /* Reject out-of-range values early. Large positive sizes are
2472 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002473 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
2476 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002477 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002479 /* Make sure end is always >= buf */
2480 if (end < buf) {
2481 end = ((void *)-1);
2482 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002485 while (*fmt) {
2486 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002487 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002488
2489 fmt += read;
2490
2491 switch (spec.type) {
2492 case FORMAT_TYPE_NONE: {
2493 int copy = read;
2494 if (str < end) {
2495 if (copy > end - str)
2496 copy = end - str;
2497 memcpy(str, old_fmt, copy);
2498 }
2499 str += read;
2500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502
Vegard Nossumed681a92009-03-14 12:08:50 +01002503 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002504 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002507 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002508 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
André Goddard Rosad4be1512009-12-14 18:00:59 -08002511 case FORMAT_TYPE_CHAR: {
2512 char c;
2513
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002514 if (!(spec.flags & LEFT)) {
2515 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002516 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 *str = ' ';
2518 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002521 }
2522 c = (unsigned char) va_arg(args, int);
2523 if (str < end)
2524 *str = c;
2525 ++str;
2526 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002527 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002528 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002530 }
2531 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002534 case FORMAT_TYPE_STR:
2535 str = string(str, end, va_arg(args, char *), spec);
2536 break;
2537
2538 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002539 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002540 spec);
2541 while (isalnum(*fmt))
2542 fmt++;
2543 break;
2544
2545 case FORMAT_TYPE_PERCENT_CHAR:
2546 if (str < end)
2547 *str = '%';
2548 ++str;
2549 break;
2550
2551 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002552 /*
2553 * Presumably the arguments passed gcc's type
2554 * checking, but there is no safe or sane way
2555 * for us to continue parsing the format and
2556 * fetching from the va_list; the remaining
2557 * specifiers and arguments would be out of
2558 * sync.
2559 */
2560 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002561
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002562 default:
2563 switch (spec.type) {
2564 case FORMAT_TYPE_LONG_LONG:
2565 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002567 case FORMAT_TYPE_ULONG:
2568 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002570 case FORMAT_TYPE_LONG:
2571 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002573 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002574 if (spec.flags & SIGN)
2575 num = va_arg(args, ssize_t);
2576 else
2577 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002578 break;
2579 case FORMAT_TYPE_PTRDIFF:
2580 num = va_arg(args, ptrdiff_t);
2581 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002582 case FORMAT_TYPE_UBYTE:
2583 num = (unsigned char) va_arg(args, int);
2584 break;
2585 case FORMAT_TYPE_BYTE:
2586 num = (signed char) va_arg(args, int);
2587 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002588 case FORMAT_TYPE_USHORT:
2589 num = (unsigned short) va_arg(args, int);
2590 break;
2591 case FORMAT_TYPE_SHORT:
2592 num = (short) va_arg(args, int);
2593 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002594 case FORMAT_TYPE_INT:
2595 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002598 num = va_arg(args, unsigned int);
2599 }
2600
2601 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002604
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002605out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002606 if (size > 0) {
2607 if (str < end)
2608 *str = '\0';
2609 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002610 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002611 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002612
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002613 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617EXPORT_SYMBOL(vsnprintf);
2618
2619/**
2620 * vscnprintf - Format a string and place it in a buffer
2621 * @buf: The buffer to place the result into
2622 * @size: The size of the buffer, including the trailing null space
2623 * @fmt: The format string to use
2624 * @args: Arguments for the format string
2625 *
2626 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002627 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 * returns 0.
2629 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002630 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002631 *
2632 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 */
2634int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2635{
2636 int i;
2637
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002638 i = vsnprintf(buf, size, fmt, args);
2639
Anton Arapovb921c692011-01-12 16:59:49 -08002640 if (likely(i < size))
2641 return i;
2642 if (size != 0)
2643 return size - 1;
2644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646EXPORT_SYMBOL(vscnprintf);
2647
2648/**
2649 * snprintf - Format a string and place it in a buffer
2650 * @buf: The buffer to place the result into
2651 * @size: The size of the buffer, including the trailing null space
2652 * @fmt: The format string to use
2653 * @...: Arguments for the format string
2654 *
2655 * The return value is the number of characters which would be
2656 * generated for the given input, excluding the trailing null,
2657 * as per ISO C99. If the return is greater than or equal to
2658 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002659 *
2660 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002662int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
2664 va_list args;
2665 int i;
2666
2667 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002668 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return i;
2672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673EXPORT_SYMBOL(snprintf);
2674
2675/**
2676 * scnprintf - Format a string and place it in a buffer
2677 * @buf: The buffer to place the result into
2678 * @size: The size of the buffer, including the trailing null space
2679 * @fmt: The format string to use
2680 * @...: Arguments for the format string
2681 *
2682 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002683 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 */
2685
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002686int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
2688 va_list args;
2689 int i;
2690
2691 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002692 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002694
Anton Arapovb921c692011-01-12 16:59:49 -08002695 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697EXPORT_SYMBOL(scnprintf);
2698
2699/**
2700 * vsprintf - Format a string and place it in a buffer
2701 * @buf: The buffer to place the result into
2702 * @fmt: The format string to use
2703 * @args: Arguments for the format string
2704 *
2705 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002706 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 * buffer overflows.
2708 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002709 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002710 *
2711 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 */
2713int vsprintf(char *buf, const char *fmt, va_list args)
2714{
2715 return vsnprintf(buf, INT_MAX, fmt, args);
2716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717EXPORT_SYMBOL(vsprintf);
2718
2719/**
2720 * sprintf - Format a string and place it in a buffer
2721 * @buf: The buffer to place the result into
2722 * @fmt: The format string to use
2723 * @...: Arguments for the format string
2724 *
2725 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002726 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002728 *
2729 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002731int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 va_list args;
2734 int i;
2735
2736 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002737 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return i;
2741}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742EXPORT_SYMBOL(sprintf);
2743
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002744#ifdef CONFIG_BINARY_PRINTF
2745/*
2746 * bprintf service:
2747 * vbin_printf() - VA arguments to binary data
2748 * bstr_printf() - Binary data to text string
2749 */
2750
2751/**
2752 * vbin_printf - Parse a format string and place args' binary value in a buffer
2753 * @bin_buf: The buffer to place args' binary value
2754 * @size: The size of the buffer(by words(32bits), not characters)
2755 * @fmt: The format string to use
2756 * @args: Arguments for the format string
2757 *
2758 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002759 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002760 *
2761 * The return value is the number of words(32bits) which would be generated for
2762 * the given input.
2763 *
2764 * NOTE:
2765 * If the return value is greater than @size, the resulting bin_buf is NOT
2766 * valid for bstr_printf().
2767 */
2768int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2769{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002770 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002771 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002772 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002773
2774 str = (char *)bin_buf;
2775 end = (char *)(bin_buf + size);
2776
2777#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002778({ \
2779 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002780 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002781 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002782 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002783 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002784 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002785 *(u32 *)str = *(u32 *)&val8; \
2786 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002787 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002788 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002789 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002790 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002791 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002792 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002793 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002794 *(typeof(type) *)str = (type)(long)val4; \
2795 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002796 } \
2797 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002798 value; \
2799})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002800
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002801 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002802 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002803
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002804 fmt += read;
2805
2806 switch (spec.type) {
2807 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002808 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002809 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002810 case FORMAT_TYPE_INVALID:
2811 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002812
Vegard Nossumed681a92009-03-14 12:08:50 +01002813 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002814 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002815 width = (int)save_arg(int);
2816 /* Pointers may require the width */
2817 if (*fmt == 'p')
2818 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002819 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002820
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002821 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002822 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002823 break;
2824
2825 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002826 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002827 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002828 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002829
Petr Mladek3e5903e2019-04-17 13:53:48 +02002830 err_msg = check_pointer_msg(save_str);
2831 if (err_msg)
2832 save_str = err_msg;
2833
André Goddard Rosa6c356632009-12-14 18:00:56 -08002834 len = strlen(save_str) + 1;
2835 if (str + len < end)
2836 memcpy(str, save_str, len);
2837 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002838 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002839 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002840
2841 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002842 /* Dereferenced pointers must be done now */
2843 switch (*fmt) {
2844 /* Dereference of functions is still OK */
2845 case 'S':
2846 case 's':
2847 case 'F':
2848 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002849 case 'x':
2850 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002851 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002852 save_arg(void *);
2853 break;
2854 default:
2855 if (!isalnum(*fmt)) {
2856 save_arg(void *);
2857 break;
2858 }
2859 str = pointer(fmt, str, end, va_arg(args, void *),
2860 spec);
2861 if (str + 1 < end)
2862 *str++ = '\0';
2863 else
2864 end[-1] = '\0'; /* Must be nul terminated */
2865 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002866 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002867 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002868 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002869 break;
2870
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002871 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002872 switch (spec.type) {
2873
2874 case FORMAT_TYPE_LONG_LONG:
2875 save_arg(long long);
2876 break;
2877 case FORMAT_TYPE_ULONG:
2878 case FORMAT_TYPE_LONG:
2879 save_arg(unsigned long);
2880 break;
2881 case FORMAT_TYPE_SIZE_T:
2882 save_arg(size_t);
2883 break;
2884 case FORMAT_TYPE_PTRDIFF:
2885 save_arg(ptrdiff_t);
2886 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002887 case FORMAT_TYPE_UBYTE:
2888 case FORMAT_TYPE_BYTE:
2889 save_arg(char);
2890 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002891 case FORMAT_TYPE_USHORT:
2892 case FORMAT_TYPE_SHORT:
2893 save_arg(short);
2894 break;
2895 default:
2896 save_arg(int);
2897 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002898 }
2899 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002900
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002901out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002902 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002903#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002904}
2905EXPORT_SYMBOL_GPL(vbin_printf);
2906
2907/**
2908 * bstr_printf - Format a string from binary arguments and place it in a buffer
2909 * @buf: The buffer to place the result into
2910 * @size: The size of the buffer, including the trailing null space
2911 * @fmt: The format string to use
2912 * @bin_buf: Binary arguments for the format string
2913 *
2914 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2915 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2916 * a binary buffer that generated by vbin_printf.
2917 *
2918 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002919 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002920 *
2921 * The return value is the number of characters which would
2922 * be generated for the given input, excluding the trailing
2923 * '\0', as per ISO C99. If you want to have the exact
2924 * number of characters written into @buf as return value
2925 * (not including the trailing '\0'), use vscnprintf(). If the
2926 * return is greater than or equal to @size, the resulting
2927 * string is truncated.
2928 */
2929int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2930{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002931 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002932 char *str, *end;
2933 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002934
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002935 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002936 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002937
2938 str = buf;
2939 end = buf + size;
2940
2941#define get_arg(type) \
2942({ \
2943 typeof(type) value; \
2944 if (sizeof(type) == 8) { \
2945 args = PTR_ALIGN(args, sizeof(u32)); \
2946 *(u32 *)&value = *(u32 *)args; \
2947 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2948 } else { \
2949 args = PTR_ALIGN(args, sizeof(type)); \
2950 value = *(typeof(type) *)args; \
2951 } \
2952 args += sizeof(type); \
2953 value; \
2954})
2955
2956 /* Make sure end is always >= buf */
2957 if (end < buf) {
2958 end = ((void *)-1);
2959 size = end - buf;
2960 }
2961
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002962 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002963 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002964 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002965
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002966 fmt += read;
2967
2968 switch (spec.type) {
2969 case FORMAT_TYPE_NONE: {
2970 int copy = read;
2971 if (str < end) {
2972 if (copy > end - str)
2973 copy = end - str;
2974 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002975 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002976 str += read;
2977 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002978 }
2979
Vegard Nossumed681a92009-03-14 12:08:50 +01002980 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002981 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002982 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002983
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002984 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002985 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002986 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002987
André Goddard Rosad4be1512009-12-14 18:00:59 -08002988 case FORMAT_TYPE_CHAR: {
2989 char c;
2990
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002991 if (!(spec.flags & LEFT)) {
2992 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002993 if (str < end)
2994 *str = ' ';
2995 ++str;
2996 }
2997 }
2998 c = (unsigned char) get_arg(char);
2999 if (str < end)
3000 *str = c;
3001 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003002 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003003 if (str < end)
3004 *str = ' ';
3005 ++str;
3006 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003007 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003008 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003009
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003010 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003011 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003012 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003013 str = string(str, end, (char *)str_arg, spec);
3014 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003015 }
3016
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003017 case FORMAT_TYPE_PTR: {
3018 bool process = false;
3019 int copy, len;
3020 /* Non function dereferences were already done */
3021 switch (*fmt) {
3022 case 'S':
3023 case 's':
3024 case 'F':
3025 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04003026 case 'x':
3027 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02003028 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003029 process = true;
3030 break;
3031 default:
3032 if (!isalnum(*fmt)) {
3033 process = true;
3034 break;
3035 }
3036 /* Pointer dereference was already processed */
3037 if (str < end) {
3038 len = copy = strlen(args);
3039 if (copy > end - str)
3040 copy = end - str;
3041 memcpy(str, args, copy);
3042 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003043 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003044 }
3045 }
3046 if (process)
3047 str = pointer(fmt, str, end, get_arg(void *), spec);
3048
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003049 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003050 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003051 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003052 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003053
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003054 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003055 if (str < end)
3056 *str = '%';
3057 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003058 break;
3059
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003060 case FORMAT_TYPE_INVALID:
3061 goto out;
3062
André Goddard Rosad4be1512009-12-14 18:00:59 -08003063 default: {
3064 unsigned long long num;
3065
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003066 switch (spec.type) {
3067
3068 case FORMAT_TYPE_LONG_LONG:
3069 num = get_arg(long long);
3070 break;
3071 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003072 case FORMAT_TYPE_LONG:
3073 num = get_arg(unsigned long);
3074 break;
3075 case FORMAT_TYPE_SIZE_T:
3076 num = get_arg(size_t);
3077 break;
3078 case FORMAT_TYPE_PTRDIFF:
3079 num = get_arg(ptrdiff_t);
3080 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003081 case FORMAT_TYPE_UBYTE:
3082 num = get_arg(unsigned char);
3083 break;
3084 case FORMAT_TYPE_BYTE:
3085 num = get_arg(signed char);
3086 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003087 case FORMAT_TYPE_USHORT:
3088 num = get_arg(unsigned short);
3089 break;
3090 case FORMAT_TYPE_SHORT:
3091 num = get_arg(short);
3092 break;
3093 case FORMAT_TYPE_UINT:
3094 num = get_arg(unsigned int);
3095 break;
3096 default:
3097 num = get_arg(int);
3098 }
3099
3100 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003101 } /* default: */
3102 } /* switch(spec.type) */
3103 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003104
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003105out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003106 if (size > 0) {
3107 if (str < end)
3108 *str = '\0';
3109 else
3110 end[-1] = '\0';
3111 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003112
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003113#undef get_arg
3114
3115 /* the trailing null byte doesn't count towards the total */
3116 return str - buf;
3117}
3118EXPORT_SYMBOL_GPL(bstr_printf);
3119
3120/**
3121 * bprintf - Parse a format string and place args' binary value in a buffer
3122 * @bin_buf: The buffer to place args' binary value
3123 * @size: The size of the buffer(by words(32bits), not characters)
3124 * @fmt: The format string to use
3125 * @...: Arguments for the format string
3126 *
3127 * The function returns the number of words(u32) written
3128 * into @bin_buf.
3129 */
3130int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3131{
3132 va_list args;
3133 int ret;
3134
3135 va_start(args, fmt);
3136 ret = vbin_printf(bin_buf, size, fmt, args);
3137 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003138
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003139 return ret;
3140}
3141EXPORT_SYMBOL_GPL(bprintf);
3142
3143#endif /* CONFIG_BINARY_PRINTF */
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145/**
3146 * vsscanf - Unformat a buffer into a list of arguments
3147 * @buf: input buffer
3148 * @fmt: format of buffer
3149 * @args: arguments
3150 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003151int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 const char *str = buf;
3154 char *next;
3155 char digit;
3156 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003157 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003158 unsigned int base;
3159 union {
3160 long long s;
3161 unsigned long long u;
3162 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003163 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003164 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Jan Beulichda990752012-10-04 17:13:24 -07003166 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /* skip any white space in format */
3168 /* white space in format matchs any amount of
3169 * white space, including none, in the input.
3170 */
3171 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003172 fmt = skip_spaces(++fmt);
3173 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175
3176 /* anything that is not a conversion must match exactly */
3177 if (*fmt != '%' && *fmt) {
3178 if (*fmt++ != *str++)
3179 break;
3180 continue;
3181 }
3182
3183 if (!*fmt)
3184 break;
3185 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* skip this conversion.
3188 * advance both strings to next white space
3189 */
3190 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003191 if (!*str)
3192 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003193 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3194 /* '%*[' not yet supported, invalid format */
3195 if (*fmt == '[')
3196 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 while (!isspace(*str) && *str)
3200 str++;
3201 continue;
3202 }
3203
3204 /* get field width */
3205 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003206 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003208 if (field_width <= 0)
3209 break;
3210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 /* get conversion qualifier */
3213 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003214 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003215 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 qualifier = *fmt++;
3217 if (unlikely(qualifier == *fmt)) {
3218 if (qualifier == 'h') {
3219 qualifier = 'H';
3220 fmt++;
3221 } else if (qualifier == 'l') {
3222 qualifier = 'L';
3223 fmt++;
3224 }
3225 }
3226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Jan Beulichda990752012-10-04 17:13:24 -07003228 if (!*fmt)
3229 break;
3230
3231 if (*fmt == 'n') {
3232 /* return number of characters read so far */
3233 *va_arg(args, int *) = str - buf;
3234 ++fmt;
3235 continue;
3236 }
3237
3238 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 break;
3240
André Goddard Rosad4be1512009-12-14 18:00:59 -08003241 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003242 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003243
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003244 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 case 'c':
3246 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003247 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (field_width == -1)
3249 field_width = 1;
3250 do {
3251 *s++ = *str++;
3252 } while (--field_width > 0 && *str);
3253 num++;
3254 }
3255 continue;
3256 case 's':
3257 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003258 char *s = (char *)va_arg(args, char *);
3259 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003260 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003262 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003265 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 *s = '\0';
3268 num++;
3269 }
3270 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003271 /*
3272 * Warning: This implementation of the '[' conversion specifier
3273 * deviates from its glibc counterpart in the following ways:
3274 * (1) It does NOT support ranges i.e. '-' is NOT a special
3275 * character
3276 * (2) It cannot match the closing bracket ']' itself
3277 * (3) A field width is required
3278 * (4) '%*[' (discard matching input) is currently not supported
3279 *
3280 * Example usage:
3281 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3282 * buf1, buf2, buf3);
3283 * if (ret < 3)
3284 * // etc..
3285 */
3286 case '[':
3287 {
3288 char *s = (char *)va_arg(args, char *);
3289 DECLARE_BITMAP(set, 256) = {0};
3290 unsigned int len = 0;
3291 bool negate = (*fmt == '^');
3292
3293 /* field width is required */
3294 if (field_width == -1)
3295 return num;
3296
3297 if (negate)
3298 ++fmt;
3299
3300 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3301 set_bit((u8)*fmt, set);
3302
3303 /* no ']' or no character set found */
3304 if (!*fmt || !len)
3305 return num;
3306 ++fmt;
3307
3308 if (negate) {
3309 bitmap_complement(set, set, 256);
3310 /* exclude null '\0' byte */
3311 clear_bit(0, set);
3312 }
3313
3314 /* match must be non-empty */
3315 if (!test_bit((u8)*str, set))
3316 return num;
3317
3318 while (test_bit((u8)*str, set) && field_width--)
3319 *s++ = *str++;
3320 *s = '\0';
3321 ++num;
3322 }
3323 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 case 'o':
3325 base = 8;
3326 break;
3327 case 'x':
3328 case 'X':
3329 base = 16;
3330 break;
3331 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003332 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003333 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003335 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003336 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 case 'u':
3338 break;
3339 case '%':
3340 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003341 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return num;
3343 continue;
3344 default:
3345 /* invalid format; stop here */
3346 return num;
3347 }
3348
3349 /* have some sort of integer conversion.
3350 * first, skip white space in buffer.
3351 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003352 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 digit = *str;
3355 if (is_sign && digit == '-')
3356 digit = *(str + 1);
3357
3358 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003359 || (base == 16 && !isxdigit(digit))
3360 || (base == 10 && !isdigit(digit))
3361 || (base == 8 && (!isdigit(digit) || digit > '7'))
3362 || (base == 0 && !isdigit(digit)))
3363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Jan Beulich53809752012-12-17 16:01:31 -08003365 if (is_sign)
3366 val.s = qualifier != 'L' ?
3367 simple_strtol(str, &next, base) :
3368 simple_strtoll(str, &next, base);
3369 else
3370 val.u = qualifier != 'L' ?
3371 simple_strtoul(str, &next, base) :
3372 simple_strtoull(str, &next, base);
3373
3374 if (field_width > 0 && next - str > field_width) {
3375 if (base == 0)
3376 _parse_integer_fixup_radix(str, &base);
3377 while (next - str > field_width) {
3378 if (is_sign)
3379 val.s = div_s64(val.s, base);
3380 else
3381 val.u = div_u64(val.u, base);
3382 --next;
3383 }
3384 }
3385
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003386 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003388 if (is_sign)
3389 *va_arg(args, signed char *) = val.s;
3390 else
3391 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 break;
3393 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003394 if (is_sign)
3395 *va_arg(args, short *) = val.s;
3396 else
3397 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 break;
3399 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003400 if (is_sign)
3401 *va_arg(args, long *) = val.s;
3402 else
3403 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 break;
3405 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003406 if (is_sign)
3407 *va_arg(args, long long *) = val.s;
3408 else
3409 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003412 *va_arg(args, size_t *) = val.u;
3413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 default:
Jan Beulich53809752012-12-17 16:01:31 -08003415 if (is_sign)
3416 *va_arg(args, int *) = val.s;
3417 else
3418 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 break;
3420 }
3421 num++;
3422
3423 if (!next)
3424 break;
3425 str = next;
3426 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return num;
3429}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430EXPORT_SYMBOL(vsscanf);
3431
3432/**
3433 * sscanf - Unformat a buffer into a list of arguments
3434 * @buf: input buffer
3435 * @fmt: formatting of buffer
3436 * @...: resulting arguments
3437 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003438int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
3440 va_list args;
3441 int i;
3442
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003443 va_start(args, fmt);
3444 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return i;
3448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449EXPORT_SYMBOL(sscanf);