blob: 6917d36f7ec9cd3551c15678176a84f84156ab5f [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>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050024#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/types.h>
26#include <linux/string.h>
27#include <linux/ctype.h>
28#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070029#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080030#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070031#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110032#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040033#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080034#include <linux/cred.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020035#include <linux/rtc.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070036#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020037#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000038#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110039#include <linux/siphash.h>
40#include <linux/compiler.h>
Sakari Ailusa92eb762019-10-03 15:32:16 +030041#include <linux/property.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
Petr Mladekc8c3b582019-04-17 13:53:50 +0200617/* Be careful: error messages must fit into the given buffer. */
618static char *error_string(char *buf, char *end, const char *s,
619 struct printf_spec spec)
620{
621 /*
622 * Hard limit to avoid a completely insane messages. It actually
623 * works pretty well because most error messages are in
624 * the many pointer format modifiers.
625 */
626 if (spec.precision == -1)
627 spec.precision = 2 * sizeof(void *);
628
629 return string_nocheck(buf, end, s, spec);
630}
631
Petr Mladek3e5903e2019-04-17 13:53:48 +0200632/*
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200633 * Do not call any complex external code here. Nested printk()/vsprintf()
634 * might cause infinite loops. Failures might break printk() and would
635 * be hard to debug.
Petr Mladek3e5903e2019-04-17 13:53:48 +0200636 */
637static const char *check_pointer_msg(const void *ptr)
638{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200639 if (!ptr)
640 return "(null)";
641
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200642 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
Petr Mladek3e5903e2019-04-17 13:53:48 +0200643 return "(efault)";
644
645 return NULL;
646}
647
648static int check_pointer(char **buf, char *end, const void *ptr,
649 struct printf_spec spec)
650{
651 const char *err_msg;
652
653 err_msg = check_pointer_msg(ptr);
654 if (err_msg) {
Petr Mladekc8c3b582019-04-17 13:53:50 +0200655 *buf = error_string(*buf, end, err_msg, spec);
Petr Mladek3e5903e2019-04-17 13:53:48 +0200656 return -EFAULT;
657 }
658
659 return 0;
660}
661
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800662static noinline_for_stack
Petr Mladekd529ac42019-04-17 13:53:43 +0200663char *string(char *buf, char *end, const char *s,
664 struct printf_spec spec)
665{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200666 if (check_pointer(&buf, end, s, spec))
667 return buf;
Petr Mladekd529ac42019-04-17 13:53:43 +0200668
669 return string_nocheck(buf, end, s, spec);
670}
671
YueHaibingce9d3ec2019-04-27 00:46:30 +0800672static char *pointer_string(char *buf, char *end,
673 const void *ptr,
674 struct printf_spec spec)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200675{
676 spec.base = 16;
677 spec.flags |= SMALL;
678 if (spec.field_width == -1) {
679 spec.field_width = 2 * sizeof(ptr);
680 spec.flags |= ZEROPAD;
681 }
682
683 return number(buf, end, (unsigned long int)ptr, spec);
684}
685
686/* Make pointers available for printing early in the boot sequence. */
687static int debug_boot_weak_hash __ro_after_init;
688
689static int __init debug_boot_weak_hash_enable(char *str)
690{
691 debug_boot_weak_hash = 1;
692 pr_info("debug_boot_weak_hash enabled\n");
693 return 0;
694}
695early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
696
697static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
698static siphash_key_t ptr_key __read_mostly;
699
700static void enable_ptr_key_workfn(struct work_struct *work)
701{
702 get_random_bytes(&ptr_key, sizeof(ptr_key));
703 /* Needs to run from preemptible context */
704 static_branch_disable(&not_filled_random_ptr_key);
705}
706
707static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
708
709static void fill_random_ptr_key(struct random_ready_callback *unused)
710{
711 /* This may be in an interrupt handler. */
712 queue_work(system_unbound_wq, &enable_ptr_key_work);
713}
714
715static struct random_ready_callback random_ready = {
716 .func = fill_random_ptr_key
717};
718
719static int __init initialize_ptr_random(void)
720{
721 int key_size = sizeof(ptr_key);
722 int ret;
723
724 /* Use hw RNG if available. */
725 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
726 static_branch_disable(&not_filled_random_ptr_key);
727 return 0;
728 }
729
730 ret = add_random_ready_callback(&random_ready);
731 if (!ret) {
732 return 0;
733 } else if (ret == -EALREADY) {
734 /* This is in preemptible context */
735 enable_ptr_key_workfn(&enable_ptr_key_work);
736 return 0;
737 }
738
739 return ret;
740}
741early_initcall(initialize_ptr_random);
742
743/* Maps a pointer to a 32 bit unique identifier. */
744static char *ptr_to_id(char *buf, char *end, const void *ptr,
745 struct printf_spec spec)
746{
747 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
748 unsigned long hashval;
749
750 /* When debugging early boot use non-cryptographically secure hash. */
751 if (unlikely(debug_boot_weak_hash)) {
752 hashval = hash_long((unsigned long)ptr, 32);
753 return pointer_string(buf, end, (const void *)hashval, spec);
754 }
755
756 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
757 spec.field_width = 2 * sizeof(ptr);
758 /* string length must be less than default_width */
Petr Mladekc8c3b582019-04-17 13:53:50 +0200759 return error_string(buf, end, str, spec);
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200760 }
761
762#ifdef CONFIG_64BIT
763 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
764 /*
765 * Mask off the first 32 bits, this makes explicit that we have
766 * modified the address (and 32 bits is plenty for a unique ID).
767 */
768 hashval = hashval & 0xffffffff;
769#else
770 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
771#endif
772 return pointer_string(buf, end, (const void *)hashval, spec);
773}
774
Petr Mladek6eea2422019-04-17 13:53:41 +0200775int kptr_restrict __read_mostly;
776
777static noinline_for_stack
778char *restricted_pointer(char *buf, char *end, const void *ptr,
779 struct printf_spec spec)
780{
781 switch (kptr_restrict) {
782 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200783 /* Handle as %p, hash and do _not_ leak addresses. */
784 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200785 case 1: {
786 const struct cred *cred;
787
788 /*
789 * kptr_restrict==1 cannot be used in IRQ context
790 * because its test for CAP_SYSLOG would be meaningless.
791 */
792 if (in_irq() || in_serving_softirq() || in_nmi()) {
793 if (spec.field_width == -1)
794 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200795 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200796 }
797
798 /*
799 * Only print the real pointer value if the current
800 * process has CAP_SYSLOG and is running with the
801 * same credentials it started with. This is because
802 * access to files is checked at open() time, but %pK
803 * checks permission at read() time. We don't want to
804 * leak pointer values if a binary opens a file using
805 * %pK and then elevates privileges before reading it.
806 */
807 cred = current_cred();
808 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
809 !uid_eq(cred->euid, cred->uid) ||
810 !gid_eq(cred->egid, cred->gid))
811 ptr = NULL;
812 break;
813 }
814 case 2:
815 default:
816 /* Always print 0's for %pK */
817 ptr = NULL;
818 break;
819 }
820
821 return pointer_string(buf, end, ptr, spec);
822}
823
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200824static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400825char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
826 const char *fmt)
827{
828 const char *array[4], *s;
829 const struct dentry *p;
830 int depth;
831 int i, n;
832
833 switch (fmt[1]) {
834 case '2': case '3': case '4':
835 depth = fmt[1] - '0';
836 break;
837 default:
838 depth = 1;
839 }
840
841 rcu_read_lock();
842 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200843 if (check_pointer(&buf, end, d, spec)) {
844 rcu_read_unlock();
845 return buf;
846 }
847
Mark Rutland6aa7de02017-10-23 14:07:29 -0700848 p = READ_ONCE(d->d_parent);
849 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400850 if (p == d) {
851 if (i)
852 array[i] = "";
853 i++;
854 break;
855 }
856 }
857 s = array[--i];
858 for (n = 0; n != spec.precision; n++, buf++) {
859 char c = *s++;
860 if (!c) {
861 if (!i)
862 break;
863 c = '/';
864 s = array[--i];
865 }
866 if (buf < end)
867 *buf = c;
868 }
869 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800870 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400871}
872
Jia He36594b32019-08-09 09:24:56 +0800873static noinline_for_stack
874char *file_dentry_name(char *buf, char *end, const struct file *f,
875 struct printf_spec spec, const char *fmt)
876{
877 if (check_pointer(&buf, end, f, spec))
878 return buf;
879
880 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
881}
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400882#ifdef CONFIG_BLOCK
883static noinline_for_stack
884char *bdev_name(char *buf, char *end, struct block_device *bdev,
885 struct printf_spec spec, const char *fmt)
886{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200887 struct gendisk *hd;
888
889 if (check_pointer(&buf, end, bdev, spec))
890 return buf;
891
892 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400893 buf = string(buf, end, hd->disk_name, spec);
894 if (bdev->bd_part->partno) {
895 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
896 if (buf < end)
897 *buf = 'p';
898 buf++;
899 }
900 buf = number(buf, end, bdev->bd_part->partno, spec);
901 }
902 return buf;
903}
904#endif
905
Joe Perchescf3b4292010-05-24 14:33:16 -0700906static noinline_for_stack
907char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800908 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700909{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800910 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700911#ifdef CONFIG_KALLSYMS
912 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800913#endif
914
915 if (fmt[1] == 'R')
916 ptr = __builtin_extract_return_addr(ptr);
917 value = (unsigned long)ptr;
918
919#ifdef CONFIG_KALLSYMS
920 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900921 sprint_backtrace(sym, value);
Sakari Ailus9af77062019-10-03 15:32:14 +0300922 else if (*fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200923 sprint_symbol(sym, value);
924 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700925 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800926
Petr Mladekd529ac42019-04-17 13:53:43 +0200927 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700928#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800929 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700930#endif
931}
932
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200933static const struct printf_spec default_str_spec = {
934 .field_width = -1,
935 .precision = -1,
936};
937
Andy Shevchenko54433972018-02-16 23:07:06 +0200938static const struct printf_spec default_flag_spec = {
939 .base = 16,
940 .precision = -1,
941 .flags = SPECIAL | SMALL,
942};
943
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200944static const struct printf_spec default_dec_spec = {
945 .base = 10,
946 .precision = -1,
947};
948
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200949static const struct printf_spec default_dec02_spec = {
950 .base = 10,
951 .field_width = 2,
952 .precision = -1,
953 .flags = ZEROPAD,
954};
955
956static const struct printf_spec default_dec04_spec = {
957 .base = 10,
958 .field_width = 4,
959 .precision = -1,
960 .flags = ZEROPAD,
961};
962
Joe Perchescf3b4292010-05-24 14:33:16 -0700963static noinline_for_stack
964char *resource_string(char *buf, char *end, struct resource *res,
965 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100966{
967#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600968#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100969#endif
970
971#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600972#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100973#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700974 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100975 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700976 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100977 .precision = -1,
978 .flags = SPECIAL | SMALL | ZEROPAD,
979 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700980 static const struct printf_spec mem_spec = {
981 .base = 16,
982 .field_width = MEM_RSRC_PRINTK_SIZE,
983 .precision = -1,
984 .flags = SPECIAL | SMALL | ZEROPAD,
985 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700986 static const struct printf_spec bus_spec = {
987 .base = 16,
988 .field_width = 2,
989 .precision = -1,
990 .flags = SMALL | ZEROPAD,
991 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700992 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600993 .field_width = -1,
994 .precision = 10,
995 .flags = LEFT,
996 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600997
998 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
999 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1000#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1001#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001002#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001003#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1004 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1005 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1006
Linus Torvalds332d2e72008-10-20 15:07:34 +11001007 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001008 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001009 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +11001010
Petr Mladek3e5903e2019-04-17 13:53:48 +02001011 if (check_pointer(&buf, end, res, spec))
1012 return buf;
1013
Linus Torvalds332d2e72008-10-20 15:07:34 +11001014 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001015 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001016 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001017 specp = &io_spec;
1018 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001019 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001020 specp = &mem_spec;
1021 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001022 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001023 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001024 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001025 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001026 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001027 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001028 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001029 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001030 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001031 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001032 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001033 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001034 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001035 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001036 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001037 p = number(p, pend, resource_size(res), *specp);
1038 } else {
1039 p = number(p, pend, res->start, *specp);
1040 if (res->start != res->end) {
1041 *p++ = '-';
1042 p = number(p, pend, res->end, *specp);
1043 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001044 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001045 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001046 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001047 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001048 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001049 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001050 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001051 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001052 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001053 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001054 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001055 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001056 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001057 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001058 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001059 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001060
Petr Mladekd529ac42019-04-17 13:53:43 +02001061 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001062}
1063
Joe Perchescf3b4292010-05-24 14:33:16 -07001064static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001065char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1066 const char *fmt)
1067{
Steven Rostedt360603a2013-05-28 15:47:39 -04001068 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001069 negative value, fallback to the default */
1070 char separator;
1071
1072 if (spec.field_width == 0)
1073 /* nothing to print */
1074 return buf;
1075
Petr Mladek3e5903e2019-04-17 13:53:48 +02001076 if (check_pointer(&buf, end, addr, spec))
1077 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001078
1079 switch (fmt[1]) {
1080 case 'C':
1081 separator = ':';
1082 break;
1083 case 'D':
1084 separator = '-';
1085 break;
1086 case 'N':
1087 separator = 0;
1088 break;
1089 default:
1090 separator = ' ';
1091 break;
1092 }
1093
1094 if (spec.field_width > 0)
1095 len = min_t(int, spec.field_width, 64);
1096
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001097 for (i = 0; i < len; ++i) {
1098 if (buf < end)
1099 *buf = hex_asc_hi(addr[i]);
1100 ++buf;
1101 if (buf < end)
1102 *buf = hex_asc_lo(addr[i]);
1103 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001104
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001105 if (separator && i != len - 1) {
1106 if (buf < end)
1107 *buf = separator;
1108 ++buf;
1109 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001110 }
1111
1112 return buf;
1113}
1114
1115static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001116char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1117 struct printf_spec spec, const char *fmt)
1118{
1119 const int CHUNKSZ = 32;
1120 int nr_bits = max_t(int, spec.field_width, 0);
1121 int i, chunksz;
1122 bool first = true;
1123
Petr Mladek3e5903e2019-04-17 13:53:48 +02001124 if (check_pointer(&buf, end, bitmap, spec))
1125 return buf;
1126
Tejun Heodbc760b2015-02-13 14:36:53 -08001127 /* reused to print numbers */
1128 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1129
1130 chunksz = nr_bits & (CHUNKSZ - 1);
1131 if (chunksz == 0)
1132 chunksz = CHUNKSZ;
1133
1134 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1135 for (; i >= 0; i -= CHUNKSZ) {
1136 u32 chunkmask, val;
1137 int word, bit;
1138
1139 chunkmask = ((1ULL << chunksz) - 1);
1140 word = i / BITS_PER_LONG;
1141 bit = i % BITS_PER_LONG;
1142 val = (bitmap[word] >> bit) & chunkmask;
1143
1144 if (!first) {
1145 if (buf < end)
1146 *buf = ',';
1147 buf++;
1148 }
1149 first = false;
1150
1151 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1152 buf = number(buf, end, val, spec);
1153
1154 chunksz = CHUNKSZ;
1155 }
1156 return buf;
1157}
1158
1159static noinline_for_stack
1160char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1161 struct printf_spec spec, const char *fmt)
1162{
1163 int nr_bits = max_t(int, spec.field_width, 0);
1164 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1165 int cur, rbot, rtop;
1166 bool first = true;
1167
Petr Mladek3e5903e2019-04-17 13:53:48 +02001168 if (check_pointer(&buf, end, bitmap, spec))
1169 return buf;
1170
Tejun Heodbc760b2015-02-13 14:36:53 -08001171 rbot = cur = find_first_bit(bitmap, nr_bits);
1172 while (cur < nr_bits) {
1173 rtop = cur;
1174 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1175 if (cur < nr_bits && cur <= rtop + 1)
1176 continue;
1177
1178 if (!first) {
1179 if (buf < end)
1180 *buf = ',';
1181 buf++;
1182 }
1183 first = false;
1184
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001185 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001186 if (rbot < rtop) {
1187 if (buf < end)
1188 *buf = '-';
1189 buf++;
1190
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001191 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001192 }
1193
1194 rbot = cur;
1195 }
1196 return buf;
1197}
1198
1199static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001200char *mac_address_string(char *buf, char *end, u8 *addr,
1201 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001202{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001203 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001204 char *p = mac_addr;
1205 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001206 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001207 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001208
Petr Mladek3e5903e2019-04-17 13:53:48 +02001209 if (check_pointer(&buf, end, addr, spec))
1210 return buf;
1211
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001212 switch (fmt[1]) {
1213 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001214 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001215 break;
1216
1217 case 'R':
1218 reversed = true;
1219 /* fall through */
1220
1221 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001222 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001223 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001224 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001225
1226 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001227 if (reversed)
1228 p = hex_byte_pack(p, addr[5 - i]);
1229 else
1230 p = hex_byte_pack(p, addr[i]);
1231
Joe Perches8a27f7c2009-08-17 12:29:44 +00001232 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001233 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001234 }
1235 *p = '\0';
1236
Petr Mladekd529ac42019-04-17 13:53:43 +02001237 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001238}
1239
Joe Perchescf3b4292010-05-24 14:33:16 -07001240static noinline_for_stack
1241char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001242{
Harvey Harrison689afa72008-10-28 16:04:44 -07001243 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001244 bool leading_zeros = (fmt[0] == 'i');
1245 int index;
1246 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001247
Joe Perches0159f242010-01-13 20:23:30 -08001248 switch (fmt[2]) {
1249 case 'h':
1250#ifdef __BIG_ENDIAN
1251 index = 0;
1252 step = 1;
1253#else
1254 index = 3;
1255 step = -1;
1256#endif
1257 break;
1258 case 'l':
1259 index = 3;
1260 step = -1;
1261 break;
1262 case 'n':
1263 case 'b':
1264 default:
1265 index = 0;
1266 step = 1;
1267 break;
1268 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001269 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001270 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001271 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001272 if (leading_zeros) {
1273 if (digits < 3)
1274 *p++ = '0';
1275 if (digits < 2)
1276 *p++ = '0';
1277 }
1278 /* reverse the digits in the quad */
1279 while (digits--)
1280 *p++ = temp[digits];
1281 if (i < 3)
1282 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001283 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001284 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001285 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001286
Joe Perches8a27f7c2009-08-17 12:29:44 +00001287 return p;
1288}
1289
Joe Perchescf3b4292010-05-24 14:33:16 -07001290static noinline_for_stack
1291char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001292{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001293 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001294 unsigned char zerolength[8];
1295 int longest = 1;
1296 int colonpos = -1;
1297 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001298 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001299 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001300 bool useIPv4;
1301 struct in6_addr in6;
1302
1303 memcpy(&in6, addr, sizeof(struct in6_addr));
1304
1305 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001306
1307 memset(zerolength, 0, sizeof(zerolength));
1308
1309 if (useIPv4)
1310 range = 6;
1311 else
1312 range = 8;
1313
1314 /* find position of longest 0 run */
1315 for (i = 0; i < range; i++) {
1316 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001317 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001318 break;
1319 zerolength[i]++;
1320 }
1321 }
1322 for (i = 0; i < range; i++) {
1323 if (zerolength[i] > longest) {
1324 longest = zerolength[i];
1325 colonpos = i;
1326 }
1327 }
Joe Perches29cf5192011-06-09 11:23:37 -07001328 if (longest == 1) /* don't compress a single 0 */
1329 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001330
1331 /* emit address */
1332 for (i = 0; i < range; i++) {
1333 if (i == colonpos) {
1334 if (needcolon || i == 0)
1335 *p++ = ':';
1336 *p++ = ':';
1337 needcolon = false;
1338 i += longest - 1;
1339 continue;
1340 }
1341 if (needcolon) {
1342 *p++ = ':';
1343 needcolon = false;
1344 }
1345 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001346 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001347 hi = word >> 8;
1348 lo = word & 0xff;
1349 if (hi) {
1350 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001351 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001352 else
1353 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001354 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001355 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001356 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001357 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001358 else
1359 *p++ = hex_asc_lo(lo);
1360 needcolon = true;
1361 }
1362
1363 if (useIPv4) {
1364 if (needcolon)
1365 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001366 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001367 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001368 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001369
Joe Perches8a27f7c2009-08-17 12:29:44 +00001370 return p;
1371}
1372
Joe Perchescf3b4292010-05-24 14:33:16 -07001373static noinline_for_stack
1374char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001375{
1376 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001377
Harvey Harrison689afa72008-10-28 16:04:44 -07001378 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001379 p = hex_byte_pack(p, *addr++);
1380 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001381 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001382 *p++ = ':';
1383 }
1384 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001385
Joe Perches8a27f7c2009-08-17 12:29:44 +00001386 return p;
1387}
1388
Joe Perchescf3b4292010-05-24 14:33:16 -07001389static noinline_for_stack
1390char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1391 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001392{
1393 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1394
1395 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001396 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001397 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001398 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001399
Petr Mladekd529ac42019-04-17 13:53:43 +02001400 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001401}
1402
Joe Perchescf3b4292010-05-24 14:33:16 -07001403static noinline_for_stack
1404char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1405 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001406{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001407 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001408
Joe Perches0159f242010-01-13 20:23:30 -08001409 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001410
Petr Mladekd529ac42019-04-17 13:53:43 +02001411 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001412}
1413
Joe Perchescf3b4292010-05-24 14:33:16 -07001414static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001415char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1416 struct printf_spec spec, const char *fmt)
1417{
1418 bool have_p = false, have_s = false, have_f = false, have_c = false;
1419 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1420 sizeof(":12345") + sizeof("/123456789") +
1421 sizeof("%1234567890")];
1422 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1423 const u8 *addr = (const u8 *) &sa->sin6_addr;
1424 char fmt6[2] = { fmt[0], '6' };
1425 u8 off = 0;
1426
1427 fmt++;
1428 while (isalpha(*++fmt)) {
1429 switch (*fmt) {
1430 case 'p':
1431 have_p = true;
1432 break;
1433 case 'f':
1434 have_f = true;
1435 break;
1436 case 's':
1437 have_s = true;
1438 break;
1439 case 'c':
1440 have_c = true;
1441 break;
1442 }
1443 }
1444
1445 if (have_p || have_s || have_f) {
1446 *p = '[';
1447 off = 1;
1448 }
1449
1450 if (fmt6[0] == 'I' && have_c)
1451 p = ip6_compressed_string(ip6_addr + off, addr);
1452 else
1453 p = ip6_string(ip6_addr + off, addr, fmt6);
1454
1455 if (have_p || have_s || have_f)
1456 *p++ = ']';
1457
1458 if (have_p) {
1459 *p++ = ':';
1460 p = number(p, pend, ntohs(sa->sin6_port), spec);
1461 }
1462 if (have_f) {
1463 *p++ = '/';
1464 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1465 IPV6_FLOWINFO_MASK), spec);
1466 }
1467 if (have_s) {
1468 *p++ = '%';
1469 p = number(p, pend, sa->sin6_scope_id, spec);
1470 }
1471 *p = '\0';
1472
Petr Mladekd529ac42019-04-17 13:53:43 +02001473 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001474}
1475
1476static noinline_for_stack
1477char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1478 struct printf_spec spec, const char *fmt)
1479{
1480 bool have_p = false;
1481 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1482 char *pend = ip4_addr + sizeof(ip4_addr);
1483 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1484 char fmt4[3] = { fmt[0], '4', 0 };
1485
1486 fmt++;
1487 while (isalpha(*++fmt)) {
1488 switch (*fmt) {
1489 case 'p':
1490 have_p = true;
1491 break;
1492 case 'h':
1493 case 'l':
1494 case 'n':
1495 case 'b':
1496 fmt4[2] = *fmt;
1497 break;
1498 }
1499 }
1500
1501 p = ip4_string(ip4_addr, addr, fmt4);
1502 if (have_p) {
1503 *p++ = ':';
1504 p = number(p, pend, ntohs(sa->sin_port), spec);
1505 }
1506 *p = '\0';
1507
Petr Mladekd529ac42019-04-17 13:53:43 +02001508 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001509}
1510
1511static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001512char *ip_addr_string(char *buf, char *end, const void *ptr,
1513 struct printf_spec spec, const char *fmt)
1514{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001515 char *err_fmt_msg;
1516
Petr Mladek3e5903e2019-04-17 13:53:48 +02001517 if (check_pointer(&buf, end, ptr, spec))
1518 return buf;
1519
Petr Mladekf00cc102019-04-17 13:53:44 +02001520 switch (fmt[1]) {
1521 case '6':
1522 return ip6_addr_string(buf, end, ptr, spec, fmt);
1523 case '4':
1524 return ip4_addr_string(buf, end, ptr, spec, fmt);
1525 case 'S': {
1526 const union {
1527 struct sockaddr raw;
1528 struct sockaddr_in v4;
1529 struct sockaddr_in6 v6;
1530 } *sa = ptr;
1531
1532 switch (sa->raw.sa_family) {
1533 case AF_INET:
1534 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1535 case AF_INET6:
1536 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1537 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001538 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001539 }}
1540 }
1541
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001542 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001543 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001544}
1545
1546static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001547char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1548 const char *fmt)
1549{
1550 bool found = true;
1551 int count = 1;
1552 unsigned int flags = 0;
1553 int len;
1554
1555 if (spec.field_width == 0)
1556 return buf; /* nothing to print */
1557
Petr Mladek3e5903e2019-04-17 13:53:48 +02001558 if (check_pointer(&buf, end, addr, spec))
1559 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001560
1561 do {
1562 switch (fmt[count++]) {
1563 case 'a':
1564 flags |= ESCAPE_ANY;
1565 break;
1566 case 'c':
1567 flags |= ESCAPE_SPECIAL;
1568 break;
1569 case 'h':
1570 flags |= ESCAPE_HEX;
1571 break;
1572 case 'n':
1573 flags |= ESCAPE_NULL;
1574 break;
1575 case 'o':
1576 flags |= ESCAPE_OCTAL;
1577 break;
1578 case 'p':
1579 flags |= ESCAPE_NP;
1580 break;
1581 case 's':
1582 flags |= ESCAPE_SPACE;
1583 break;
1584 default:
1585 found = false;
1586 break;
1587 }
1588 } while (found);
1589
1590 if (!flags)
1591 flags = ESCAPE_ANY_NP;
1592
1593 len = spec.field_width < 0 ? 1 : spec.field_width;
1594
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001595 /*
1596 * string_escape_mem() writes as many characters as it can to
1597 * the given buffer, and returns the total size of the output
1598 * had the buffer been big enough.
1599 */
1600 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001601
1602 return buf;
1603}
1604
Petr Mladek3e5903e2019-04-17 13:53:48 +02001605static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1606 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001607{
1608 va_list va;
1609
Petr Mladek3e5903e2019-04-17 13:53:48 +02001610 if (check_pointer(&buf, end, va_fmt, spec))
1611 return buf;
1612
Petr Mladek45c3e932019-04-17 13:53:45 +02001613 va_copy(va, *va_fmt->va);
1614 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1615 va_end(va);
1616
1617 return buf;
1618}
1619
Andy Shevchenko71dca952014-10-13 15:55:18 -07001620static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001621char *uuid_string(char *buf, char *end, const u8 *addr,
1622 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001623{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001624 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001625 char *p = uuid;
1626 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001627 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001628 bool uc = false;
1629
Petr Mladek3e5903e2019-04-17 13:53:48 +02001630 if (check_pointer(&buf, end, addr, spec))
1631 return buf;
1632
Joe Perches9ac6e442009-12-14 18:01:09 -08001633 switch (*(++fmt)) {
1634 case 'L':
1635 uc = true; /* fall-through */
1636 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001637 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001638 break;
1639 case 'B':
1640 uc = true;
1641 break;
1642 }
1643
1644 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001645 if (uc)
1646 p = hex_byte_pack_upper(p, addr[index[i]]);
1647 else
1648 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001649 switch (i) {
1650 case 3:
1651 case 5:
1652 case 7:
1653 case 9:
1654 *p++ = '-';
1655 break;
1656 }
1657 }
1658
1659 *p = 0;
1660
Petr Mladekd529ac42019-04-17 13:53:43 +02001661 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001662}
1663
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001664static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001665char *netdev_bits(char *buf, char *end, const void *addr,
1666 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001667{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001668 unsigned long long num;
1669 int size;
1670
Petr Mladek3e5903e2019-04-17 13:53:48 +02001671 if (check_pointer(&buf, end, addr, spec))
1672 return buf;
1673
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001674 switch (fmt[1]) {
1675 case 'F':
1676 num = *(const netdev_features_t *)addr;
1677 size = sizeof(netdev_features_t);
1678 break;
1679 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001680 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001681 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001682
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001683 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001684}
1685
Joe Perchesaaf07622014-01-23 15:54:17 -08001686static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001687char *address_val(char *buf, char *end, const void *addr,
1688 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001689{
1690 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001691 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001692
Petr Mladek3e5903e2019-04-17 13:53:48 +02001693 if (check_pointer(&buf, end, addr, spec))
1694 return buf;
1695
Joe Perchesaaf07622014-01-23 15:54:17 -08001696 switch (fmt[1]) {
1697 case 'd':
1698 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001699 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001700 break;
1701 case 'p':
1702 default:
1703 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001704 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001705 break;
1706 }
1707
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001708 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001709}
1710
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001711static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001712char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1713{
1714 int year = tm->tm_year + (r ? 0 : 1900);
1715 int mon = tm->tm_mon + (r ? 0 : 1);
1716
1717 buf = number(buf, end, year, default_dec04_spec);
1718 if (buf < end)
1719 *buf = '-';
1720 buf++;
1721
1722 buf = number(buf, end, mon, default_dec02_spec);
1723 if (buf < end)
1724 *buf = '-';
1725 buf++;
1726
1727 return number(buf, end, tm->tm_mday, default_dec02_spec);
1728}
1729
1730static noinline_for_stack
1731char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1732{
1733 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1734 if (buf < end)
1735 *buf = ':';
1736 buf++;
1737
1738 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1739 if (buf < end)
1740 *buf = ':';
1741 buf++;
1742
1743 return number(buf, end, tm->tm_sec, default_dec02_spec);
1744}
1745
1746static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001747char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1748 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001749{
1750 bool have_t = true, have_d = true;
1751 bool raw = false;
1752 int count = 2;
1753
Petr Mladek3e5903e2019-04-17 13:53:48 +02001754 if (check_pointer(&buf, end, tm, spec))
1755 return buf;
1756
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001757 switch (fmt[count]) {
1758 case 'd':
1759 have_t = false;
1760 count++;
1761 break;
1762 case 't':
1763 have_d = false;
1764 count++;
1765 break;
1766 }
1767
1768 raw = fmt[count] == 'r';
1769
1770 if (have_d)
1771 buf = date_str(buf, end, tm, raw);
1772 if (have_d && have_t) {
1773 /* Respect ISO 8601 */
1774 if (buf < end)
1775 *buf = 'T';
1776 buf++;
1777 }
1778 if (have_t)
1779 buf = time_str(buf, end, tm, raw);
1780
1781 return buf;
1782}
1783
1784static noinline_for_stack
1785char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1786 const char *fmt)
1787{
1788 switch (fmt[1]) {
1789 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001790 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001791 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001792 return error_string(buf, end, "(%ptR?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001793 }
1794}
1795
1796static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001797char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1798 const char *fmt)
1799{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001800 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001801 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001802
Petr Mladek3e5903e2019-04-17 13:53:48 +02001803 if (check_pointer(&buf, end, clk, spec))
1804 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001805
1806 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001807 case 'n':
1808 default:
1809#ifdef CONFIG_COMMON_CLK
1810 return string(buf, end, __clk_get_name(clk), spec);
1811#else
Geert Uytterhoeven4ca96aa2019-07-01 16:00:09 +02001812 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001813#endif
1814 }
1815}
1816
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001817static
1818char *format_flags(char *buf, char *end, unsigned long flags,
1819 const struct trace_print_flags *names)
1820{
1821 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001822
1823 for ( ; flags && names->name; names++) {
1824 mask = names->mask;
1825 if ((flags & mask) != mask)
1826 continue;
1827
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001828 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001829
1830 flags &= ~mask;
1831 if (flags) {
1832 if (buf < end)
1833 *buf = '|';
1834 buf++;
1835 }
1836 }
1837
1838 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001839 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001840
1841 return buf;
1842}
1843
1844static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001845char *flags_string(char *buf, char *end, void *flags_ptr,
1846 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001847{
1848 unsigned long flags;
1849 const struct trace_print_flags *names;
1850
Petr Mladek3e5903e2019-04-17 13:53:48 +02001851 if (check_pointer(&buf, end, flags_ptr, spec))
1852 return buf;
1853
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001854 switch (fmt[1]) {
1855 case 'p':
1856 flags = *(unsigned long *)flags_ptr;
1857 /* Remove zone id */
1858 flags &= (1UL << NR_PAGEFLAGS) - 1;
1859 names = pageflag_names;
1860 break;
1861 case 'v':
1862 flags = *(unsigned long *)flags_ptr;
1863 names = vmaflag_names;
1864 break;
1865 case 'g':
1866 flags = *(gfp_t *)flags_ptr;
1867 names = gfpflag_names;
1868 break;
1869 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001870 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001871 }
1872
1873 return format_flags(buf, end, flags, names);
1874}
1875
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001876static noinline_for_stack
Sakari Ailusa92eb762019-10-03 15:32:16 +03001877char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1878 char *end)
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001879{
1880 int depth;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001881
Sakari Ailusa92eb762019-10-03 15:32:16 +03001882 /* Loop starting from the root node to the current node. */
1883 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1884 struct fwnode_handle *__fwnode =
1885 fwnode_get_nth_parent(fwnode, depth);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001886
Sakari Ailusa92eb762019-10-03 15:32:16 +03001887 buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001888 default_str_spec);
Sakari Ailusa92eb762019-10-03 15:32:16 +03001889 buf = string(buf, end, fwnode_get_name(__fwnode),
1890 default_str_spec);
1891
1892 fwnode_handle_put(__fwnode);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001893 }
Sakari Ailusa92eb762019-10-03 15:32:16 +03001894
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001895 return buf;
1896}
1897
1898static noinline_for_stack
1899char *device_node_string(char *buf, char *end, struct device_node *dn,
1900 struct printf_spec spec, const char *fmt)
1901{
1902 char tbuf[sizeof("xxxx") + 1];
1903 const char *p;
1904 int ret;
1905 char *buf_start = buf;
1906 struct property *prop;
1907 bool has_mult, pass;
1908 static const struct printf_spec num_spec = {
1909 .flags = SMALL,
1910 .field_width = -1,
1911 .precision = -1,
1912 .base = 10,
1913 };
1914
1915 struct printf_spec str_spec = spec;
1916 str_spec.field_width = -1;
1917
1918 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001919 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001920
Petr Mladek3e5903e2019-04-17 13:53:48 +02001921 if (check_pointer(&buf, end, dn, spec))
1922 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001923
1924 /* simple case without anything any more format specifiers */
1925 fmt++;
1926 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1927 fmt = "f";
1928
1929 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001930 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001931 if (pass) {
1932 if (buf < end)
1933 *buf = ':';
1934 buf++;
1935 }
1936
1937 switch (*fmt) {
1938 case 'f': /* full_name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001939 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
1940 end);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001941 break;
1942 case 'n': /* name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001943 p = fwnode_get_name(of_fwnode_handle(dn));
Rob Herring6d0a70a2018-08-27 08:13:56 -05001944 precision = str_spec.precision;
1945 str_spec.precision = strchrnul(p, '@') - p;
1946 buf = string(buf, end, p, str_spec);
1947 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001948 break;
1949 case 'p': /* phandle */
1950 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1951 break;
1952 case 'P': /* path-spec */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001953 p = fwnode_get_name(of_fwnode_handle(dn));
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001954 if (!p[1])
1955 p = "/";
1956 buf = string(buf, end, p, str_spec);
1957 break;
1958 case 'F': /* flags */
1959 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1960 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1961 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1962 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1963 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02001964 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001965 break;
1966 case 'c': /* major compatible string */
1967 ret = of_property_read_string(dn, "compatible", &p);
1968 if (!ret)
1969 buf = string(buf, end, p, str_spec);
1970 break;
1971 case 'C': /* full compatible string */
1972 has_mult = false;
1973 of_property_for_each_string(dn, "compatible", prop, p) {
1974 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02001975 buf = string_nocheck(buf, end, ",", str_spec);
1976 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001977 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02001978 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001979
1980 has_mult = true;
1981 }
1982 break;
1983 default:
1984 break;
1985 }
1986 }
1987
1988 return widen_string(buf, buf - buf_start, end, spec);
1989}
1990
Petr Mladek798cc272019-04-17 13:53:46 +02001991static char *kobject_string(char *buf, char *end, void *ptr,
1992 struct printf_spec spec, const char *fmt)
1993{
1994 switch (fmt[1]) {
1995 case 'F':
1996 return device_node_string(buf, end, ptr, spec, fmt + 1);
1997 }
1998
Petr Mladekc8c3b582019-04-17 13:53:50 +02001999 return error_string(buf, end, "(%pO?)", spec);
Petr Mladek798cc272019-04-17 13:53:46 +02002000}
2001
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002002/*
2003 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2004 * by an extra set of alphanumeric characters that are extended format
2005 * specifiers.
2006 *
Joe Perches0b523762017-05-08 15:55:36 -07002007 * Please update scripts/checkpatch.pl when adding/removing conversion
2008 * characters. (Search for "check for vsprintf extension").
2009 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002010 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002011 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002012 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2013 * - 's' For symbolic direct pointers (or function descriptors) without offset
Sakari Ailus9af77062019-10-03 15:32:14 +03002014 * - '[Ss]R' as above with __builtin_extract_return_addr() translation
Sakari Ailus1586c5a2019-10-03 15:32:15 +03002015 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2016 * %ps and %pS. Be careful when re-using these specifiers.
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002017 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002018 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2019 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002020 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2021 * width which must be explicitly specified either as part of the
2022 * format string '%32b[l]' or through '%*b[l]', [l] selects
2023 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002024 * - 'M' For a 6-byte MAC address, it prints the address in the
2025 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002026 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002027 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002028 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002029 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002030 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2031 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2032 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002033 * [S][pfs]
2034 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2035 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002036 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2037 * IPv6 omits the colons (01020304...0f)
2038 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002039 * [S][pfs]
2040 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2041 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2042 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2043 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07002044 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002045 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2046 * of the following flags (see string_escape_mem() for the
2047 * details):
2048 * a - ESCAPE_ANY
2049 * c - ESCAPE_SPECIAL
2050 * h - ESCAPE_HEX
2051 * n - ESCAPE_NULL
2052 * o - ESCAPE_OCTAL
2053 * p - ESCAPE_NP
2054 * s - ESCAPE_SPACE
2055 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002056 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2057 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2058 * Options for %pU are:
2059 * b big endian lower case hex (default)
2060 * B big endian UPPER case hex
2061 * l little endian lower case hex
2062 * L little endian UPPER case hex
2063 * big endian output byte order is:
2064 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2065 * little endian output byte order is:
2066 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002067 * - 'V' For a struct va_format which contains a format string * and va_list *,
2068 * call vsnprintf(->format, *->va_list).
2069 * Implements a "recursive vsnprintf".
2070 * Do not use this feature without some mechanism to verify the
2071 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002072 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002073 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002074 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2075 * a certain separator (' ' by default):
2076 * C colon
2077 * D dash
2078 * N no separator
2079 * The maximum supported length is 64 bytes of the input. Consider
2080 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002081 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2082 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002083 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2084 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002085 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002086 * - 't[R][dt][r]' For time and date as represented:
2087 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002088 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2089 * (legacy clock framework) of the clock
2090 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2091 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002092 * - 'G' For flags to be printed as a collection of symbolic strings that would
2093 * construct the specific value. Supported flags given by option:
2094 * p page flags (see struct page) given as pointer to unsigned long
2095 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2096 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002097 * - 'OF[fnpPcCF]' For a device tree object
2098 * Without any optional arguments prints the full_name
2099 * f device node full_name
2100 * n device node name
2101 * p device node phandle
2102 * P device node path spec (name + @unit)
2103 * F device node flags
2104 * c major compatible string
2105 * C full compatible string
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002106 * - 'x' For printing the address. Equivalent to "%lx".
2107 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002108 * ** When making changes please also update:
2109 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002110 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002111 * Note: The default behaviour (unadorned %p) is to hash the address,
2112 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002113 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002114static noinline_for_stack
2115char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2116 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002117{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002118 switch (*fmt) {
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002119 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002120 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002121 ptr = dereference_symbol_descriptor(ptr);
2122 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002123 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002124 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002125 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002126 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002127 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002128 case 'h':
2129 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002130 case 'b':
2131 switch (fmt[1]) {
2132 case 'l':
2133 return bitmap_list_string(buf, end, ptr, spec, fmt);
2134 default:
2135 return bitmap_string(buf, end, ptr, spec, fmt);
2136 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002137 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2138 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002139 /* [mM]F (FDDI) */
2140 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002141 return mac_address_string(buf, end, ptr, spec, fmt);
2142 case 'I': /* Formatted IP supported
2143 * 4: 1.2.3.4
2144 * 6: 0001:0203:...:0708
2145 * 6c: 1::708 or 1::1.2.3.4
2146 */
2147 case 'i': /* Contiguous:
2148 * 4: 001.002.003.004
2149 * 6: 000102...0f
2150 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002151 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002152 case 'E':
2153 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002154 case 'U':
2155 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002156 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002157 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002158 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002159 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002160 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002161 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002162 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002163 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002164 case 'd':
2165 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002166 case 't':
2167 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002168 case 'C':
2169 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002170 case 'D':
Jia He36594b32019-08-09 09:24:56 +08002171 return file_dentry_name(buf, end, ptr, spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002172#ifdef CONFIG_BLOCK
2173 case 'g':
2174 return bdev_name(buf, end, ptr, spec, fmt);
2175#endif
2176
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002177 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002178 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002179 case 'O':
Petr Mladek798cc272019-04-17 13:53:46 +02002180 return kobject_string(buf, end, ptr, spec, fmt);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002181 case 'x':
2182 return pointer_string(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002183 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002184
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002185 /* default is to _not_ leak addresses, hash before printing */
2186 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002187}
2188
2189/*
2190 * Helper function to decode printf style format.
2191 * Each call decode a token from the format and return the
2192 * number of characters read (or likely the delta where it wants
2193 * to go on the next call).
2194 * The decoded token is returned through the parameters
2195 *
2196 * 'h', 'l', or 'L' for integer fields
2197 * 'z' support added 23/7/1999 S.H.
2198 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002199 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002200 * 't' added for ptrdiff_t
2201 *
2202 * @fmt: the format string
2203 * @type of the token returned
2204 * @flags: various flags such as +, -, # tokens..
2205 * @field_width: overwritten width
2206 * @base: base of the number (octal, hex, ...)
2207 * @precision: precision of a number
2208 * @qualifier: qualifier of a number (long, size_t, ...)
2209 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002210static noinline_for_stack
2211int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002212{
2213 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002214 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002215
2216 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002217 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002218 if (spec->field_width < 0) {
2219 spec->field_width = -spec->field_width;
2220 spec->flags |= LEFT;
2221 }
2222 spec->type = FORMAT_TYPE_NONE;
2223 goto precision;
2224 }
2225
2226 /* we finished early by reading the precision */
2227 if (spec->type == FORMAT_TYPE_PRECISION) {
2228 if (spec->precision < 0)
2229 spec->precision = 0;
2230
2231 spec->type = FORMAT_TYPE_NONE;
2232 goto qualifier;
2233 }
2234
2235 /* By default */
2236 spec->type = FORMAT_TYPE_NONE;
2237
2238 for (; *fmt ; ++fmt) {
2239 if (*fmt == '%')
2240 break;
2241 }
2242
2243 /* Return the current non-format string */
2244 if (fmt != start || !*fmt)
2245 return fmt - start;
2246
2247 /* Process flags */
2248 spec->flags = 0;
2249
2250 while (1) { /* this also skips first '%' */
2251 bool found = true;
2252
2253 ++fmt;
2254
2255 switch (*fmt) {
2256 case '-': spec->flags |= LEFT; break;
2257 case '+': spec->flags |= PLUS; break;
2258 case ' ': spec->flags |= SPACE; break;
2259 case '#': spec->flags |= SPECIAL; break;
2260 case '0': spec->flags |= ZEROPAD; break;
2261 default: found = false;
2262 }
2263
2264 if (!found)
2265 break;
2266 }
2267
2268 /* get field width */
2269 spec->field_width = -1;
2270
2271 if (isdigit(*fmt))
2272 spec->field_width = skip_atoi(&fmt);
2273 else if (*fmt == '*') {
2274 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002275 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002276 return ++fmt - start;
2277 }
2278
2279precision:
2280 /* get the precision */
2281 spec->precision = -1;
2282 if (*fmt == '.') {
2283 ++fmt;
2284 if (isdigit(*fmt)) {
2285 spec->precision = skip_atoi(&fmt);
2286 if (spec->precision < 0)
2287 spec->precision = 0;
2288 } else if (*fmt == '*') {
2289 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002290 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002291 return ++fmt - start;
2292 }
2293 }
2294
2295qualifier:
2296 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002297 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002298 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002299 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002300 qualifier = *fmt++;
2301 if (unlikely(qualifier == *fmt)) {
2302 if (qualifier == 'l') {
2303 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002304 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002305 } else if (qualifier == 'h') {
2306 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002307 ++fmt;
2308 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002309 }
2310 }
2311
2312 /* default base */
2313 spec->base = 10;
2314 switch (*fmt) {
2315 case 'c':
2316 spec->type = FORMAT_TYPE_CHAR;
2317 return ++fmt - start;
2318
2319 case 's':
2320 spec->type = FORMAT_TYPE_STR;
2321 return ++fmt - start;
2322
2323 case 'p':
2324 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002325 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002326
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002327 case '%':
2328 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2329 return ++fmt - start;
2330
2331 /* integer number formats - set up the flags and "break" */
2332 case 'o':
2333 spec->base = 8;
2334 break;
2335
2336 case 'x':
2337 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002338 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002339
2340 case 'X':
2341 spec->base = 16;
2342 break;
2343
2344 case 'd':
2345 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002346 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002347 case 'u':
2348 break;
2349
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002350 case 'n':
2351 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002352 * Since %n poses a greater security risk than
2353 * utility, treat it as any other invalid or
2354 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002355 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002356 /* Fall-through */
2357
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002358 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002359 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002360 spec->type = FORMAT_TYPE_INVALID;
2361 return fmt - start;
2362 }
2363
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002364 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002365 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002366 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002367 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2368 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002369 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002370 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002371 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002372 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002373 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002374 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2375 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002376 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002377 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2378 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002379 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002380 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2381 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002382 }
2383
2384 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002385}
2386
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002387static void
2388set_field_width(struct printf_spec *spec, int width)
2389{
2390 spec->field_width = width;
2391 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2392 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2393 }
2394}
2395
2396static void
2397set_precision(struct printf_spec *spec, int prec)
2398{
2399 spec->precision = prec;
2400 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2401 spec->precision = clamp(prec, 0, PRECISION_MAX);
2402 }
2403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405/**
2406 * vsnprintf - Format a string and place it in a buffer
2407 * @buf: The buffer to place the result into
2408 * @size: The size of the buffer, including the trailing null space
2409 * @fmt: The format string to use
2410 * @args: Arguments for the format string
2411 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002412 * This function generally follows C99 vsnprintf, but has some
2413 * extensions and a few limitations:
2414 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002415 * - ``%n`` is unsupported
2416 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002417 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002418 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002419 * extensive description.
2420 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002421 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 * The return value is the number of characters which would
2424 * be generated for the given input, excluding the trailing
2425 * '\0', as per ISO C99. If you want to have the exact
2426 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002427 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 * return is greater than or equal to @size, the resulting
2429 * string is truncated.
2430 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002431 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 */
2433int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002436 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002437 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002439 /* Reject out-of-range values early. Large positive sizes are
2440 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002441 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002445 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002447 /* Make sure end is always >= buf */
2448 if (end < buf) {
2449 end = ((void *)-1);
2450 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002453 while (*fmt) {
2454 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002455 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002456
2457 fmt += read;
2458
2459 switch (spec.type) {
2460 case FORMAT_TYPE_NONE: {
2461 int copy = read;
2462 if (str < end) {
2463 if (copy > end - str)
2464 copy = end - str;
2465 memcpy(str, old_fmt, copy);
2466 }
2467 str += read;
2468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470
Vegard Nossumed681a92009-03-14 12:08:50 +01002471 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002472 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002475 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002476 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
André Goddard Rosad4be1512009-12-14 18:00:59 -08002479 case FORMAT_TYPE_CHAR: {
2480 char c;
2481
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002482 if (!(spec.flags & LEFT)) {
2483 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002484 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 *str = ' ';
2486 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002489 }
2490 c = (unsigned char) va_arg(args, int);
2491 if (str < end)
2492 *str = c;
2493 ++str;
2494 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002495 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002496 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002498 }
2499 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002502 case FORMAT_TYPE_STR:
2503 str = string(str, end, va_arg(args, char *), spec);
2504 break;
2505
2506 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002507 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002508 spec);
2509 while (isalnum(*fmt))
2510 fmt++;
2511 break;
2512
2513 case FORMAT_TYPE_PERCENT_CHAR:
2514 if (str < end)
2515 *str = '%';
2516 ++str;
2517 break;
2518
2519 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002520 /*
2521 * Presumably the arguments passed gcc's type
2522 * checking, but there is no safe or sane way
2523 * for us to continue parsing the format and
2524 * fetching from the va_list; the remaining
2525 * specifiers and arguments would be out of
2526 * sync.
2527 */
2528 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002529
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002530 default:
2531 switch (spec.type) {
2532 case FORMAT_TYPE_LONG_LONG:
2533 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002535 case FORMAT_TYPE_ULONG:
2536 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002538 case FORMAT_TYPE_LONG:
2539 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002541 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002542 if (spec.flags & SIGN)
2543 num = va_arg(args, ssize_t);
2544 else
2545 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002546 break;
2547 case FORMAT_TYPE_PTRDIFF:
2548 num = va_arg(args, ptrdiff_t);
2549 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002550 case FORMAT_TYPE_UBYTE:
2551 num = (unsigned char) va_arg(args, int);
2552 break;
2553 case FORMAT_TYPE_BYTE:
2554 num = (signed char) va_arg(args, int);
2555 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002556 case FORMAT_TYPE_USHORT:
2557 num = (unsigned short) va_arg(args, int);
2558 break;
2559 case FORMAT_TYPE_SHORT:
2560 num = (short) va_arg(args, int);
2561 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002562 case FORMAT_TYPE_INT:
2563 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002564 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002566 num = va_arg(args, unsigned int);
2567 }
2568
2569 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002572
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002573out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002574 if (size > 0) {
2575 if (str < end)
2576 *str = '\0';
2577 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002578 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002579 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002580
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002581 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585EXPORT_SYMBOL(vsnprintf);
2586
2587/**
2588 * vscnprintf - Format a string and place it in a buffer
2589 * @buf: The buffer to place the result into
2590 * @size: The size of the buffer, including the trailing null space
2591 * @fmt: The format string to use
2592 * @args: Arguments for the format string
2593 *
2594 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002595 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 * returns 0.
2597 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002598 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002599 *
2600 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 */
2602int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2603{
2604 int i;
2605
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002606 i = vsnprintf(buf, size, fmt, args);
2607
Anton Arapovb921c692011-01-12 16:59:49 -08002608 if (likely(i < size))
2609 return i;
2610 if (size != 0)
2611 return size - 1;
2612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614EXPORT_SYMBOL(vscnprintf);
2615
2616/**
2617 * snprintf - Format a string and place it in a buffer
2618 * @buf: The buffer to place the result into
2619 * @size: The size of the buffer, including the trailing null space
2620 * @fmt: The format string to use
2621 * @...: Arguments for the format string
2622 *
2623 * The return value is the number of characters which would be
2624 * generated for the given input, excluding the trailing null,
2625 * as per ISO C99. If the return is greater than or equal to
2626 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002627 *
2628 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002630int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 va_list args;
2633 int i;
2634
2635 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002636 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return i;
2640}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641EXPORT_SYMBOL(snprintf);
2642
2643/**
2644 * scnprintf - Format a string and place it in a buffer
2645 * @buf: The buffer to place the result into
2646 * @size: The size of the buffer, including the trailing null space
2647 * @fmt: The format string to use
2648 * @...: Arguments for the format string
2649 *
2650 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002651 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 */
2653
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002654int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 va_list args;
2657 int i;
2658
2659 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002660 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002662
Anton Arapovb921c692011-01-12 16:59:49 -08002663 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665EXPORT_SYMBOL(scnprintf);
2666
2667/**
2668 * vsprintf - Format a string and place it in a buffer
2669 * @buf: The buffer to place the result into
2670 * @fmt: The format string to use
2671 * @args: Arguments for the format string
2672 *
2673 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002674 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 * buffer overflows.
2676 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002677 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002678 *
2679 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 */
2681int vsprintf(char *buf, const char *fmt, va_list args)
2682{
2683 return vsnprintf(buf, INT_MAX, fmt, args);
2684}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685EXPORT_SYMBOL(vsprintf);
2686
2687/**
2688 * sprintf - Format a string and place it in a buffer
2689 * @buf: The buffer to place the result into
2690 * @fmt: The format string to use
2691 * @...: Arguments for the format string
2692 *
2693 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002694 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002696 *
2697 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002699int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
2701 va_list args;
2702 int i;
2703
2704 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002705 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return i;
2709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710EXPORT_SYMBOL(sprintf);
2711
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002712#ifdef CONFIG_BINARY_PRINTF
2713/*
2714 * bprintf service:
2715 * vbin_printf() - VA arguments to binary data
2716 * bstr_printf() - Binary data to text string
2717 */
2718
2719/**
2720 * vbin_printf - Parse a format string and place args' binary value in a buffer
2721 * @bin_buf: The buffer to place args' binary value
2722 * @size: The size of the buffer(by words(32bits), not characters)
2723 * @fmt: The format string to use
2724 * @args: Arguments for the format string
2725 *
2726 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002727 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002728 *
2729 * The return value is the number of words(32bits) which would be generated for
2730 * the given input.
2731 *
2732 * NOTE:
2733 * If the return value is greater than @size, the resulting bin_buf is NOT
2734 * valid for bstr_printf().
2735 */
2736int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2737{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002738 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002739 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002740 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002741
2742 str = (char *)bin_buf;
2743 end = (char *)(bin_buf + size);
2744
2745#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002746({ \
2747 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002748 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002749 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002750 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002751 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002752 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002753 *(u32 *)str = *(u32 *)&val8; \
2754 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002755 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002756 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002757 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002758 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002759 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002760 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002761 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002762 *(typeof(type) *)str = (type)(long)val4; \
2763 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002764 } \
2765 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002766 value; \
2767})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002768
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002769 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002770 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002771
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002772 fmt += read;
2773
2774 switch (spec.type) {
2775 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002776 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002777 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002778 case FORMAT_TYPE_INVALID:
2779 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002780
Vegard Nossumed681a92009-03-14 12:08:50 +01002781 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002782 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002783 width = (int)save_arg(int);
2784 /* Pointers may require the width */
2785 if (*fmt == 'p')
2786 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002787 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002788
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002789 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002790 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002791 break;
2792
2793 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002794 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002795 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002796 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002797
Petr Mladek3e5903e2019-04-17 13:53:48 +02002798 err_msg = check_pointer_msg(save_str);
2799 if (err_msg)
2800 save_str = err_msg;
2801
André Goddard Rosa6c356632009-12-14 18:00:56 -08002802 len = strlen(save_str) + 1;
2803 if (str + len < end)
2804 memcpy(str, save_str, len);
2805 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002806 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002807 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002808
2809 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002810 /* Dereferenced pointers must be done now */
2811 switch (*fmt) {
2812 /* Dereference of functions is still OK */
2813 case 'S':
2814 case 's':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002815 case 'x':
2816 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002817 save_arg(void *);
2818 break;
2819 default:
2820 if (!isalnum(*fmt)) {
2821 save_arg(void *);
2822 break;
2823 }
2824 str = pointer(fmt, str, end, va_arg(args, void *),
2825 spec);
2826 if (str + 1 < end)
2827 *str++ = '\0';
2828 else
2829 end[-1] = '\0'; /* Must be nul terminated */
2830 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002831 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002832 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002833 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002834 break;
2835
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002836 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002837 switch (spec.type) {
2838
2839 case FORMAT_TYPE_LONG_LONG:
2840 save_arg(long long);
2841 break;
2842 case FORMAT_TYPE_ULONG:
2843 case FORMAT_TYPE_LONG:
2844 save_arg(unsigned long);
2845 break;
2846 case FORMAT_TYPE_SIZE_T:
2847 save_arg(size_t);
2848 break;
2849 case FORMAT_TYPE_PTRDIFF:
2850 save_arg(ptrdiff_t);
2851 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002852 case FORMAT_TYPE_UBYTE:
2853 case FORMAT_TYPE_BYTE:
2854 save_arg(char);
2855 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002856 case FORMAT_TYPE_USHORT:
2857 case FORMAT_TYPE_SHORT:
2858 save_arg(short);
2859 break;
2860 default:
2861 save_arg(int);
2862 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002863 }
2864 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002865
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002866out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002867 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002868#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002869}
2870EXPORT_SYMBOL_GPL(vbin_printf);
2871
2872/**
2873 * bstr_printf - Format a string from binary arguments and place it in a buffer
2874 * @buf: The buffer to place the result into
2875 * @size: The size of the buffer, including the trailing null space
2876 * @fmt: The format string to use
2877 * @bin_buf: Binary arguments for the format string
2878 *
2879 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2880 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2881 * a binary buffer that generated by vbin_printf.
2882 *
2883 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002884 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002885 *
2886 * The return value is the number of characters which would
2887 * be generated for the given input, excluding the trailing
2888 * '\0', as per ISO C99. If you want to have the exact
2889 * number of characters written into @buf as return value
2890 * (not including the trailing '\0'), use vscnprintf(). If the
2891 * return is greater than or equal to @size, the resulting
2892 * string is truncated.
2893 */
2894int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2895{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002896 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002897 char *str, *end;
2898 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002899
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002900 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002901 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002902
2903 str = buf;
2904 end = buf + size;
2905
2906#define get_arg(type) \
2907({ \
2908 typeof(type) value; \
2909 if (sizeof(type) == 8) { \
2910 args = PTR_ALIGN(args, sizeof(u32)); \
2911 *(u32 *)&value = *(u32 *)args; \
2912 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2913 } else { \
2914 args = PTR_ALIGN(args, sizeof(type)); \
2915 value = *(typeof(type) *)args; \
2916 } \
2917 args += sizeof(type); \
2918 value; \
2919})
2920
2921 /* Make sure end is always >= buf */
2922 if (end < buf) {
2923 end = ((void *)-1);
2924 size = end - buf;
2925 }
2926
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002927 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002928 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002929 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002930
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002931 fmt += read;
2932
2933 switch (spec.type) {
2934 case FORMAT_TYPE_NONE: {
2935 int copy = read;
2936 if (str < end) {
2937 if (copy > end - str)
2938 copy = end - str;
2939 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002940 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002941 str += read;
2942 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002943 }
2944
Vegard Nossumed681a92009-03-14 12:08:50 +01002945 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002946 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002947 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002948
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002949 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002950 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002951 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002952
André Goddard Rosad4be1512009-12-14 18:00:59 -08002953 case FORMAT_TYPE_CHAR: {
2954 char c;
2955
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002956 if (!(spec.flags & LEFT)) {
2957 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002958 if (str < end)
2959 *str = ' ';
2960 ++str;
2961 }
2962 }
2963 c = (unsigned char) get_arg(char);
2964 if (str < end)
2965 *str = c;
2966 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002967 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002968 if (str < end)
2969 *str = ' ';
2970 ++str;
2971 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002972 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002973 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002974
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002975 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002976 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002977 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002978 str = string(str, end, (char *)str_arg, spec);
2979 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002980 }
2981
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002982 case FORMAT_TYPE_PTR: {
2983 bool process = false;
2984 int copy, len;
2985 /* Non function dereferences were already done */
2986 switch (*fmt) {
2987 case 'S':
2988 case 's':
2989 case 'F':
2990 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002991 case 'x':
2992 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002993 process = true;
2994 break;
2995 default:
2996 if (!isalnum(*fmt)) {
2997 process = true;
2998 break;
2999 }
3000 /* Pointer dereference was already processed */
3001 if (str < end) {
3002 len = copy = strlen(args);
3003 if (copy > end - str)
3004 copy = end - str;
3005 memcpy(str, args, copy);
3006 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003007 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003008 }
3009 }
3010 if (process)
3011 str = pointer(fmt, str, end, get_arg(void *), spec);
3012
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003013 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003014 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003015 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003016 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003017
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003018 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003019 if (str < end)
3020 *str = '%';
3021 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003022 break;
3023
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003024 case FORMAT_TYPE_INVALID:
3025 goto out;
3026
André Goddard Rosad4be1512009-12-14 18:00:59 -08003027 default: {
3028 unsigned long long num;
3029
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003030 switch (spec.type) {
3031
3032 case FORMAT_TYPE_LONG_LONG:
3033 num = get_arg(long long);
3034 break;
3035 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003036 case FORMAT_TYPE_LONG:
3037 num = get_arg(unsigned long);
3038 break;
3039 case FORMAT_TYPE_SIZE_T:
3040 num = get_arg(size_t);
3041 break;
3042 case FORMAT_TYPE_PTRDIFF:
3043 num = get_arg(ptrdiff_t);
3044 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003045 case FORMAT_TYPE_UBYTE:
3046 num = get_arg(unsigned char);
3047 break;
3048 case FORMAT_TYPE_BYTE:
3049 num = get_arg(signed char);
3050 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003051 case FORMAT_TYPE_USHORT:
3052 num = get_arg(unsigned short);
3053 break;
3054 case FORMAT_TYPE_SHORT:
3055 num = get_arg(short);
3056 break;
3057 case FORMAT_TYPE_UINT:
3058 num = get_arg(unsigned int);
3059 break;
3060 default:
3061 num = get_arg(int);
3062 }
3063
3064 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003065 } /* default: */
3066 } /* switch(spec.type) */
3067 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003068
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003069out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003070 if (size > 0) {
3071 if (str < end)
3072 *str = '\0';
3073 else
3074 end[-1] = '\0';
3075 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003076
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003077#undef get_arg
3078
3079 /* the trailing null byte doesn't count towards the total */
3080 return str - buf;
3081}
3082EXPORT_SYMBOL_GPL(bstr_printf);
3083
3084/**
3085 * bprintf - Parse a format string and place args' binary value in a buffer
3086 * @bin_buf: The buffer to place args' binary value
3087 * @size: The size of the buffer(by words(32bits), not characters)
3088 * @fmt: The format string to use
3089 * @...: Arguments for the format string
3090 *
3091 * The function returns the number of words(u32) written
3092 * into @bin_buf.
3093 */
3094int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3095{
3096 va_list args;
3097 int ret;
3098
3099 va_start(args, fmt);
3100 ret = vbin_printf(bin_buf, size, fmt, args);
3101 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003102
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003103 return ret;
3104}
3105EXPORT_SYMBOL_GPL(bprintf);
3106
3107#endif /* CONFIG_BINARY_PRINTF */
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109/**
3110 * vsscanf - Unformat a buffer into a list of arguments
3111 * @buf: input buffer
3112 * @fmt: format of buffer
3113 * @args: arguments
3114 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003115int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 const char *str = buf;
3118 char *next;
3119 char digit;
3120 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003121 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003122 unsigned int base;
3123 union {
3124 long long s;
3125 unsigned long long u;
3126 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003127 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003128 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Jan Beulichda990752012-10-04 17:13:24 -07003130 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /* skip any white space in format */
3132 /* white space in format matchs any amount of
3133 * white space, including none, in the input.
3134 */
3135 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003136 fmt = skip_spaces(++fmt);
3137 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
3139
3140 /* anything that is not a conversion must match exactly */
3141 if (*fmt != '%' && *fmt) {
3142 if (*fmt++ != *str++)
3143 break;
3144 continue;
3145 }
3146
3147 if (!*fmt)
3148 break;
3149 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 /* skip this conversion.
3152 * advance both strings to next white space
3153 */
3154 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003155 if (!*str)
3156 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003157 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3158 /* '%*[' not yet supported, invalid format */
3159 if (*fmt == '[')
3160 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 while (!isspace(*str) && *str)
3164 str++;
3165 continue;
3166 }
3167
3168 /* get field width */
3169 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003170 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003172 if (field_width <= 0)
3173 break;
3174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 /* get conversion qualifier */
3177 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003178 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003179 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 qualifier = *fmt++;
3181 if (unlikely(qualifier == *fmt)) {
3182 if (qualifier == 'h') {
3183 qualifier = 'H';
3184 fmt++;
3185 } else if (qualifier == 'l') {
3186 qualifier = 'L';
3187 fmt++;
3188 }
3189 }
3190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Jan Beulichda990752012-10-04 17:13:24 -07003192 if (!*fmt)
3193 break;
3194
3195 if (*fmt == 'n') {
3196 /* return number of characters read so far */
3197 *va_arg(args, int *) = str - buf;
3198 ++fmt;
3199 continue;
3200 }
3201
3202 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 break;
3204
André Goddard Rosad4be1512009-12-14 18:00:59 -08003205 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003206 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003207
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003208 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 case 'c':
3210 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003211 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (field_width == -1)
3213 field_width = 1;
3214 do {
3215 *s++ = *str++;
3216 } while (--field_width > 0 && *str);
3217 num++;
3218 }
3219 continue;
3220 case 's':
3221 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003222 char *s = (char *)va_arg(args, char *);
3223 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003224 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003226 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003229 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 *s = '\0';
3232 num++;
3233 }
3234 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003235 /*
3236 * Warning: This implementation of the '[' conversion specifier
3237 * deviates from its glibc counterpart in the following ways:
3238 * (1) It does NOT support ranges i.e. '-' is NOT a special
3239 * character
3240 * (2) It cannot match the closing bracket ']' itself
3241 * (3) A field width is required
3242 * (4) '%*[' (discard matching input) is currently not supported
3243 *
3244 * Example usage:
3245 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3246 * buf1, buf2, buf3);
3247 * if (ret < 3)
3248 * // etc..
3249 */
3250 case '[':
3251 {
3252 char *s = (char *)va_arg(args, char *);
3253 DECLARE_BITMAP(set, 256) = {0};
3254 unsigned int len = 0;
3255 bool negate = (*fmt == '^');
3256
3257 /* field width is required */
3258 if (field_width == -1)
3259 return num;
3260
3261 if (negate)
3262 ++fmt;
3263
3264 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3265 set_bit((u8)*fmt, set);
3266
3267 /* no ']' or no character set found */
3268 if (!*fmt || !len)
3269 return num;
3270 ++fmt;
3271
3272 if (negate) {
3273 bitmap_complement(set, set, 256);
3274 /* exclude null '\0' byte */
3275 clear_bit(0, set);
3276 }
3277
3278 /* match must be non-empty */
3279 if (!test_bit((u8)*str, set))
3280 return num;
3281
3282 while (test_bit((u8)*str, set) && field_width--)
3283 *s++ = *str++;
3284 *s = '\0';
3285 ++num;
3286 }
3287 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 case 'o':
3289 base = 8;
3290 break;
3291 case 'x':
3292 case 'X':
3293 base = 16;
3294 break;
3295 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003296 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003297 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003299 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003300 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 case 'u':
3302 break;
3303 case '%':
3304 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003305 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return num;
3307 continue;
3308 default:
3309 /* invalid format; stop here */
3310 return num;
3311 }
3312
3313 /* have some sort of integer conversion.
3314 * first, skip white space in buffer.
3315 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003316 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 digit = *str;
3319 if (is_sign && digit == '-')
3320 digit = *(str + 1);
3321
3322 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003323 || (base == 16 && !isxdigit(digit))
3324 || (base == 10 && !isdigit(digit))
3325 || (base == 8 && (!isdigit(digit) || digit > '7'))
3326 || (base == 0 && !isdigit(digit)))
3327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Jan Beulich53809752012-12-17 16:01:31 -08003329 if (is_sign)
3330 val.s = qualifier != 'L' ?
3331 simple_strtol(str, &next, base) :
3332 simple_strtoll(str, &next, base);
3333 else
3334 val.u = qualifier != 'L' ?
3335 simple_strtoul(str, &next, base) :
3336 simple_strtoull(str, &next, base);
3337
3338 if (field_width > 0 && next - str > field_width) {
3339 if (base == 0)
3340 _parse_integer_fixup_radix(str, &base);
3341 while (next - str > field_width) {
3342 if (is_sign)
3343 val.s = div_s64(val.s, base);
3344 else
3345 val.u = div_u64(val.u, base);
3346 --next;
3347 }
3348 }
3349
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003350 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003352 if (is_sign)
3353 *va_arg(args, signed char *) = val.s;
3354 else
3355 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 break;
3357 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003358 if (is_sign)
3359 *va_arg(args, short *) = val.s;
3360 else
3361 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 break;
3363 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003364 if (is_sign)
3365 *va_arg(args, long *) = val.s;
3366 else
3367 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 break;
3369 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003370 if (is_sign)
3371 *va_arg(args, long long *) = val.s;
3372 else
3373 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003376 *va_arg(args, size_t *) = val.u;
3377 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 default:
Jan Beulich53809752012-12-17 16:01:31 -08003379 if (is_sign)
3380 *va_arg(args, int *) = val.s;
3381 else
3382 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 break;
3384 }
3385 num++;
3386
3387 if (!next)
3388 break;
3389 str = next;
3390 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 return num;
3393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394EXPORT_SYMBOL(vsscanf);
3395
3396/**
3397 * sscanf - Unformat a buffer into a list of arguments
3398 * @buf: input buffer
3399 * @fmt: formatting of buffer
3400 * @...: resulting arguments
3401 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003402int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 va_list args;
3405 int i;
3406
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003407 va_start(args, fmt);
3408 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return i;
3412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413EXPORT_SYMBOL(sscanf);