blob: 532b6606a18a605c34335b81cdb371d0d04a8b45 [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
797 /* When debugging early boot use non-cryptographically secure hash. */
798 if (unlikely(debug_boot_weak_hash)) {
799 hashval = hash_long((unsigned long)ptr, 32);
800 return pointer_string(buf, end, (const void *)hashval, spec);
801 }
802
803 ret = __ptr_to_hashval(ptr, &hashval);
804 if (ret) {
805 spec.field_width = 2 * sizeof(ptr);
806 /* string length must be less than default_width */
807 return error_string(buf, end, str, spec);
808 }
809
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200810 return pointer_string(buf, end, (const void *)hashval, spec);
811}
812
Petr Mladek6eea2422019-04-17 13:53:41 +0200813int kptr_restrict __read_mostly;
814
815static noinline_for_stack
816char *restricted_pointer(char *buf, char *end, const void *ptr,
817 struct printf_spec spec)
818{
819 switch (kptr_restrict) {
820 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200821 /* Handle as %p, hash and do _not_ leak addresses. */
822 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200823 case 1: {
824 const struct cred *cred;
825
826 /*
827 * kptr_restrict==1 cannot be used in IRQ context
828 * because its test for CAP_SYSLOG would be meaningless.
829 */
830 if (in_irq() || in_serving_softirq() || in_nmi()) {
831 if (spec.field_width == -1)
832 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200833 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200834 }
835
836 /*
837 * Only print the real pointer value if the current
838 * process has CAP_SYSLOG and is running with the
839 * same credentials it started with. This is because
840 * access to files is checked at open() time, but %pK
841 * checks permission at read() time. We don't want to
842 * leak pointer values if a binary opens a file using
843 * %pK and then elevates privileges before reading it.
844 */
845 cred = current_cred();
846 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
847 !uid_eq(cred->euid, cred->uid) ||
848 !gid_eq(cred->egid, cred->gid))
849 ptr = NULL;
850 break;
851 }
852 case 2:
853 default:
854 /* Always print 0's for %pK */
855 ptr = NULL;
856 break;
857 }
858
859 return pointer_string(buf, end, ptr, spec);
860}
861
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200862static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400863char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
864 const char *fmt)
865{
866 const char *array[4], *s;
867 const struct dentry *p;
868 int depth;
869 int i, n;
870
871 switch (fmt[1]) {
872 case '2': case '3': case '4':
873 depth = fmt[1] - '0';
874 break;
875 default:
876 depth = 1;
877 }
878
879 rcu_read_lock();
880 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200881 if (check_pointer(&buf, end, d, spec)) {
882 rcu_read_unlock();
883 return buf;
884 }
885
Mark Rutland6aa7de02017-10-23 14:07:29 -0700886 p = READ_ONCE(d->d_parent);
887 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400888 if (p == d) {
889 if (i)
890 array[i] = "";
891 i++;
892 break;
893 }
894 }
895 s = array[--i];
896 for (n = 0; n != spec.precision; n++, buf++) {
897 char c = *s++;
898 if (!c) {
899 if (!i)
900 break;
901 c = '/';
902 s = array[--i];
903 }
904 if (buf < end)
905 *buf = c;
906 }
907 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800908 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400909}
910
Jia He36594b32019-08-09 09:24:56 +0800911static noinline_for_stack
912char *file_dentry_name(char *buf, char *end, const struct file *f,
913 struct printf_spec spec, const char *fmt)
914{
915 if (check_pointer(&buf, end, f, spec))
916 return buf;
917
918 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
919}
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400920#ifdef CONFIG_BLOCK
921static noinline_for_stack
922char *bdev_name(char *buf, char *end, struct block_device *bdev,
923 struct printf_spec spec, const char *fmt)
924{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200925 struct gendisk *hd;
926
927 if (check_pointer(&buf, end, bdev, spec))
928 return buf;
929
930 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400931 buf = string(buf, end, hd->disk_name, spec);
932 if (bdev->bd_part->partno) {
933 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
934 if (buf < end)
935 *buf = 'p';
936 buf++;
937 }
938 buf = number(buf, end, bdev->bd_part->partno, spec);
939 }
940 return buf;
941}
942#endif
943
Joe Perchescf3b4292010-05-24 14:33:16 -0700944static noinline_for_stack
945char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800946 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700947{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800948 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700949#ifdef CONFIG_KALLSYMS
950 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800951#endif
952
953 if (fmt[1] == 'R')
954 ptr = __builtin_extract_return_addr(ptr);
955 value = (unsigned long)ptr;
956
957#ifdef CONFIG_KALLSYMS
958 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900959 sprint_backtrace(sym, value);
Sakari Ailus9af77062019-10-03 15:32:14 +0300960 else if (*fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200961 sprint_symbol(sym, value);
962 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700963 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800964
Petr Mladekd529ac42019-04-17 13:53:43 +0200965 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700966#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800967 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700968#endif
969}
970
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200971static const struct printf_spec default_str_spec = {
972 .field_width = -1,
973 .precision = -1,
974};
975
Andy Shevchenko54433972018-02-16 23:07:06 +0200976static const struct printf_spec default_flag_spec = {
977 .base = 16,
978 .precision = -1,
979 .flags = SPECIAL | SMALL,
980};
981
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200982static const struct printf_spec default_dec_spec = {
983 .base = 10,
984 .precision = -1,
985};
986
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200987static const struct printf_spec default_dec02_spec = {
988 .base = 10,
989 .field_width = 2,
990 .precision = -1,
991 .flags = ZEROPAD,
992};
993
994static const struct printf_spec default_dec04_spec = {
995 .base = 10,
996 .field_width = 4,
997 .precision = -1,
998 .flags = ZEROPAD,
999};
1000
Joe Perchescf3b4292010-05-24 14:33:16 -07001001static noinline_for_stack
1002char *resource_string(char *buf, char *end, struct resource *res,
1003 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +11001004{
1005#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -06001006#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +11001007#endif
1008
1009#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -06001010#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +11001011#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001012 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001013 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001014 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001015 .precision = -1,
1016 .flags = SPECIAL | SMALL | ZEROPAD,
1017 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001018 static const struct printf_spec mem_spec = {
1019 .base = 16,
1020 .field_width = MEM_RSRC_PRINTK_SIZE,
1021 .precision = -1,
1022 .flags = SPECIAL | SMALL | ZEROPAD,
1023 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001024 static const struct printf_spec bus_spec = {
1025 .base = 16,
1026 .field_width = 2,
1027 .precision = -1,
1028 .flags = SMALL | ZEROPAD,
1029 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001030 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001031 .field_width = -1,
1032 .precision = 10,
1033 .flags = LEFT,
1034 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001035
1036 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1037 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1038#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1039#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001040#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001041#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1042 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1043 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1044
Linus Torvalds332d2e72008-10-20 15:07:34 +11001045 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001046 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001047 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +11001048
Petr Mladek3e5903e2019-04-17 13:53:48 +02001049 if (check_pointer(&buf, end, res, spec))
1050 return buf;
1051
Linus Torvalds332d2e72008-10-20 15:07:34 +11001052 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001053 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001054 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001055 specp = &io_spec;
1056 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001057 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001058 specp = &mem_spec;
1059 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001060 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001061 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001062 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001063 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001064 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001065 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001066 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001067 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001068 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001069 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001070 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001071 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001072 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001073 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001074 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001075 p = number(p, pend, resource_size(res), *specp);
1076 } else {
1077 p = number(p, pend, res->start, *specp);
1078 if (res->start != res->end) {
1079 *p++ = '-';
1080 p = number(p, pend, res->end, *specp);
1081 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001082 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001083 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001084 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001085 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001086 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001087 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001088 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001089 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001090 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001091 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001092 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001093 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001094 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001095 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001096 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001097 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001098
Petr Mladekd529ac42019-04-17 13:53:43 +02001099 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001100}
1101
Joe Perchescf3b4292010-05-24 14:33:16 -07001102static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001103char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1104 const char *fmt)
1105{
Steven Rostedt360603a2013-05-28 15:47:39 -04001106 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001107 negative value, fallback to the default */
1108 char separator;
1109
1110 if (spec.field_width == 0)
1111 /* nothing to print */
1112 return buf;
1113
Petr Mladek3e5903e2019-04-17 13:53:48 +02001114 if (check_pointer(&buf, end, addr, spec))
1115 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001116
1117 switch (fmt[1]) {
1118 case 'C':
1119 separator = ':';
1120 break;
1121 case 'D':
1122 separator = '-';
1123 break;
1124 case 'N':
1125 separator = 0;
1126 break;
1127 default:
1128 separator = ' ';
1129 break;
1130 }
1131
1132 if (spec.field_width > 0)
1133 len = min_t(int, spec.field_width, 64);
1134
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001135 for (i = 0; i < len; ++i) {
1136 if (buf < end)
1137 *buf = hex_asc_hi(addr[i]);
1138 ++buf;
1139 if (buf < end)
1140 *buf = hex_asc_lo(addr[i]);
1141 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001142
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001143 if (separator && i != len - 1) {
1144 if (buf < end)
1145 *buf = separator;
1146 ++buf;
1147 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001148 }
1149
1150 return buf;
1151}
1152
1153static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001154char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1155 struct printf_spec spec, const char *fmt)
1156{
1157 const int CHUNKSZ = 32;
1158 int nr_bits = max_t(int, spec.field_width, 0);
1159 int i, chunksz;
1160 bool first = true;
1161
Petr Mladek3e5903e2019-04-17 13:53:48 +02001162 if (check_pointer(&buf, end, bitmap, spec))
1163 return buf;
1164
Tejun Heodbc760b2015-02-13 14:36:53 -08001165 /* reused to print numbers */
1166 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1167
1168 chunksz = nr_bits & (CHUNKSZ - 1);
1169 if (chunksz == 0)
1170 chunksz = CHUNKSZ;
1171
1172 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1173 for (; i >= 0; i -= CHUNKSZ) {
1174 u32 chunkmask, val;
1175 int word, bit;
1176
1177 chunkmask = ((1ULL << chunksz) - 1);
1178 word = i / BITS_PER_LONG;
1179 bit = i % BITS_PER_LONG;
1180 val = (bitmap[word] >> bit) & chunkmask;
1181
1182 if (!first) {
1183 if (buf < end)
1184 *buf = ',';
1185 buf++;
1186 }
1187 first = false;
1188
1189 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1190 buf = number(buf, end, val, spec);
1191
1192 chunksz = CHUNKSZ;
1193 }
1194 return buf;
1195}
1196
1197static noinline_for_stack
1198char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1199 struct printf_spec spec, const char *fmt)
1200{
1201 int nr_bits = max_t(int, spec.field_width, 0);
1202 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1203 int cur, rbot, rtop;
1204 bool first = true;
1205
Petr Mladek3e5903e2019-04-17 13:53:48 +02001206 if (check_pointer(&buf, end, bitmap, spec))
1207 return buf;
1208
Tejun Heodbc760b2015-02-13 14:36:53 -08001209 rbot = cur = find_first_bit(bitmap, nr_bits);
1210 while (cur < nr_bits) {
1211 rtop = cur;
1212 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1213 if (cur < nr_bits && cur <= rtop + 1)
1214 continue;
1215
1216 if (!first) {
1217 if (buf < end)
1218 *buf = ',';
1219 buf++;
1220 }
1221 first = false;
1222
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001223 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001224 if (rbot < rtop) {
1225 if (buf < end)
1226 *buf = '-';
1227 buf++;
1228
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001229 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001230 }
1231
1232 rbot = cur;
1233 }
1234 return buf;
1235}
1236
1237static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001238char *mac_address_string(char *buf, char *end, u8 *addr,
1239 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001240{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001241 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001242 char *p = mac_addr;
1243 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001244 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001245 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001246
Petr Mladek3e5903e2019-04-17 13:53:48 +02001247 if (check_pointer(&buf, end, addr, spec))
1248 return buf;
1249
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001250 switch (fmt[1]) {
1251 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001252 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001253 break;
1254
1255 case 'R':
1256 reversed = true;
1257 /* fall through */
1258
1259 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001260 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001261 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001262 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001263
1264 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001265 if (reversed)
1266 p = hex_byte_pack(p, addr[5 - i]);
1267 else
1268 p = hex_byte_pack(p, addr[i]);
1269
Joe Perches8a27f7c2009-08-17 12:29:44 +00001270 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001271 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001272 }
1273 *p = '\0';
1274
Petr Mladekd529ac42019-04-17 13:53:43 +02001275 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001276}
1277
Joe Perchescf3b4292010-05-24 14:33:16 -07001278static noinline_for_stack
1279char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001280{
Harvey Harrison689afa72008-10-28 16:04:44 -07001281 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001282 bool leading_zeros = (fmt[0] == 'i');
1283 int index;
1284 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001285
Joe Perches0159f242010-01-13 20:23:30 -08001286 switch (fmt[2]) {
1287 case 'h':
1288#ifdef __BIG_ENDIAN
1289 index = 0;
1290 step = 1;
1291#else
1292 index = 3;
1293 step = -1;
1294#endif
1295 break;
1296 case 'l':
1297 index = 3;
1298 step = -1;
1299 break;
1300 case 'n':
1301 case 'b':
1302 default:
1303 index = 0;
1304 step = 1;
1305 break;
1306 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001307 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001308 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001309 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001310 if (leading_zeros) {
1311 if (digits < 3)
1312 *p++ = '0';
1313 if (digits < 2)
1314 *p++ = '0';
1315 }
1316 /* reverse the digits in the quad */
1317 while (digits--)
1318 *p++ = temp[digits];
1319 if (i < 3)
1320 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001321 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001322 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001323 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001324
Joe Perches8a27f7c2009-08-17 12:29:44 +00001325 return p;
1326}
1327
Joe Perchescf3b4292010-05-24 14:33:16 -07001328static noinline_for_stack
1329char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001330{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001331 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001332 unsigned char zerolength[8];
1333 int longest = 1;
1334 int colonpos = -1;
1335 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001336 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001337 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001338 bool useIPv4;
1339 struct in6_addr in6;
1340
1341 memcpy(&in6, addr, sizeof(struct in6_addr));
1342
1343 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001344
1345 memset(zerolength, 0, sizeof(zerolength));
1346
1347 if (useIPv4)
1348 range = 6;
1349 else
1350 range = 8;
1351
1352 /* find position of longest 0 run */
1353 for (i = 0; i < range; i++) {
1354 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001355 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001356 break;
1357 zerolength[i]++;
1358 }
1359 }
1360 for (i = 0; i < range; i++) {
1361 if (zerolength[i] > longest) {
1362 longest = zerolength[i];
1363 colonpos = i;
1364 }
1365 }
Joe Perches29cf5192011-06-09 11:23:37 -07001366 if (longest == 1) /* don't compress a single 0 */
1367 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001368
1369 /* emit address */
1370 for (i = 0; i < range; i++) {
1371 if (i == colonpos) {
1372 if (needcolon || i == 0)
1373 *p++ = ':';
1374 *p++ = ':';
1375 needcolon = false;
1376 i += longest - 1;
1377 continue;
1378 }
1379 if (needcolon) {
1380 *p++ = ':';
1381 needcolon = false;
1382 }
1383 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001384 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001385 hi = word >> 8;
1386 lo = word & 0xff;
1387 if (hi) {
1388 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001389 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001390 else
1391 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001392 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001393 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001394 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001395 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001396 else
1397 *p++ = hex_asc_lo(lo);
1398 needcolon = true;
1399 }
1400
1401 if (useIPv4) {
1402 if (needcolon)
1403 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001404 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001405 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001406 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001407
Joe Perches8a27f7c2009-08-17 12:29:44 +00001408 return p;
1409}
1410
Joe Perchescf3b4292010-05-24 14:33:16 -07001411static noinline_for_stack
1412char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001413{
1414 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001415
Harvey Harrison689afa72008-10-28 16:04:44 -07001416 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001417 p = hex_byte_pack(p, *addr++);
1418 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001419 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001420 *p++ = ':';
1421 }
1422 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001423
Joe Perches8a27f7c2009-08-17 12:29:44 +00001424 return p;
1425}
1426
Joe Perchescf3b4292010-05-24 14:33:16 -07001427static noinline_for_stack
1428char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1429 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001430{
1431 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1432
1433 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001434 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001435 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001436 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001437
Petr Mladekd529ac42019-04-17 13:53:43 +02001438 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001439}
1440
Joe Perchescf3b4292010-05-24 14:33:16 -07001441static noinline_for_stack
1442char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1443 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001444{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001445 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001446
Joe Perches0159f242010-01-13 20:23:30 -08001447 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001448
Petr Mladekd529ac42019-04-17 13:53:43 +02001449 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001450}
1451
Joe Perchescf3b4292010-05-24 14:33:16 -07001452static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001453char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1454 struct printf_spec spec, const char *fmt)
1455{
1456 bool have_p = false, have_s = false, have_f = false, have_c = false;
1457 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1458 sizeof(":12345") + sizeof("/123456789") +
1459 sizeof("%1234567890")];
1460 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1461 const u8 *addr = (const u8 *) &sa->sin6_addr;
1462 char fmt6[2] = { fmt[0], '6' };
1463 u8 off = 0;
1464
1465 fmt++;
1466 while (isalpha(*++fmt)) {
1467 switch (*fmt) {
1468 case 'p':
1469 have_p = true;
1470 break;
1471 case 'f':
1472 have_f = true;
1473 break;
1474 case 's':
1475 have_s = true;
1476 break;
1477 case 'c':
1478 have_c = true;
1479 break;
1480 }
1481 }
1482
1483 if (have_p || have_s || have_f) {
1484 *p = '[';
1485 off = 1;
1486 }
1487
1488 if (fmt6[0] == 'I' && have_c)
1489 p = ip6_compressed_string(ip6_addr + off, addr);
1490 else
1491 p = ip6_string(ip6_addr + off, addr, fmt6);
1492
1493 if (have_p || have_s || have_f)
1494 *p++ = ']';
1495
1496 if (have_p) {
1497 *p++ = ':';
1498 p = number(p, pend, ntohs(sa->sin6_port), spec);
1499 }
1500 if (have_f) {
1501 *p++ = '/';
1502 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1503 IPV6_FLOWINFO_MASK), spec);
1504 }
1505 if (have_s) {
1506 *p++ = '%';
1507 p = number(p, pend, sa->sin6_scope_id, spec);
1508 }
1509 *p = '\0';
1510
Petr Mladekd529ac42019-04-17 13:53:43 +02001511 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001512}
1513
1514static noinline_for_stack
1515char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1516 struct printf_spec spec, const char *fmt)
1517{
1518 bool have_p = false;
1519 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1520 char *pend = ip4_addr + sizeof(ip4_addr);
1521 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1522 char fmt4[3] = { fmt[0], '4', 0 };
1523
1524 fmt++;
1525 while (isalpha(*++fmt)) {
1526 switch (*fmt) {
1527 case 'p':
1528 have_p = true;
1529 break;
1530 case 'h':
1531 case 'l':
1532 case 'n':
1533 case 'b':
1534 fmt4[2] = *fmt;
1535 break;
1536 }
1537 }
1538
1539 p = ip4_string(ip4_addr, addr, fmt4);
1540 if (have_p) {
1541 *p++ = ':';
1542 p = number(p, pend, ntohs(sa->sin_port), spec);
1543 }
1544 *p = '\0';
1545
Petr Mladekd529ac42019-04-17 13:53:43 +02001546 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001547}
1548
1549static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001550char *ip_addr_string(char *buf, char *end, const void *ptr,
1551 struct printf_spec spec, const char *fmt)
1552{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001553 char *err_fmt_msg;
1554
Petr Mladek3e5903e2019-04-17 13:53:48 +02001555 if (check_pointer(&buf, end, ptr, spec))
1556 return buf;
1557
Petr Mladekf00cc102019-04-17 13:53:44 +02001558 switch (fmt[1]) {
1559 case '6':
1560 return ip6_addr_string(buf, end, ptr, spec, fmt);
1561 case '4':
1562 return ip4_addr_string(buf, end, ptr, spec, fmt);
1563 case 'S': {
1564 const union {
1565 struct sockaddr raw;
1566 struct sockaddr_in v4;
1567 struct sockaddr_in6 v6;
1568 } *sa = ptr;
1569
1570 switch (sa->raw.sa_family) {
1571 case AF_INET:
1572 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1573 case AF_INET6:
1574 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1575 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001576 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001577 }}
1578 }
1579
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001580 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001581 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001582}
1583
1584static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001585char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1586 const char *fmt)
1587{
1588 bool found = true;
1589 int count = 1;
1590 unsigned int flags = 0;
1591 int len;
1592
1593 if (spec.field_width == 0)
1594 return buf; /* nothing to print */
1595
Petr Mladek3e5903e2019-04-17 13:53:48 +02001596 if (check_pointer(&buf, end, addr, spec))
1597 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001598
1599 do {
1600 switch (fmt[count++]) {
1601 case 'a':
1602 flags |= ESCAPE_ANY;
1603 break;
1604 case 'c':
1605 flags |= ESCAPE_SPECIAL;
1606 break;
1607 case 'h':
1608 flags |= ESCAPE_HEX;
1609 break;
1610 case 'n':
1611 flags |= ESCAPE_NULL;
1612 break;
1613 case 'o':
1614 flags |= ESCAPE_OCTAL;
1615 break;
1616 case 'p':
1617 flags |= ESCAPE_NP;
1618 break;
1619 case 's':
1620 flags |= ESCAPE_SPACE;
1621 break;
1622 default:
1623 found = false;
1624 break;
1625 }
1626 } while (found);
1627
1628 if (!flags)
1629 flags = ESCAPE_ANY_NP;
1630
1631 len = spec.field_width < 0 ? 1 : spec.field_width;
1632
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001633 /*
1634 * string_escape_mem() writes as many characters as it can to
1635 * the given buffer, and returns the total size of the output
1636 * had the buffer been big enough.
1637 */
1638 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001639
1640 return buf;
1641}
1642
Petr Mladek3e5903e2019-04-17 13:53:48 +02001643static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1644 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001645{
1646 va_list va;
1647
Petr Mladek3e5903e2019-04-17 13:53:48 +02001648 if (check_pointer(&buf, end, va_fmt, spec))
1649 return buf;
1650
Petr Mladek45c3e932019-04-17 13:53:45 +02001651 va_copy(va, *va_fmt->va);
1652 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1653 va_end(va);
1654
1655 return buf;
1656}
1657
Andy Shevchenko71dca952014-10-13 15:55:18 -07001658static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001659char *uuid_string(char *buf, char *end, const u8 *addr,
1660 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001661{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001662 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001663 char *p = uuid;
1664 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001665 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001666 bool uc = false;
1667
Petr Mladek3e5903e2019-04-17 13:53:48 +02001668 if (check_pointer(&buf, end, addr, spec))
1669 return buf;
1670
Joe Perches9ac6e442009-12-14 18:01:09 -08001671 switch (*(++fmt)) {
1672 case 'L':
1673 uc = true; /* fall-through */
1674 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001675 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001676 break;
1677 case 'B':
1678 uc = true;
1679 break;
1680 }
1681
1682 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001683 if (uc)
1684 p = hex_byte_pack_upper(p, addr[index[i]]);
1685 else
1686 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001687 switch (i) {
1688 case 3:
1689 case 5:
1690 case 7:
1691 case 9:
1692 *p++ = '-';
1693 break;
1694 }
1695 }
1696
1697 *p = 0;
1698
Petr Mladekd529ac42019-04-17 13:53:43 +02001699 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001700}
1701
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001702static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001703char *netdev_bits(char *buf, char *end, const void *addr,
1704 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001705{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001706 unsigned long long num;
1707 int size;
1708
Petr Mladek3e5903e2019-04-17 13:53:48 +02001709 if (check_pointer(&buf, end, addr, spec))
1710 return buf;
1711
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001712 switch (fmt[1]) {
1713 case 'F':
1714 num = *(const netdev_features_t *)addr;
1715 size = sizeof(netdev_features_t);
1716 break;
1717 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001718 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001719 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001720
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001721 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001722}
1723
Joe Perchesaaf07622014-01-23 15:54:17 -08001724static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001725char *address_val(char *buf, char *end, const void *addr,
1726 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001727{
1728 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001729 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001730
Petr Mladek3e5903e2019-04-17 13:53:48 +02001731 if (check_pointer(&buf, end, addr, spec))
1732 return buf;
1733
Joe Perchesaaf07622014-01-23 15:54:17 -08001734 switch (fmt[1]) {
1735 case 'd':
1736 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001737 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001738 break;
1739 case 'p':
1740 default:
1741 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001742 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001743 break;
1744 }
1745
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001746 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001747}
1748
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001749static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001750char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1751{
1752 int year = tm->tm_year + (r ? 0 : 1900);
1753 int mon = tm->tm_mon + (r ? 0 : 1);
1754
1755 buf = number(buf, end, year, default_dec04_spec);
1756 if (buf < end)
1757 *buf = '-';
1758 buf++;
1759
1760 buf = number(buf, end, mon, default_dec02_spec);
1761 if (buf < end)
1762 *buf = '-';
1763 buf++;
1764
1765 return number(buf, end, tm->tm_mday, default_dec02_spec);
1766}
1767
1768static noinline_for_stack
1769char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1770{
1771 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1772 if (buf < end)
1773 *buf = ':';
1774 buf++;
1775
1776 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1777 if (buf < end)
1778 *buf = ':';
1779 buf++;
1780
1781 return number(buf, end, tm->tm_sec, default_dec02_spec);
1782}
1783
1784static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001785char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1786 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001787{
1788 bool have_t = true, have_d = true;
1789 bool raw = false;
1790 int count = 2;
1791
Petr Mladek3e5903e2019-04-17 13:53:48 +02001792 if (check_pointer(&buf, end, tm, spec))
1793 return buf;
1794
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001795 switch (fmt[count]) {
1796 case 'd':
1797 have_t = false;
1798 count++;
1799 break;
1800 case 't':
1801 have_d = false;
1802 count++;
1803 break;
1804 }
1805
1806 raw = fmt[count] == 'r';
1807
1808 if (have_d)
1809 buf = date_str(buf, end, tm, raw);
1810 if (have_d && have_t) {
1811 /* Respect ISO 8601 */
1812 if (buf < end)
1813 *buf = 'T';
1814 buf++;
1815 }
1816 if (have_t)
1817 buf = time_str(buf, end, tm, raw);
1818
1819 return buf;
1820}
1821
1822static noinline_for_stack
1823char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1824 const char *fmt)
1825{
1826 switch (fmt[1]) {
1827 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001828 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001829 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001830 return error_string(buf, end, "(%ptR?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001831 }
1832}
1833
1834static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001835char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1836 const char *fmt)
1837{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001838 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001839 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001840
Petr Mladek3e5903e2019-04-17 13:53:48 +02001841 if (check_pointer(&buf, end, clk, spec))
1842 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001843
1844 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001845 case 'n':
1846 default:
1847#ifdef CONFIG_COMMON_CLK
1848 return string(buf, end, __clk_get_name(clk), spec);
1849#else
Geert Uytterhoeven4ca96aa2019-07-01 16:00:09 +02001850 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001851#endif
1852 }
1853}
1854
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001855static
1856char *format_flags(char *buf, char *end, unsigned long flags,
1857 const struct trace_print_flags *names)
1858{
1859 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001860
1861 for ( ; flags && names->name; names++) {
1862 mask = names->mask;
1863 if ((flags & mask) != mask)
1864 continue;
1865
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001866 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001867
1868 flags &= ~mask;
1869 if (flags) {
1870 if (buf < end)
1871 *buf = '|';
1872 buf++;
1873 }
1874 }
1875
1876 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001877 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001878
1879 return buf;
1880}
1881
1882static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001883char *flags_string(char *buf, char *end, void *flags_ptr,
1884 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001885{
1886 unsigned long flags;
1887 const struct trace_print_flags *names;
1888
Petr Mladek3e5903e2019-04-17 13:53:48 +02001889 if (check_pointer(&buf, end, flags_ptr, spec))
1890 return buf;
1891
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001892 switch (fmt[1]) {
1893 case 'p':
1894 flags = *(unsigned long *)flags_ptr;
1895 /* Remove zone id */
1896 flags &= (1UL << NR_PAGEFLAGS) - 1;
1897 names = pageflag_names;
1898 break;
1899 case 'v':
1900 flags = *(unsigned long *)flags_ptr;
1901 names = vmaflag_names;
1902 break;
1903 case 'g':
1904 flags = *(gfp_t *)flags_ptr;
1905 names = gfpflag_names;
1906 break;
1907 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001908 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001909 }
1910
1911 return format_flags(buf, end, flags, names);
1912}
1913
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001914static noinline_for_stack
Sakari Ailusa92eb762019-10-03 15:32:16 +03001915char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1916 char *end)
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001917{
1918 int depth;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001919
Sakari Ailusa92eb762019-10-03 15:32:16 +03001920 /* Loop starting from the root node to the current node. */
1921 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1922 struct fwnode_handle *__fwnode =
1923 fwnode_get_nth_parent(fwnode, depth);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001924
Sakari Ailusa92eb762019-10-03 15:32:16 +03001925 buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001926 default_str_spec);
Sakari Ailusa92eb762019-10-03 15:32:16 +03001927 buf = string(buf, end, fwnode_get_name(__fwnode),
1928 default_str_spec);
1929
1930 fwnode_handle_put(__fwnode);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001931 }
Sakari Ailusa92eb762019-10-03 15:32:16 +03001932
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001933 return buf;
1934}
1935
1936static noinline_for_stack
1937char *device_node_string(char *buf, char *end, struct device_node *dn,
1938 struct printf_spec spec, const char *fmt)
1939{
1940 char tbuf[sizeof("xxxx") + 1];
1941 const char *p;
1942 int ret;
1943 char *buf_start = buf;
1944 struct property *prop;
1945 bool has_mult, pass;
1946 static const struct printf_spec num_spec = {
1947 .flags = SMALL,
1948 .field_width = -1,
1949 .precision = -1,
1950 .base = 10,
1951 };
1952
1953 struct printf_spec str_spec = spec;
1954 str_spec.field_width = -1;
1955
Sakari Ailus83abc5a2019-10-03 15:32:17 +03001956 if (fmt[0] != 'F')
1957 return error_string(buf, end, "(%pO?)", spec);
1958
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001959 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001960 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001961
Petr Mladek3e5903e2019-04-17 13:53:48 +02001962 if (check_pointer(&buf, end, dn, spec))
1963 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001964
1965 /* simple case without anything any more format specifiers */
1966 fmt++;
1967 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1968 fmt = "f";
1969
1970 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001971 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001972 if (pass) {
1973 if (buf < end)
1974 *buf = ':';
1975 buf++;
1976 }
1977
1978 switch (*fmt) {
1979 case 'f': /* full_name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001980 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
1981 end);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001982 break;
1983 case 'n': /* name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001984 p = fwnode_get_name(of_fwnode_handle(dn));
Rob Herring6d0a70a2018-08-27 08:13:56 -05001985 precision = str_spec.precision;
1986 str_spec.precision = strchrnul(p, '@') - p;
1987 buf = string(buf, end, p, str_spec);
1988 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001989 break;
1990 case 'p': /* phandle */
1991 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1992 break;
1993 case 'P': /* path-spec */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001994 p = fwnode_get_name(of_fwnode_handle(dn));
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001995 if (!p[1])
1996 p = "/";
1997 buf = string(buf, end, p, str_spec);
1998 break;
1999 case 'F': /* flags */
2000 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2001 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2002 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2003 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2004 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02002005 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002006 break;
2007 case 'c': /* major compatible string */
2008 ret = of_property_read_string(dn, "compatible", &p);
2009 if (!ret)
2010 buf = string(buf, end, p, str_spec);
2011 break;
2012 case 'C': /* full compatible string */
2013 has_mult = false;
2014 of_property_for_each_string(dn, "compatible", prop, p) {
2015 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02002016 buf = string_nocheck(buf, end, ",", str_spec);
2017 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002018 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02002019 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002020
2021 has_mult = true;
2022 }
2023 break;
2024 default:
2025 break;
2026 }
2027 }
2028
2029 return widen_string(buf, buf - buf_start, end, spec);
2030}
2031
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002032static noinline_for_stack
2033char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2034 struct printf_spec spec, const char *fmt)
Petr Mladek798cc272019-04-17 13:53:46 +02002035{
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002036 struct printf_spec str_spec = spec;
2037 char *buf_start = buf;
2038
2039 str_spec.field_width = -1;
2040
2041 if (*fmt != 'w')
2042 return error_string(buf, end, "(%pf?)", spec);
2043
2044 if (check_pointer(&buf, end, fwnode, spec))
2045 return buf;
2046
2047 fmt++;
2048
2049 switch (*fmt) {
2050 case 'P': /* name */
2051 buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
2052 break;
2053 case 'f': /* full_name */
2054 default:
2055 buf = fwnode_full_name_string(fwnode, buf, end);
2056 break;
Petr Mladek798cc272019-04-17 13:53:46 +02002057 }
2058
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002059 return widen_string(buf, buf - buf_start, end, spec);
Petr Mladek798cc272019-04-17 13:53:46 +02002060}
2061
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002062/*
2063 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2064 * by an extra set of alphanumeric characters that are extended format
2065 * specifiers.
2066 *
Joe Perches0b523762017-05-08 15:55:36 -07002067 * Please update scripts/checkpatch.pl when adding/removing conversion
2068 * characters. (Search for "check for vsprintf extension").
2069 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002070 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002071 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002072 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2073 * - 's' For symbolic direct pointers (or function descriptors) without offset
Sakari Ailus9af77062019-10-03 15:32:14 +03002074 * - '[Ss]R' as above with __builtin_extract_return_addr() translation
Sakari Ailus1586c5a2019-10-03 15:32:15 +03002075 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2076 * %ps and %pS. Be careful when re-using these specifiers.
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002077 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002078 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2079 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002080 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2081 * width which must be explicitly specified either as part of the
2082 * format string '%32b[l]' or through '%*b[l]', [l] selects
2083 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002084 * - 'M' For a 6-byte MAC address, it prints the address in the
2085 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002086 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002087 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002088 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002089 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002090 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2091 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2092 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002093 * [S][pfs]
2094 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2095 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002096 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2097 * IPv6 omits the colons (01020304...0f)
2098 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002099 * [S][pfs]
2100 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2101 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2102 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2103 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07002104 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002105 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2106 * of the following flags (see string_escape_mem() for the
2107 * details):
2108 * a - ESCAPE_ANY
2109 * c - ESCAPE_SPECIAL
2110 * h - ESCAPE_HEX
2111 * n - ESCAPE_NULL
2112 * o - ESCAPE_OCTAL
2113 * p - ESCAPE_NP
2114 * s - ESCAPE_SPACE
2115 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002116 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2117 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2118 * Options for %pU are:
2119 * b big endian lower case hex (default)
2120 * B big endian UPPER case hex
2121 * l little endian lower case hex
2122 * L little endian UPPER case hex
2123 * big endian output byte order is:
2124 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2125 * little endian output byte order is:
2126 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002127 * - 'V' For a struct va_format which contains a format string * and va_list *,
2128 * call vsnprintf(->format, *->va_list).
2129 * Implements a "recursive vsnprintf".
2130 * Do not use this feature without some mechanism to verify the
2131 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002132 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002133 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002134 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2135 * a certain separator (' ' by default):
2136 * C colon
2137 * D dash
2138 * N no separator
2139 * The maximum supported length is 64 bytes of the input. Consider
2140 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002141 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2142 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002143 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2144 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002145 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002146 * - 't[R][dt][r]' For time and date as represented:
2147 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002148 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2149 * (legacy clock framework) of the clock
2150 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2151 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002152 * - 'G' For flags to be printed as a collection of symbolic strings that would
2153 * construct the specific value. Supported flags given by option:
2154 * p page flags (see struct page) given as pointer to unsigned long
2155 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2156 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002157 * - 'OF[fnpPcCF]' For a device tree object
2158 * Without any optional arguments prints the full_name
2159 * f device node full_name
2160 * n device node name
2161 * p device node phandle
2162 * P device node path spec (name + @unit)
2163 * F device node flags
2164 * c major compatible string
2165 * C full compatible string
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002166 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2167 * Without an option prints the full name of the node
2168 * f full name
2169 * P node name, including a possible unit address
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002170 * - 'x' For printing the address. Equivalent to "%lx".
Daniel Borkmannb2a52122020-05-15 12:11:18 +02002171 * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2172 * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2173 * or user (u) memory to probe, and:
2174 * s a string, equivalent to "%s" on direct vsnprintf() use
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002175 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002176 * ** When making changes please also update:
2177 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002178 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002179 * Note: The default behaviour (unadorned %p) is to hash the address,
2180 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002181 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002182static noinline_for_stack
2183char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2184 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002185{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002186 switch (*fmt) {
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002187 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002188 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002189 ptr = dereference_symbol_descriptor(ptr);
2190 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002191 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002192 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002193 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002194 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002195 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002196 case 'h':
2197 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002198 case 'b':
2199 switch (fmt[1]) {
2200 case 'l':
2201 return bitmap_list_string(buf, end, ptr, spec, fmt);
2202 default:
2203 return bitmap_string(buf, end, ptr, spec, fmt);
2204 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002205 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2206 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002207 /* [mM]F (FDDI) */
2208 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002209 return mac_address_string(buf, end, ptr, spec, fmt);
2210 case 'I': /* Formatted IP supported
2211 * 4: 1.2.3.4
2212 * 6: 0001:0203:...:0708
2213 * 6c: 1::708 or 1::1.2.3.4
2214 */
2215 case 'i': /* Contiguous:
2216 * 4: 001.002.003.004
2217 * 6: 000102...0f
2218 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002219 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002220 case 'E':
2221 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002222 case 'U':
2223 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002224 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002225 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002226 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002227 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002228 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002229 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002230 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002231 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002232 case 'd':
2233 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002234 case 't':
2235 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002236 case 'C':
2237 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002238 case 'D':
Jia He36594b32019-08-09 09:24:56 +08002239 return file_dentry_name(buf, end, ptr, spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002240#ifdef CONFIG_BLOCK
2241 case 'g':
2242 return bdev_name(buf, end, ptr, spec, fmt);
2243#endif
2244
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002245 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002246 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002247 case 'O':
Sakari Ailus83abc5a2019-10-03 15:32:17 +03002248 return device_node_string(buf, end, ptr, spec, fmt + 1);
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002249 case 'f':
2250 return fwnode_string(buf, end, ptr, spec, fmt + 1);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002251 case 'x':
2252 return pointer_string(buf, end, ptr, spec);
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002253 case 'e':
2254 /* %pe with a non-ERR_PTR gets treated as plain %p */
2255 if (!IS_ERR(ptr))
2256 break;
2257 return err_ptr(buf, end, ptr, spec);
Daniel Borkmannb2a52122020-05-15 12:11:18 +02002258 case 'u':
2259 case 'k':
2260 switch (fmt[1]) {
2261 case 's':
2262 return string(buf, end, ptr, spec);
2263 default:
2264 return error_string(buf, end, "(einval)", spec);
2265 }
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002266 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002267
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002268 /* default is to _not_ leak addresses, hash before printing */
2269 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002270}
2271
2272/*
2273 * Helper function to decode printf style format.
2274 * Each call decode a token from the format and return the
2275 * number of characters read (or likely the delta where it wants
2276 * to go on the next call).
2277 * The decoded token is returned through the parameters
2278 *
2279 * 'h', 'l', or 'L' for integer fields
2280 * 'z' support added 23/7/1999 S.H.
2281 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002282 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002283 * 't' added for ptrdiff_t
2284 *
2285 * @fmt: the format string
2286 * @type of the token returned
2287 * @flags: various flags such as +, -, # tokens..
2288 * @field_width: overwritten width
2289 * @base: base of the number (octal, hex, ...)
2290 * @precision: precision of a number
2291 * @qualifier: qualifier of a number (long, size_t, ...)
2292 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002293static noinline_for_stack
2294int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002295{
2296 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002297 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002298
2299 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002300 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002301 if (spec->field_width < 0) {
2302 spec->field_width = -spec->field_width;
2303 spec->flags |= LEFT;
2304 }
2305 spec->type = FORMAT_TYPE_NONE;
2306 goto precision;
2307 }
2308
2309 /* we finished early by reading the precision */
2310 if (spec->type == FORMAT_TYPE_PRECISION) {
2311 if (spec->precision < 0)
2312 spec->precision = 0;
2313
2314 spec->type = FORMAT_TYPE_NONE;
2315 goto qualifier;
2316 }
2317
2318 /* By default */
2319 spec->type = FORMAT_TYPE_NONE;
2320
2321 for (; *fmt ; ++fmt) {
2322 if (*fmt == '%')
2323 break;
2324 }
2325
2326 /* Return the current non-format string */
2327 if (fmt != start || !*fmt)
2328 return fmt - start;
2329
2330 /* Process flags */
2331 spec->flags = 0;
2332
2333 while (1) { /* this also skips first '%' */
2334 bool found = true;
2335
2336 ++fmt;
2337
2338 switch (*fmt) {
2339 case '-': spec->flags |= LEFT; break;
2340 case '+': spec->flags |= PLUS; break;
2341 case ' ': spec->flags |= SPACE; break;
2342 case '#': spec->flags |= SPECIAL; break;
2343 case '0': spec->flags |= ZEROPAD; break;
2344 default: found = false;
2345 }
2346
2347 if (!found)
2348 break;
2349 }
2350
2351 /* get field width */
2352 spec->field_width = -1;
2353
2354 if (isdigit(*fmt))
2355 spec->field_width = skip_atoi(&fmt);
2356 else if (*fmt == '*') {
2357 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002358 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002359 return ++fmt - start;
2360 }
2361
2362precision:
2363 /* get the precision */
2364 spec->precision = -1;
2365 if (*fmt == '.') {
2366 ++fmt;
2367 if (isdigit(*fmt)) {
2368 spec->precision = skip_atoi(&fmt);
2369 if (spec->precision < 0)
2370 spec->precision = 0;
2371 } else if (*fmt == '*') {
2372 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002373 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002374 return ++fmt - start;
2375 }
2376 }
2377
2378qualifier:
2379 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002380 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002381 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002382 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002383 qualifier = *fmt++;
2384 if (unlikely(qualifier == *fmt)) {
2385 if (qualifier == 'l') {
2386 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002387 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002388 } else if (qualifier == 'h') {
2389 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002390 ++fmt;
2391 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002392 }
2393 }
2394
2395 /* default base */
2396 spec->base = 10;
2397 switch (*fmt) {
2398 case 'c':
2399 spec->type = FORMAT_TYPE_CHAR;
2400 return ++fmt - start;
2401
2402 case 's':
2403 spec->type = FORMAT_TYPE_STR;
2404 return ++fmt - start;
2405
2406 case 'p':
2407 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002408 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002409
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002410 case '%':
2411 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2412 return ++fmt - start;
2413
2414 /* integer number formats - set up the flags and "break" */
2415 case 'o':
2416 spec->base = 8;
2417 break;
2418
2419 case 'x':
2420 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002421 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002422
2423 case 'X':
2424 spec->base = 16;
2425 break;
2426
2427 case 'd':
2428 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002429 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002430 case 'u':
2431 break;
2432
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002433 case 'n':
2434 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002435 * Since %n poses a greater security risk than
2436 * utility, treat it as any other invalid or
2437 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002438 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002439 /* Fall-through */
2440
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002441 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002442 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002443 spec->type = FORMAT_TYPE_INVALID;
2444 return fmt - start;
2445 }
2446
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002447 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002448 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002449 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002450 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2451 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002452 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002453 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002454 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002455 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002456 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002457 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2458 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002459 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002460 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2461 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002462 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002463 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2464 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002465 }
2466
2467 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002468}
2469
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002470static void
2471set_field_width(struct printf_spec *spec, int width)
2472{
2473 spec->field_width = width;
2474 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2475 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2476 }
2477}
2478
2479static void
2480set_precision(struct printf_spec *spec, int prec)
2481{
2482 spec->precision = prec;
2483 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2484 spec->precision = clamp(prec, 0, PRECISION_MAX);
2485 }
2486}
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/**
2489 * vsnprintf - Format a string and place it in a buffer
2490 * @buf: The buffer to place the result into
2491 * @size: The size of the buffer, including the trailing null space
2492 * @fmt: The format string to use
2493 * @args: Arguments for the format string
2494 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002495 * This function generally follows C99 vsnprintf, but has some
2496 * extensions and a few limitations:
2497 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002498 * - ``%n`` is unsupported
2499 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002500 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002501 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002502 * extensive description.
2503 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002504 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002505 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 * The return value is the number of characters which would
2507 * be generated for the given input, excluding the trailing
2508 * '\0', as per ISO C99. If you want to have the exact
2509 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002510 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 * return is greater than or equal to @size, the resulting
2512 * string is truncated.
2513 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002514 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
2516int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2517{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002519 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002520 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002522 /* Reject out-of-range values early. Large positive sizes are
2523 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002524 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002528 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002530 /* Make sure end is always >= buf */
2531 if (end < buf) {
2532 end = ((void *)-1);
2533 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002536 while (*fmt) {
2537 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002538 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002539
2540 fmt += read;
2541
2542 switch (spec.type) {
2543 case FORMAT_TYPE_NONE: {
2544 int copy = read;
2545 if (str < end) {
2546 if (copy > end - str)
2547 copy = end - str;
2548 memcpy(str, old_fmt, copy);
2549 }
2550 str += read;
2551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553
Vegard Nossumed681a92009-03-14 12:08:50 +01002554 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002555 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002556 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002558 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002559 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
André Goddard Rosad4be1512009-12-14 18:00:59 -08002562 case FORMAT_TYPE_CHAR: {
2563 char c;
2564
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002565 if (!(spec.flags & LEFT)) {
2566 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002567 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 *str = ' ';
2569 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002572 }
2573 c = (unsigned char) va_arg(args, int);
2574 if (str < end)
2575 *str = c;
2576 ++str;
2577 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002578 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002579 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002581 }
2582 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002585 case FORMAT_TYPE_STR:
2586 str = string(str, end, va_arg(args, char *), spec);
2587 break;
2588
2589 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002590 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002591 spec);
2592 while (isalnum(*fmt))
2593 fmt++;
2594 break;
2595
2596 case FORMAT_TYPE_PERCENT_CHAR:
2597 if (str < end)
2598 *str = '%';
2599 ++str;
2600 break;
2601
2602 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002603 /*
2604 * Presumably the arguments passed gcc's type
2605 * checking, but there is no safe or sane way
2606 * for us to continue parsing the format and
2607 * fetching from the va_list; the remaining
2608 * specifiers and arguments would be out of
2609 * sync.
2610 */
2611 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002612
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002613 default:
2614 switch (spec.type) {
2615 case FORMAT_TYPE_LONG_LONG:
2616 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002618 case FORMAT_TYPE_ULONG:
2619 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002621 case FORMAT_TYPE_LONG:
2622 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002624 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002625 if (spec.flags & SIGN)
2626 num = va_arg(args, ssize_t);
2627 else
2628 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002629 break;
2630 case FORMAT_TYPE_PTRDIFF:
2631 num = va_arg(args, ptrdiff_t);
2632 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002633 case FORMAT_TYPE_UBYTE:
2634 num = (unsigned char) va_arg(args, int);
2635 break;
2636 case FORMAT_TYPE_BYTE:
2637 num = (signed char) va_arg(args, int);
2638 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002639 case FORMAT_TYPE_USHORT:
2640 num = (unsigned short) va_arg(args, int);
2641 break;
2642 case FORMAT_TYPE_SHORT:
2643 num = (short) va_arg(args, int);
2644 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002645 case FORMAT_TYPE_INT:
2646 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002649 num = va_arg(args, unsigned int);
2650 }
2651
2652 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002655
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002656out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002657 if (size > 0) {
2658 if (str < end)
2659 *str = '\0';
2660 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002661 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002662 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002663
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002664 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668EXPORT_SYMBOL(vsnprintf);
2669
2670/**
2671 * vscnprintf - Format a string and place it in a buffer
2672 * @buf: The buffer to place the result into
2673 * @size: The size of the buffer, including the trailing null space
2674 * @fmt: The format string to use
2675 * @args: Arguments for the format string
2676 *
2677 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002678 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 * returns 0.
2680 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002681 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002682 *
2683 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 */
2685int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2686{
2687 int i;
2688
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002689 i = vsnprintf(buf, size, fmt, args);
2690
Anton Arapovb921c692011-01-12 16:59:49 -08002691 if (likely(i < size))
2692 return i;
2693 if (size != 0)
2694 return size - 1;
2695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697EXPORT_SYMBOL(vscnprintf);
2698
2699/**
2700 * snprintf - Format a string and place it in a buffer
2701 * @buf: The buffer to place the result into
2702 * @size: The size of the buffer, including the trailing null space
2703 * @fmt: The format string to use
2704 * @...: Arguments for the format string
2705 *
2706 * The return value is the number of characters which would be
2707 * generated for the given input, excluding the trailing null,
2708 * as per ISO C99. If the return is greater than or equal to
2709 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002710 *
2711 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002713int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 va_list args;
2716 int i;
2717
2718 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002719 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return i;
2723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724EXPORT_SYMBOL(snprintf);
2725
2726/**
2727 * scnprintf - Format a string and place it in a buffer
2728 * @buf: The buffer to place the result into
2729 * @size: The size of the buffer, including the trailing null space
2730 * @fmt: The format string to use
2731 * @...: Arguments for the format string
2732 *
2733 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002734 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
2736
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002737int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 va_list args;
2740 int i;
2741
2742 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002743 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002745
Anton Arapovb921c692011-01-12 16:59:49 -08002746 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748EXPORT_SYMBOL(scnprintf);
2749
2750/**
2751 * vsprintf - Format a string and place it in a buffer
2752 * @buf: The buffer to place the result into
2753 * @fmt: The format string to use
2754 * @args: Arguments for the format string
2755 *
2756 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002757 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 * buffer overflows.
2759 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002760 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002761 *
2762 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 */
2764int vsprintf(char *buf, const char *fmt, va_list args)
2765{
2766 return vsnprintf(buf, INT_MAX, fmt, args);
2767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768EXPORT_SYMBOL(vsprintf);
2769
2770/**
2771 * sprintf - Format a string and place it in a buffer
2772 * @buf: The buffer to place the result into
2773 * @fmt: The format string to use
2774 * @...: Arguments for the format string
2775 *
2776 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002777 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002779 *
2780 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002782int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 va_list args;
2785 int i;
2786
2787 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002788 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return i;
2792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793EXPORT_SYMBOL(sprintf);
2794
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002795#ifdef CONFIG_BINARY_PRINTF
2796/*
2797 * bprintf service:
2798 * vbin_printf() - VA arguments to binary data
2799 * bstr_printf() - Binary data to text string
2800 */
2801
2802/**
2803 * vbin_printf - Parse a format string and place args' binary value in a buffer
2804 * @bin_buf: The buffer to place args' binary value
2805 * @size: The size of the buffer(by words(32bits), not characters)
2806 * @fmt: The format string to use
2807 * @args: Arguments for the format string
2808 *
2809 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002810 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002811 *
2812 * The return value is the number of words(32bits) which would be generated for
2813 * the given input.
2814 *
2815 * NOTE:
2816 * If the return value is greater than @size, the resulting bin_buf is NOT
2817 * valid for bstr_printf().
2818 */
2819int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2820{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002821 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002822 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002823 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002824
2825 str = (char *)bin_buf;
2826 end = (char *)(bin_buf + size);
2827
2828#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002829({ \
2830 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002831 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002832 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002833 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002834 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002835 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002836 *(u32 *)str = *(u32 *)&val8; \
2837 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002838 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002839 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002840 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002841 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002842 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002843 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002844 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002845 *(typeof(type) *)str = (type)(long)val4; \
2846 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002847 } \
2848 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002849 value; \
2850})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002851
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002852 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002853 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002854
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002855 fmt += read;
2856
2857 switch (spec.type) {
2858 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002859 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002860 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002861 case FORMAT_TYPE_INVALID:
2862 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002863
Vegard Nossumed681a92009-03-14 12:08:50 +01002864 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002865 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002866 width = (int)save_arg(int);
2867 /* Pointers may require the width */
2868 if (*fmt == 'p')
2869 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002870 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002871
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002872 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002873 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002874 break;
2875
2876 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002877 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002878 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002879 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002880
Petr Mladek3e5903e2019-04-17 13:53:48 +02002881 err_msg = check_pointer_msg(save_str);
2882 if (err_msg)
2883 save_str = err_msg;
2884
André Goddard Rosa6c356632009-12-14 18:00:56 -08002885 len = strlen(save_str) + 1;
2886 if (str + len < end)
2887 memcpy(str, save_str, len);
2888 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002889 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002890 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002891
2892 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002893 /* Dereferenced pointers must be done now */
2894 switch (*fmt) {
2895 /* Dereference of functions is still OK */
2896 case 'S':
2897 case 's':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002898 case 'x':
2899 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002900 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002901 save_arg(void *);
2902 break;
2903 default:
2904 if (!isalnum(*fmt)) {
2905 save_arg(void *);
2906 break;
2907 }
2908 str = pointer(fmt, str, end, va_arg(args, void *),
2909 spec);
2910 if (str + 1 < end)
2911 *str++ = '\0';
2912 else
2913 end[-1] = '\0'; /* Must be nul terminated */
2914 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002915 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002916 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002917 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002918 break;
2919
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002920 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002921 switch (spec.type) {
2922
2923 case FORMAT_TYPE_LONG_LONG:
2924 save_arg(long long);
2925 break;
2926 case FORMAT_TYPE_ULONG:
2927 case FORMAT_TYPE_LONG:
2928 save_arg(unsigned long);
2929 break;
2930 case FORMAT_TYPE_SIZE_T:
2931 save_arg(size_t);
2932 break;
2933 case FORMAT_TYPE_PTRDIFF:
2934 save_arg(ptrdiff_t);
2935 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002936 case FORMAT_TYPE_UBYTE:
2937 case FORMAT_TYPE_BYTE:
2938 save_arg(char);
2939 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002940 case FORMAT_TYPE_USHORT:
2941 case FORMAT_TYPE_SHORT:
2942 save_arg(short);
2943 break;
2944 default:
2945 save_arg(int);
2946 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002947 }
2948 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002949
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002950out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002951 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002952#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002953}
2954EXPORT_SYMBOL_GPL(vbin_printf);
2955
2956/**
2957 * bstr_printf - Format a string from binary arguments and place it in a buffer
2958 * @buf: The buffer to place the result into
2959 * @size: The size of the buffer, including the trailing null space
2960 * @fmt: The format string to use
2961 * @bin_buf: Binary arguments for the format string
2962 *
2963 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2964 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2965 * a binary buffer that generated by vbin_printf.
2966 *
2967 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002968 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002969 *
2970 * The return value is the number of characters which would
2971 * be generated for the given input, excluding the trailing
2972 * '\0', as per ISO C99. If you want to have the exact
2973 * number of characters written into @buf as return value
2974 * (not including the trailing '\0'), use vscnprintf(). If the
2975 * return is greater than or equal to @size, the resulting
2976 * string is truncated.
2977 */
2978int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2979{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002980 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002981 char *str, *end;
2982 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002983
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002984 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002985 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002986
2987 str = buf;
2988 end = buf + size;
2989
2990#define get_arg(type) \
2991({ \
2992 typeof(type) value; \
2993 if (sizeof(type) == 8) { \
2994 args = PTR_ALIGN(args, sizeof(u32)); \
2995 *(u32 *)&value = *(u32 *)args; \
2996 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2997 } else { \
2998 args = PTR_ALIGN(args, sizeof(type)); \
2999 value = *(typeof(type) *)args; \
3000 } \
3001 args += sizeof(type); \
3002 value; \
3003})
3004
3005 /* Make sure end is always >= buf */
3006 if (end < buf) {
3007 end = ((void *)-1);
3008 size = end - buf;
3009 }
3010
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003011 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003012 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003013 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003014
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003015 fmt += read;
3016
3017 switch (spec.type) {
3018 case FORMAT_TYPE_NONE: {
3019 int copy = read;
3020 if (str < end) {
3021 if (copy > end - str)
3022 copy = end - str;
3023 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003024 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003025 str += read;
3026 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003027 }
3028
Vegard Nossumed681a92009-03-14 12:08:50 +01003029 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003030 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003031 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003032
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003033 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003034 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003035 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003036
André Goddard Rosad4be1512009-12-14 18:00:59 -08003037 case FORMAT_TYPE_CHAR: {
3038 char c;
3039
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003040 if (!(spec.flags & LEFT)) {
3041 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003042 if (str < end)
3043 *str = ' ';
3044 ++str;
3045 }
3046 }
3047 c = (unsigned char) get_arg(char);
3048 if (str < end)
3049 *str = c;
3050 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003051 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003052 if (str < end)
3053 *str = ' ';
3054 ++str;
3055 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003056 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003057 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003058
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003059 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003060 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003061 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003062 str = string(str, end, (char *)str_arg, spec);
3063 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003064 }
3065
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003066 case FORMAT_TYPE_PTR: {
3067 bool process = false;
3068 int copy, len;
3069 /* Non function dereferences were already done */
3070 switch (*fmt) {
3071 case 'S':
3072 case 's':
3073 case 'F':
3074 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04003075 case 'x':
3076 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02003077 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003078 process = true;
3079 break;
3080 default:
3081 if (!isalnum(*fmt)) {
3082 process = true;
3083 break;
3084 }
3085 /* Pointer dereference was already processed */
3086 if (str < end) {
3087 len = copy = strlen(args);
3088 if (copy > end - str)
3089 copy = end - str;
3090 memcpy(str, args, copy);
3091 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003092 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003093 }
3094 }
3095 if (process)
3096 str = pointer(fmt, str, end, get_arg(void *), spec);
3097
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003098 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003099 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003100 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003101 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003102
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003103 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003104 if (str < end)
3105 *str = '%';
3106 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003107 break;
3108
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003109 case FORMAT_TYPE_INVALID:
3110 goto out;
3111
André Goddard Rosad4be1512009-12-14 18:00:59 -08003112 default: {
3113 unsigned long long num;
3114
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003115 switch (spec.type) {
3116
3117 case FORMAT_TYPE_LONG_LONG:
3118 num = get_arg(long long);
3119 break;
3120 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003121 case FORMAT_TYPE_LONG:
3122 num = get_arg(unsigned long);
3123 break;
3124 case FORMAT_TYPE_SIZE_T:
3125 num = get_arg(size_t);
3126 break;
3127 case FORMAT_TYPE_PTRDIFF:
3128 num = get_arg(ptrdiff_t);
3129 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003130 case FORMAT_TYPE_UBYTE:
3131 num = get_arg(unsigned char);
3132 break;
3133 case FORMAT_TYPE_BYTE:
3134 num = get_arg(signed char);
3135 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003136 case FORMAT_TYPE_USHORT:
3137 num = get_arg(unsigned short);
3138 break;
3139 case FORMAT_TYPE_SHORT:
3140 num = get_arg(short);
3141 break;
3142 case FORMAT_TYPE_UINT:
3143 num = get_arg(unsigned int);
3144 break;
3145 default:
3146 num = get_arg(int);
3147 }
3148
3149 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003150 } /* default: */
3151 } /* switch(spec.type) */
3152 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003153
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003154out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003155 if (size > 0) {
3156 if (str < end)
3157 *str = '\0';
3158 else
3159 end[-1] = '\0';
3160 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003161
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003162#undef get_arg
3163
3164 /* the trailing null byte doesn't count towards the total */
3165 return str - buf;
3166}
3167EXPORT_SYMBOL_GPL(bstr_printf);
3168
3169/**
3170 * bprintf - Parse a format string and place args' binary value in a buffer
3171 * @bin_buf: The buffer to place args' binary value
3172 * @size: The size of the buffer(by words(32bits), not characters)
3173 * @fmt: The format string to use
3174 * @...: Arguments for the format string
3175 *
3176 * The function returns the number of words(u32) written
3177 * into @bin_buf.
3178 */
3179int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3180{
3181 va_list args;
3182 int ret;
3183
3184 va_start(args, fmt);
3185 ret = vbin_printf(bin_buf, size, fmt, args);
3186 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003187
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003188 return ret;
3189}
3190EXPORT_SYMBOL_GPL(bprintf);
3191
3192#endif /* CONFIG_BINARY_PRINTF */
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194/**
3195 * vsscanf - Unformat a buffer into a list of arguments
3196 * @buf: input buffer
3197 * @fmt: format of buffer
3198 * @args: arguments
3199 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003200int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 const char *str = buf;
3203 char *next;
3204 char digit;
3205 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003206 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003207 unsigned int base;
3208 union {
3209 long long s;
3210 unsigned long long u;
3211 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003212 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003213 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Jan Beulichda990752012-10-04 17:13:24 -07003215 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 /* skip any white space in format */
3217 /* white space in format matchs any amount of
3218 * white space, including none, in the input.
3219 */
3220 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003221 fmt = skip_spaces(++fmt);
3222 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
3224
3225 /* anything that is not a conversion must match exactly */
3226 if (*fmt != '%' && *fmt) {
3227 if (*fmt++ != *str++)
3228 break;
3229 continue;
3230 }
3231
3232 if (!*fmt)
3233 break;
3234 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 /* skip this conversion.
3237 * advance both strings to next white space
3238 */
3239 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003240 if (!*str)
3241 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003242 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3243 /* '%*[' not yet supported, invalid format */
3244 if (*fmt == '[')
3245 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 while (!isspace(*str) && *str)
3249 str++;
3250 continue;
3251 }
3252
3253 /* get field width */
3254 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003255 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003257 if (field_width <= 0)
3258 break;
3259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 /* get conversion qualifier */
3262 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003263 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003264 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 qualifier = *fmt++;
3266 if (unlikely(qualifier == *fmt)) {
3267 if (qualifier == 'h') {
3268 qualifier = 'H';
3269 fmt++;
3270 } else if (qualifier == 'l') {
3271 qualifier = 'L';
3272 fmt++;
3273 }
3274 }
3275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Jan Beulichda990752012-10-04 17:13:24 -07003277 if (!*fmt)
3278 break;
3279
3280 if (*fmt == 'n') {
3281 /* return number of characters read so far */
3282 *va_arg(args, int *) = str - buf;
3283 ++fmt;
3284 continue;
3285 }
3286
3287 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 break;
3289
André Goddard Rosad4be1512009-12-14 18:00:59 -08003290 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003291 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003292
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003293 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 case 'c':
3295 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003296 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 if (field_width == -1)
3298 field_width = 1;
3299 do {
3300 *s++ = *str++;
3301 } while (--field_width > 0 && *str);
3302 num++;
3303 }
3304 continue;
3305 case 's':
3306 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003307 char *s = (char *)va_arg(args, char *);
3308 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003309 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003311 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003314 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 *s = '\0';
3317 num++;
3318 }
3319 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003320 /*
3321 * Warning: This implementation of the '[' conversion specifier
3322 * deviates from its glibc counterpart in the following ways:
3323 * (1) It does NOT support ranges i.e. '-' is NOT a special
3324 * character
3325 * (2) It cannot match the closing bracket ']' itself
3326 * (3) A field width is required
3327 * (4) '%*[' (discard matching input) is currently not supported
3328 *
3329 * Example usage:
3330 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3331 * buf1, buf2, buf3);
3332 * if (ret < 3)
3333 * // etc..
3334 */
3335 case '[':
3336 {
3337 char *s = (char *)va_arg(args, char *);
3338 DECLARE_BITMAP(set, 256) = {0};
3339 unsigned int len = 0;
3340 bool negate = (*fmt == '^');
3341
3342 /* field width is required */
3343 if (field_width == -1)
3344 return num;
3345
3346 if (negate)
3347 ++fmt;
3348
3349 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3350 set_bit((u8)*fmt, set);
3351
3352 /* no ']' or no character set found */
3353 if (!*fmt || !len)
3354 return num;
3355 ++fmt;
3356
3357 if (negate) {
3358 bitmap_complement(set, set, 256);
3359 /* exclude null '\0' byte */
3360 clear_bit(0, set);
3361 }
3362
3363 /* match must be non-empty */
3364 if (!test_bit((u8)*str, set))
3365 return num;
3366
3367 while (test_bit((u8)*str, set) && field_width--)
3368 *s++ = *str++;
3369 *s = '\0';
3370 ++num;
3371 }
3372 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 case 'o':
3374 base = 8;
3375 break;
3376 case 'x':
3377 case 'X':
3378 base = 16;
3379 break;
3380 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003381 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003382 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003384 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003385 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 case 'u':
3387 break;
3388 case '%':
3389 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003390 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 return num;
3392 continue;
3393 default:
3394 /* invalid format; stop here */
3395 return num;
3396 }
3397
3398 /* have some sort of integer conversion.
3399 * first, skip white space in buffer.
3400 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003401 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
3403 digit = *str;
3404 if (is_sign && digit == '-')
3405 digit = *(str + 1);
3406
3407 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003408 || (base == 16 && !isxdigit(digit))
3409 || (base == 10 && !isdigit(digit))
3410 || (base == 8 && (!isdigit(digit) || digit > '7'))
3411 || (base == 0 && !isdigit(digit)))
3412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Jan Beulich53809752012-12-17 16:01:31 -08003414 if (is_sign)
3415 val.s = qualifier != 'L' ?
3416 simple_strtol(str, &next, base) :
3417 simple_strtoll(str, &next, base);
3418 else
3419 val.u = qualifier != 'L' ?
3420 simple_strtoul(str, &next, base) :
3421 simple_strtoull(str, &next, base);
3422
3423 if (field_width > 0 && next - str > field_width) {
3424 if (base == 0)
3425 _parse_integer_fixup_radix(str, &base);
3426 while (next - str > field_width) {
3427 if (is_sign)
3428 val.s = div_s64(val.s, base);
3429 else
3430 val.u = div_u64(val.u, base);
3431 --next;
3432 }
3433 }
3434
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003435 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003437 if (is_sign)
3438 *va_arg(args, signed char *) = val.s;
3439 else
3440 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 break;
3442 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003443 if (is_sign)
3444 *va_arg(args, short *) = val.s;
3445 else
3446 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 break;
3448 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003449 if (is_sign)
3450 *va_arg(args, long *) = val.s;
3451 else
3452 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 break;
3454 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003455 if (is_sign)
3456 *va_arg(args, long long *) = val.s;
3457 else
3458 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003461 *va_arg(args, size_t *) = val.u;
3462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 default:
Jan Beulich53809752012-12-17 16:01:31 -08003464 if (is_sign)
3465 *va_arg(args, int *) = val.s;
3466 else
3467 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 break;
3469 }
3470 num++;
3471
3472 if (!next)
3473 break;
3474 str = next;
3475 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 return num;
3478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479EXPORT_SYMBOL(vsscanf);
3480
3481/**
3482 * sscanf - Unformat a buffer into a list of arguments
3483 * @buf: input buffer
3484 * @fmt: formatting of buffer
3485 * @...: resulting arguments
3486 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003487int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
3489 va_list args;
3490 int i;
3491
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003492 va_start(args, fmt);
3493 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 return i;
3497}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498EXPORT_SYMBOL(sscanf);