blob: 5a1bf311b9134fc65263d985360038c46f0c39f5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/lib/vsprintf.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9/*
10 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 */
12
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080013/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
15 * - changed to provide snprintf and vsnprintf functions
16 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
17 * - scnprintf and vscnprintf
18 */
19
20#include <stdarg.h>
Rasmus Villemoesef27ac12019-03-07 16:27:03 -080021#include <linux/build_bug.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070022#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070023#include <linux/clk-provider.h>
Rasmus Villemoes57f56772019-10-15 21:07:05 +020024#include <linux/errname.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050025#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/ctype.h>
29#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070030#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080031#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070032#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110033#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040034#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080035#include <linux/cred.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020036#include <linux/rtc.h>
Andy Shevchenko7daac5b2020-04-15 20:00:44 +030037#include <linux/time.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070038#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020039#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000040#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110041#include <linux/siphash.h>
42#include <linux/compiler.h>
Sakari Ailusa92eb762019-10-03 15:32:16 +030043#include <linux/property.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040044#ifdef CONFIG_BLOCK
45#include <linux/blkdev.h>
46#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070048#include "../mm/internal.h" /* For the trace_print_flags arrays */
49
Tim Schmielau4e57b682005-10-30 15:03:48 -080050#include <asm/page.h> /* for PAGE_SIZE */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070051#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Andy Shevchenko71dca952014-10-13 15:55:18 -070053#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070054#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070055
Christophe Leroy83ed3e22022-02-17 09:49:59 +010056/* Disable pointer hashing if requested */
57bool no_hash_pointers __ro_after_init;
58EXPORT_SYMBOL_GPL(no_hash_pointers);
59
Richard Fitzgerald9e914f52021-05-14 17:12:04 +010060static unsigned long long simple_strntoull(const char *startp, size_t max_chars,
61 char **endp, unsigned int base)
62{
63 const char *cp;
64 unsigned long long result = 0ULL;
65 size_t prefix_chars;
66 unsigned int rv;
67
68 cp = _parse_integer_fixup_radix(startp, &base);
69 prefix_chars = cp - startp;
70 if (prefix_chars < max_chars) {
71 rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
72 /* FIXME */
73 cp += (rv & ~KSTRTOX_OVERFLOW);
74 } else {
75 /* Field too short for prefix + digit, skip over without converting */
76 cp = startp + max_chars;
77 }
78
79 if (endp)
80 *endp = (char *)cp;
81
82 return result;
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * simple_strtoull - convert a string to an unsigned long long
87 * @cp: The start of the string
88 * @endp: A pointer to the end of the parsed string will be placed here
89 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080090 *
Andy Shevchenkoe8cc2b92020-02-21 10:57:23 +020091 * This function has caveats. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
Harvey Harrison22d27052008-10-16 13:40:35 -070093unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Richard Fitzgerald9e914f52021-05-14 17:12:04 +010095 return simple_strntoull(cp, INT_MAX, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097EXPORT_SYMBOL(simple_strtoull);
98
99/**
André Goddard Rosa922ac252009-12-14 18:01:01 -0800100 * simple_strtoul - convert a string to an unsigned long
101 * @cp: The start of the string
102 * @endp: A pointer to the end of the parsed string will be placed here
103 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800104 *
Andy Shevchenkoe8cc2b92020-02-21 10:57:23 +0200105 * This function has caveats. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -0800106 */
107unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
108{
109 return simple_strtoull(cp, endp, base);
110}
111EXPORT_SYMBOL(simple_strtoul);
112
113/**
114 * simple_strtol - convert a string to a signed long
115 * @cp: The start of the string
116 * @endp: A pointer to the end of the parsed string will be placed here
117 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800118 *
Andy Shevchenkoe8cc2b92020-02-21 10:57:23 +0200119 * This function has caveats. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -0800120 */
121long simple_strtol(const char *cp, char **endp, unsigned int base)
122{
123 if (*cp == '-')
124 return -simple_strtoul(cp + 1, endp, base);
125
126 return simple_strtoul(cp, endp, base);
127}
128EXPORT_SYMBOL(simple_strtol);
129
Richard Fitzgerald9e914f52021-05-14 17:12:04 +0100130static long long simple_strntoll(const char *cp, size_t max_chars, char **endp,
131 unsigned int base)
132{
133 /*
134 * simple_strntoull() safely handles receiving max_chars==0 in the
135 * case cp[0] == '-' && max_chars == 1.
136 * If max_chars == 0 we can drop through and pass it to simple_strntoull()
137 * and the content of *cp is irrelevant.
138 */
139 if (*cp == '-' && max_chars > 0)
140 return -simple_strntoull(cp + 1, max_chars - 1, endp, base);
141
142 return simple_strntoull(cp, max_chars, endp, base);
143}
144
André Goddard Rosa922ac252009-12-14 18:01:01 -0800145/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * simple_strtoll - convert a string to a signed long long
147 * @cp: The start of the string
148 * @endp: A pointer to the end of the parsed string will be placed here
149 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800150 *
Andy Shevchenkoe8cc2b92020-02-21 10:57:23 +0200151 * This function has caveats. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700153long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Richard Fitzgerald9e914f52021-05-14 17:12:04 +0100155 return simple_strntoll(cp, INT_MAX, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400157EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Joe Perchescf3b4292010-05-24 14:33:16 -0700159static noinline_for_stack
160int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800162 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800164 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800166 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return i;
169}
170
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700171/*
172 * Decimal conversion is by far the most typical, and is used for
173 * /proc and /sys data. This directly impacts e.g. top performance
174 * with many processes running. We optimize it for speed by emitting
175 * two characters at a time, using a 200 byte lookup table. This
176 * roughly halves the number of multiplications compared to computing
177 * the digits one at a time. Implementation strongly inspired by the
178 * previous version, which in turn used ideas described at
179 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
180 * from the author, Douglas W. Jones).
181 *
182 * It turns out there is precisely one 26 bit fixed-point
183 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
184 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
185 * range happens to be somewhat larger (x <= 1073741898), but that's
186 * irrelevant for our purpose.
187 *
188 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
189 * need a 32x32->64 bit multiply, so we simply use the same constant.
190 *
191 * For dividing a number in the range [100, 10^4-1] by 100, there are
192 * several options. The simplest is (x * 0x147b) >> 19, which is valid
193 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700194 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700195
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700196static const u16 decpair[100] = {
197#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
198 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
199 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
200 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
201 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
202 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
203 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
204 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
205 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
206 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
207 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
208#undef _
209};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700210
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700211/*
212 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700213 * immediately jump to out_r where two 0s would be written but only
214 * one of them accounted for in buf. This is needed by ip4_string
215 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700216*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700217static noinline_for_stack
218char *put_dec_trunc8(char *buf, unsigned r)
219{
220 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700221
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700222 /* 1 <= r < 10^8 */
223 if (r < 100)
224 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700225
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700226 /* 100 <= r < 10^8 */
227 q = (r * (u64)0x28f5c29) >> 32;
228 *((u16 *)buf) = decpair[r - 100*q];
229 buf += 2;
230
231 /* 1 <= q < 10^6 */
232 if (q < 100)
233 goto out_q;
234
235 /* 100 <= q < 10^6 */
236 r = (q * (u64)0x28f5c29) >> 32;
237 *((u16 *)buf) = decpair[q - 100*r];
238 buf += 2;
239
240 /* 1 <= r < 10^4 */
241 if (r < 100)
242 goto out_r;
243
244 /* 100 <= r < 10^4 */
245 q = (r * 0x147b) >> 19;
246 *((u16 *)buf) = decpair[r - 100*q];
247 buf += 2;
248out_q:
249 /* 1 <= q < 100 */
250 r = q;
251out_r:
252 /* 1 <= r < 100 */
253 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700254 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700255 return buf;
256}
257
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700258#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
259static noinline_for_stack
260char *put_dec_full8(char *buf, unsigned r)
261{
262 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700263
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700264 /* 0 <= r < 10^8 */
265 q = (r * (u64)0x28f5c29) >> 32;
266 *((u16 *)buf) = decpair[r - 100*q];
267 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700268
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700269 /* 0 <= q < 10^6 */
270 r = (q * (u64)0x28f5c29) >> 32;
271 *((u16 *)buf) = decpair[q - 100*r];
272 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700273
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700274 /* 0 <= r < 10^4 */
275 q = (r * 0x147b) >> 19;
276 *((u16 *)buf) = decpair[r - 100*q];
277 buf += 2;
278
279 /* 0 <= q < 100 */
280 *((u16 *)buf) = decpair[q];
281 buf += 2;
282 return buf;
283}
284
285static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700286char *put_dec(char *buf, unsigned long long n)
287{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700288 if (n >= 100*1000*1000)
289 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
290 /* 1 <= n <= 1.6e11 */
291 if (n >= 100*1000*1000)
292 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
293 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700294 return put_dec_trunc8(buf, n);
295}
296
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700297#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700298
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700299static void
300put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700301{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700302 unsigned q;
303
304 /* 0 <= r < 10^4 */
305 q = (r * 0x147b) >> 19;
306 *((u16 *)buf) = decpair[r - 100*q];
307 buf += 2;
308 /* 0 <= q < 100 */
309 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700310}
311
312/*
313 * Call put_dec_full4 on x % 10000, return x / 10000.
314 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
315 * holds for all x < 1,128,869,999. The largest value this
316 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700317 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700318 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700319static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700320unsigned put_dec_helper4(char *buf, unsigned x)
321{
322 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
323
324 put_dec_full4(buf, x - q * 10000);
325 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700326}
327
328/* Based on code by Douglas W. Jones found at
329 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
330 * (with permission from the author).
331 * Performs no 64-bit division and hence should be fast on 32-bit machines.
332 */
333static
334char *put_dec(char *buf, unsigned long long n)
335{
336 uint32_t d3, d2, d1, q, h;
337
338 if (n < 100*1000*1000)
339 return put_dec_trunc8(buf, n);
340
341 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
342 h = (n >> 32);
343 d2 = (h ) & 0xffff;
344 d3 = (h >> 16); /* implicit "& 0xffff" */
345
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700346 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
347 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700348 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700349 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700350
George Spelvin23591722012-10-04 17:12:29 -0700351 q += 7671 * d3 + 9496 * d2 + 6 * d1;
352 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700353
George Spelvin23591722012-10-04 17:12:29 -0700354 q += 4749 * d3 + 42 * d2;
355 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700356
George Spelvin23591722012-10-04 17:12:29 -0700357 q += 281 * d3;
358 buf += 12;
359 if (q)
360 buf = put_dec_trunc8(buf, q);
361 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700362 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800363
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700364 return buf;
365}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700366
367#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700368
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700369/*
370 * Convert passed number to decimal string.
371 * Returns the length of string. On buffer overflow, returns 0.
372 *
373 * If speed is not important, use snprintf(). It's easy to read the code.
374 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700375int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700376{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700377 /* put_dec requires 2-byte alignment of the buffer. */
378 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700379 int idx, len;
380
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700381 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
382 if (num <= 9) {
383 tmp[0] = '0' + num;
384 len = 1;
385 } else {
386 len = put_dec(tmp, num) - tmp;
387 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700388
Andrei Vagind1be35c2018-04-10 16:31:16 -0700389 if (len > size || width > size)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700390 return 0;
Andrei Vagind1be35c2018-04-10 16:31:16 -0700391
392 if (width > len) {
393 width = width - len;
394 for (idx = 0; idx < width; idx++)
395 buf[idx] = ' ';
396 } else {
397 width = 0;
398 }
399
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700400 for (idx = 0; idx < len; ++idx)
Andrei Vagind1be35c2018-04-10 16:31:16 -0700401 buf[idx + width] = tmp[len - idx - 1];
402
403 return len + width;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700404}
405
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700406#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700407#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#define PLUS 4 /* show plus */
409#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700410#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700411#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
412#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Andy Shevchenkob8866902020-07-31 21:08:22 +0300414static_assert(ZEROPAD == ('0' - ' '));
415static_assert(SMALL == ' ');
416
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100417enum format_type {
418 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100419 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100420 FORMAT_TYPE_PRECISION,
421 FORMAT_TYPE_CHAR,
422 FORMAT_TYPE_STR,
423 FORMAT_TYPE_PTR,
424 FORMAT_TYPE_PERCENT_CHAR,
425 FORMAT_TYPE_INVALID,
426 FORMAT_TYPE_LONG_LONG,
427 FORMAT_TYPE_ULONG,
428 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800429 FORMAT_TYPE_UBYTE,
430 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100431 FORMAT_TYPE_USHORT,
432 FORMAT_TYPE_SHORT,
433 FORMAT_TYPE_UINT,
434 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100435 FORMAT_TYPE_SIZE_T,
436 FORMAT_TYPE_PTRDIFF
437};
438
439struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800440 unsigned int type:8; /* format_type enum */
441 signed int field_width:24; /* width of output field */
442 unsigned int flags:8; /* flags to number() */
443 unsigned int base:8; /* number base, 8, 10 or 16 only */
444 signed int precision:16; /* # of digits/chars */
445} __packed;
Rasmus Villemoesef27ac12019-03-07 16:27:03 -0800446static_assert(sizeof(struct printf_spec) == 8);
447
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800448#define FIELD_WIDTH_MAX ((1 << 23) - 1)
449#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100450
Joe Perchescf3b4292010-05-24 14:33:16 -0700451static noinline_for_stack
452char *number(char *buf, char *end, unsigned long long num,
453 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700455 /* put_dec requires 2-byte alignment of the buffer. */
456 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100457 char sign;
458 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100459 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700461 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800462 int field_width = spec.field_width;
463 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100465 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
466 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100467 locase = (spec.flags & SMALL);
468 if (spec.flags & LEFT)
469 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100471 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800472 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800474 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800475 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100476 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800478 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100479 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800481 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100485 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800486 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700487 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800488 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490
491 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700493 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700494 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100495 else if (spec.base != 10) { /* 8 or 16 */
496 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700497 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800498
499 if (spec.base == 16)
500 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700501 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700502 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700503 num >>= shift;
504 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700505 } else { /* base 10 */
506 i = put_dec(tmp, num) - tmp;
507 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700508
509 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800510 if (i > precision)
511 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700512 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800513 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700514 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800515 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700516 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *buf = ' ';
518 ++buf;
519 }
520 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700521 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700523 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *buf = sign;
525 ++buf;
526 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700527 /* "0x" / "0" prefix */
528 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700529 if (spec.base == 16 || !is_zero) {
530 if (buf < end)
531 *buf = '0';
532 ++buf;
533 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100534 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700535 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100536 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ++buf;
538 }
539 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700540 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100541 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700542 char c = ' ' + (spec.flags & ZEROPAD);
Andy Shevchenkob8866902020-07-31 21:08:22 +0300543
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800544 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700545 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *buf = c;
547 ++buf;
548 }
549 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700550 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800551 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700552 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *buf = '0';
554 ++buf;
555 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700556 /* actual digits of result */
557 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700558 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 *buf = tmp[i];
560 ++buf;
561 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700562 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800563 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700564 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 *buf = ' ';
566 ++buf;
567 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return buf;
570}
571
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800572static noinline_for_stack
573char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
574{
575 struct printf_spec spec;
576
577 spec.type = FORMAT_TYPE_PTR;
578 spec.field_width = 2 + 2 * size; /* 0x + hex */
579 spec.flags = SPECIAL | SMALL | ZEROPAD;
580 spec.base = 16;
581 spec.precision = -1;
582
583 return number(buf, end, num, spec);
584}
585
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800586static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400587{
588 size_t size;
589 if (buf >= end) /* nowhere to put anything */
590 return;
591 size = end - buf;
592 if (size <= spaces) {
593 memset(buf, ' ', size);
594 return;
595 }
596 if (len) {
597 if (len > size - spaces)
598 len = size - spaces;
599 memmove(buf + spaces, buf, len);
600 }
601 memset(buf, ' ', spaces);
602}
603
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800604/*
605 * Handle field width padding for a string.
606 * @buf: current buffer position
607 * @n: length of string
608 * @end: end of output buffer
609 * @spec: for field width and flags
610 * Returns: new buffer position after padding.
611 */
612static noinline_for_stack
613char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
614{
615 unsigned spaces;
616
617 if (likely(n >= spec.field_width))
618 return buf;
619 /* we want to pad the sucker */
620 spaces = spec.field_width - n;
621 if (!(spec.flags & LEFT)) {
622 move_right(buf - n, end, n, spaces);
623 return buf + spaces;
624 }
625 while (spaces--) {
626 if (buf < end)
627 *buf = ' ';
628 ++buf;
629 }
630 return buf;
631}
632
Petr Mladekd529ac42019-04-17 13:53:43 +0200633/* Handle string from a well known address. */
634static char *string_nocheck(char *buf, char *end, const char *s,
635 struct printf_spec spec)
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800636{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800637 int len = 0;
Youngmin Namb314dd42019-06-10 16:47:07 +0900638 int lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800639
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800640 while (lim--) {
641 char c = *s++;
642 if (!c)
643 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800644 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800645 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800646 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800647 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800648 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800649 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800650}
651
Rasmus Villemoes57f56772019-10-15 21:07:05 +0200652static char *err_ptr(char *buf, char *end, void *ptr,
653 struct printf_spec spec)
654{
655 int err = PTR_ERR(ptr);
656 const char *sym = errname(err);
657
658 if (sym)
659 return string_nocheck(buf, end, sym, spec);
660
661 /*
662 * Somebody passed ERR_PTR(-1234) or some other non-existing
663 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
664 * printing it as its decimal representation.
665 */
666 spec.flags |= SIGN;
667 spec.base = 10;
668 return number(buf, end, err, spec);
669}
670
Petr Mladekc8c3b582019-04-17 13:53:50 +0200671/* Be careful: error messages must fit into the given buffer. */
672static char *error_string(char *buf, char *end, const char *s,
673 struct printf_spec spec)
674{
675 /*
676 * Hard limit to avoid a completely insane messages. It actually
677 * works pretty well because most error messages are in
678 * the many pointer format modifiers.
679 */
680 if (spec.precision == -1)
681 spec.precision = 2 * sizeof(void *);
682
683 return string_nocheck(buf, end, s, spec);
684}
685
Petr Mladek3e5903e2019-04-17 13:53:48 +0200686/*
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200687 * Do not call any complex external code here. Nested printk()/vsprintf()
688 * might cause infinite loops. Failures might break printk() and would
689 * be hard to debug.
Petr Mladek3e5903e2019-04-17 13:53:48 +0200690 */
691static const char *check_pointer_msg(const void *ptr)
692{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200693 if (!ptr)
694 return "(null)";
695
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200696 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
Petr Mladek3e5903e2019-04-17 13:53:48 +0200697 return "(efault)";
698
699 return NULL;
700}
701
702static int check_pointer(char **buf, char *end, const void *ptr,
703 struct printf_spec spec)
704{
705 const char *err_msg;
706
707 err_msg = check_pointer_msg(ptr);
708 if (err_msg) {
Petr Mladekc8c3b582019-04-17 13:53:50 +0200709 *buf = error_string(*buf, end, err_msg, spec);
Petr Mladek3e5903e2019-04-17 13:53:48 +0200710 return -EFAULT;
711 }
712
713 return 0;
714}
715
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800716static noinline_for_stack
Petr Mladekd529ac42019-04-17 13:53:43 +0200717char *string(char *buf, char *end, const char *s,
718 struct printf_spec spec)
719{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200720 if (check_pointer(&buf, end, s, spec))
721 return buf;
Petr Mladekd529ac42019-04-17 13:53:43 +0200722
723 return string_nocheck(buf, end, s, spec);
724}
725
YueHaibingce9d3ec2019-04-27 00:46:30 +0800726static char *pointer_string(char *buf, char *end,
727 const void *ptr,
728 struct printf_spec spec)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200729{
730 spec.base = 16;
731 spec.flags |= SMALL;
732 if (spec.field_width == -1) {
733 spec.field_width = 2 * sizeof(ptr);
734 spec.flags |= ZEROPAD;
735 }
736
737 return number(buf, end, (unsigned long int)ptr, spec);
738}
739
740/* Make pointers available for printing early in the boot sequence. */
741static int debug_boot_weak_hash __ro_after_init;
742
743static int __init debug_boot_weak_hash_enable(char *str)
744{
745 debug_boot_weak_hash = 1;
746 pr_info("debug_boot_weak_hash enabled\n");
747 return 0;
748}
749early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
750
751static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
752static siphash_key_t ptr_key __read_mostly;
753
754static void enable_ptr_key_workfn(struct work_struct *work)
755{
756 get_random_bytes(&ptr_key, sizeof(ptr_key));
757 /* Needs to run from preemptible context */
758 static_branch_disable(&not_filled_random_ptr_key);
759}
760
761static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
762
Jason A. Donenfeld849e7b72022-03-01 20:03:49 +0100763static int fill_random_ptr_key(struct notifier_block *nb,
764 unsigned long action, void *data)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200765{
766 /* This may be in an interrupt handler. */
767 queue_work(system_unbound_wq, &enable_ptr_key_work);
Jason A. Donenfeld849e7b72022-03-01 20:03:49 +0100768 return 0;
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200769}
770
Jason A. Donenfeld849e7b72022-03-01 20:03:49 +0100771static struct notifier_block random_ready = {
772 .notifier_call = fill_random_ptr_key
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200773};
774
775static int __init initialize_ptr_random(void)
776{
777 int key_size = sizeof(ptr_key);
778 int ret;
779
780 /* Use hw RNG if available. */
781 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
782 static_branch_disable(&not_filled_random_ptr_key);
783 return 0;
784 }
785
Jason A. Donenfeld849e7b72022-03-01 20:03:49 +0100786 ret = register_random_ready_notifier(&random_ready);
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200787 if (!ret) {
788 return 0;
789 } else if (ret == -EALREADY) {
790 /* This is in preemptible context */
791 enable_ptr_key_workfn(&enable_ptr_key_work);
792 return 0;
793 }
794
795 return ret;
796}
797early_initcall(initialize_ptr_random);
798
799/* Maps a pointer to a 32 bit unique identifier. */
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800800static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200801{
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200802 unsigned long hashval;
803
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800804 if (static_branch_unlikely(&not_filled_random_ptr_key))
805 return -EAGAIN;
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200806
807#ifdef CONFIG_64BIT
808 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
809 /*
810 * Mask off the first 32 bits, this makes explicit that we have
811 * modified the address (and 32 bits is plenty for a unique ID).
812 */
813 hashval = hashval & 0xffffffff;
814#else
815 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
816#endif
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800817 *hashval_out = hashval;
818 return 0;
819}
820
821int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
822{
823 return __ptr_to_hashval(ptr, hashval_out);
824}
825
826static char *ptr_to_id(char *buf, char *end, const void *ptr,
827 struct printf_spec spec)
828{
829 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
830 unsigned long hashval;
831 int ret;
832
Ilya Dryomov7bd57fb2020-05-19 13:26:57 +0200833 /*
834 * Print the real pointer value for NULL and error pointers,
835 * as they are not actual addresses.
836 */
837 if (IS_ERR_OR_NULL(ptr))
838 return pointer_string(buf, end, ptr, spec);
839
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800840 /* When debugging early boot use non-cryptographically secure hash. */
841 if (unlikely(debug_boot_weak_hash)) {
842 hashval = hash_long((unsigned long)ptr, 32);
843 return pointer_string(buf, end, (const void *)hashval, spec);
844 }
845
846 ret = __ptr_to_hashval(ptr, &hashval);
847 if (ret) {
848 spec.field_width = 2 * sizeof(ptr);
849 /* string length must be less than default_width */
850 return error_string(buf, end, str, spec);
851 }
852
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200853 return pointer_string(buf, end, (const void *)hashval, spec);
854}
855
Christophe Leroy83ed3e22022-02-17 09:49:59 +0100856static char *default_pointer(char *buf, char *end, const void *ptr,
857 struct printf_spec spec)
858{
859 /*
860 * default is to _not_ leak addresses, so hash before printing,
861 * unless no_hash_pointers is specified on the command line.
862 */
863 if (unlikely(no_hash_pointers))
864 return pointer_string(buf, end, ptr, spec);
865
866 return ptr_to_id(buf, end, ptr, spec);
867}
868
Petr Mladek6eea2422019-04-17 13:53:41 +0200869int kptr_restrict __read_mostly;
870
871static noinline_for_stack
872char *restricted_pointer(char *buf, char *end, const void *ptr,
873 struct printf_spec spec)
874{
875 switch (kptr_restrict) {
876 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200877 /* Handle as %p, hash and do _not_ leak addresses. */
Christophe Leroy83ed3e22022-02-17 09:49:59 +0100878 return default_pointer(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200879 case 1: {
880 const struct cred *cred;
881
882 /*
883 * kptr_restrict==1 cannot be used in IRQ context
884 * because its test for CAP_SYSLOG would be meaningless.
885 */
886 if (in_irq() || in_serving_softirq() || in_nmi()) {
887 if (spec.field_width == -1)
888 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200889 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200890 }
891
892 /*
893 * Only print the real pointer value if the current
894 * process has CAP_SYSLOG and is running with the
895 * same credentials it started with. This is because
896 * access to files is checked at open() time, but %pK
897 * checks permission at read() time. We don't want to
898 * leak pointer values if a binary opens a file using
899 * %pK and then elevates privileges before reading it.
900 */
901 cred = current_cred();
902 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
903 !uid_eq(cred->euid, cred->uid) ||
904 !gid_eq(cred->egid, cred->gid))
905 ptr = NULL;
906 break;
907 }
908 case 2:
909 default:
910 /* Always print 0's for %pK */
911 ptr = NULL;
912 break;
913 }
914
915 return pointer_string(buf, end, ptr, spec);
916}
917
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200918static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400919char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
920 const char *fmt)
921{
922 const char *array[4], *s;
923 const struct dentry *p;
924 int depth;
925 int i, n;
926
927 switch (fmt[1]) {
928 case '2': case '3': case '4':
929 depth = fmt[1] - '0';
930 break;
931 default:
932 depth = 1;
933 }
934
935 rcu_read_lock();
936 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200937 if (check_pointer(&buf, end, d, spec)) {
938 rcu_read_unlock();
939 return buf;
940 }
941
Mark Rutland6aa7de02017-10-23 14:07:29 -0700942 p = READ_ONCE(d->d_parent);
943 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400944 if (p == d) {
945 if (i)
946 array[i] = "";
947 i++;
948 break;
949 }
950 }
951 s = array[--i];
952 for (n = 0; n != spec.precision; n++, buf++) {
953 char c = *s++;
954 if (!c) {
955 if (!i)
956 break;
957 c = '/';
958 s = array[--i];
959 }
960 if (buf < end)
961 *buf = c;
962 }
963 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800964 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400965}
966
Jia He36594b32019-08-09 09:24:56 +0800967static noinline_for_stack
968char *file_dentry_name(char *buf, char *end, const struct file *f,
969 struct printf_spec spec, const char *fmt)
970{
971 if (check_pointer(&buf, end, f, spec))
972 return buf;
973
974 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
975}
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400976#ifdef CONFIG_BLOCK
977static noinline_for_stack
978char *bdev_name(char *buf, char *end, struct block_device *bdev,
979 struct printf_spec spec, const char *fmt)
980{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200981 struct gendisk *hd;
982
983 if (check_pointer(&buf, end, bdev, spec))
984 return buf;
985
986 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400987 buf = string(buf, end, hd->disk_name, spec);
Christoph Hellwig700cd592020-09-03 07:41:04 +0200988 if (bdev->bd_partno) {
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400989 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
990 if (buf < end)
991 *buf = 'p';
992 buf++;
993 }
Christoph Hellwig700cd592020-09-03 07:41:04 +0200994 buf = number(buf, end, bdev->bd_partno, spec);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400995 }
996 return buf;
997}
998#endif
999
Joe Perchescf3b4292010-05-24 14:33:16 -07001000static noinline_for_stack
1001char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -08001002 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001003{
Joe Perchesb0d33c22012-12-12 10:18:50 -08001004 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001005#ifdef CONFIG_KALLSYMS
1006 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -08001007#endif
1008
1009 if (fmt[1] == 'R')
1010 ptr = __builtin_extract_return_addr(ptr);
1011 value = (unsigned long)ptr;
1012
1013#ifdef CONFIG_KALLSYMS
1014 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001015 sprint_backtrace(sym, value);
Sakari Ailus9af77062019-10-03 15:32:14 +03001016 else if (*fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001017 sprint_symbol(sym, value);
1018 else
Stephen Boyd4796dd22012-05-29 15:07:33 -07001019 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001020
Petr Mladekd529ac42019-04-17 13:53:43 +02001021 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001022#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001023 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001024#endif
1025}
1026
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001027static const struct printf_spec default_str_spec = {
1028 .field_width = -1,
1029 .precision = -1,
1030};
1031
Andy Shevchenko54433972018-02-16 23:07:06 +02001032static const struct printf_spec default_flag_spec = {
1033 .base = 16,
1034 .precision = -1,
1035 .flags = SPECIAL | SMALL,
1036};
1037
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001038static const struct printf_spec default_dec_spec = {
1039 .base = 10,
1040 .precision = -1,
1041};
1042
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001043static const struct printf_spec default_dec02_spec = {
1044 .base = 10,
1045 .field_width = 2,
1046 .precision = -1,
1047 .flags = ZEROPAD,
1048};
1049
1050static const struct printf_spec default_dec04_spec = {
1051 .base = 10,
1052 .field_width = 4,
1053 .precision = -1,
1054 .flags = ZEROPAD,
1055};
1056
Joe Perchescf3b4292010-05-24 14:33:16 -07001057static noinline_for_stack
1058char *resource_string(char *buf, char *end, struct resource *res,
1059 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +11001060{
1061#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -06001062#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +11001063#endif
1064
1065#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -06001066#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +11001067#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001068 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001069 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001070 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001071 .precision = -1,
1072 .flags = SPECIAL | SMALL | ZEROPAD,
1073 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001074 static const struct printf_spec mem_spec = {
1075 .base = 16,
1076 .field_width = MEM_RSRC_PRINTK_SIZE,
1077 .precision = -1,
1078 .flags = SPECIAL | SMALL | ZEROPAD,
1079 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001080 static const struct printf_spec bus_spec = {
1081 .base = 16,
1082 .field_width = 2,
1083 .precision = -1,
1084 .flags = SMALL | ZEROPAD,
1085 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001086 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001087 .field_width = -1,
1088 .precision = 10,
1089 .flags = LEFT,
1090 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001091
1092 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1093 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1094#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1095#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001096#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001097#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1098 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1099 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1100
Linus Torvalds332d2e72008-10-20 15:07:34 +11001101 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001102 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001103 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +11001104
Petr Mladek3e5903e2019-04-17 13:53:48 +02001105 if (check_pointer(&buf, end, res, spec))
1106 return buf;
1107
Linus Torvalds332d2e72008-10-20 15:07:34 +11001108 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001109 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001110 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001111 specp = &io_spec;
1112 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001113 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001114 specp = &mem_spec;
1115 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001116 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001117 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001118 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001119 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001120 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001121 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001122 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001123 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001124 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001125 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001126 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001127 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001128 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001129 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001130 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001131 p = number(p, pend, resource_size(res), *specp);
1132 } else {
1133 p = number(p, pend, res->start, *specp);
1134 if (res->start != res->end) {
1135 *p++ = '-';
1136 p = number(p, pend, res->end, *specp);
1137 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001138 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001139 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001140 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001141 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001142 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001143 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001144 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001145 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001146 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001147 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001148 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001149 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001150 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001151 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001152 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001153 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001154
Petr Mladekd529ac42019-04-17 13:53:43 +02001155 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001156}
1157
Joe Perchescf3b4292010-05-24 14:33:16 -07001158static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001159char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1160 const char *fmt)
1161{
Steven Rostedt360603a2013-05-28 15:47:39 -04001162 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001163 negative value, fallback to the default */
1164 char separator;
1165
1166 if (spec.field_width == 0)
1167 /* nothing to print */
1168 return buf;
1169
Petr Mladek3e5903e2019-04-17 13:53:48 +02001170 if (check_pointer(&buf, end, addr, spec))
1171 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001172
1173 switch (fmt[1]) {
1174 case 'C':
1175 separator = ':';
1176 break;
1177 case 'D':
1178 separator = '-';
1179 break;
1180 case 'N':
1181 separator = 0;
1182 break;
1183 default:
1184 separator = ' ';
1185 break;
1186 }
1187
1188 if (spec.field_width > 0)
1189 len = min_t(int, spec.field_width, 64);
1190
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001191 for (i = 0; i < len; ++i) {
1192 if (buf < end)
1193 *buf = hex_asc_hi(addr[i]);
1194 ++buf;
1195 if (buf < end)
1196 *buf = hex_asc_lo(addr[i]);
1197 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001198
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001199 if (separator && i != len - 1) {
1200 if (buf < end)
1201 *buf = separator;
1202 ++buf;
1203 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001204 }
1205
1206 return buf;
1207}
1208
1209static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001210char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1211 struct printf_spec spec, const char *fmt)
1212{
1213 const int CHUNKSZ = 32;
1214 int nr_bits = max_t(int, spec.field_width, 0);
1215 int i, chunksz;
1216 bool first = true;
1217
Petr Mladek3e5903e2019-04-17 13:53:48 +02001218 if (check_pointer(&buf, end, bitmap, spec))
1219 return buf;
1220
Tejun Heodbc760b2015-02-13 14:36:53 -08001221 /* reused to print numbers */
1222 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1223
1224 chunksz = nr_bits & (CHUNKSZ - 1);
1225 if (chunksz == 0)
1226 chunksz = CHUNKSZ;
1227
1228 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1229 for (; i >= 0; i -= CHUNKSZ) {
1230 u32 chunkmask, val;
1231 int word, bit;
1232
1233 chunkmask = ((1ULL << chunksz) - 1);
1234 word = i / BITS_PER_LONG;
1235 bit = i % BITS_PER_LONG;
1236 val = (bitmap[word] >> bit) & chunkmask;
1237
1238 if (!first) {
1239 if (buf < end)
1240 *buf = ',';
1241 buf++;
1242 }
1243 first = false;
1244
1245 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1246 buf = number(buf, end, val, spec);
1247
1248 chunksz = CHUNKSZ;
1249 }
1250 return buf;
1251}
1252
1253static noinline_for_stack
1254char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1255 struct printf_spec spec, const char *fmt)
1256{
1257 int nr_bits = max_t(int, spec.field_width, 0);
1258 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1259 int cur, rbot, rtop;
1260 bool first = true;
1261
Petr Mladek3e5903e2019-04-17 13:53:48 +02001262 if (check_pointer(&buf, end, bitmap, spec))
1263 return buf;
1264
Tejun Heodbc760b2015-02-13 14:36:53 -08001265 rbot = cur = find_first_bit(bitmap, nr_bits);
1266 while (cur < nr_bits) {
1267 rtop = cur;
1268 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1269 if (cur < nr_bits && cur <= rtop + 1)
1270 continue;
1271
1272 if (!first) {
1273 if (buf < end)
1274 *buf = ',';
1275 buf++;
1276 }
1277 first = false;
1278
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001279 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001280 if (rbot < rtop) {
1281 if (buf < end)
1282 *buf = '-';
1283 buf++;
1284
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001285 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001286 }
1287
1288 rbot = cur;
1289 }
1290 return buf;
1291}
1292
1293static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001294char *mac_address_string(char *buf, char *end, u8 *addr,
1295 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001296{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001297 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001298 char *p = mac_addr;
1299 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001300 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001301 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001302
Petr Mladek3e5903e2019-04-17 13:53:48 +02001303 if (check_pointer(&buf, end, addr, spec))
1304 return buf;
1305
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001306 switch (fmt[1]) {
1307 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001308 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001309 break;
1310
1311 case 'R':
1312 reversed = true;
Gustavo A. R. Silva6a9dc5f2020-08-24 15:36:14 -05001313 /* fall through */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001314
1315 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001316 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001317 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001318 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001319
1320 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001321 if (reversed)
1322 p = hex_byte_pack(p, addr[5 - i]);
1323 else
1324 p = hex_byte_pack(p, addr[i]);
1325
Joe Perches8a27f7c2009-08-17 12:29:44 +00001326 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001327 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001328 }
1329 *p = '\0';
1330
Petr Mladekd529ac42019-04-17 13:53:43 +02001331 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001332}
1333
Joe Perchescf3b4292010-05-24 14:33:16 -07001334static noinline_for_stack
1335char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001336{
Harvey Harrison689afa72008-10-28 16:04:44 -07001337 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001338 bool leading_zeros = (fmt[0] == 'i');
1339 int index;
1340 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001341
Joe Perches0159f242010-01-13 20:23:30 -08001342 switch (fmt[2]) {
1343 case 'h':
1344#ifdef __BIG_ENDIAN
1345 index = 0;
1346 step = 1;
1347#else
1348 index = 3;
1349 step = -1;
1350#endif
1351 break;
1352 case 'l':
1353 index = 3;
1354 step = -1;
1355 break;
1356 case 'n':
1357 case 'b':
1358 default:
1359 index = 0;
1360 step = 1;
1361 break;
1362 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001363 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001364 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001365 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001366 if (leading_zeros) {
1367 if (digits < 3)
1368 *p++ = '0';
1369 if (digits < 2)
1370 *p++ = '0';
1371 }
1372 /* reverse the digits in the quad */
1373 while (digits--)
1374 *p++ = temp[digits];
1375 if (i < 3)
1376 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001377 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001378 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001379 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001380
Joe Perches8a27f7c2009-08-17 12:29:44 +00001381 return p;
1382}
1383
Joe Perchescf3b4292010-05-24 14:33:16 -07001384static noinline_for_stack
1385char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001386{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001387 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001388 unsigned char zerolength[8];
1389 int longest = 1;
1390 int colonpos = -1;
1391 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001392 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001393 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001394 bool useIPv4;
1395 struct in6_addr in6;
1396
1397 memcpy(&in6, addr, sizeof(struct in6_addr));
1398
1399 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001400
1401 memset(zerolength, 0, sizeof(zerolength));
1402
1403 if (useIPv4)
1404 range = 6;
1405 else
1406 range = 8;
1407
1408 /* find position of longest 0 run */
1409 for (i = 0; i < range; i++) {
1410 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001411 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001412 break;
1413 zerolength[i]++;
1414 }
1415 }
1416 for (i = 0; i < range; i++) {
1417 if (zerolength[i] > longest) {
1418 longest = zerolength[i];
1419 colonpos = i;
1420 }
1421 }
Joe Perches29cf5192011-06-09 11:23:37 -07001422 if (longest == 1) /* don't compress a single 0 */
1423 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001424
1425 /* emit address */
1426 for (i = 0; i < range; i++) {
1427 if (i == colonpos) {
1428 if (needcolon || i == 0)
1429 *p++ = ':';
1430 *p++ = ':';
1431 needcolon = false;
1432 i += longest - 1;
1433 continue;
1434 }
1435 if (needcolon) {
1436 *p++ = ':';
1437 needcolon = false;
1438 }
1439 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001440 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001441 hi = word >> 8;
1442 lo = word & 0xff;
1443 if (hi) {
1444 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001445 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001446 else
1447 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001448 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001449 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001450 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001451 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001452 else
1453 *p++ = hex_asc_lo(lo);
1454 needcolon = true;
1455 }
1456
1457 if (useIPv4) {
1458 if (needcolon)
1459 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001460 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001461 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001462 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001463
Joe Perches8a27f7c2009-08-17 12:29:44 +00001464 return p;
1465}
1466
Joe Perchescf3b4292010-05-24 14:33:16 -07001467static noinline_for_stack
1468char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001469{
1470 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001471
Harvey Harrison689afa72008-10-28 16:04:44 -07001472 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001473 p = hex_byte_pack(p, *addr++);
1474 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001475 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001476 *p++ = ':';
1477 }
1478 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001479
Joe Perches8a27f7c2009-08-17 12:29:44 +00001480 return p;
1481}
1482
Joe Perchescf3b4292010-05-24 14:33:16 -07001483static noinline_for_stack
1484char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1485 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001486{
1487 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1488
1489 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001490 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001491 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001492 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001493
Petr Mladekd529ac42019-04-17 13:53:43 +02001494 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001495}
1496
Joe Perchescf3b4292010-05-24 14:33:16 -07001497static noinline_for_stack
1498char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1499 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001500{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001501 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001502
Joe Perches0159f242010-01-13 20:23:30 -08001503 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001504
Petr Mladekd529ac42019-04-17 13:53:43 +02001505 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001506}
1507
Joe Perchescf3b4292010-05-24 14:33:16 -07001508static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001509char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1510 struct printf_spec spec, const char *fmt)
1511{
1512 bool have_p = false, have_s = false, have_f = false, have_c = false;
1513 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1514 sizeof(":12345") + sizeof("/123456789") +
1515 sizeof("%1234567890")];
1516 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1517 const u8 *addr = (const u8 *) &sa->sin6_addr;
1518 char fmt6[2] = { fmt[0], '6' };
1519 u8 off = 0;
1520
1521 fmt++;
1522 while (isalpha(*++fmt)) {
1523 switch (*fmt) {
1524 case 'p':
1525 have_p = true;
1526 break;
1527 case 'f':
1528 have_f = true;
1529 break;
1530 case 's':
1531 have_s = true;
1532 break;
1533 case 'c':
1534 have_c = true;
1535 break;
1536 }
1537 }
1538
1539 if (have_p || have_s || have_f) {
1540 *p = '[';
1541 off = 1;
1542 }
1543
1544 if (fmt6[0] == 'I' && have_c)
1545 p = ip6_compressed_string(ip6_addr + off, addr);
1546 else
1547 p = ip6_string(ip6_addr + off, addr, fmt6);
1548
1549 if (have_p || have_s || have_f)
1550 *p++ = ']';
1551
1552 if (have_p) {
1553 *p++ = ':';
1554 p = number(p, pend, ntohs(sa->sin6_port), spec);
1555 }
1556 if (have_f) {
1557 *p++ = '/';
1558 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1559 IPV6_FLOWINFO_MASK), spec);
1560 }
1561 if (have_s) {
1562 *p++ = '%';
1563 p = number(p, pend, sa->sin6_scope_id, spec);
1564 }
1565 *p = '\0';
1566
Petr Mladekd529ac42019-04-17 13:53:43 +02001567 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001568}
1569
1570static noinline_for_stack
1571char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1572 struct printf_spec spec, const char *fmt)
1573{
1574 bool have_p = false;
1575 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1576 char *pend = ip4_addr + sizeof(ip4_addr);
1577 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1578 char fmt4[3] = { fmt[0], '4', 0 };
1579
1580 fmt++;
1581 while (isalpha(*++fmt)) {
1582 switch (*fmt) {
1583 case 'p':
1584 have_p = true;
1585 break;
1586 case 'h':
1587 case 'l':
1588 case 'n':
1589 case 'b':
1590 fmt4[2] = *fmt;
1591 break;
1592 }
1593 }
1594
1595 p = ip4_string(ip4_addr, addr, fmt4);
1596 if (have_p) {
1597 *p++ = ':';
1598 p = number(p, pend, ntohs(sa->sin_port), spec);
1599 }
1600 *p = '\0';
1601
Petr Mladekd529ac42019-04-17 13:53:43 +02001602 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001603}
1604
1605static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001606char *ip_addr_string(char *buf, char *end, const void *ptr,
1607 struct printf_spec spec, const char *fmt)
1608{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001609 char *err_fmt_msg;
1610
Petr Mladek3e5903e2019-04-17 13:53:48 +02001611 if (check_pointer(&buf, end, ptr, spec))
1612 return buf;
1613
Petr Mladekf00cc102019-04-17 13:53:44 +02001614 switch (fmt[1]) {
1615 case '6':
1616 return ip6_addr_string(buf, end, ptr, spec, fmt);
1617 case '4':
1618 return ip4_addr_string(buf, end, ptr, spec, fmt);
1619 case 'S': {
1620 const union {
1621 struct sockaddr raw;
1622 struct sockaddr_in v4;
1623 struct sockaddr_in6 v6;
1624 } *sa = ptr;
1625
1626 switch (sa->raw.sa_family) {
1627 case AF_INET:
1628 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1629 case AF_INET6:
1630 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1631 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001632 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001633 }}
1634 }
1635
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001636 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001637 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001638}
1639
1640static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001641char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1642 const char *fmt)
1643{
1644 bool found = true;
1645 int count = 1;
1646 unsigned int flags = 0;
1647 int len;
1648
1649 if (spec.field_width == 0)
1650 return buf; /* nothing to print */
1651
Petr Mladek3e5903e2019-04-17 13:53:48 +02001652 if (check_pointer(&buf, end, addr, spec))
1653 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001654
1655 do {
1656 switch (fmt[count++]) {
1657 case 'a':
1658 flags |= ESCAPE_ANY;
1659 break;
1660 case 'c':
1661 flags |= ESCAPE_SPECIAL;
1662 break;
1663 case 'h':
1664 flags |= ESCAPE_HEX;
1665 break;
1666 case 'n':
1667 flags |= ESCAPE_NULL;
1668 break;
1669 case 'o':
1670 flags |= ESCAPE_OCTAL;
1671 break;
1672 case 'p':
1673 flags |= ESCAPE_NP;
1674 break;
1675 case 's':
1676 flags |= ESCAPE_SPACE;
1677 break;
1678 default:
1679 found = false;
1680 break;
1681 }
1682 } while (found);
1683
1684 if (!flags)
1685 flags = ESCAPE_ANY_NP;
1686
1687 len = spec.field_width < 0 ? 1 : spec.field_width;
1688
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001689 /*
1690 * string_escape_mem() writes as many characters as it can to
1691 * the given buffer, and returns the total size of the output
1692 * had the buffer been big enough.
1693 */
1694 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001695
1696 return buf;
1697}
1698
Petr Mladek3e5903e2019-04-17 13:53:48 +02001699static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1700 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001701{
1702 va_list va;
1703
Petr Mladek3e5903e2019-04-17 13:53:48 +02001704 if (check_pointer(&buf, end, va_fmt, spec))
1705 return buf;
1706
Petr Mladek45c3e932019-04-17 13:53:45 +02001707 va_copy(va, *va_fmt->va);
1708 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1709 va_end(va);
1710
1711 return buf;
1712}
1713
Andy Shevchenko71dca952014-10-13 15:55:18 -07001714static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001715char *uuid_string(char *buf, char *end, const u8 *addr,
1716 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001717{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001718 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001719 char *p = uuid;
1720 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001721 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001722 bool uc = false;
1723
Petr Mladek3e5903e2019-04-17 13:53:48 +02001724 if (check_pointer(&buf, end, addr, spec))
1725 return buf;
1726
Joe Perches9ac6e442009-12-14 18:01:09 -08001727 switch (*(++fmt)) {
1728 case 'L':
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001729 uc = true;
Gustavo A. R. Silva6a9dc5f2020-08-24 15:36:14 -05001730 /* fall through */
Joe Perches9ac6e442009-12-14 18:01:09 -08001731 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001732 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001733 break;
1734 case 'B':
1735 uc = true;
1736 break;
1737 }
1738
1739 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001740 if (uc)
1741 p = hex_byte_pack_upper(p, addr[index[i]]);
1742 else
1743 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001744 switch (i) {
1745 case 3:
1746 case 5:
1747 case 7:
1748 case 9:
1749 *p++ = '-';
1750 break;
1751 }
1752 }
1753
1754 *p = 0;
1755
Petr Mladekd529ac42019-04-17 13:53:43 +02001756 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001757}
1758
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001759static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001760char *netdev_bits(char *buf, char *end, const void *addr,
1761 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001762{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001763 unsigned long long num;
1764 int size;
1765
Petr Mladek3e5903e2019-04-17 13:53:48 +02001766 if (check_pointer(&buf, end, addr, spec))
1767 return buf;
1768
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001769 switch (fmt[1]) {
1770 case 'F':
1771 num = *(const netdev_features_t *)addr;
1772 size = sizeof(netdev_features_t);
1773 break;
1774 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001775 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001776 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001777
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001778 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001779}
1780
Joe Perchesaaf07622014-01-23 15:54:17 -08001781static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001782char *address_val(char *buf, char *end, const void *addr,
1783 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001784{
1785 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001786 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001787
Petr Mladek3e5903e2019-04-17 13:53:48 +02001788 if (check_pointer(&buf, end, addr, spec))
1789 return buf;
1790
Joe Perchesaaf07622014-01-23 15:54:17 -08001791 switch (fmt[1]) {
1792 case 'd':
1793 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001794 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001795 break;
1796 case 'p':
1797 default:
1798 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001799 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001800 break;
1801 }
1802
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001803 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001804}
1805
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001806static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001807char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1808{
1809 int year = tm->tm_year + (r ? 0 : 1900);
1810 int mon = tm->tm_mon + (r ? 0 : 1);
1811
1812 buf = number(buf, end, year, default_dec04_spec);
1813 if (buf < end)
1814 *buf = '-';
1815 buf++;
1816
1817 buf = number(buf, end, mon, default_dec02_spec);
1818 if (buf < end)
1819 *buf = '-';
1820 buf++;
1821
1822 return number(buf, end, tm->tm_mday, default_dec02_spec);
1823}
1824
1825static noinline_for_stack
1826char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1827{
1828 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1829 if (buf < end)
1830 *buf = ':';
1831 buf++;
1832
1833 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1834 if (buf < end)
1835 *buf = ':';
1836 buf++;
1837
1838 return number(buf, end, tm->tm_sec, default_dec02_spec);
1839}
1840
1841static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001842char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1843 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001844{
1845 bool have_t = true, have_d = true;
1846 bool raw = false;
1847 int count = 2;
1848
Petr Mladek3e5903e2019-04-17 13:53:48 +02001849 if (check_pointer(&buf, end, tm, spec))
1850 return buf;
1851
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001852 switch (fmt[count]) {
1853 case 'd':
1854 have_t = false;
1855 count++;
1856 break;
1857 case 't':
1858 have_d = false;
1859 count++;
1860 break;
1861 }
1862
1863 raw = fmt[count] == 'r';
1864
1865 if (have_d)
1866 buf = date_str(buf, end, tm, raw);
1867 if (have_d && have_t) {
1868 /* Respect ISO 8601 */
1869 if (buf < end)
1870 *buf = 'T';
1871 buf++;
1872 }
1873 if (have_t)
1874 buf = time_str(buf, end, tm, raw);
1875
1876 return buf;
1877}
1878
1879static noinline_for_stack
Andy Shevchenko7daac5b2020-04-15 20:00:44 +03001880char *time64_str(char *buf, char *end, const time64_t time,
1881 struct printf_spec spec, const char *fmt)
1882{
1883 struct rtc_time rtc_time;
1884 struct tm tm;
1885
1886 time64_to_tm(time, 0, &tm);
1887
1888 rtc_time.tm_sec = tm.tm_sec;
1889 rtc_time.tm_min = tm.tm_min;
1890 rtc_time.tm_hour = tm.tm_hour;
1891 rtc_time.tm_mday = tm.tm_mday;
1892 rtc_time.tm_mon = tm.tm_mon;
1893 rtc_time.tm_year = tm.tm_year;
1894 rtc_time.tm_wday = tm.tm_wday;
1895 rtc_time.tm_yday = tm.tm_yday;
1896
1897 rtc_time.tm_isdst = 0;
1898
1899 return rtc_str(buf, end, &rtc_time, spec, fmt);
1900}
1901
1902static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001903char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1904 const char *fmt)
1905{
1906 switch (fmt[1]) {
1907 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001908 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko7daac5b2020-04-15 20:00:44 +03001909 case 'T':
1910 return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001911 default:
Andy Shevchenko7daac5b2020-04-15 20:00:44 +03001912 return error_string(buf, end, "(%pt?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001913 }
1914}
1915
1916static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001917char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1918 const char *fmt)
1919{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001920 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001921 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001922
Petr Mladek3e5903e2019-04-17 13:53:48 +02001923 if (check_pointer(&buf, end, clk, spec))
1924 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001925
1926 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001927 case 'n':
1928 default:
1929#ifdef CONFIG_COMMON_CLK
1930 return string(buf, end, __clk_get_name(clk), spec);
1931#else
Geert Uytterhoeven4ca96aa2019-07-01 16:00:09 +02001932 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001933#endif
1934 }
1935}
1936
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001937static
1938char *format_flags(char *buf, char *end, unsigned long flags,
1939 const struct trace_print_flags *names)
1940{
1941 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001942
1943 for ( ; flags && names->name; names++) {
1944 mask = names->mask;
1945 if ((flags & mask) != mask)
1946 continue;
1947
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001948 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001949
1950 flags &= ~mask;
1951 if (flags) {
1952 if (buf < end)
1953 *buf = '|';
1954 buf++;
1955 }
1956 }
1957
1958 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001959 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001960
1961 return buf;
1962}
1963
1964static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001965char *flags_string(char *buf, char *end, void *flags_ptr,
1966 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001967{
1968 unsigned long flags;
1969 const struct trace_print_flags *names;
1970
Petr Mladek3e5903e2019-04-17 13:53:48 +02001971 if (check_pointer(&buf, end, flags_ptr, spec))
1972 return buf;
1973
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001974 switch (fmt[1]) {
1975 case 'p':
1976 flags = *(unsigned long *)flags_ptr;
1977 /* Remove zone id */
1978 flags &= (1UL << NR_PAGEFLAGS) - 1;
1979 names = pageflag_names;
1980 break;
1981 case 'v':
1982 flags = *(unsigned long *)flags_ptr;
1983 names = vmaflag_names;
1984 break;
1985 case 'g':
Andy Shevchenko30d497a2020-07-31 21:08:24 +03001986 flags = (__force unsigned long)(*(gfp_t *)flags_ptr);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001987 names = gfpflag_names;
1988 break;
1989 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001990 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001991 }
1992
1993 return format_flags(buf, end, flags, names);
1994}
1995
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001996static noinline_for_stack
Sakari Ailusa92eb762019-10-03 15:32:16 +03001997char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1998 char *end)
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001999{
2000 int depth;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002001
Sakari Ailusa92eb762019-10-03 15:32:16 +03002002 /* Loop starting from the root node to the current node. */
2003 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
2004 struct fwnode_handle *__fwnode =
2005 fwnode_get_nth_parent(fwnode, depth);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002006
Sakari Ailusa92eb762019-10-03 15:32:16 +03002007 buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02002008 default_str_spec);
Sakari Ailusa92eb762019-10-03 15:32:16 +03002009 buf = string(buf, end, fwnode_get_name(__fwnode),
2010 default_str_spec);
2011
2012 fwnode_handle_put(__fwnode);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002013 }
Sakari Ailusa92eb762019-10-03 15:32:16 +03002014
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002015 return buf;
2016}
2017
2018static noinline_for_stack
2019char *device_node_string(char *buf, char *end, struct device_node *dn,
2020 struct printf_spec spec, const char *fmt)
2021{
2022 char tbuf[sizeof("xxxx") + 1];
2023 const char *p;
2024 int ret;
2025 char *buf_start = buf;
2026 struct property *prop;
2027 bool has_mult, pass;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002028
2029 struct printf_spec str_spec = spec;
2030 str_spec.field_width = -1;
2031
Sakari Ailus83abc5a2019-10-03 15:32:17 +03002032 if (fmt[0] != 'F')
2033 return error_string(buf, end, "(%pO?)", spec);
2034
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002035 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02002036 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002037
Petr Mladek3e5903e2019-04-17 13:53:48 +02002038 if (check_pointer(&buf, end, dn, spec))
2039 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002040
2041 /* simple case without anything any more format specifiers */
2042 fmt++;
2043 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
2044 fmt = "f";
2045
2046 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05002047 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002048 if (pass) {
2049 if (buf < end)
2050 *buf = ':';
2051 buf++;
2052 }
2053
2054 switch (*fmt) {
2055 case 'f': /* full_name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03002056 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
2057 end);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002058 break;
2059 case 'n': /* name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03002060 p = fwnode_get_name(of_fwnode_handle(dn));
Rob Herring6d0a70a2018-08-27 08:13:56 -05002061 precision = str_spec.precision;
2062 str_spec.precision = strchrnul(p, '@') - p;
2063 buf = string(buf, end, p, str_spec);
2064 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002065 break;
2066 case 'p': /* phandle */
Andy Shevchenko09ceb8d2020-07-31 21:08:23 +03002067 buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002068 break;
2069 case 'P': /* path-spec */
Sakari Ailusa92eb762019-10-03 15:32:16 +03002070 p = fwnode_get_name(of_fwnode_handle(dn));
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002071 if (!p[1])
2072 p = "/";
2073 buf = string(buf, end, p, str_spec);
2074 break;
2075 case 'F': /* flags */
2076 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2077 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2078 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2079 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2080 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02002081 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002082 break;
2083 case 'c': /* major compatible string */
2084 ret = of_property_read_string(dn, "compatible", &p);
2085 if (!ret)
2086 buf = string(buf, end, p, str_spec);
2087 break;
2088 case 'C': /* full compatible string */
2089 has_mult = false;
2090 of_property_for_each_string(dn, "compatible", prop, p) {
2091 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02002092 buf = string_nocheck(buf, end, ",", str_spec);
2093 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002094 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02002095 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002096
2097 has_mult = true;
2098 }
2099 break;
2100 default:
2101 break;
2102 }
2103 }
2104
2105 return widen_string(buf, buf - buf_start, end, spec);
2106}
2107
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002108static noinline_for_stack
2109char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2110 struct printf_spec spec, const char *fmt)
Petr Mladek798cc272019-04-17 13:53:46 +02002111{
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002112 struct printf_spec str_spec = spec;
2113 char *buf_start = buf;
2114
2115 str_spec.field_width = -1;
2116
2117 if (*fmt != 'w')
2118 return error_string(buf, end, "(%pf?)", spec);
2119
2120 if (check_pointer(&buf, end, fwnode, spec))
2121 return buf;
2122
2123 fmt++;
2124
2125 switch (*fmt) {
2126 case 'P': /* name */
2127 buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
2128 break;
2129 case 'f': /* full_name */
2130 default:
2131 buf = fwnode_full_name_string(fwnode, buf, end);
2132 break;
Petr Mladek798cc272019-04-17 13:53:46 +02002133 }
2134
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002135 return widen_string(buf, buf - buf_start, end, spec);
Petr Mladek798cc272019-04-17 13:53:46 +02002136}
2137
Timur Tabifc461a12021-02-14 10:13:48 -06002138static int __init no_hash_pointers_enable(char *str)
2139{
2140 no_hash_pointers = true;
2141
2142 pr_warn("**********************************************************\n");
2143 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2144 pr_warn("** **\n");
2145 pr_warn("** This system shows unhashed kernel memory addresses **\n");
2146 pr_warn("** via the console, logs, and other interfaces. This **\n");
2147 pr_warn("** might reduce the security of your system. **\n");
2148 pr_warn("** **\n");
2149 pr_warn("** If you see this message and you are not debugging **\n");
2150 pr_warn("** the kernel, report this immediately to your system **\n");
2151 pr_warn("** administrator! **\n");
2152 pr_warn("** **\n");
2153 pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2154 pr_warn("**********************************************************\n");
2155
2156 return 0;
2157}
2158early_param("no_hash_pointers", no_hash_pointers_enable);
2159
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002160/*
2161 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2162 * by an extra set of alphanumeric characters that are extended format
2163 * specifiers.
2164 *
Joe Perches0b523762017-05-08 15:55:36 -07002165 * Please update scripts/checkpatch.pl when adding/removing conversion
2166 * characters. (Search for "check for vsprintf extension").
2167 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002168 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002169 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002170 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2171 * - 's' For symbolic direct pointers (or function descriptors) without offset
Sakari Ailus9af77062019-10-03 15:32:14 +03002172 * - '[Ss]R' as above with __builtin_extract_return_addr() translation
Sakari Ailus1586c5a2019-10-03 15:32:15 +03002173 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2174 * %ps and %pS. Be careful when re-using these specifiers.
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002175 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002176 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2177 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002178 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2179 * width which must be explicitly specified either as part of the
2180 * format string '%32b[l]' or through '%*b[l]', [l] selects
2181 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002182 * - 'M' For a 6-byte MAC address, it prints the address in the
2183 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002184 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002185 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002186 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002187 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002188 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2189 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2190 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002191 * [S][pfs]
2192 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2193 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002194 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2195 * IPv6 omits the colons (01020304...0f)
2196 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002197 * [S][pfs]
2198 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2199 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2200 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2201 * - 'I[6S]c' for IPv6 addresses printed as specified by
Alexander A. Klimov8eda94b2020-07-02 22:05:36 +02002202 * https://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002203 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2204 * of the following flags (see string_escape_mem() for the
2205 * details):
2206 * a - ESCAPE_ANY
2207 * c - ESCAPE_SPECIAL
2208 * h - ESCAPE_HEX
2209 * n - ESCAPE_NULL
2210 * o - ESCAPE_OCTAL
2211 * p - ESCAPE_NP
2212 * s - ESCAPE_SPACE
2213 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002214 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2215 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2216 * Options for %pU are:
2217 * b big endian lower case hex (default)
2218 * B big endian UPPER case hex
2219 * l little endian lower case hex
2220 * L little endian UPPER case hex
2221 * big endian output byte order is:
2222 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2223 * little endian output byte order is:
2224 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002225 * - 'V' For a struct va_format which contains a format string * and va_list *,
2226 * call vsnprintf(->format, *->va_list).
2227 * Implements a "recursive vsnprintf".
2228 * Do not use this feature without some mechanism to verify the
2229 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002230 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002231 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002232 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2233 * a certain separator (' ' by default):
2234 * C colon
2235 * D dash
2236 * N no separator
2237 * The maximum supported length is 64 bytes of the input. Consider
2238 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002239 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2240 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002241 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2242 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002243 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko7daac5b2020-04-15 20:00:44 +03002244 * - 't[RT][dt][r]' For time and date as represented by:
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002245 * R struct rtc_time
Andy Shevchenko7daac5b2020-04-15 20:00:44 +03002246 * T time64_t
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002247 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2248 * (legacy clock framework) of the clock
2249 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2250 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002251 * - 'G' For flags to be printed as a collection of symbolic strings that would
2252 * construct the specific value. Supported flags given by option:
2253 * p page flags (see struct page) given as pointer to unsigned long
2254 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2255 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002256 * - 'OF[fnpPcCF]' For a device tree object
2257 * Without any optional arguments prints the full_name
2258 * f device node full_name
2259 * n device node name
2260 * p device node phandle
2261 * P device node path spec (name + @unit)
2262 * F device node flags
2263 * c major compatible string
2264 * C full compatible string
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002265 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2266 * Without an option prints the full name of the node
2267 * f full name
2268 * P node name, including a possible unit address
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002269 * - 'x' For printing the address. Equivalent to "%lx".
Daniel Borkmannb2a52122020-05-15 12:11:18 +02002270 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2271 * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2272 * or user (u) memory to probe, and:
2273 * s a string, equivalent to "%s" on direct vsnprintf() use
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002274 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002275 * ** When making changes please also update:
2276 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002277 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002278 * Note: The default behaviour (unadorned %p) is to hash the address,
2279 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002280 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002281static noinline_for_stack
2282char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2283 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002284{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002285 switch (*fmt) {
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002286 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002287 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002288 ptr = dereference_symbol_descriptor(ptr);
Gustavo A. R. Silva6a9dc5f2020-08-24 15:36:14 -05002289 /* fall through */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002290 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002291 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002292 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002293 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002294 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002295 case 'h':
2296 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002297 case 'b':
2298 switch (fmt[1]) {
2299 case 'l':
2300 return bitmap_list_string(buf, end, ptr, spec, fmt);
2301 default:
2302 return bitmap_string(buf, end, ptr, spec, fmt);
2303 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002304 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2305 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002306 /* [mM]F (FDDI) */
2307 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002308 return mac_address_string(buf, end, ptr, spec, fmt);
2309 case 'I': /* Formatted IP supported
2310 * 4: 1.2.3.4
2311 * 6: 0001:0203:...:0708
2312 * 6c: 1::708 or 1::1.2.3.4
2313 */
2314 case 'i': /* Contiguous:
2315 * 4: 001.002.003.004
2316 * 6: 000102...0f
2317 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002318 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002319 case 'E':
2320 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002321 case 'U':
2322 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002323 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002324 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002325 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002326 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002327 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002328 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002329 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002330 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002331 case 'd':
2332 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002333 case 't':
2334 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002335 case 'C':
2336 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002337 case 'D':
Jia He36594b32019-08-09 09:24:56 +08002338 return file_dentry_name(buf, end, ptr, spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002339#ifdef CONFIG_BLOCK
2340 case 'g':
2341 return bdev_name(buf, end, ptr, spec, fmt);
2342#endif
2343
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002344 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002345 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002346 case 'O':
Sakari Ailus83abc5a2019-10-03 15:32:17 +03002347 return device_node_string(buf, end, ptr, spec, fmt + 1);
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002348 case 'f':
2349 return fwnode_string(buf, end, ptr, spec, fmt + 1);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002350 case 'x':
2351 return pointer_string(buf, end, ptr, spec);
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002352 case 'e':
2353 /* %pe with a non-ERR_PTR gets treated as plain %p */
2354 if (!IS_ERR(ptr))
Christophe Leroy83ed3e22022-02-17 09:49:59 +01002355 return default_pointer(buf, end, ptr, spec);
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002356 return err_ptr(buf, end, ptr, spec);
Daniel Borkmannb2a52122020-05-15 12:11:18 +02002357 case 'u':
2358 case 'k':
2359 switch (fmt[1]) {
2360 case 's':
2361 return string(buf, end, ptr, spec);
2362 default:
2363 return error_string(buf, end, "(einval)", spec);
2364 }
Christophe Leroy83ed3e22022-02-17 09:49:59 +01002365 default:
2366 return default_pointer(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002367 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002368}
2369
2370/*
2371 * Helper function to decode printf style format.
2372 * Each call decode a token from the format and return the
2373 * number of characters read (or likely the delta where it wants
2374 * to go on the next call).
2375 * The decoded token is returned through the parameters
2376 *
2377 * 'h', 'l', or 'L' for integer fields
2378 * 'z' support added 23/7/1999 S.H.
2379 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002380 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002381 * 't' added for ptrdiff_t
2382 *
2383 * @fmt: the format string
2384 * @type of the token returned
2385 * @flags: various flags such as +, -, # tokens..
2386 * @field_width: overwritten width
2387 * @base: base of the number (octal, hex, ...)
2388 * @precision: precision of a number
2389 * @qualifier: qualifier of a number (long, size_t, ...)
2390 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002391static noinline_for_stack
2392int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002393{
2394 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002395 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002396
2397 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002398 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002399 if (spec->field_width < 0) {
2400 spec->field_width = -spec->field_width;
2401 spec->flags |= LEFT;
2402 }
2403 spec->type = FORMAT_TYPE_NONE;
2404 goto precision;
2405 }
2406
2407 /* we finished early by reading the precision */
2408 if (spec->type == FORMAT_TYPE_PRECISION) {
2409 if (spec->precision < 0)
2410 spec->precision = 0;
2411
2412 spec->type = FORMAT_TYPE_NONE;
2413 goto qualifier;
2414 }
2415
2416 /* By default */
2417 spec->type = FORMAT_TYPE_NONE;
2418
2419 for (; *fmt ; ++fmt) {
2420 if (*fmt == '%')
2421 break;
2422 }
2423
2424 /* Return the current non-format string */
2425 if (fmt != start || !*fmt)
2426 return fmt - start;
2427
2428 /* Process flags */
2429 spec->flags = 0;
2430
2431 while (1) { /* this also skips first '%' */
2432 bool found = true;
2433
2434 ++fmt;
2435
2436 switch (*fmt) {
2437 case '-': spec->flags |= LEFT; break;
2438 case '+': spec->flags |= PLUS; break;
2439 case ' ': spec->flags |= SPACE; break;
2440 case '#': spec->flags |= SPECIAL; break;
2441 case '0': spec->flags |= ZEROPAD; break;
2442 default: found = false;
2443 }
2444
2445 if (!found)
2446 break;
2447 }
2448
2449 /* get field width */
2450 spec->field_width = -1;
2451
2452 if (isdigit(*fmt))
2453 spec->field_width = skip_atoi(&fmt);
2454 else if (*fmt == '*') {
2455 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002456 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002457 return ++fmt - start;
2458 }
2459
2460precision:
2461 /* get the precision */
2462 spec->precision = -1;
2463 if (*fmt == '.') {
2464 ++fmt;
2465 if (isdigit(*fmt)) {
2466 spec->precision = skip_atoi(&fmt);
2467 if (spec->precision < 0)
2468 spec->precision = 0;
2469 } else if (*fmt == '*') {
2470 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002471 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002472 return ++fmt - start;
2473 }
2474 }
2475
2476qualifier:
2477 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002478 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002479 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002480 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002481 qualifier = *fmt++;
2482 if (unlikely(qualifier == *fmt)) {
2483 if (qualifier == 'l') {
2484 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002485 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002486 } else if (qualifier == 'h') {
2487 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002488 ++fmt;
2489 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002490 }
2491 }
2492
2493 /* default base */
2494 spec->base = 10;
2495 switch (*fmt) {
2496 case 'c':
2497 spec->type = FORMAT_TYPE_CHAR;
2498 return ++fmt - start;
2499
2500 case 's':
2501 spec->type = FORMAT_TYPE_STR;
2502 return ++fmt - start;
2503
2504 case 'p':
2505 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002506 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002507
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002508 case '%':
2509 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2510 return ++fmt - start;
2511
2512 /* integer number formats - set up the flags and "break" */
2513 case 'o':
2514 spec->base = 8;
2515 break;
2516
2517 case 'x':
2518 spec->flags |= SMALL;
Gustavo A. R. Silva6a9dc5f2020-08-24 15:36:14 -05002519 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002520
2521 case 'X':
2522 spec->base = 16;
2523 break;
2524
2525 case 'd':
2526 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002527 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002528 case 'u':
2529 break;
2530
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002531 case 'n':
2532 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002533 * Since %n poses a greater security risk than
2534 * utility, treat it as any other invalid or
2535 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002536 */
Gustavo A. R. Silva6a9dc5f2020-08-24 15:36:14 -05002537 /* fall through */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002538
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002539 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002540 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002541 spec->type = FORMAT_TYPE_INVALID;
2542 return fmt - start;
2543 }
2544
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002545 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002546 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002547 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002548 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2549 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002550 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002551 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002552 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002553 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002554 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002555 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2556 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002557 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002558 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2559 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002560 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002561 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2562 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002563 }
2564
2565 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002566}
2567
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002568static void
2569set_field_width(struct printf_spec *spec, int width)
2570{
2571 spec->field_width = width;
2572 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2573 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2574 }
2575}
2576
2577static void
2578set_precision(struct printf_spec *spec, int prec)
2579{
2580 spec->precision = prec;
2581 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2582 spec->precision = clamp(prec, 0, PRECISION_MAX);
2583 }
2584}
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586/**
2587 * vsnprintf - Format a string and place it in a buffer
2588 * @buf: The buffer to place the result into
2589 * @size: The size of the buffer, including the trailing null space
2590 * @fmt: The format string to use
2591 * @args: Arguments for the format string
2592 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002593 * This function generally follows C99 vsnprintf, but has some
2594 * extensions and a few limitations:
2595 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002596 * - ``%n`` is unsupported
2597 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002598 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002599 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002600 * extensive description.
2601 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002602 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002603 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 * The return value is the number of characters which would
2605 * be generated for the given input, excluding the trailing
2606 * '\0', as per ISO C99. If you want to have the exact
2607 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002608 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 * return is greater than or equal to @size, the resulting
2610 * string is truncated.
2611 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002612 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 */
2614int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2615{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002617 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002618 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002620 /* Reject out-of-range values early. Large positive sizes are
2621 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002622 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002626 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002628 /* Make sure end is always >= buf */
2629 if (end < buf) {
2630 end = ((void *)-1);
2631 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
2633
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002634 while (*fmt) {
2635 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002636 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002637
2638 fmt += read;
2639
2640 switch (spec.type) {
2641 case FORMAT_TYPE_NONE: {
2642 int copy = read;
2643 if (str < end) {
2644 if (copy > end - str)
2645 copy = end - str;
2646 memcpy(str, old_fmt, copy);
2647 }
2648 str += read;
2649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
2651
Vegard Nossumed681a92009-03-14 12:08:50 +01002652 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002653 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002656 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002657 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
André Goddard Rosad4be1512009-12-14 18:00:59 -08002660 case FORMAT_TYPE_CHAR: {
2661 char c;
2662
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002663 if (!(spec.flags & LEFT)) {
2664 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002665 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 *str = ' ';
2667 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002670 }
2671 c = (unsigned char) va_arg(args, int);
2672 if (str < end)
2673 *str = c;
2674 ++str;
2675 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002676 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002677 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002679 }
2680 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002683 case FORMAT_TYPE_STR:
2684 str = string(str, end, va_arg(args, char *), spec);
2685 break;
2686
2687 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002688 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002689 spec);
2690 while (isalnum(*fmt))
2691 fmt++;
2692 break;
2693
2694 case FORMAT_TYPE_PERCENT_CHAR:
2695 if (str < end)
2696 *str = '%';
2697 ++str;
2698 break;
2699
2700 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002701 /*
2702 * Presumably the arguments passed gcc's type
2703 * checking, but there is no safe or sane way
2704 * for us to continue parsing the format and
2705 * fetching from the va_list; the remaining
2706 * specifiers and arguments would be out of
2707 * sync.
2708 */
2709 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002710
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002711 default:
2712 switch (spec.type) {
2713 case FORMAT_TYPE_LONG_LONG:
2714 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002716 case FORMAT_TYPE_ULONG:
2717 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002719 case FORMAT_TYPE_LONG:
2720 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002722 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002723 if (spec.flags & SIGN)
2724 num = va_arg(args, ssize_t);
2725 else
2726 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002727 break;
2728 case FORMAT_TYPE_PTRDIFF:
2729 num = va_arg(args, ptrdiff_t);
2730 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002731 case FORMAT_TYPE_UBYTE:
2732 num = (unsigned char) va_arg(args, int);
2733 break;
2734 case FORMAT_TYPE_BYTE:
2735 num = (signed char) va_arg(args, int);
2736 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002737 case FORMAT_TYPE_USHORT:
2738 num = (unsigned short) va_arg(args, int);
2739 break;
2740 case FORMAT_TYPE_SHORT:
2741 num = (short) va_arg(args, int);
2742 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002743 case FORMAT_TYPE_INT:
2744 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002747 num = va_arg(args, unsigned int);
2748 }
2749
2750 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002753
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002754out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002755 if (size > 0) {
2756 if (str < end)
2757 *str = '\0';
2758 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002759 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002760 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002761
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002762 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766EXPORT_SYMBOL(vsnprintf);
2767
2768/**
2769 * vscnprintf - Format a string and place it in a buffer
2770 * @buf: The buffer to place the result into
2771 * @size: The size of the buffer, including the trailing null space
2772 * @fmt: The format string to use
2773 * @args: Arguments for the format string
2774 *
2775 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002776 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 * returns 0.
2778 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002779 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002780 *
2781 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 */
2783int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2784{
2785 int i;
2786
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002787 i = vsnprintf(buf, size, fmt, args);
2788
Anton Arapovb921c692011-01-12 16:59:49 -08002789 if (likely(i < size))
2790 return i;
2791 if (size != 0)
2792 return size - 1;
2793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795EXPORT_SYMBOL(vscnprintf);
2796
2797/**
2798 * snprintf - Format a string and place it in a buffer
2799 * @buf: The buffer to place the result into
2800 * @size: The size of the buffer, including the trailing null space
2801 * @fmt: The format string to use
2802 * @...: Arguments for the format string
2803 *
2804 * The return value is the number of characters which would be
2805 * generated for the given input, excluding the trailing null,
2806 * as per ISO C99. If the return is greater than or equal to
2807 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002808 *
2809 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002811int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
2813 va_list args;
2814 int i;
2815
2816 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002817 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return i;
2821}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822EXPORT_SYMBOL(snprintf);
2823
2824/**
2825 * scnprintf - Format a string and place it in a buffer
2826 * @buf: The buffer to place the result into
2827 * @size: The size of the buffer, including the trailing null space
2828 * @fmt: The format string to use
2829 * @...: Arguments for the format string
2830 *
2831 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002832 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 */
2834
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002835int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 va_list args;
2838 int i;
2839
2840 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002841 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002843
Anton Arapovb921c692011-01-12 16:59:49 -08002844 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846EXPORT_SYMBOL(scnprintf);
2847
2848/**
2849 * vsprintf - Format a string and place it in a buffer
2850 * @buf: The buffer to place the result into
2851 * @fmt: The format string to use
2852 * @args: Arguments for the format string
2853 *
2854 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002855 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 * buffer overflows.
2857 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002858 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002859 *
2860 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 */
2862int vsprintf(char *buf, const char *fmt, va_list args)
2863{
2864 return vsnprintf(buf, INT_MAX, fmt, args);
2865}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866EXPORT_SYMBOL(vsprintf);
2867
2868/**
2869 * sprintf - Format a string and place it in a buffer
2870 * @buf: The buffer to place the result into
2871 * @fmt: The format string to use
2872 * @...: Arguments for the format string
2873 *
2874 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002875 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002877 *
2878 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002880int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
2882 va_list args;
2883 int i;
2884
2885 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002886 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return i;
2890}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891EXPORT_SYMBOL(sprintf);
2892
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002893#ifdef CONFIG_BINARY_PRINTF
2894/*
2895 * bprintf service:
2896 * vbin_printf() - VA arguments to binary data
2897 * bstr_printf() - Binary data to text string
2898 */
2899
2900/**
2901 * vbin_printf - Parse a format string and place args' binary value in a buffer
2902 * @bin_buf: The buffer to place args' binary value
2903 * @size: The size of the buffer(by words(32bits), not characters)
2904 * @fmt: The format string to use
2905 * @args: Arguments for the format string
2906 *
2907 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002908 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002909 *
2910 * The return value is the number of words(32bits) which would be generated for
2911 * the given input.
2912 *
2913 * NOTE:
2914 * If the return value is greater than @size, the resulting bin_buf is NOT
2915 * valid for bstr_printf().
2916 */
2917int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2918{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002919 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002920 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002921 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002922
2923 str = (char *)bin_buf;
2924 end = (char *)(bin_buf + size);
2925
2926#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002927({ \
2928 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002929 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002930 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002931 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002932 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002933 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002934 *(u32 *)str = *(u32 *)&val8; \
2935 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002936 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002937 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002938 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002939 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002940 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002941 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002942 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002943 *(typeof(type) *)str = (type)(long)val4; \
2944 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002945 } \
2946 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002947 value; \
2948})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002949
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002950 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002951 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002952
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002953 fmt += read;
2954
2955 switch (spec.type) {
2956 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002957 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002958 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002959 case FORMAT_TYPE_INVALID:
2960 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002961
Vegard Nossumed681a92009-03-14 12:08:50 +01002962 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002963 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002964 width = (int)save_arg(int);
2965 /* Pointers may require the width */
2966 if (*fmt == 'p')
2967 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002968 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002969
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002970 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002971 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002972 break;
2973
2974 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002975 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002976 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002977 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002978
Petr Mladek3e5903e2019-04-17 13:53:48 +02002979 err_msg = check_pointer_msg(save_str);
2980 if (err_msg)
2981 save_str = err_msg;
2982
André Goddard Rosa6c356632009-12-14 18:00:56 -08002983 len = strlen(save_str) + 1;
2984 if (str + len < end)
2985 memcpy(str, save_str, len);
2986 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002987 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002988 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002989
2990 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002991 /* Dereferenced pointers must be done now */
2992 switch (*fmt) {
2993 /* Dereference of functions is still OK */
2994 case 'S':
2995 case 's':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002996 case 'x':
2997 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002998 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002999 save_arg(void *);
3000 break;
3001 default:
3002 if (!isalnum(*fmt)) {
3003 save_arg(void *);
3004 break;
3005 }
3006 str = pointer(fmt, str, end, va_arg(args, void *),
3007 spec);
3008 if (str + 1 < end)
3009 *str++ = '\0';
3010 else
3011 end[-1] = '\0'; /* Must be nul terminated */
3012 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003013 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003014 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003015 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003016 break;
3017
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003018 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003019 switch (spec.type) {
3020
3021 case FORMAT_TYPE_LONG_LONG:
3022 save_arg(long long);
3023 break;
3024 case FORMAT_TYPE_ULONG:
3025 case FORMAT_TYPE_LONG:
3026 save_arg(unsigned long);
3027 break;
3028 case FORMAT_TYPE_SIZE_T:
3029 save_arg(size_t);
3030 break;
3031 case FORMAT_TYPE_PTRDIFF:
3032 save_arg(ptrdiff_t);
3033 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003034 case FORMAT_TYPE_UBYTE:
3035 case FORMAT_TYPE_BYTE:
3036 save_arg(char);
3037 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003038 case FORMAT_TYPE_USHORT:
3039 case FORMAT_TYPE_SHORT:
3040 save_arg(short);
3041 break;
3042 default:
3043 save_arg(int);
3044 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003045 }
3046 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003047
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003048out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003049 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003050#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003051}
3052EXPORT_SYMBOL_GPL(vbin_printf);
3053
3054/**
3055 * bstr_printf - Format a string from binary arguments and place it in a buffer
3056 * @buf: The buffer to place the result into
3057 * @size: The size of the buffer, including the trailing null space
3058 * @fmt: The format string to use
3059 * @bin_buf: Binary arguments for the format string
3060 *
3061 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
3062 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
3063 * a binary buffer that generated by vbin_printf.
3064 *
3065 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04003066 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003067 *
3068 * The return value is the number of characters which would
3069 * be generated for the given input, excluding the trailing
3070 * '\0', as per ISO C99. If you want to have the exact
3071 * number of characters written into @buf as return value
3072 * (not including the trailing '\0'), use vscnprintf(). If the
3073 * return is greater than or equal to @size, the resulting
3074 * string is truncated.
3075 */
3076int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
3077{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003078 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08003079 char *str, *end;
3080 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003081
Rasmus Villemoes762abb52015-11-06 16:30:23 -08003082 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003083 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003084
3085 str = buf;
3086 end = buf + size;
3087
3088#define get_arg(type) \
3089({ \
3090 typeof(type) value; \
3091 if (sizeof(type) == 8) { \
3092 args = PTR_ALIGN(args, sizeof(u32)); \
3093 *(u32 *)&value = *(u32 *)args; \
3094 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
3095 } else { \
3096 args = PTR_ALIGN(args, sizeof(type)); \
3097 value = *(typeof(type) *)args; \
3098 } \
3099 args += sizeof(type); \
3100 value; \
3101})
3102
3103 /* Make sure end is always >= buf */
3104 if (end < buf) {
3105 end = ((void *)-1);
3106 size = end - buf;
3107 }
3108
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003109 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003110 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003111 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003112
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003113 fmt += read;
3114
3115 switch (spec.type) {
3116 case FORMAT_TYPE_NONE: {
3117 int copy = read;
3118 if (str < end) {
3119 if (copy > end - str)
3120 copy = end - str;
3121 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003122 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003123 str += read;
3124 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003125 }
3126
Vegard Nossumed681a92009-03-14 12:08:50 +01003127 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003128 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003129 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003130
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003131 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003132 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003133 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003134
André Goddard Rosad4be1512009-12-14 18:00:59 -08003135 case FORMAT_TYPE_CHAR: {
3136 char c;
3137
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003138 if (!(spec.flags & LEFT)) {
3139 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003140 if (str < end)
3141 *str = ' ';
3142 ++str;
3143 }
3144 }
3145 c = (unsigned char) get_arg(char);
3146 if (str < end)
3147 *str = c;
3148 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003149 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003150 if (str < end)
3151 *str = ' ';
3152 ++str;
3153 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003154 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003155 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003156
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003157 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003158 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003159 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003160 str = string(str, end, (char *)str_arg, spec);
3161 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003162 }
3163
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003164 case FORMAT_TYPE_PTR: {
3165 bool process = false;
3166 int copy, len;
3167 /* Non function dereferences were already done */
3168 switch (*fmt) {
3169 case 'S':
3170 case 's':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04003171 case 'x':
3172 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02003173 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003174 process = true;
3175 break;
3176 default:
3177 if (!isalnum(*fmt)) {
3178 process = true;
3179 break;
3180 }
3181 /* Pointer dereference was already processed */
3182 if (str < end) {
3183 len = copy = strlen(args);
3184 if (copy > end - str)
3185 copy = end - str;
3186 memcpy(str, args, copy);
3187 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003188 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003189 }
3190 }
3191 if (process)
3192 str = pointer(fmt, str, end, get_arg(void *), spec);
3193
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003194 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003195 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003196 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003197 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003198
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003199 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003200 if (str < end)
3201 *str = '%';
3202 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003203 break;
3204
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003205 case FORMAT_TYPE_INVALID:
3206 goto out;
3207
André Goddard Rosad4be1512009-12-14 18:00:59 -08003208 default: {
3209 unsigned long long num;
3210
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003211 switch (spec.type) {
3212
3213 case FORMAT_TYPE_LONG_LONG:
3214 num = get_arg(long long);
3215 break;
3216 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003217 case FORMAT_TYPE_LONG:
3218 num = get_arg(unsigned long);
3219 break;
3220 case FORMAT_TYPE_SIZE_T:
3221 num = get_arg(size_t);
3222 break;
3223 case FORMAT_TYPE_PTRDIFF:
3224 num = get_arg(ptrdiff_t);
3225 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003226 case FORMAT_TYPE_UBYTE:
3227 num = get_arg(unsigned char);
3228 break;
3229 case FORMAT_TYPE_BYTE:
3230 num = get_arg(signed char);
3231 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003232 case FORMAT_TYPE_USHORT:
3233 num = get_arg(unsigned short);
3234 break;
3235 case FORMAT_TYPE_SHORT:
3236 num = get_arg(short);
3237 break;
3238 case FORMAT_TYPE_UINT:
3239 num = get_arg(unsigned int);
3240 break;
3241 default:
3242 num = get_arg(int);
3243 }
3244
3245 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003246 } /* default: */
3247 } /* switch(spec.type) */
3248 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003249
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003250out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003251 if (size > 0) {
3252 if (str < end)
3253 *str = '\0';
3254 else
3255 end[-1] = '\0';
3256 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003257
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003258#undef get_arg
3259
3260 /* the trailing null byte doesn't count towards the total */
3261 return str - buf;
3262}
3263EXPORT_SYMBOL_GPL(bstr_printf);
3264
3265/**
3266 * bprintf - Parse a format string and place args' binary value in a buffer
3267 * @bin_buf: The buffer to place args' binary value
3268 * @size: The size of the buffer(by words(32bits), not characters)
3269 * @fmt: The format string to use
3270 * @...: Arguments for the format string
3271 *
3272 * The function returns the number of words(u32) written
3273 * into @bin_buf.
3274 */
3275int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3276{
3277 va_list args;
3278 int ret;
3279
3280 va_start(args, fmt);
3281 ret = vbin_printf(bin_buf, size, fmt, args);
3282 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003283
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003284 return ret;
3285}
3286EXPORT_SYMBOL_GPL(bprintf);
3287
3288#endif /* CONFIG_BINARY_PRINTF */
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290/**
3291 * vsscanf - Unformat a buffer into a list of arguments
3292 * @buf: input buffer
3293 * @fmt: format of buffer
3294 * @args: arguments
3295 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003296int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
3298 const char *str = buf;
3299 char *next;
3300 char digit;
3301 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003302 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003303 unsigned int base;
3304 union {
3305 long long s;
3306 unsigned long long u;
3307 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003308 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003309 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Jan Beulichda990752012-10-04 17:13:24 -07003311 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 /* skip any white space in format */
3313 /* white space in format matchs any amount of
3314 * white space, including none, in the input.
3315 */
3316 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003317 fmt = skip_spaces(++fmt);
3318 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320
3321 /* anything that is not a conversion must match exactly */
3322 if (*fmt != '%' && *fmt) {
3323 if (*fmt++ != *str++)
3324 break;
3325 continue;
3326 }
3327
3328 if (!*fmt)
3329 break;
3330 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 /* skip this conversion.
3333 * advance both strings to next white space
3334 */
3335 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003336 if (!*str)
3337 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003338 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3339 /* '%*[' not yet supported, invalid format */
3340 if (*fmt == '[')
3341 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 while (!isspace(*str) && *str)
3345 str++;
3346 continue;
3347 }
3348
3349 /* get field width */
3350 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003351 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003353 if (field_width <= 0)
3354 break;
3355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 /* get conversion qualifier */
3358 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003359 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003360 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 qualifier = *fmt++;
3362 if (unlikely(qualifier == *fmt)) {
3363 if (qualifier == 'h') {
3364 qualifier = 'H';
3365 fmt++;
3366 } else if (qualifier == 'l') {
3367 qualifier = 'L';
3368 fmt++;
3369 }
3370 }
3371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Jan Beulichda990752012-10-04 17:13:24 -07003373 if (!*fmt)
3374 break;
3375
3376 if (*fmt == 'n') {
3377 /* return number of characters read so far */
3378 *va_arg(args, int *) = str - buf;
3379 ++fmt;
3380 continue;
3381 }
3382
3383 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 break;
3385
André Goddard Rosad4be1512009-12-14 18:00:59 -08003386 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003387 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003388
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003389 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 case 'c':
3391 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003392 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 if (field_width == -1)
3394 field_width = 1;
3395 do {
3396 *s++ = *str++;
3397 } while (--field_width > 0 && *str);
3398 num++;
3399 }
3400 continue;
3401 case 's':
3402 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003403 char *s = (char *)va_arg(args, char *);
3404 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003405 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003407 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003410 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 *s = '\0';
3413 num++;
3414 }
3415 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003416 /*
3417 * Warning: This implementation of the '[' conversion specifier
3418 * deviates from its glibc counterpart in the following ways:
3419 * (1) It does NOT support ranges i.e. '-' is NOT a special
3420 * character
3421 * (2) It cannot match the closing bracket ']' itself
3422 * (3) A field width is required
3423 * (4) '%*[' (discard matching input) is currently not supported
3424 *
3425 * Example usage:
3426 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3427 * buf1, buf2, buf3);
3428 * if (ret < 3)
3429 * // etc..
3430 */
3431 case '[':
3432 {
3433 char *s = (char *)va_arg(args, char *);
3434 DECLARE_BITMAP(set, 256) = {0};
3435 unsigned int len = 0;
3436 bool negate = (*fmt == '^');
3437
3438 /* field width is required */
3439 if (field_width == -1)
3440 return num;
3441
3442 if (negate)
3443 ++fmt;
3444
3445 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3446 set_bit((u8)*fmt, set);
3447
3448 /* no ']' or no character set found */
3449 if (!*fmt || !len)
3450 return num;
3451 ++fmt;
3452
3453 if (negate) {
3454 bitmap_complement(set, set, 256);
3455 /* exclude null '\0' byte */
3456 clear_bit(0, set);
3457 }
3458
3459 /* match must be non-empty */
3460 if (!test_bit((u8)*str, set))
3461 return num;
3462
3463 while (test_bit((u8)*str, set) && field_width--)
3464 *s++ = *str++;
3465 *s = '\0';
3466 ++num;
3467 }
3468 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 case 'o':
3470 base = 8;
3471 break;
3472 case 'x':
3473 case 'X':
3474 base = 16;
3475 break;
3476 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003477 base = 0;
Gustavo A. R. Silva6a9dc5f2020-08-24 15:36:14 -05003478 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003480 is_sign = true;
Gustavo A. R. Silva6a9dc5f2020-08-24 15:36:14 -05003481 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 case 'u':
3483 break;
3484 case '%':
3485 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003486 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return num;
3488 continue;
3489 default:
3490 /* invalid format; stop here */
3491 return num;
3492 }
3493
3494 /* have some sort of integer conversion.
3495 * first, skip white space in buffer.
3496 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003497 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 digit = *str;
3500 if (is_sign && digit == '-')
3501 digit = *(str + 1);
3502
3503 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003504 || (base == 16 && !isxdigit(digit))
3505 || (base == 10 && !isdigit(digit))
3506 || (base == 8 && (!isdigit(digit) || digit > '7'))
3507 || (base == 0 && !isdigit(digit)))
3508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Jan Beulich53809752012-12-17 16:01:31 -08003510 if (is_sign)
Richard Fitzgerald9e914f52021-05-14 17:12:04 +01003511 val.s = simple_strntoll(str,
3512 field_width >= 0 ? field_width : INT_MAX,
3513 &next, base);
Jan Beulich53809752012-12-17 16:01:31 -08003514 else
Richard Fitzgerald9e914f52021-05-14 17:12:04 +01003515 val.u = simple_strntoull(str,
3516 field_width >= 0 ? field_width : INT_MAX,
3517 &next, base);
Jan Beulich53809752012-12-17 16:01:31 -08003518
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003519 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003521 if (is_sign)
3522 *va_arg(args, signed char *) = val.s;
3523 else
3524 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 break;
3526 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003527 if (is_sign)
3528 *va_arg(args, short *) = val.s;
3529 else
3530 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 break;
3532 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003533 if (is_sign)
3534 *va_arg(args, long *) = val.s;
3535 else
3536 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 break;
3538 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003539 if (is_sign)
3540 *va_arg(args, long long *) = val.s;
3541 else
3542 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003545 *va_arg(args, size_t *) = val.u;
3546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 default:
Jan Beulich53809752012-12-17 16:01:31 -08003548 if (is_sign)
3549 *va_arg(args, int *) = val.s;
3550 else
3551 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 break;
3553 }
3554 num++;
3555
3556 if (!next)
3557 break;
3558 str = next;
3559 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 return num;
3562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563EXPORT_SYMBOL(vsscanf);
3564
3565/**
3566 * sscanf - Unformat a buffer into a list of arguments
3567 * @buf: input buffer
3568 * @fmt: formatting of buffer
3569 * @...: resulting arguments
3570 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003571int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
3573 va_list args;
3574 int i;
3575
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003576 va_start(args, fmt);
3577 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 return i;
3581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582EXPORT_SYMBOL(sscanf);