blob: 7c47ad52ce2f7448cda5da17cb2bf25527b0eb12 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/lib/vsprintf.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9/*
10 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 */
12
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080013/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
15 * - changed to provide snprintf and vsnprintf functions
16 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
17 * - scnprintf and vscnprintf
18 */
19
20#include <stdarg.h>
Rasmus Villemoesef27ac12019-03-07 16:27:03 -080021#include <linux/build_bug.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070022#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070023#include <linux/clk-provider.h>
Rasmus Villemoes57f56772019-10-15 21:07:05 +020024#include <linux/errname.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050025#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/types.h>
27#include <linux/string.h>
28#include <linux/ctype.h>
29#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070030#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080031#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070032#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110033#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040034#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080035#include <linux/cred.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020036#include <linux/rtc.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070037#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020038#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000039#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110040#include <linux/siphash.h>
41#include <linux/compiler.h>
Sakari Ailusa92eb762019-10-03 15:32:16 +030042#include <linux/property.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040043#ifdef CONFIG_BLOCK
44#include <linux/blkdev.h>
45#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070047#include "../mm/internal.h" /* For the trace_print_flags arrays */
48
Tim Schmielau4e57b682005-10-30 15:03:48 -080049#include <asm/page.h> /* for PAGE_SIZE */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070050#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Andy Shevchenko71dca952014-10-13 15:55:18 -070052#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070053#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * simple_strtoull - convert a string to an unsigned long long
57 * @cp: The start of the string
58 * @endp: A pointer to the end of the parsed string will be placed here
59 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080060 *
61 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Harvey Harrison22d27052008-10-16 13:40:35 -070063unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070065 unsigned long long result;
66 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070068 cp = _parse_integer_fixup_radix(cp, &base);
69 rv = _parse_integer(cp, base, &result);
70 /* FIXME */
71 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (endp)
74 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 return result;
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078EXPORT_SYMBOL(simple_strtoull);
79
80/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080081 * simple_strtoul - convert a string to an unsigned long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080085 *
86 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080087 */
88unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
89{
90 return simple_strtoull(cp, endp, base);
91}
92EXPORT_SYMBOL(simple_strtoul);
93
94/**
95 * simple_strtol - convert a string to a signed long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080099 *
100 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -0800101 */
102long simple_strtol(const char *cp, char **endp, unsigned int base)
103{
104 if (*cp == '-')
105 return -simple_strtoul(cp + 1, endp, base);
106
107 return simple_strtoul(cp, endp, base);
108}
109EXPORT_SYMBOL(simple_strtol);
110
111/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * simple_strtoll - convert a string to a signed long long
113 * @cp: The start of the string
114 * @endp: A pointer to the end of the parsed string will be placed here
115 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800116 *
117 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700119long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800121 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700122 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800123
Harvey Harrison22d27052008-10-16 13:40:35 -0700124 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400126EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Joe Perchescf3b4292010-05-24 14:33:16 -0700128static noinline_for_stack
129int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800131 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800133 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800135 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return i;
138}
139
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700140/*
141 * Decimal conversion is by far the most typical, and is used for
142 * /proc and /sys data. This directly impacts e.g. top performance
143 * with many processes running. We optimize it for speed by emitting
144 * two characters at a time, using a 200 byte lookup table. This
145 * roughly halves the number of multiplications compared to computing
146 * the digits one at a time. Implementation strongly inspired by the
147 * previous version, which in turn used ideas described at
148 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
149 * from the author, Douglas W. Jones).
150 *
151 * It turns out there is precisely one 26 bit fixed-point
152 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
153 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
154 * range happens to be somewhat larger (x <= 1073741898), but that's
155 * irrelevant for our purpose.
156 *
157 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
158 * need a 32x32->64 bit multiply, so we simply use the same constant.
159 *
160 * For dividing a number in the range [100, 10^4-1] by 100, there are
161 * several options. The simplest is (x * 0x147b) >> 19, which is valid
162 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700163 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700164
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700165static const u16 decpair[100] = {
166#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
167 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
168 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
169 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
170 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
171 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
172 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
173 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
174 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
175 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
176 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
177#undef _
178};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700179
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700180/*
181 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700182 * immediately jump to out_r where two 0s would be written but only
183 * one of them accounted for in buf. This is needed by ip4_string
184 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700185*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700186static noinline_for_stack
187char *put_dec_trunc8(char *buf, unsigned r)
188{
189 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700190
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700191 /* 1 <= r < 10^8 */
192 if (r < 100)
193 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700194
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700195 /* 100 <= r < 10^8 */
196 q = (r * (u64)0x28f5c29) >> 32;
197 *((u16 *)buf) = decpair[r - 100*q];
198 buf += 2;
199
200 /* 1 <= q < 10^6 */
201 if (q < 100)
202 goto out_q;
203
204 /* 100 <= q < 10^6 */
205 r = (q * (u64)0x28f5c29) >> 32;
206 *((u16 *)buf) = decpair[q - 100*r];
207 buf += 2;
208
209 /* 1 <= r < 10^4 */
210 if (r < 100)
211 goto out_r;
212
213 /* 100 <= r < 10^4 */
214 q = (r * 0x147b) >> 19;
215 *((u16 *)buf) = decpair[r - 100*q];
216 buf += 2;
217out_q:
218 /* 1 <= q < 100 */
219 r = q;
220out_r:
221 /* 1 <= r < 100 */
222 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700223 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700224 return buf;
225}
226
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700227#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
228static noinline_for_stack
229char *put_dec_full8(char *buf, unsigned r)
230{
231 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700232
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700233 /* 0 <= r < 10^8 */
234 q = (r * (u64)0x28f5c29) >> 32;
235 *((u16 *)buf) = decpair[r - 100*q];
236 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700237
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700238 /* 0 <= q < 10^6 */
239 r = (q * (u64)0x28f5c29) >> 32;
240 *((u16 *)buf) = decpair[q - 100*r];
241 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700242
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700243 /* 0 <= r < 10^4 */
244 q = (r * 0x147b) >> 19;
245 *((u16 *)buf) = decpair[r - 100*q];
246 buf += 2;
247
248 /* 0 <= q < 100 */
249 *((u16 *)buf) = decpair[q];
250 buf += 2;
251 return buf;
252}
253
254static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700255char *put_dec(char *buf, unsigned long long n)
256{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700257 if (n >= 100*1000*1000)
258 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
259 /* 1 <= n <= 1.6e11 */
260 if (n >= 100*1000*1000)
261 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
262 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700263 return put_dec_trunc8(buf, n);
264}
265
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700266#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700267
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700268static void
269put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700270{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700271 unsigned q;
272
273 /* 0 <= r < 10^4 */
274 q = (r * 0x147b) >> 19;
275 *((u16 *)buf) = decpair[r - 100*q];
276 buf += 2;
277 /* 0 <= q < 100 */
278 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700279}
280
281/*
282 * Call put_dec_full4 on x % 10000, return x / 10000.
283 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
284 * holds for all x < 1,128,869,999. The largest value this
285 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700286 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700287 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700288static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700289unsigned put_dec_helper4(char *buf, unsigned x)
290{
291 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
292
293 put_dec_full4(buf, x - q * 10000);
294 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700295}
296
297/* Based on code by Douglas W. Jones found at
298 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
299 * (with permission from the author).
300 * Performs no 64-bit division and hence should be fast on 32-bit machines.
301 */
302static
303char *put_dec(char *buf, unsigned long long n)
304{
305 uint32_t d3, d2, d1, q, h;
306
307 if (n < 100*1000*1000)
308 return put_dec_trunc8(buf, n);
309
310 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
311 h = (n >> 32);
312 d2 = (h ) & 0xffff;
313 d3 = (h >> 16); /* implicit "& 0xffff" */
314
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700315 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
316 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700317 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700318 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700319
George Spelvin23591722012-10-04 17:12:29 -0700320 q += 7671 * d3 + 9496 * d2 + 6 * d1;
321 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700322
George Spelvin23591722012-10-04 17:12:29 -0700323 q += 4749 * d3 + 42 * d2;
324 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700325
George Spelvin23591722012-10-04 17:12:29 -0700326 q += 281 * d3;
327 buf += 12;
328 if (q)
329 buf = put_dec_trunc8(buf, q);
330 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700331 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800332
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700333 return buf;
334}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700335
336#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700337
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700338/*
339 * Convert passed number to decimal string.
340 * Returns the length of string. On buffer overflow, returns 0.
341 *
342 * If speed is not important, use snprintf(). It's easy to read the code.
343 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700344int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700345{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700346 /* put_dec requires 2-byte alignment of the buffer. */
347 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700348 int idx, len;
349
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700350 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
351 if (num <= 9) {
352 tmp[0] = '0' + num;
353 len = 1;
354 } else {
355 len = put_dec(tmp, num) - tmp;
356 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700357
Andrei Vagind1be35c2018-04-10 16:31:16 -0700358 if (len > size || width > size)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700359 return 0;
Andrei Vagind1be35c2018-04-10 16:31:16 -0700360
361 if (width > len) {
362 width = width - len;
363 for (idx = 0; idx < width; idx++)
364 buf[idx] = ' ';
365 } else {
366 width = 0;
367 }
368
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700369 for (idx = 0; idx < len; ++idx)
Andrei Vagind1be35c2018-04-10 16:31:16 -0700370 buf[idx + width] = tmp[len - idx - 1];
371
372 return len + width;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700373}
374
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700375#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700376#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#define PLUS 4 /* show plus */
378#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700379#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700380#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
381#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100383enum format_type {
384 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100385 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100386 FORMAT_TYPE_PRECISION,
387 FORMAT_TYPE_CHAR,
388 FORMAT_TYPE_STR,
389 FORMAT_TYPE_PTR,
390 FORMAT_TYPE_PERCENT_CHAR,
391 FORMAT_TYPE_INVALID,
392 FORMAT_TYPE_LONG_LONG,
393 FORMAT_TYPE_ULONG,
394 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800395 FORMAT_TYPE_UBYTE,
396 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100397 FORMAT_TYPE_USHORT,
398 FORMAT_TYPE_SHORT,
399 FORMAT_TYPE_UINT,
400 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100401 FORMAT_TYPE_SIZE_T,
402 FORMAT_TYPE_PTRDIFF
403};
404
405struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800406 unsigned int type:8; /* format_type enum */
407 signed int field_width:24; /* width of output field */
408 unsigned int flags:8; /* flags to number() */
409 unsigned int base:8; /* number base, 8, 10 or 16 only */
410 signed int precision:16; /* # of digits/chars */
411} __packed;
Rasmus Villemoesef27ac12019-03-07 16:27:03 -0800412static_assert(sizeof(struct printf_spec) == 8);
413
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800414#define FIELD_WIDTH_MAX ((1 << 23) - 1)
415#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100416
Joe Perchescf3b4292010-05-24 14:33:16 -0700417static noinline_for_stack
418char *number(char *buf, char *end, unsigned long long num,
419 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700421 /* put_dec requires 2-byte alignment of the buffer. */
422 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100423 char sign;
424 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100425 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700427 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800428 int field_width = spec.field_width;
429 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100431 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
432 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100433 locase = (spec.flags & SMALL);
434 if (spec.flags & LEFT)
435 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100437 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800438 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800440 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800441 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100442 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800444 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100445 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800447 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100451 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800452 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700453 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800454 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700456
457 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700459 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700460 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100461 else if (spec.base != 10) { /* 8 or 16 */
462 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700463 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800464
465 if (spec.base == 16)
466 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700467 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700468 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700469 num >>= shift;
470 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700471 } else { /* base 10 */
472 i = put_dec(tmp, num) - tmp;
473 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700474
475 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800476 if (i > precision)
477 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700478 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800479 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700480 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800481 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700482 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *buf = ' ';
484 ++buf;
485 }
486 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700487 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700489 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *buf = sign;
491 ++buf;
492 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700493 /* "0x" / "0" prefix */
494 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700495 if (spec.base == 16 || !is_zero) {
496 if (buf < end)
497 *buf = '0';
498 ++buf;
499 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100500 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700501 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100502 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ++buf;
504 }
505 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700506 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100507 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700508 char c = ' ' + (spec.flags & ZEROPAD);
509 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800510 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700511 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 *buf = c;
513 ++buf;
514 }
515 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700516 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800517 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700518 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *buf = '0';
520 ++buf;
521 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700522 /* actual digits of result */
523 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700524 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 *buf = tmp[i];
526 ++buf;
527 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700528 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800529 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700530 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *buf = ' ';
532 ++buf;
533 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return buf;
536}
537
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800538static noinline_for_stack
539char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
540{
541 struct printf_spec spec;
542
543 spec.type = FORMAT_TYPE_PTR;
544 spec.field_width = 2 + 2 * size; /* 0x + hex */
545 spec.flags = SPECIAL | SMALL | ZEROPAD;
546 spec.base = 16;
547 spec.precision = -1;
548
549 return number(buf, end, num, spec);
550}
551
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800552static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400553{
554 size_t size;
555 if (buf >= end) /* nowhere to put anything */
556 return;
557 size = end - buf;
558 if (size <= spaces) {
559 memset(buf, ' ', size);
560 return;
561 }
562 if (len) {
563 if (len > size - spaces)
564 len = size - spaces;
565 memmove(buf + spaces, buf, len);
566 }
567 memset(buf, ' ', spaces);
568}
569
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800570/*
571 * Handle field width padding for a string.
572 * @buf: current buffer position
573 * @n: length of string
574 * @end: end of output buffer
575 * @spec: for field width and flags
576 * Returns: new buffer position after padding.
577 */
578static noinline_for_stack
579char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
580{
581 unsigned spaces;
582
583 if (likely(n >= spec.field_width))
584 return buf;
585 /* we want to pad the sucker */
586 spaces = spec.field_width - n;
587 if (!(spec.flags & LEFT)) {
588 move_right(buf - n, end, n, spaces);
589 return buf + spaces;
590 }
591 while (spaces--) {
592 if (buf < end)
593 *buf = ' ';
594 ++buf;
595 }
596 return buf;
597}
598
Petr Mladekd529ac42019-04-17 13:53:43 +0200599/* Handle string from a well known address. */
600static char *string_nocheck(char *buf, char *end, const char *s,
601 struct printf_spec spec)
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800602{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800603 int len = 0;
Youngmin Namb314dd42019-06-10 16:47:07 +0900604 int lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800605
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800606 while (lim--) {
607 char c = *s++;
608 if (!c)
609 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800610 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800611 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800612 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800613 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800614 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800615 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800616}
617
Rasmus Villemoes57f56772019-10-15 21:07:05 +0200618static char *err_ptr(char *buf, char *end, void *ptr,
619 struct printf_spec spec)
620{
621 int err = PTR_ERR(ptr);
622 const char *sym = errname(err);
623
624 if (sym)
625 return string_nocheck(buf, end, sym, spec);
626
627 /*
628 * Somebody passed ERR_PTR(-1234) or some other non-existing
629 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
630 * printing it as its decimal representation.
631 */
632 spec.flags |= SIGN;
633 spec.base = 10;
634 return number(buf, end, err, spec);
635}
636
Petr Mladekc8c3b582019-04-17 13:53:50 +0200637/* Be careful: error messages must fit into the given buffer. */
638static char *error_string(char *buf, char *end, const char *s,
639 struct printf_spec spec)
640{
641 /*
642 * Hard limit to avoid a completely insane messages. It actually
643 * works pretty well because most error messages are in
644 * the many pointer format modifiers.
645 */
646 if (spec.precision == -1)
647 spec.precision = 2 * sizeof(void *);
648
649 return string_nocheck(buf, end, s, spec);
650}
651
Petr Mladek3e5903e2019-04-17 13:53:48 +0200652/*
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200653 * Do not call any complex external code here. Nested printk()/vsprintf()
654 * might cause infinite loops. Failures might break printk() and would
655 * be hard to debug.
Petr Mladek3e5903e2019-04-17 13:53:48 +0200656 */
657static const char *check_pointer_msg(const void *ptr)
658{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200659 if (!ptr)
660 return "(null)";
661
Petr Mladek2ac5a3b2019-05-10 10:42:13 +0200662 if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
Petr Mladek3e5903e2019-04-17 13:53:48 +0200663 return "(efault)";
664
665 return NULL;
666}
667
668static int check_pointer(char **buf, char *end, const void *ptr,
669 struct printf_spec spec)
670{
671 const char *err_msg;
672
673 err_msg = check_pointer_msg(ptr);
674 if (err_msg) {
Petr Mladekc8c3b582019-04-17 13:53:50 +0200675 *buf = error_string(*buf, end, err_msg, spec);
Petr Mladek3e5903e2019-04-17 13:53:48 +0200676 return -EFAULT;
677 }
678
679 return 0;
680}
681
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800682static noinline_for_stack
Petr Mladekd529ac42019-04-17 13:53:43 +0200683char *string(char *buf, char *end, const char *s,
684 struct printf_spec spec)
685{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200686 if (check_pointer(&buf, end, s, spec))
687 return buf;
Petr Mladekd529ac42019-04-17 13:53:43 +0200688
689 return string_nocheck(buf, end, s, spec);
690}
691
YueHaibingce9d3ec2019-04-27 00:46:30 +0800692static char *pointer_string(char *buf, char *end,
693 const void *ptr,
694 struct printf_spec spec)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200695{
696 spec.base = 16;
697 spec.flags |= SMALL;
698 if (spec.field_width == -1) {
699 spec.field_width = 2 * sizeof(ptr);
700 spec.flags |= ZEROPAD;
701 }
702
703 return number(buf, end, (unsigned long int)ptr, spec);
704}
705
706/* Make pointers available for printing early in the boot sequence. */
707static int debug_boot_weak_hash __ro_after_init;
708
709static int __init debug_boot_weak_hash_enable(char *str)
710{
711 debug_boot_weak_hash = 1;
712 pr_info("debug_boot_weak_hash enabled\n");
713 return 0;
714}
715early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
716
717static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
718static siphash_key_t ptr_key __read_mostly;
719
720static void enable_ptr_key_workfn(struct work_struct *work)
721{
722 get_random_bytes(&ptr_key, sizeof(ptr_key));
723 /* Needs to run from preemptible context */
724 static_branch_disable(&not_filled_random_ptr_key);
725}
726
727static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
728
729static void fill_random_ptr_key(struct random_ready_callback *unused)
730{
731 /* This may be in an interrupt handler. */
732 queue_work(system_unbound_wq, &enable_ptr_key_work);
733}
734
735static struct random_ready_callback random_ready = {
736 .func = fill_random_ptr_key
737};
738
739static int __init initialize_ptr_random(void)
740{
741 int key_size = sizeof(ptr_key);
742 int ret;
743
744 /* Use hw RNG if available. */
745 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
746 static_branch_disable(&not_filled_random_ptr_key);
747 return 0;
748 }
749
750 ret = add_random_ready_callback(&random_ready);
751 if (!ret) {
752 return 0;
753 } else if (ret == -EALREADY) {
754 /* This is in preemptible context */
755 enable_ptr_key_workfn(&enable_ptr_key_work);
756 return 0;
757 }
758
759 return ret;
760}
761early_initcall(initialize_ptr_random);
762
763/* Maps a pointer to a 32 bit unique identifier. */
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800764static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200765{
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200766 unsigned long hashval;
767
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800768 if (static_branch_unlikely(&not_filled_random_ptr_key))
769 return -EAGAIN;
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200770
771#ifdef CONFIG_64BIT
772 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
773 /*
774 * Mask off the first 32 bits, this makes explicit that we have
775 * modified the address (and 32 bits is plenty for a unique ID).
776 */
777 hashval = hashval & 0xffffffff;
778#else
779 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
780#endif
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800781 *hashval_out = hashval;
782 return 0;
783}
784
785int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
786{
787 return __ptr_to_hashval(ptr, hashval_out);
788}
789
790static char *ptr_to_id(char *buf, char *end, const void *ptr,
791 struct printf_spec spec)
792{
793 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
794 unsigned long hashval;
795 int ret;
796
Ilya Dryomov7bd57fb2020-05-19 13:26:57 +0200797 /*
798 * Print the real pointer value for NULL and error pointers,
799 * as they are not actual addresses.
800 */
801 if (IS_ERR_OR_NULL(ptr))
802 return pointer_string(buf, end, ptr, spec);
803
Joel Fernandes (Google)e4dcad22019-11-30 17:50:33 -0800804 /* When debugging early boot use non-cryptographically secure hash. */
805 if (unlikely(debug_boot_weak_hash)) {
806 hashval = hash_long((unsigned long)ptr, 32);
807 return pointer_string(buf, end, (const void *)hashval, spec);
808 }
809
810 ret = __ptr_to_hashval(ptr, &hashval);
811 if (ret) {
812 spec.field_width = 2 * sizeof(ptr);
813 /* string length must be less than default_width */
814 return error_string(buf, end, str, spec);
815 }
816
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200817 return pointer_string(buf, end, (const void *)hashval, spec);
818}
819
Petr Mladek6eea2422019-04-17 13:53:41 +0200820int kptr_restrict __read_mostly;
821
822static noinline_for_stack
823char *restricted_pointer(char *buf, char *end, const void *ptr,
824 struct printf_spec spec)
825{
826 switch (kptr_restrict) {
827 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200828 /* Handle as %p, hash and do _not_ leak addresses. */
829 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200830 case 1: {
831 const struct cred *cred;
832
833 /*
834 * kptr_restrict==1 cannot be used in IRQ context
835 * because its test for CAP_SYSLOG would be meaningless.
836 */
837 if (in_irq() || in_serving_softirq() || in_nmi()) {
838 if (spec.field_width == -1)
839 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200840 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200841 }
842
843 /*
844 * Only print the real pointer value if the current
845 * process has CAP_SYSLOG and is running with the
846 * same credentials it started with. This is because
847 * access to files is checked at open() time, but %pK
848 * checks permission at read() time. We don't want to
849 * leak pointer values if a binary opens a file using
850 * %pK and then elevates privileges before reading it.
851 */
852 cred = current_cred();
853 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
854 !uid_eq(cred->euid, cred->uid) ||
855 !gid_eq(cred->egid, cred->gid))
856 ptr = NULL;
857 break;
858 }
859 case 2:
860 default:
861 /* Always print 0's for %pK */
862 ptr = NULL;
863 break;
864 }
865
866 return pointer_string(buf, end, ptr, spec);
867}
868
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200869static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400870char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
871 const char *fmt)
872{
873 const char *array[4], *s;
874 const struct dentry *p;
875 int depth;
876 int i, n;
877
878 switch (fmt[1]) {
879 case '2': case '3': case '4':
880 depth = fmt[1] - '0';
881 break;
882 default:
883 depth = 1;
884 }
885
886 rcu_read_lock();
887 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200888 if (check_pointer(&buf, end, d, spec)) {
889 rcu_read_unlock();
890 return buf;
891 }
892
Mark Rutland6aa7de02017-10-23 14:07:29 -0700893 p = READ_ONCE(d->d_parent);
894 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400895 if (p == d) {
896 if (i)
897 array[i] = "";
898 i++;
899 break;
900 }
901 }
902 s = array[--i];
903 for (n = 0; n != spec.precision; n++, buf++) {
904 char c = *s++;
905 if (!c) {
906 if (!i)
907 break;
908 c = '/';
909 s = array[--i];
910 }
911 if (buf < end)
912 *buf = c;
913 }
914 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800915 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400916}
917
Jia He36594b32019-08-09 09:24:56 +0800918static noinline_for_stack
919char *file_dentry_name(char *buf, char *end, const struct file *f,
920 struct printf_spec spec, const char *fmt)
921{
922 if (check_pointer(&buf, end, f, spec))
923 return buf;
924
925 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
926}
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400927#ifdef CONFIG_BLOCK
928static noinline_for_stack
929char *bdev_name(char *buf, char *end, struct block_device *bdev,
930 struct printf_spec spec, const char *fmt)
931{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200932 struct gendisk *hd;
933
934 if (check_pointer(&buf, end, bdev, spec))
935 return buf;
936
937 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400938 buf = string(buf, end, hd->disk_name, spec);
939 if (bdev->bd_part->partno) {
940 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
941 if (buf < end)
942 *buf = 'p';
943 buf++;
944 }
945 buf = number(buf, end, bdev->bd_part->partno, spec);
946 }
947 return buf;
948}
949#endif
950
Joe Perchescf3b4292010-05-24 14:33:16 -0700951static noinline_for_stack
952char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800953 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700954{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800955 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700956#ifdef CONFIG_KALLSYMS
957 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800958#endif
959
960 if (fmt[1] == 'R')
961 ptr = __builtin_extract_return_addr(ptr);
962 value = (unsigned long)ptr;
963
964#ifdef CONFIG_KALLSYMS
965 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900966 sprint_backtrace(sym, value);
Sakari Ailus9af77062019-10-03 15:32:14 +0300967 else if (*fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200968 sprint_symbol(sym, value);
969 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700970 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800971
Petr Mladekd529ac42019-04-17 13:53:43 +0200972 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700973#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800974 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700975#endif
976}
977
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200978static const struct printf_spec default_str_spec = {
979 .field_width = -1,
980 .precision = -1,
981};
982
Andy Shevchenko54433972018-02-16 23:07:06 +0200983static const struct printf_spec default_flag_spec = {
984 .base = 16,
985 .precision = -1,
986 .flags = SPECIAL | SMALL,
987};
988
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200989static const struct printf_spec default_dec_spec = {
990 .base = 10,
991 .precision = -1,
992};
993
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200994static const struct printf_spec default_dec02_spec = {
995 .base = 10,
996 .field_width = 2,
997 .precision = -1,
998 .flags = ZEROPAD,
999};
1000
1001static const struct printf_spec default_dec04_spec = {
1002 .base = 10,
1003 .field_width = 4,
1004 .precision = -1,
1005 .flags = ZEROPAD,
1006};
1007
Joe Perchescf3b4292010-05-24 14:33:16 -07001008static noinline_for_stack
1009char *resource_string(char *buf, char *end, struct resource *res,
1010 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +11001011{
1012#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -06001013#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +11001014#endif
1015
1016#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -06001017#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +11001018#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001019 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001020 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001021 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001022 .precision = -1,
1023 .flags = SPECIAL | SMALL | ZEROPAD,
1024 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001025 static const struct printf_spec mem_spec = {
1026 .base = 16,
1027 .field_width = MEM_RSRC_PRINTK_SIZE,
1028 .precision = -1,
1029 .flags = SPECIAL | SMALL | ZEROPAD,
1030 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001031 static const struct printf_spec bus_spec = {
1032 .base = 16,
1033 .field_width = 2,
1034 .precision = -1,
1035 .flags = SMALL | ZEROPAD,
1036 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001037 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001038 .field_width = -1,
1039 .precision = 10,
1040 .flags = LEFT,
1041 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001042
1043 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1044 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1045#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1046#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001047#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001048#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1049 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1050 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1051
Linus Torvalds332d2e72008-10-20 15:07:34 +11001052 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001053 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001054 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +11001055
Petr Mladek3e5903e2019-04-17 13:53:48 +02001056 if (check_pointer(&buf, end, res, spec))
1057 return buf;
1058
Linus Torvalds332d2e72008-10-20 15:07:34 +11001059 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001060 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001061 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001062 specp = &io_spec;
1063 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001064 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001065 specp = &mem_spec;
1066 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001067 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001068 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001069 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001070 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001071 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001072 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001073 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001074 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001075 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001076 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001077 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001078 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001079 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001080 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001081 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001082 p = number(p, pend, resource_size(res), *specp);
1083 } else {
1084 p = number(p, pend, res->start, *specp);
1085 if (res->start != res->end) {
1086 *p++ = '-';
1087 p = number(p, pend, res->end, *specp);
1088 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001089 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001090 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001091 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001092 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001093 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001094 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001095 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001096 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001097 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001098 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001099 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001100 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001101 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001102 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001103 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001104 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001105
Petr Mladekd529ac42019-04-17 13:53:43 +02001106 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001107}
1108
Joe Perchescf3b4292010-05-24 14:33:16 -07001109static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001110char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1111 const char *fmt)
1112{
Steven Rostedt360603a2013-05-28 15:47:39 -04001113 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001114 negative value, fallback to the default */
1115 char separator;
1116
1117 if (spec.field_width == 0)
1118 /* nothing to print */
1119 return buf;
1120
Petr Mladek3e5903e2019-04-17 13:53:48 +02001121 if (check_pointer(&buf, end, addr, spec))
1122 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001123
1124 switch (fmt[1]) {
1125 case 'C':
1126 separator = ':';
1127 break;
1128 case 'D':
1129 separator = '-';
1130 break;
1131 case 'N':
1132 separator = 0;
1133 break;
1134 default:
1135 separator = ' ';
1136 break;
1137 }
1138
1139 if (spec.field_width > 0)
1140 len = min_t(int, spec.field_width, 64);
1141
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001142 for (i = 0; i < len; ++i) {
1143 if (buf < end)
1144 *buf = hex_asc_hi(addr[i]);
1145 ++buf;
1146 if (buf < end)
1147 *buf = hex_asc_lo(addr[i]);
1148 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001149
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001150 if (separator && i != len - 1) {
1151 if (buf < end)
1152 *buf = separator;
1153 ++buf;
1154 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001155 }
1156
1157 return buf;
1158}
1159
1160static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001161char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1162 struct printf_spec spec, const char *fmt)
1163{
1164 const int CHUNKSZ = 32;
1165 int nr_bits = max_t(int, spec.field_width, 0);
1166 int i, chunksz;
1167 bool first = true;
1168
Petr Mladek3e5903e2019-04-17 13:53:48 +02001169 if (check_pointer(&buf, end, bitmap, spec))
1170 return buf;
1171
Tejun Heodbc760b2015-02-13 14:36:53 -08001172 /* reused to print numbers */
1173 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1174
1175 chunksz = nr_bits & (CHUNKSZ - 1);
1176 if (chunksz == 0)
1177 chunksz = CHUNKSZ;
1178
1179 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1180 for (; i >= 0; i -= CHUNKSZ) {
1181 u32 chunkmask, val;
1182 int word, bit;
1183
1184 chunkmask = ((1ULL << chunksz) - 1);
1185 word = i / BITS_PER_LONG;
1186 bit = i % BITS_PER_LONG;
1187 val = (bitmap[word] >> bit) & chunkmask;
1188
1189 if (!first) {
1190 if (buf < end)
1191 *buf = ',';
1192 buf++;
1193 }
1194 first = false;
1195
1196 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1197 buf = number(buf, end, val, spec);
1198
1199 chunksz = CHUNKSZ;
1200 }
1201 return buf;
1202}
1203
1204static noinline_for_stack
1205char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1206 struct printf_spec spec, const char *fmt)
1207{
1208 int nr_bits = max_t(int, spec.field_width, 0);
1209 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1210 int cur, rbot, rtop;
1211 bool first = true;
1212
Petr Mladek3e5903e2019-04-17 13:53:48 +02001213 if (check_pointer(&buf, end, bitmap, spec))
1214 return buf;
1215
Tejun Heodbc760b2015-02-13 14:36:53 -08001216 rbot = cur = find_first_bit(bitmap, nr_bits);
1217 while (cur < nr_bits) {
1218 rtop = cur;
1219 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1220 if (cur < nr_bits && cur <= rtop + 1)
1221 continue;
1222
1223 if (!first) {
1224 if (buf < end)
1225 *buf = ',';
1226 buf++;
1227 }
1228 first = false;
1229
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001230 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001231 if (rbot < rtop) {
1232 if (buf < end)
1233 *buf = '-';
1234 buf++;
1235
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001236 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001237 }
1238
1239 rbot = cur;
1240 }
1241 return buf;
1242}
1243
1244static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001245char *mac_address_string(char *buf, char *end, u8 *addr,
1246 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001247{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001248 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001249 char *p = mac_addr;
1250 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001251 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001252 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001253
Petr Mladek3e5903e2019-04-17 13:53:48 +02001254 if (check_pointer(&buf, end, addr, spec))
1255 return buf;
1256
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001257 switch (fmt[1]) {
1258 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001259 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001260 break;
1261
1262 case 'R':
1263 reversed = true;
1264 /* fall through */
1265
1266 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001267 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001268 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001269 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001270
1271 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001272 if (reversed)
1273 p = hex_byte_pack(p, addr[5 - i]);
1274 else
1275 p = hex_byte_pack(p, addr[i]);
1276
Joe Perches8a27f7c2009-08-17 12:29:44 +00001277 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001278 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001279 }
1280 *p = '\0';
1281
Petr Mladekd529ac42019-04-17 13:53:43 +02001282 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001283}
1284
Joe Perchescf3b4292010-05-24 14:33:16 -07001285static noinline_for_stack
1286char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001287{
Harvey Harrison689afa72008-10-28 16:04:44 -07001288 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001289 bool leading_zeros = (fmt[0] == 'i');
1290 int index;
1291 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001292
Joe Perches0159f242010-01-13 20:23:30 -08001293 switch (fmt[2]) {
1294 case 'h':
1295#ifdef __BIG_ENDIAN
1296 index = 0;
1297 step = 1;
1298#else
1299 index = 3;
1300 step = -1;
1301#endif
1302 break;
1303 case 'l':
1304 index = 3;
1305 step = -1;
1306 break;
1307 case 'n':
1308 case 'b':
1309 default:
1310 index = 0;
1311 step = 1;
1312 break;
1313 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001314 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001315 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001316 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001317 if (leading_zeros) {
1318 if (digits < 3)
1319 *p++ = '0';
1320 if (digits < 2)
1321 *p++ = '0';
1322 }
1323 /* reverse the digits in the quad */
1324 while (digits--)
1325 *p++ = temp[digits];
1326 if (i < 3)
1327 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001328 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001329 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001330 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001331
Joe Perches8a27f7c2009-08-17 12:29:44 +00001332 return p;
1333}
1334
Joe Perchescf3b4292010-05-24 14:33:16 -07001335static noinline_for_stack
1336char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001337{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001338 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001339 unsigned char zerolength[8];
1340 int longest = 1;
1341 int colonpos = -1;
1342 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001343 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001344 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001345 bool useIPv4;
1346 struct in6_addr in6;
1347
1348 memcpy(&in6, addr, sizeof(struct in6_addr));
1349
1350 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001351
1352 memset(zerolength, 0, sizeof(zerolength));
1353
1354 if (useIPv4)
1355 range = 6;
1356 else
1357 range = 8;
1358
1359 /* find position of longest 0 run */
1360 for (i = 0; i < range; i++) {
1361 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001362 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001363 break;
1364 zerolength[i]++;
1365 }
1366 }
1367 for (i = 0; i < range; i++) {
1368 if (zerolength[i] > longest) {
1369 longest = zerolength[i];
1370 colonpos = i;
1371 }
1372 }
Joe Perches29cf5192011-06-09 11:23:37 -07001373 if (longest == 1) /* don't compress a single 0 */
1374 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001375
1376 /* emit address */
1377 for (i = 0; i < range; i++) {
1378 if (i == colonpos) {
1379 if (needcolon || i == 0)
1380 *p++ = ':';
1381 *p++ = ':';
1382 needcolon = false;
1383 i += longest - 1;
1384 continue;
1385 }
1386 if (needcolon) {
1387 *p++ = ':';
1388 needcolon = false;
1389 }
1390 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001391 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001392 hi = word >> 8;
1393 lo = word & 0xff;
1394 if (hi) {
1395 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001396 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001397 else
1398 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001399 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001400 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001401 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001402 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001403 else
1404 *p++ = hex_asc_lo(lo);
1405 needcolon = true;
1406 }
1407
1408 if (useIPv4) {
1409 if (needcolon)
1410 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001411 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001412 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001413 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001414
Joe Perches8a27f7c2009-08-17 12:29:44 +00001415 return p;
1416}
1417
Joe Perchescf3b4292010-05-24 14:33:16 -07001418static noinline_for_stack
1419char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001420{
1421 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001422
Harvey Harrison689afa72008-10-28 16:04:44 -07001423 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001424 p = hex_byte_pack(p, *addr++);
1425 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001426 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001427 *p++ = ':';
1428 }
1429 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001430
Joe Perches8a27f7c2009-08-17 12:29:44 +00001431 return p;
1432}
1433
Joe Perchescf3b4292010-05-24 14:33:16 -07001434static noinline_for_stack
1435char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1436 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001437{
1438 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1439
1440 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001441 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001442 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001443 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001444
Petr Mladekd529ac42019-04-17 13:53:43 +02001445 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001446}
1447
Joe Perchescf3b4292010-05-24 14:33:16 -07001448static noinline_for_stack
1449char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1450 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001451{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001452 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001453
Joe Perches0159f242010-01-13 20:23:30 -08001454 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001455
Petr Mladekd529ac42019-04-17 13:53:43 +02001456 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001457}
1458
Joe Perchescf3b4292010-05-24 14:33:16 -07001459static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001460char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1461 struct printf_spec spec, const char *fmt)
1462{
1463 bool have_p = false, have_s = false, have_f = false, have_c = false;
1464 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1465 sizeof(":12345") + sizeof("/123456789") +
1466 sizeof("%1234567890")];
1467 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1468 const u8 *addr = (const u8 *) &sa->sin6_addr;
1469 char fmt6[2] = { fmt[0], '6' };
1470 u8 off = 0;
1471
1472 fmt++;
1473 while (isalpha(*++fmt)) {
1474 switch (*fmt) {
1475 case 'p':
1476 have_p = true;
1477 break;
1478 case 'f':
1479 have_f = true;
1480 break;
1481 case 's':
1482 have_s = true;
1483 break;
1484 case 'c':
1485 have_c = true;
1486 break;
1487 }
1488 }
1489
1490 if (have_p || have_s || have_f) {
1491 *p = '[';
1492 off = 1;
1493 }
1494
1495 if (fmt6[0] == 'I' && have_c)
1496 p = ip6_compressed_string(ip6_addr + off, addr);
1497 else
1498 p = ip6_string(ip6_addr + off, addr, fmt6);
1499
1500 if (have_p || have_s || have_f)
1501 *p++ = ']';
1502
1503 if (have_p) {
1504 *p++ = ':';
1505 p = number(p, pend, ntohs(sa->sin6_port), spec);
1506 }
1507 if (have_f) {
1508 *p++ = '/';
1509 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1510 IPV6_FLOWINFO_MASK), spec);
1511 }
1512 if (have_s) {
1513 *p++ = '%';
1514 p = number(p, pend, sa->sin6_scope_id, spec);
1515 }
1516 *p = '\0';
1517
Petr Mladekd529ac42019-04-17 13:53:43 +02001518 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001519}
1520
1521static noinline_for_stack
1522char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1523 struct printf_spec spec, const char *fmt)
1524{
1525 bool have_p = false;
1526 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1527 char *pend = ip4_addr + sizeof(ip4_addr);
1528 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1529 char fmt4[3] = { fmt[0], '4', 0 };
1530
1531 fmt++;
1532 while (isalpha(*++fmt)) {
1533 switch (*fmt) {
1534 case 'p':
1535 have_p = true;
1536 break;
1537 case 'h':
1538 case 'l':
1539 case 'n':
1540 case 'b':
1541 fmt4[2] = *fmt;
1542 break;
1543 }
1544 }
1545
1546 p = ip4_string(ip4_addr, addr, fmt4);
1547 if (have_p) {
1548 *p++ = ':';
1549 p = number(p, pend, ntohs(sa->sin_port), spec);
1550 }
1551 *p = '\0';
1552
Petr Mladekd529ac42019-04-17 13:53:43 +02001553 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001554}
1555
1556static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001557char *ip_addr_string(char *buf, char *end, const void *ptr,
1558 struct printf_spec spec, const char *fmt)
1559{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001560 char *err_fmt_msg;
1561
Petr Mladek3e5903e2019-04-17 13:53:48 +02001562 if (check_pointer(&buf, end, ptr, spec))
1563 return buf;
1564
Petr Mladekf00cc102019-04-17 13:53:44 +02001565 switch (fmt[1]) {
1566 case '6':
1567 return ip6_addr_string(buf, end, ptr, spec, fmt);
1568 case '4':
1569 return ip4_addr_string(buf, end, ptr, spec, fmt);
1570 case 'S': {
1571 const union {
1572 struct sockaddr raw;
1573 struct sockaddr_in v4;
1574 struct sockaddr_in6 v6;
1575 } *sa = ptr;
1576
1577 switch (sa->raw.sa_family) {
1578 case AF_INET:
1579 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1580 case AF_INET6:
1581 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1582 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001583 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001584 }}
1585 }
1586
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001587 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001588 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001589}
1590
1591static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001592char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1593 const char *fmt)
1594{
1595 bool found = true;
1596 int count = 1;
1597 unsigned int flags = 0;
1598 int len;
1599
1600 if (spec.field_width == 0)
1601 return buf; /* nothing to print */
1602
Petr Mladek3e5903e2019-04-17 13:53:48 +02001603 if (check_pointer(&buf, end, addr, spec))
1604 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001605
1606 do {
1607 switch (fmt[count++]) {
1608 case 'a':
1609 flags |= ESCAPE_ANY;
1610 break;
1611 case 'c':
1612 flags |= ESCAPE_SPECIAL;
1613 break;
1614 case 'h':
1615 flags |= ESCAPE_HEX;
1616 break;
1617 case 'n':
1618 flags |= ESCAPE_NULL;
1619 break;
1620 case 'o':
1621 flags |= ESCAPE_OCTAL;
1622 break;
1623 case 'p':
1624 flags |= ESCAPE_NP;
1625 break;
1626 case 's':
1627 flags |= ESCAPE_SPACE;
1628 break;
1629 default:
1630 found = false;
1631 break;
1632 }
1633 } while (found);
1634
1635 if (!flags)
1636 flags = ESCAPE_ANY_NP;
1637
1638 len = spec.field_width < 0 ? 1 : spec.field_width;
1639
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001640 /*
1641 * string_escape_mem() writes as many characters as it can to
1642 * the given buffer, and returns the total size of the output
1643 * had the buffer been big enough.
1644 */
1645 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001646
1647 return buf;
1648}
1649
Petr Mladek3e5903e2019-04-17 13:53:48 +02001650static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1651 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001652{
1653 va_list va;
1654
Petr Mladek3e5903e2019-04-17 13:53:48 +02001655 if (check_pointer(&buf, end, va_fmt, spec))
1656 return buf;
1657
Petr Mladek45c3e932019-04-17 13:53:45 +02001658 va_copy(va, *va_fmt->va);
1659 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1660 va_end(va);
1661
1662 return buf;
1663}
1664
Andy Shevchenko71dca952014-10-13 15:55:18 -07001665static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001666char *uuid_string(char *buf, char *end, const u8 *addr,
1667 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001668{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001669 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001670 char *p = uuid;
1671 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001672 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001673 bool uc = false;
1674
Petr Mladek3e5903e2019-04-17 13:53:48 +02001675 if (check_pointer(&buf, end, addr, spec))
1676 return buf;
1677
Joe Perches9ac6e442009-12-14 18:01:09 -08001678 switch (*(++fmt)) {
1679 case 'L':
1680 uc = true; /* fall-through */
1681 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001682 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001683 break;
1684 case 'B':
1685 uc = true;
1686 break;
1687 }
1688
1689 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001690 if (uc)
1691 p = hex_byte_pack_upper(p, addr[index[i]]);
1692 else
1693 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001694 switch (i) {
1695 case 3:
1696 case 5:
1697 case 7:
1698 case 9:
1699 *p++ = '-';
1700 break;
1701 }
1702 }
1703
1704 *p = 0;
1705
Petr Mladekd529ac42019-04-17 13:53:43 +02001706 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001707}
1708
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001709static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001710char *netdev_bits(char *buf, char *end, const void *addr,
1711 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001712{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001713 unsigned long long num;
1714 int size;
1715
Petr Mladek3e5903e2019-04-17 13:53:48 +02001716 if (check_pointer(&buf, end, addr, spec))
1717 return buf;
1718
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001719 switch (fmt[1]) {
1720 case 'F':
1721 num = *(const netdev_features_t *)addr;
1722 size = sizeof(netdev_features_t);
1723 break;
1724 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001725 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001726 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001727
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001728 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001729}
1730
Joe Perchesaaf07622014-01-23 15:54:17 -08001731static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001732char *address_val(char *buf, char *end, const void *addr,
1733 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001734{
1735 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001736 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001737
Petr Mladek3e5903e2019-04-17 13:53:48 +02001738 if (check_pointer(&buf, end, addr, spec))
1739 return buf;
1740
Joe Perchesaaf07622014-01-23 15:54:17 -08001741 switch (fmt[1]) {
1742 case 'd':
1743 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001744 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001745 break;
1746 case 'p':
1747 default:
1748 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001749 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001750 break;
1751 }
1752
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001753 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001754}
1755
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001756static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001757char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1758{
1759 int year = tm->tm_year + (r ? 0 : 1900);
1760 int mon = tm->tm_mon + (r ? 0 : 1);
1761
1762 buf = number(buf, end, year, default_dec04_spec);
1763 if (buf < end)
1764 *buf = '-';
1765 buf++;
1766
1767 buf = number(buf, end, mon, default_dec02_spec);
1768 if (buf < end)
1769 *buf = '-';
1770 buf++;
1771
1772 return number(buf, end, tm->tm_mday, default_dec02_spec);
1773}
1774
1775static noinline_for_stack
1776char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1777{
1778 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1779 if (buf < end)
1780 *buf = ':';
1781 buf++;
1782
1783 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1784 if (buf < end)
1785 *buf = ':';
1786 buf++;
1787
1788 return number(buf, end, tm->tm_sec, default_dec02_spec);
1789}
1790
1791static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001792char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1793 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001794{
1795 bool have_t = true, have_d = true;
1796 bool raw = false;
1797 int count = 2;
1798
Petr Mladek3e5903e2019-04-17 13:53:48 +02001799 if (check_pointer(&buf, end, tm, spec))
1800 return buf;
1801
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001802 switch (fmt[count]) {
1803 case 'd':
1804 have_t = false;
1805 count++;
1806 break;
1807 case 't':
1808 have_d = false;
1809 count++;
1810 break;
1811 }
1812
1813 raw = fmt[count] == 'r';
1814
1815 if (have_d)
1816 buf = date_str(buf, end, tm, raw);
1817 if (have_d && have_t) {
1818 /* Respect ISO 8601 */
1819 if (buf < end)
1820 *buf = 'T';
1821 buf++;
1822 }
1823 if (have_t)
1824 buf = time_str(buf, end, tm, raw);
1825
1826 return buf;
1827}
1828
1829static noinline_for_stack
1830char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1831 const char *fmt)
1832{
1833 switch (fmt[1]) {
1834 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001835 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001836 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001837 return error_string(buf, end, "(%ptR?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001838 }
1839}
1840
1841static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001842char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1843 const char *fmt)
1844{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001845 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001846 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001847
Petr Mladek3e5903e2019-04-17 13:53:48 +02001848 if (check_pointer(&buf, end, clk, spec))
1849 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001850
1851 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001852 case 'n':
1853 default:
1854#ifdef CONFIG_COMMON_CLK
1855 return string(buf, end, __clk_get_name(clk), spec);
1856#else
Geert Uytterhoeven4ca96aa2019-07-01 16:00:09 +02001857 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001858#endif
1859 }
1860}
1861
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001862static
1863char *format_flags(char *buf, char *end, unsigned long flags,
1864 const struct trace_print_flags *names)
1865{
1866 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001867
1868 for ( ; flags && names->name; names++) {
1869 mask = names->mask;
1870 if ((flags & mask) != mask)
1871 continue;
1872
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001873 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001874
1875 flags &= ~mask;
1876 if (flags) {
1877 if (buf < end)
1878 *buf = '|';
1879 buf++;
1880 }
1881 }
1882
1883 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001884 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001885
1886 return buf;
1887}
1888
1889static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001890char *flags_string(char *buf, char *end, void *flags_ptr,
1891 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001892{
1893 unsigned long flags;
1894 const struct trace_print_flags *names;
1895
Petr Mladek3e5903e2019-04-17 13:53:48 +02001896 if (check_pointer(&buf, end, flags_ptr, spec))
1897 return buf;
1898
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001899 switch (fmt[1]) {
1900 case 'p':
1901 flags = *(unsigned long *)flags_ptr;
1902 /* Remove zone id */
1903 flags &= (1UL << NR_PAGEFLAGS) - 1;
1904 names = pageflag_names;
1905 break;
1906 case 'v':
1907 flags = *(unsigned long *)flags_ptr;
1908 names = vmaflag_names;
1909 break;
1910 case 'g':
1911 flags = *(gfp_t *)flags_ptr;
1912 names = gfpflag_names;
1913 break;
1914 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001915 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001916 }
1917
1918 return format_flags(buf, end, flags, names);
1919}
1920
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001921static noinline_for_stack
Sakari Ailusa92eb762019-10-03 15:32:16 +03001922char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1923 char *end)
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001924{
1925 int depth;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001926
Sakari Ailusa92eb762019-10-03 15:32:16 +03001927 /* Loop starting from the root node to the current node. */
1928 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1929 struct fwnode_handle *__fwnode =
1930 fwnode_get_nth_parent(fwnode, depth);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001931
Sakari Ailusa92eb762019-10-03 15:32:16 +03001932 buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001933 default_str_spec);
Sakari Ailusa92eb762019-10-03 15:32:16 +03001934 buf = string(buf, end, fwnode_get_name(__fwnode),
1935 default_str_spec);
1936
1937 fwnode_handle_put(__fwnode);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001938 }
Sakari Ailusa92eb762019-10-03 15:32:16 +03001939
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001940 return buf;
1941}
1942
1943static noinline_for_stack
1944char *device_node_string(char *buf, char *end, struct device_node *dn,
1945 struct printf_spec spec, const char *fmt)
1946{
1947 char tbuf[sizeof("xxxx") + 1];
1948 const char *p;
1949 int ret;
1950 char *buf_start = buf;
1951 struct property *prop;
1952 bool has_mult, pass;
1953 static const struct printf_spec num_spec = {
1954 .flags = SMALL,
1955 .field_width = -1,
1956 .precision = -1,
1957 .base = 10,
1958 };
1959
1960 struct printf_spec str_spec = spec;
1961 str_spec.field_width = -1;
1962
Sakari Ailus83abc5a2019-10-03 15:32:17 +03001963 if (fmt[0] != 'F')
1964 return error_string(buf, end, "(%pO?)", spec);
1965
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001966 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001967 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001968
Petr Mladek3e5903e2019-04-17 13:53:48 +02001969 if (check_pointer(&buf, end, dn, spec))
1970 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001971
1972 /* simple case without anything any more format specifiers */
1973 fmt++;
1974 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1975 fmt = "f";
1976
1977 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001978 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001979 if (pass) {
1980 if (buf < end)
1981 *buf = ':';
1982 buf++;
1983 }
1984
1985 switch (*fmt) {
1986 case 'f': /* full_name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001987 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
1988 end);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001989 break;
1990 case 'n': /* name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001991 p = fwnode_get_name(of_fwnode_handle(dn));
Rob Herring6d0a70a2018-08-27 08:13:56 -05001992 precision = str_spec.precision;
1993 str_spec.precision = strchrnul(p, '@') - p;
1994 buf = string(buf, end, p, str_spec);
1995 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001996 break;
1997 case 'p': /* phandle */
1998 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1999 break;
2000 case 'P': /* path-spec */
Sakari Ailusa92eb762019-10-03 15:32:16 +03002001 p = fwnode_get_name(of_fwnode_handle(dn));
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002002 if (!p[1])
2003 p = "/";
2004 buf = string(buf, end, p, str_spec);
2005 break;
2006 case 'F': /* flags */
2007 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2008 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2009 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2010 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2011 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02002012 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002013 break;
2014 case 'c': /* major compatible string */
2015 ret = of_property_read_string(dn, "compatible", &p);
2016 if (!ret)
2017 buf = string(buf, end, p, str_spec);
2018 break;
2019 case 'C': /* full compatible string */
2020 has_mult = false;
2021 of_property_for_each_string(dn, "compatible", prop, p) {
2022 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02002023 buf = string_nocheck(buf, end, ",", str_spec);
2024 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002025 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02002026 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002027
2028 has_mult = true;
2029 }
2030 break;
2031 default:
2032 break;
2033 }
2034 }
2035
2036 return widen_string(buf, buf - buf_start, end, spec);
2037}
2038
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002039static noinline_for_stack
2040char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2041 struct printf_spec spec, const char *fmt)
Petr Mladek798cc272019-04-17 13:53:46 +02002042{
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002043 struct printf_spec str_spec = spec;
2044 char *buf_start = buf;
2045
2046 str_spec.field_width = -1;
2047
2048 if (*fmt != 'w')
2049 return error_string(buf, end, "(%pf?)", spec);
2050
2051 if (check_pointer(&buf, end, fwnode, spec))
2052 return buf;
2053
2054 fmt++;
2055
2056 switch (*fmt) {
2057 case 'P': /* name */
2058 buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
2059 break;
2060 case 'f': /* full_name */
2061 default:
2062 buf = fwnode_full_name_string(fwnode, buf, end);
2063 break;
Petr Mladek798cc272019-04-17 13:53:46 +02002064 }
2065
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002066 return widen_string(buf, buf - buf_start, end, spec);
Petr Mladek798cc272019-04-17 13:53:46 +02002067}
2068
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002069/*
2070 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2071 * by an extra set of alphanumeric characters that are extended format
2072 * specifiers.
2073 *
Joe Perches0b523762017-05-08 15:55:36 -07002074 * Please update scripts/checkpatch.pl when adding/removing conversion
2075 * characters. (Search for "check for vsprintf extension").
2076 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002077 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002078 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002079 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2080 * - 's' For symbolic direct pointers (or function descriptors) without offset
Sakari Ailus9af77062019-10-03 15:32:14 +03002081 * - '[Ss]R' as above with __builtin_extract_return_addr() translation
Sakari Ailus1586c5a2019-10-03 15:32:15 +03002082 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2083 * %ps and %pS. Be careful when re-using these specifiers.
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002084 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002085 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2086 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002087 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2088 * width which must be explicitly specified either as part of the
2089 * format string '%32b[l]' or through '%*b[l]', [l] selects
2090 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002091 * - 'M' For a 6-byte MAC address, it prints the address in the
2092 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002093 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002094 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002095 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002096 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002097 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2098 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2099 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002100 * [S][pfs]
2101 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2102 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002103 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2104 * IPv6 omits the colons (01020304...0f)
2105 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002106 * [S][pfs]
2107 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2108 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2109 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2110 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07002111 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002112 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2113 * of the following flags (see string_escape_mem() for the
2114 * details):
2115 * a - ESCAPE_ANY
2116 * c - ESCAPE_SPECIAL
2117 * h - ESCAPE_HEX
2118 * n - ESCAPE_NULL
2119 * o - ESCAPE_OCTAL
2120 * p - ESCAPE_NP
2121 * s - ESCAPE_SPACE
2122 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002123 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2124 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2125 * Options for %pU are:
2126 * b big endian lower case hex (default)
2127 * B big endian UPPER case hex
2128 * l little endian lower case hex
2129 * L little endian UPPER case hex
2130 * big endian output byte order is:
2131 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2132 * little endian output byte order is:
2133 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002134 * - 'V' For a struct va_format which contains a format string * and va_list *,
2135 * call vsnprintf(->format, *->va_list).
2136 * Implements a "recursive vsnprintf".
2137 * Do not use this feature without some mechanism to verify the
2138 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002139 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002140 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002141 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2142 * a certain separator (' ' by default):
2143 * C colon
2144 * D dash
2145 * N no separator
2146 * The maximum supported length is 64 bytes of the input. Consider
2147 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002148 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2149 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002150 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2151 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002152 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002153 * - 't[R][dt][r]' For time and date as represented:
2154 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002155 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2156 * (legacy clock framework) of the clock
2157 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2158 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002159 * - 'G' For flags to be printed as a collection of symbolic strings that would
2160 * construct the specific value. Supported flags given by option:
2161 * p page flags (see struct page) given as pointer to unsigned long
2162 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2163 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002164 * - 'OF[fnpPcCF]' For a device tree object
2165 * Without any optional arguments prints the full_name
2166 * f device node full_name
2167 * n device node name
2168 * p device node phandle
2169 * P device node path spec (name + @unit)
2170 * F device node flags
2171 * c major compatible string
2172 * C full compatible string
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002173 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2174 * Without an option prints the full name of the node
2175 * f full name
2176 * P node name, including a possible unit address
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002177 * - 'x' For printing the address. Equivalent to "%lx".
Daniel Borkmannb2a52122020-05-15 12:11:18 +02002178 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2179 * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2180 * or user (u) memory to probe, and:
2181 * s a string, equivalent to "%s" on direct vsnprintf() use
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002182 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002183 * ** When making changes please also update:
2184 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002185 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002186 * Note: The default behaviour (unadorned %p) is to hash the address,
2187 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002188 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002189static noinline_for_stack
2190char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2191 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002192{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002193 switch (*fmt) {
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002194 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002195 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002196 ptr = dereference_symbol_descriptor(ptr);
2197 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002198 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002199 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002200 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002201 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002202 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002203 case 'h':
2204 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002205 case 'b':
2206 switch (fmt[1]) {
2207 case 'l':
2208 return bitmap_list_string(buf, end, ptr, spec, fmt);
2209 default:
2210 return bitmap_string(buf, end, ptr, spec, fmt);
2211 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002212 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2213 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002214 /* [mM]F (FDDI) */
2215 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002216 return mac_address_string(buf, end, ptr, spec, fmt);
2217 case 'I': /* Formatted IP supported
2218 * 4: 1.2.3.4
2219 * 6: 0001:0203:...:0708
2220 * 6c: 1::708 or 1::1.2.3.4
2221 */
2222 case 'i': /* Contiguous:
2223 * 4: 001.002.003.004
2224 * 6: 000102...0f
2225 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002226 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002227 case 'E':
2228 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002229 case 'U':
2230 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002231 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002232 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002233 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002234 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002235 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002236 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002237 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002238 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002239 case 'd':
2240 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002241 case 't':
2242 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002243 case 'C':
2244 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002245 case 'D':
Jia He36594b32019-08-09 09:24:56 +08002246 return file_dentry_name(buf, end, ptr, spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002247#ifdef CONFIG_BLOCK
2248 case 'g':
2249 return bdev_name(buf, end, ptr, spec, fmt);
2250#endif
2251
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002252 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002253 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002254 case 'O':
Sakari Ailus83abc5a2019-10-03 15:32:17 +03002255 return device_node_string(buf, end, ptr, spec, fmt + 1);
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002256 case 'f':
2257 return fwnode_string(buf, end, ptr, spec, fmt + 1);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002258 case 'x':
2259 return pointer_string(buf, end, ptr, spec);
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002260 case 'e':
2261 /* %pe with a non-ERR_PTR gets treated as plain %p */
2262 if (!IS_ERR(ptr))
2263 break;
2264 return err_ptr(buf, end, ptr, spec);
Daniel Borkmannb2a52122020-05-15 12:11:18 +02002265 case 'u':
2266 case 'k':
2267 switch (fmt[1]) {
2268 case 's':
2269 return string(buf, end, ptr, spec);
2270 default:
2271 return error_string(buf, end, "(einval)", spec);
2272 }
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002273 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002274
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002275 /* default is to _not_ leak addresses, hash before printing */
2276 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002277}
2278
2279/*
2280 * Helper function to decode printf style format.
2281 * Each call decode a token from the format and return the
2282 * number of characters read (or likely the delta where it wants
2283 * to go on the next call).
2284 * The decoded token is returned through the parameters
2285 *
2286 * 'h', 'l', or 'L' for integer fields
2287 * 'z' support added 23/7/1999 S.H.
2288 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002289 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002290 * 't' added for ptrdiff_t
2291 *
2292 * @fmt: the format string
2293 * @type of the token returned
2294 * @flags: various flags such as +, -, # tokens..
2295 * @field_width: overwritten width
2296 * @base: base of the number (octal, hex, ...)
2297 * @precision: precision of a number
2298 * @qualifier: qualifier of a number (long, size_t, ...)
2299 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002300static noinline_for_stack
2301int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002302{
2303 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002304 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002305
2306 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002307 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002308 if (spec->field_width < 0) {
2309 spec->field_width = -spec->field_width;
2310 spec->flags |= LEFT;
2311 }
2312 spec->type = FORMAT_TYPE_NONE;
2313 goto precision;
2314 }
2315
2316 /* we finished early by reading the precision */
2317 if (spec->type == FORMAT_TYPE_PRECISION) {
2318 if (spec->precision < 0)
2319 spec->precision = 0;
2320
2321 spec->type = FORMAT_TYPE_NONE;
2322 goto qualifier;
2323 }
2324
2325 /* By default */
2326 spec->type = FORMAT_TYPE_NONE;
2327
2328 for (; *fmt ; ++fmt) {
2329 if (*fmt == '%')
2330 break;
2331 }
2332
2333 /* Return the current non-format string */
2334 if (fmt != start || !*fmt)
2335 return fmt - start;
2336
2337 /* Process flags */
2338 spec->flags = 0;
2339
2340 while (1) { /* this also skips first '%' */
2341 bool found = true;
2342
2343 ++fmt;
2344
2345 switch (*fmt) {
2346 case '-': spec->flags |= LEFT; break;
2347 case '+': spec->flags |= PLUS; break;
2348 case ' ': spec->flags |= SPACE; break;
2349 case '#': spec->flags |= SPECIAL; break;
2350 case '0': spec->flags |= ZEROPAD; break;
2351 default: found = false;
2352 }
2353
2354 if (!found)
2355 break;
2356 }
2357
2358 /* get field width */
2359 spec->field_width = -1;
2360
2361 if (isdigit(*fmt))
2362 spec->field_width = skip_atoi(&fmt);
2363 else if (*fmt == '*') {
2364 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002365 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002366 return ++fmt - start;
2367 }
2368
2369precision:
2370 /* get the precision */
2371 spec->precision = -1;
2372 if (*fmt == '.') {
2373 ++fmt;
2374 if (isdigit(*fmt)) {
2375 spec->precision = skip_atoi(&fmt);
2376 if (spec->precision < 0)
2377 spec->precision = 0;
2378 } else if (*fmt == '*') {
2379 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002380 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002381 return ++fmt - start;
2382 }
2383 }
2384
2385qualifier:
2386 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002387 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002388 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002389 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002390 qualifier = *fmt++;
2391 if (unlikely(qualifier == *fmt)) {
2392 if (qualifier == 'l') {
2393 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002394 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002395 } else if (qualifier == 'h') {
2396 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002397 ++fmt;
2398 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002399 }
2400 }
2401
2402 /* default base */
2403 spec->base = 10;
2404 switch (*fmt) {
2405 case 'c':
2406 spec->type = FORMAT_TYPE_CHAR;
2407 return ++fmt - start;
2408
2409 case 's':
2410 spec->type = FORMAT_TYPE_STR;
2411 return ++fmt - start;
2412
2413 case 'p':
2414 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002415 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002416
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002417 case '%':
2418 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2419 return ++fmt - start;
2420
2421 /* integer number formats - set up the flags and "break" */
2422 case 'o':
2423 spec->base = 8;
2424 break;
2425
2426 case 'x':
2427 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002428 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002429
2430 case 'X':
2431 spec->base = 16;
2432 break;
2433
2434 case 'd':
2435 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002436 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002437 case 'u':
2438 break;
2439
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002440 case 'n':
2441 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002442 * Since %n poses a greater security risk than
2443 * utility, treat it as any other invalid or
2444 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002445 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002446 /* Fall-through */
2447
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002448 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002449 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002450 spec->type = FORMAT_TYPE_INVALID;
2451 return fmt - start;
2452 }
2453
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002454 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002455 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002456 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002457 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2458 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002459 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002460 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002461 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002462 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002463 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002464 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2465 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002466 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002467 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2468 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002469 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002470 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2471 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002472 }
2473
2474 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002475}
2476
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002477static void
2478set_field_width(struct printf_spec *spec, int width)
2479{
2480 spec->field_width = width;
2481 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2482 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2483 }
2484}
2485
2486static void
2487set_precision(struct printf_spec *spec, int prec)
2488{
2489 spec->precision = prec;
2490 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2491 spec->precision = clamp(prec, 0, PRECISION_MAX);
2492 }
2493}
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495/**
2496 * vsnprintf - Format a string and place it in a buffer
2497 * @buf: The buffer to place the result into
2498 * @size: The size of the buffer, including the trailing null space
2499 * @fmt: The format string to use
2500 * @args: Arguments for the format string
2501 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002502 * This function generally follows C99 vsnprintf, but has some
2503 * extensions and a few limitations:
2504 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002505 * - ``%n`` is unsupported
2506 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002507 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002508 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002509 * extensive description.
2510 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002511 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 * The return value is the number of characters which would
2514 * be generated for the given input, excluding the trailing
2515 * '\0', as per ISO C99. If you want to have the exact
2516 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002517 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 * return is greater than or equal to @size, the resulting
2519 * string is truncated.
2520 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002521 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 */
2523int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2524{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002526 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002527 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002529 /* Reject out-of-range values early. Large positive sizes are
2530 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002531 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002535 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002537 /* Make sure end is always >= buf */
2538 if (end < buf) {
2539 end = ((void *)-1);
2540 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002543 while (*fmt) {
2544 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002545 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002546
2547 fmt += read;
2548
2549 switch (spec.type) {
2550 case FORMAT_TYPE_NONE: {
2551 int copy = read;
2552 if (str < end) {
2553 if (copy > end - str)
2554 copy = end - str;
2555 memcpy(str, old_fmt, copy);
2556 }
2557 str += read;
2558 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560
Vegard Nossumed681a92009-03-14 12:08:50 +01002561 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002562 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002563 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002565 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002566 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
André Goddard Rosad4be1512009-12-14 18:00:59 -08002569 case FORMAT_TYPE_CHAR: {
2570 char c;
2571
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002572 if (!(spec.flags & LEFT)) {
2573 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002574 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 *str = ' ';
2576 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002579 }
2580 c = (unsigned char) va_arg(args, int);
2581 if (str < end)
2582 *str = c;
2583 ++str;
2584 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002585 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002586 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002588 }
2589 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002592 case FORMAT_TYPE_STR:
2593 str = string(str, end, va_arg(args, char *), spec);
2594 break;
2595
2596 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002597 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002598 spec);
2599 while (isalnum(*fmt))
2600 fmt++;
2601 break;
2602
2603 case FORMAT_TYPE_PERCENT_CHAR:
2604 if (str < end)
2605 *str = '%';
2606 ++str;
2607 break;
2608
2609 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002610 /*
2611 * Presumably the arguments passed gcc's type
2612 * checking, but there is no safe or sane way
2613 * for us to continue parsing the format and
2614 * fetching from the va_list; the remaining
2615 * specifiers and arguments would be out of
2616 * sync.
2617 */
2618 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002619
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002620 default:
2621 switch (spec.type) {
2622 case FORMAT_TYPE_LONG_LONG:
2623 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002625 case FORMAT_TYPE_ULONG:
2626 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002628 case FORMAT_TYPE_LONG:
2629 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002631 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002632 if (spec.flags & SIGN)
2633 num = va_arg(args, ssize_t);
2634 else
2635 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002636 break;
2637 case FORMAT_TYPE_PTRDIFF:
2638 num = va_arg(args, ptrdiff_t);
2639 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002640 case FORMAT_TYPE_UBYTE:
2641 num = (unsigned char) va_arg(args, int);
2642 break;
2643 case FORMAT_TYPE_BYTE:
2644 num = (signed char) va_arg(args, int);
2645 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002646 case FORMAT_TYPE_USHORT:
2647 num = (unsigned short) va_arg(args, int);
2648 break;
2649 case FORMAT_TYPE_SHORT:
2650 num = (short) va_arg(args, int);
2651 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002652 case FORMAT_TYPE_INT:
2653 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002656 num = va_arg(args, unsigned int);
2657 }
2658
2659 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002662
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002663out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002664 if (size > 0) {
2665 if (str < end)
2666 *str = '\0';
2667 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002668 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002669 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002670
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002671 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675EXPORT_SYMBOL(vsnprintf);
2676
2677/**
2678 * vscnprintf - Format a string and place it in a buffer
2679 * @buf: The buffer to place the result into
2680 * @size: The size of the buffer, including the trailing null space
2681 * @fmt: The format string to use
2682 * @args: Arguments for the format string
2683 *
2684 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002685 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 * returns 0.
2687 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002688 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002689 *
2690 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 */
2692int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2693{
2694 int i;
2695
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002696 i = vsnprintf(buf, size, fmt, args);
2697
Anton Arapovb921c692011-01-12 16:59:49 -08002698 if (likely(i < size))
2699 return i;
2700 if (size != 0)
2701 return size - 1;
2702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704EXPORT_SYMBOL(vscnprintf);
2705
2706/**
2707 * snprintf - Format a string and place it in a buffer
2708 * @buf: The buffer to place the result into
2709 * @size: The size of the buffer, including the trailing null space
2710 * @fmt: The format string to use
2711 * @...: Arguments for the format string
2712 *
2713 * The return value is the number of characters which would be
2714 * generated for the given input, excluding the trailing null,
2715 * as per ISO C99. If the return is greater than or equal to
2716 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002717 *
2718 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002720int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
2722 va_list args;
2723 int i;
2724
2725 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002726 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return i;
2730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731EXPORT_SYMBOL(snprintf);
2732
2733/**
2734 * scnprintf - Format a string and place it in a buffer
2735 * @buf: The buffer to place the result into
2736 * @size: The size of the buffer, including the trailing null space
2737 * @fmt: The format string to use
2738 * @...: Arguments for the format string
2739 *
2740 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002741 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 */
2743
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002744int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 va_list args;
2747 int i;
2748
2749 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002750 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002752
Anton Arapovb921c692011-01-12 16:59:49 -08002753 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755EXPORT_SYMBOL(scnprintf);
2756
2757/**
2758 * vsprintf - Format a string and place it in a buffer
2759 * @buf: The buffer to place the result into
2760 * @fmt: The format string to use
2761 * @args: Arguments for the format string
2762 *
2763 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002764 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 * buffer overflows.
2766 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002767 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002768 *
2769 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 */
2771int vsprintf(char *buf, const char *fmt, va_list args)
2772{
2773 return vsnprintf(buf, INT_MAX, fmt, args);
2774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775EXPORT_SYMBOL(vsprintf);
2776
2777/**
2778 * sprintf - Format a string and place it in a buffer
2779 * @buf: The buffer to place the result into
2780 * @fmt: The format string to use
2781 * @...: Arguments for the format string
2782 *
2783 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002784 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002786 *
2787 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002789int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
2791 va_list args;
2792 int i;
2793
2794 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002795 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return i;
2799}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800EXPORT_SYMBOL(sprintf);
2801
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002802#ifdef CONFIG_BINARY_PRINTF
2803/*
2804 * bprintf service:
2805 * vbin_printf() - VA arguments to binary data
2806 * bstr_printf() - Binary data to text string
2807 */
2808
2809/**
2810 * vbin_printf - Parse a format string and place args' binary value in a buffer
2811 * @bin_buf: The buffer to place args' binary value
2812 * @size: The size of the buffer(by words(32bits), not characters)
2813 * @fmt: The format string to use
2814 * @args: Arguments for the format string
2815 *
2816 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002817 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002818 *
2819 * The return value is the number of words(32bits) which would be generated for
2820 * the given input.
2821 *
2822 * NOTE:
2823 * If the return value is greater than @size, the resulting bin_buf is NOT
2824 * valid for bstr_printf().
2825 */
2826int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2827{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002828 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002829 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002830 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002831
2832 str = (char *)bin_buf;
2833 end = (char *)(bin_buf + size);
2834
2835#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002836({ \
2837 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002838 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002839 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002840 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002841 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002842 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002843 *(u32 *)str = *(u32 *)&val8; \
2844 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002845 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002846 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002847 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002848 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002849 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002850 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002851 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002852 *(typeof(type) *)str = (type)(long)val4; \
2853 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002854 } \
2855 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002856 value; \
2857})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002858
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002859 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002860 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002861
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002862 fmt += read;
2863
2864 switch (spec.type) {
2865 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002866 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002867 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002868 case FORMAT_TYPE_INVALID:
2869 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002870
Vegard Nossumed681a92009-03-14 12:08:50 +01002871 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002872 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002873 width = (int)save_arg(int);
2874 /* Pointers may require the width */
2875 if (*fmt == 'p')
2876 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002877 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002878
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002879 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002880 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002881 break;
2882
2883 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002884 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002885 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002886 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002887
Petr Mladek3e5903e2019-04-17 13:53:48 +02002888 err_msg = check_pointer_msg(save_str);
2889 if (err_msg)
2890 save_str = err_msg;
2891
André Goddard Rosa6c356632009-12-14 18:00:56 -08002892 len = strlen(save_str) + 1;
2893 if (str + len < end)
2894 memcpy(str, save_str, len);
2895 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002896 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002897 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002898
2899 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002900 /* Dereferenced pointers must be done now */
2901 switch (*fmt) {
2902 /* Dereference of functions is still OK */
2903 case 'S':
2904 case 's':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002905 case 'x':
2906 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002907 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002908 save_arg(void *);
2909 break;
2910 default:
2911 if (!isalnum(*fmt)) {
2912 save_arg(void *);
2913 break;
2914 }
2915 str = pointer(fmt, str, end, va_arg(args, void *),
2916 spec);
2917 if (str + 1 < end)
2918 *str++ = '\0';
2919 else
2920 end[-1] = '\0'; /* Must be nul terminated */
2921 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002922 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002923 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002924 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002925 break;
2926
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002927 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002928 switch (spec.type) {
2929
2930 case FORMAT_TYPE_LONG_LONG:
2931 save_arg(long long);
2932 break;
2933 case FORMAT_TYPE_ULONG:
2934 case FORMAT_TYPE_LONG:
2935 save_arg(unsigned long);
2936 break;
2937 case FORMAT_TYPE_SIZE_T:
2938 save_arg(size_t);
2939 break;
2940 case FORMAT_TYPE_PTRDIFF:
2941 save_arg(ptrdiff_t);
2942 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002943 case FORMAT_TYPE_UBYTE:
2944 case FORMAT_TYPE_BYTE:
2945 save_arg(char);
2946 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002947 case FORMAT_TYPE_USHORT:
2948 case FORMAT_TYPE_SHORT:
2949 save_arg(short);
2950 break;
2951 default:
2952 save_arg(int);
2953 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002954 }
2955 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002956
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002957out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002958 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002959#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002960}
2961EXPORT_SYMBOL_GPL(vbin_printf);
2962
2963/**
2964 * bstr_printf - Format a string from binary arguments and place it in a buffer
2965 * @buf: The buffer to place the result into
2966 * @size: The size of the buffer, including the trailing null space
2967 * @fmt: The format string to use
2968 * @bin_buf: Binary arguments for the format string
2969 *
2970 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2971 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2972 * a binary buffer that generated by vbin_printf.
2973 *
2974 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002975 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002976 *
2977 * The return value is the number of characters which would
2978 * be generated for the given input, excluding the trailing
2979 * '\0', as per ISO C99. If you want to have the exact
2980 * number of characters written into @buf as return value
2981 * (not including the trailing '\0'), use vscnprintf(). If the
2982 * return is greater than or equal to @size, the resulting
2983 * string is truncated.
2984 */
2985int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2986{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002987 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002988 char *str, *end;
2989 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002990
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002991 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002992 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002993
2994 str = buf;
2995 end = buf + size;
2996
2997#define get_arg(type) \
2998({ \
2999 typeof(type) value; \
3000 if (sizeof(type) == 8) { \
3001 args = PTR_ALIGN(args, sizeof(u32)); \
3002 *(u32 *)&value = *(u32 *)args; \
3003 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
3004 } else { \
3005 args = PTR_ALIGN(args, sizeof(type)); \
3006 value = *(typeof(type) *)args; \
3007 } \
3008 args += sizeof(type); \
3009 value; \
3010})
3011
3012 /* Make sure end is always >= buf */
3013 if (end < buf) {
3014 end = ((void *)-1);
3015 size = end - buf;
3016 }
3017
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003018 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003019 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003020 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003021
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003022 fmt += read;
3023
3024 switch (spec.type) {
3025 case FORMAT_TYPE_NONE: {
3026 int copy = read;
3027 if (str < end) {
3028 if (copy > end - str)
3029 copy = end - str;
3030 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003031 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003032 str += read;
3033 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003034 }
3035
Vegard Nossumed681a92009-03-14 12:08:50 +01003036 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003037 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003038 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003039
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003040 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003041 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003042 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003043
André Goddard Rosad4be1512009-12-14 18:00:59 -08003044 case FORMAT_TYPE_CHAR: {
3045 char c;
3046
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003047 if (!(spec.flags & LEFT)) {
3048 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003049 if (str < end)
3050 *str = ' ';
3051 ++str;
3052 }
3053 }
3054 c = (unsigned char) get_arg(char);
3055 if (str < end)
3056 *str = c;
3057 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003058 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003059 if (str < end)
3060 *str = ' ';
3061 ++str;
3062 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003063 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003064 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003065
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003066 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003067 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003068 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003069 str = string(str, end, (char *)str_arg, spec);
3070 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003071 }
3072
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003073 case FORMAT_TYPE_PTR: {
3074 bool process = false;
3075 int copy, len;
3076 /* Non function dereferences were already done */
3077 switch (*fmt) {
3078 case 'S':
3079 case 's':
3080 case 'F':
3081 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04003082 case 'x':
3083 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02003084 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003085 process = true;
3086 break;
3087 default:
3088 if (!isalnum(*fmt)) {
3089 process = true;
3090 break;
3091 }
3092 /* Pointer dereference was already processed */
3093 if (str < end) {
3094 len = copy = strlen(args);
3095 if (copy > end - str)
3096 copy = end - str;
3097 memcpy(str, args, copy);
3098 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003099 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003100 }
3101 }
3102 if (process)
3103 str = pointer(fmt, str, end, get_arg(void *), spec);
3104
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003105 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003106 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003107 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003108 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003109
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003110 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003111 if (str < end)
3112 *str = '%';
3113 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003114 break;
3115
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003116 case FORMAT_TYPE_INVALID:
3117 goto out;
3118
André Goddard Rosad4be1512009-12-14 18:00:59 -08003119 default: {
3120 unsigned long long num;
3121
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003122 switch (spec.type) {
3123
3124 case FORMAT_TYPE_LONG_LONG:
3125 num = get_arg(long long);
3126 break;
3127 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003128 case FORMAT_TYPE_LONG:
3129 num = get_arg(unsigned long);
3130 break;
3131 case FORMAT_TYPE_SIZE_T:
3132 num = get_arg(size_t);
3133 break;
3134 case FORMAT_TYPE_PTRDIFF:
3135 num = get_arg(ptrdiff_t);
3136 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003137 case FORMAT_TYPE_UBYTE:
3138 num = get_arg(unsigned char);
3139 break;
3140 case FORMAT_TYPE_BYTE:
3141 num = get_arg(signed char);
3142 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003143 case FORMAT_TYPE_USHORT:
3144 num = get_arg(unsigned short);
3145 break;
3146 case FORMAT_TYPE_SHORT:
3147 num = get_arg(short);
3148 break;
3149 case FORMAT_TYPE_UINT:
3150 num = get_arg(unsigned int);
3151 break;
3152 default:
3153 num = get_arg(int);
3154 }
3155
3156 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003157 } /* default: */
3158 } /* switch(spec.type) */
3159 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003160
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003161out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003162 if (size > 0) {
3163 if (str < end)
3164 *str = '\0';
3165 else
3166 end[-1] = '\0';
3167 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003168
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003169#undef get_arg
3170
3171 /* the trailing null byte doesn't count towards the total */
3172 return str - buf;
3173}
3174EXPORT_SYMBOL_GPL(bstr_printf);
3175
3176/**
3177 * bprintf - Parse a format string and place args' binary value in a buffer
3178 * @bin_buf: The buffer to place args' binary value
3179 * @size: The size of the buffer(by words(32bits), not characters)
3180 * @fmt: The format string to use
3181 * @...: Arguments for the format string
3182 *
3183 * The function returns the number of words(u32) written
3184 * into @bin_buf.
3185 */
3186int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3187{
3188 va_list args;
3189 int ret;
3190
3191 va_start(args, fmt);
3192 ret = vbin_printf(bin_buf, size, fmt, args);
3193 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003194
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003195 return ret;
3196}
3197EXPORT_SYMBOL_GPL(bprintf);
3198
3199#endif /* CONFIG_BINARY_PRINTF */
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201/**
3202 * vsscanf - Unformat a buffer into a list of arguments
3203 * @buf: input buffer
3204 * @fmt: format of buffer
3205 * @args: arguments
3206 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003207int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 const char *str = buf;
3210 char *next;
3211 char digit;
3212 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003213 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003214 unsigned int base;
3215 union {
3216 long long s;
3217 unsigned long long u;
3218 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003219 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003220 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
Jan Beulichda990752012-10-04 17:13:24 -07003222 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 /* skip any white space in format */
3224 /* white space in format matchs any amount of
3225 * white space, including none, in the input.
3226 */
3227 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003228 fmt = skip_spaces(++fmt);
3229 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
3231
3232 /* anything that is not a conversion must match exactly */
3233 if (*fmt != '%' && *fmt) {
3234 if (*fmt++ != *str++)
3235 break;
3236 continue;
3237 }
3238
3239 if (!*fmt)
3240 break;
3241 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003242
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 /* skip this conversion.
3244 * advance both strings to next white space
3245 */
3246 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003247 if (!*str)
3248 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003249 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3250 /* '%*[' not yet supported, invalid format */
3251 if (*fmt == '[')
3252 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 while (!isspace(*str) && *str)
3256 str++;
3257 continue;
3258 }
3259
3260 /* get field width */
3261 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003262 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003264 if (field_width <= 0)
3265 break;
3266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 /* get conversion qualifier */
3269 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003270 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003271 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 qualifier = *fmt++;
3273 if (unlikely(qualifier == *fmt)) {
3274 if (qualifier == 'h') {
3275 qualifier = 'H';
3276 fmt++;
3277 } else if (qualifier == 'l') {
3278 qualifier = 'L';
3279 fmt++;
3280 }
3281 }
3282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Jan Beulichda990752012-10-04 17:13:24 -07003284 if (!*fmt)
3285 break;
3286
3287 if (*fmt == 'n') {
3288 /* return number of characters read so far */
3289 *va_arg(args, int *) = str - buf;
3290 ++fmt;
3291 continue;
3292 }
3293
3294 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 break;
3296
André Goddard Rosad4be1512009-12-14 18:00:59 -08003297 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003298 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003299
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003300 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 case 'c':
3302 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003303 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (field_width == -1)
3305 field_width = 1;
3306 do {
3307 *s++ = *str++;
3308 } while (--field_width > 0 && *str);
3309 num++;
3310 }
3311 continue;
3312 case 's':
3313 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003314 char *s = (char *)va_arg(args, char *);
3315 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003316 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003318 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003321 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 *s = '\0';
3324 num++;
3325 }
3326 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003327 /*
3328 * Warning: This implementation of the '[' conversion specifier
3329 * deviates from its glibc counterpart in the following ways:
3330 * (1) It does NOT support ranges i.e. '-' is NOT a special
3331 * character
3332 * (2) It cannot match the closing bracket ']' itself
3333 * (3) A field width is required
3334 * (4) '%*[' (discard matching input) is currently not supported
3335 *
3336 * Example usage:
3337 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3338 * buf1, buf2, buf3);
3339 * if (ret < 3)
3340 * // etc..
3341 */
3342 case '[':
3343 {
3344 char *s = (char *)va_arg(args, char *);
3345 DECLARE_BITMAP(set, 256) = {0};
3346 unsigned int len = 0;
3347 bool negate = (*fmt == '^');
3348
3349 /* field width is required */
3350 if (field_width == -1)
3351 return num;
3352
3353 if (negate)
3354 ++fmt;
3355
3356 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3357 set_bit((u8)*fmt, set);
3358
3359 /* no ']' or no character set found */
3360 if (!*fmt || !len)
3361 return num;
3362 ++fmt;
3363
3364 if (negate) {
3365 bitmap_complement(set, set, 256);
3366 /* exclude null '\0' byte */
3367 clear_bit(0, set);
3368 }
3369
3370 /* match must be non-empty */
3371 if (!test_bit((u8)*str, set))
3372 return num;
3373
3374 while (test_bit((u8)*str, set) && field_width--)
3375 *s++ = *str++;
3376 *s = '\0';
3377 ++num;
3378 }
3379 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 case 'o':
3381 base = 8;
3382 break;
3383 case 'x':
3384 case 'X':
3385 base = 16;
3386 break;
3387 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003388 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003389 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003391 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003392 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 case 'u':
3394 break;
3395 case '%':
3396 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003397 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return num;
3399 continue;
3400 default:
3401 /* invalid format; stop here */
3402 return num;
3403 }
3404
3405 /* have some sort of integer conversion.
3406 * first, skip white space in buffer.
3407 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003408 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
3410 digit = *str;
3411 if (is_sign && digit == '-')
3412 digit = *(str + 1);
3413
3414 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003415 || (base == 16 && !isxdigit(digit))
3416 || (base == 10 && !isdigit(digit))
3417 || (base == 8 && (!isdigit(digit) || digit > '7'))
3418 || (base == 0 && !isdigit(digit)))
3419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Jan Beulich53809752012-12-17 16:01:31 -08003421 if (is_sign)
3422 val.s = qualifier != 'L' ?
3423 simple_strtol(str, &next, base) :
3424 simple_strtoll(str, &next, base);
3425 else
3426 val.u = qualifier != 'L' ?
3427 simple_strtoul(str, &next, base) :
3428 simple_strtoull(str, &next, base);
3429
3430 if (field_width > 0 && next - str > field_width) {
3431 if (base == 0)
3432 _parse_integer_fixup_radix(str, &base);
3433 while (next - str > field_width) {
3434 if (is_sign)
3435 val.s = div_s64(val.s, base);
3436 else
3437 val.u = div_u64(val.u, base);
3438 --next;
3439 }
3440 }
3441
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003442 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003444 if (is_sign)
3445 *va_arg(args, signed char *) = val.s;
3446 else
3447 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 break;
3449 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003450 if (is_sign)
3451 *va_arg(args, short *) = val.s;
3452 else
3453 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 break;
3455 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003456 if (is_sign)
3457 *va_arg(args, long *) = val.s;
3458 else
3459 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 break;
3461 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003462 if (is_sign)
3463 *va_arg(args, long long *) = val.s;
3464 else
3465 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003468 *va_arg(args, size_t *) = val.u;
3469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 default:
Jan Beulich53809752012-12-17 16:01:31 -08003471 if (is_sign)
3472 *va_arg(args, int *) = val.s;
3473 else
3474 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 break;
3476 }
3477 num++;
3478
3479 if (!next)
3480 break;
3481 str = next;
3482 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return num;
3485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486EXPORT_SYMBOL(vsscanf);
3487
3488/**
3489 * sscanf - Unformat a buffer into a list of arguments
3490 * @buf: input buffer
3491 * @fmt: formatting of buffer
3492 * @...: resulting arguments
3493 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003494int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495{
3496 va_list args;
3497 int i;
3498
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003499 va_start(args, fmt);
3500 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return i;
3504}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505EXPORT_SYMBOL(sscanf);