blob: dee8fc467fcfd522aedf9343fb71c667cb89b1d2 [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. */
764static char *ptr_to_id(char *buf, char *end, const void *ptr,
765 struct printf_spec spec)
766{
767 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
768 unsigned long hashval;
769
770 /* When debugging early boot use non-cryptographically secure hash. */
771 if (unlikely(debug_boot_weak_hash)) {
772 hashval = hash_long((unsigned long)ptr, 32);
773 return pointer_string(buf, end, (const void *)hashval, spec);
774 }
775
776 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
777 spec.field_width = 2 * sizeof(ptr);
778 /* string length must be less than default_width */
Petr Mladekc8c3b582019-04-17 13:53:50 +0200779 return error_string(buf, end, str, spec);
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200780 }
781
782#ifdef CONFIG_64BIT
783 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
784 /*
785 * Mask off the first 32 bits, this makes explicit that we have
786 * modified the address (and 32 bits is plenty for a unique ID).
787 */
788 hashval = hashval & 0xffffffff;
789#else
790 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
791#endif
792 return pointer_string(buf, end, (const void *)hashval, spec);
793}
794
Petr Mladek6eea2422019-04-17 13:53:41 +0200795int kptr_restrict __read_mostly;
796
797static noinline_for_stack
798char *restricted_pointer(char *buf, char *end, const void *ptr,
799 struct printf_spec spec)
800{
801 switch (kptr_restrict) {
802 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200803 /* Handle as %p, hash and do _not_ leak addresses. */
804 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200805 case 1: {
806 const struct cred *cred;
807
808 /*
809 * kptr_restrict==1 cannot be used in IRQ context
810 * because its test for CAP_SYSLOG would be meaningless.
811 */
812 if (in_irq() || in_serving_softirq() || in_nmi()) {
813 if (spec.field_width == -1)
814 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200815 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200816 }
817
818 /*
819 * Only print the real pointer value if the current
820 * process has CAP_SYSLOG and is running with the
821 * same credentials it started with. This is because
822 * access to files is checked at open() time, but %pK
823 * checks permission at read() time. We don't want to
824 * leak pointer values if a binary opens a file using
825 * %pK and then elevates privileges before reading it.
826 */
827 cred = current_cred();
828 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
829 !uid_eq(cred->euid, cred->uid) ||
830 !gid_eq(cred->egid, cred->gid))
831 ptr = NULL;
832 break;
833 }
834 case 2:
835 default:
836 /* Always print 0's for %pK */
837 ptr = NULL;
838 break;
839 }
840
841 return pointer_string(buf, end, ptr, spec);
842}
843
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200844static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400845char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
846 const char *fmt)
847{
848 const char *array[4], *s;
849 const struct dentry *p;
850 int depth;
851 int i, n;
852
853 switch (fmt[1]) {
854 case '2': case '3': case '4':
855 depth = fmt[1] - '0';
856 break;
857 default:
858 depth = 1;
859 }
860
861 rcu_read_lock();
862 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200863 if (check_pointer(&buf, end, d, spec)) {
864 rcu_read_unlock();
865 return buf;
866 }
867
Mark Rutland6aa7de02017-10-23 14:07:29 -0700868 p = READ_ONCE(d->d_parent);
869 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400870 if (p == d) {
871 if (i)
872 array[i] = "";
873 i++;
874 break;
875 }
876 }
877 s = array[--i];
878 for (n = 0; n != spec.precision; n++, buf++) {
879 char c = *s++;
880 if (!c) {
881 if (!i)
882 break;
883 c = '/';
884 s = array[--i];
885 }
886 if (buf < end)
887 *buf = c;
888 }
889 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800890 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400891}
892
Jia He36594b32019-08-09 09:24:56 +0800893static noinline_for_stack
894char *file_dentry_name(char *buf, char *end, const struct file *f,
895 struct printf_spec spec, const char *fmt)
896{
897 if (check_pointer(&buf, end, f, spec))
898 return buf;
899
900 return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
901}
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400902#ifdef CONFIG_BLOCK
903static noinline_for_stack
904char *bdev_name(char *buf, char *end, struct block_device *bdev,
905 struct printf_spec spec, const char *fmt)
906{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200907 struct gendisk *hd;
908
909 if (check_pointer(&buf, end, bdev, spec))
910 return buf;
911
912 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400913 buf = string(buf, end, hd->disk_name, spec);
914 if (bdev->bd_part->partno) {
915 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
916 if (buf < end)
917 *buf = 'p';
918 buf++;
919 }
920 buf = number(buf, end, bdev->bd_part->partno, spec);
921 }
922 return buf;
923}
924#endif
925
Joe Perchescf3b4292010-05-24 14:33:16 -0700926static noinline_for_stack
927char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800928 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700929{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800930 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700931#ifdef CONFIG_KALLSYMS
932 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800933#endif
934
935 if (fmt[1] == 'R')
936 ptr = __builtin_extract_return_addr(ptr);
937 value = (unsigned long)ptr;
938
939#ifdef CONFIG_KALLSYMS
940 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900941 sprint_backtrace(sym, value);
Sakari Ailus9af77062019-10-03 15:32:14 +0300942 else if (*fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200943 sprint_symbol(sym, value);
944 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700945 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800946
Petr Mladekd529ac42019-04-17 13:53:43 +0200947 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700948#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800949 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700950#endif
951}
952
Andy Shevchenkoabd4fe622018-02-16 23:07:05 +0200953static const struct printf_spec default_str_spec = {
954 .field_width = -1,
955 .precision = -1,
956};
957
Andy Shevchenko54433972018-02-16 23:07:06 +0200958static const struct printf_spec default_flag_spec = {
959 .base = 16,
960 .precision = -1,
961 .flags = SPECIAL | SMALL,
962};
963
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200964static const struct printf_spec default_dec_spec = {
965 .base = 10,
966 .precision = -1,
967};
968
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200969static const struct printf_spec default_dec02_spec = {
970 .base = 10,
971 .field_width = 2,
972 .precision = -1,
973 .flags = ZEROPAD,
974};
975
976static const struct printf_spec default_dec04_spec = {
977 .base = 10,
978 .field_width = 4,
979 .precision = -1,
980 .flags = ZEROPAD,
981};
982
Joe Perchescf3b4292010-05-24 14:33:16 -0700983static noinline_for_stack
984char *resource_string(char *buf, char *end, struct resource *res,
985 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100986{
987#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600988#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100989#endif
990
991#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600992#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100993#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700994 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100995 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700996 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100997 .precision = -1,
998 .flags = SPECIAL | SMALL | ZEROPAD,
999 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001000 static const struct printf_spec mem_spec = {
1001 .base = 16,
1002 .field_width = MEM_RSRC_PRINTK_SIZE,
1003 .precision = -1,
1004 .flags = SPECIAL | SMALL | ZEROPAD,
1005 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001006 static const struct printf_spec bus_spec = {
1007 .base = 16,
1008 .field_width = 2,
1009 .precision = -1,
1010 .flags = SMALL | ZEROPAD,
1011 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001012 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001013 .field_width = -1,
1014 .precision = 10,
1015 .flags = LEFT,
1016 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001017
1018 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1019 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1020#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1021#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001022#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001023#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1024 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1025 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1026
Linus Torvalds332d2e72008-10-20 15:07:34 +11001027 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001028 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001029 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +11001030
Petr Mladek3e5903e2019-04-17 13:53:48 +02001031 if (check_pointer(&buf, end, res, spec))
1032 return buf;
1033
Linus Torvalds332d2e72008-10-20 15:07:34 +11001034 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001035 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001036 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001037 specp = &io_spec;
1038 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001039 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001040 specp = &mem_spec;
1041 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001042 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001043 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001044 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001045 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001046 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001047 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001048 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001049 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001050 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001051 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001052 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001053 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001054 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001055 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001056 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001057 p = number(p, pend, resource_size(res), *specp);
1058 } else {
1059 p = number(p, pend, res->start, *specp);
1060 if (res->start != res->end) {
1061 *p++ = '-';
1062 p = number(p, pend, res->end, *specp);
1063 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001064 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001065 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001066 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001067 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001068 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001069 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001070 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001071 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001072 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001073 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001074 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001075 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001076 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001077 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001078 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001079 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001080
Petr Mladekd529ac42019-04-17 13:53:43 +02001081 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001082}
1083
Joe Perchescf3b4292010-05-24 14:33:16 -07001084static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001085char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1086 const char *fmt)
1087{
Steven Rostedt360603a2013-05-28 15:47:39 -04001088 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001089 negative value, fallback to the default */
1090 char separator;
1091
1092 if (spec.field_width == 0)
1093 /* nothing to print */
1094 return buf;
1095
Petr Mladek3e5903e2019-04-17 13:53:48 +02001096 if (check_pointer(&buf, end, addr, spec))
1097 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001098
1099 switch (fmt[1]) {
1100 case 'C':
1101 separator = ':';
1102 break;
1103 case 'D':
1104 separator = '-';
1105 break;
1106 case 'N':
1107 separator = 0;
1108 break;
1109 default:
1110 separator = ' ';
1111 break;
1112 }
1113
1114 if (spec.field_width > 0)
1115 len = min_t(int, spec.field_width, 64);
1116
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001117 for (i = 0; i < len; ++i) {
1118 if (buf < end)
1119 *buf = hex_asc_hi(addr[i]);
1120 ++buf;
1121 if (buf < end)
1122 *buf = hex_asc_lo(addr[i]);
1123 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001124
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001125 if (separator && i != len - 1) {
1126 if (buf < end)
1127 *buf = separator;
1128 ++buf;
1129 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001130 }
1131
1132 return buf;
1133}
1134
1135static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001136char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1137 struct printf_spec spec, const char *fmt)
1138{
1139 const int CHUNKSZ = 32;
1140 int nr_bits = max_t(int, spec.field_width, 0);
1141 int i, chunksz;
1142 bool first = true;
1143
Petr Mladek3e5903e2019-04-17 13:53:48 +02001144 if (check_pointer(&buf, end, bitmap, spec))
1145 return buf;
1146
Tejun Heodbc760b2015-02-13 14:36:53 -08001147 /* reused to print numbers */
1148 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1149
1150 chunksz = nr_bits & (CHUNKSZ - 1);
1151 if (chunksz == 0)
1152 chunksz = CHUNKSZ;
1153
1154 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1155 for (; i >= 0; i -= CHUNKSZ) {
1156 u32 chunkmask, val;
1157 int word, bit;
1158
1159 chunkmask = ((1ULL << chunksz) - 1);
1160 word = i / BITS_PER_LONG;
1161 bit = i % BITS_PER_LONG;
1162 val = (bitmap[word] >> bit) & chunkmask;
1163
1164 if (!first) {
1165 if (buf < end)
1166 *buf = ',';
1167 buf++;
1168 }
1169 first = false;
1170
1171 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1172 buf = number(buf, end, val, spec);
1173
1174 chunksz = CHUNKSZ;
1175 }
1176 return buf;
1177}
1178
1179static noinline_for_stack
1180char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1181 struct printf_spec spec, const char *fmt)
1182{
1183 int nr_bits = max_t(int, spec.field_width, 0);
1184 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1185 int cur, rbot, rtop;
1186 bool first = true;
1187
Petr Mladek3e5903e2019-04-17 13:53:48 +02001188 if (check_pointer(&buf, end, bitmap, spec))
1189 return buf;
1190
Tejun Heodbc760b2015-02-13 14:36:53 -08001191 rbot = cur = find_first_bit(bitmap, nr_bits);
1192 while (cur < nr_bits) {
1193 rtop = cur;
1194 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1195 if (cur < nr_bits && cur <= rtop + 1)
1196 continue;
1197
1198 if (!first) {
1199 if (buf < end)
1200 *buf = ',';
1201 buf++;
1202 }
1203 first = false;
1204
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001205 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001206 if (rbot < rtop) {
1207 if (buf < end)
1208 *buf = '-';
1209 buf++;
1210
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001211 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001212 }
1213
1214 rbot = cur;
1215 }
1216 return buf;
1217}
1218
1219static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001220char *mac_address_string(char *buf, char *end, u8 *addr,
1221 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001222{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001223 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001224 char *p = mac_addr;
1225 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001226 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001227 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001228
Petr Mladek3e5903e2019-04-17 13:53:48 +02001229 if (check_pointer(&buf, end, addr, spec))
1230 return buf;
1231
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001232 switch (fmt[1]) {
1233 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001234 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001235 break;
1236
1237 case 'R':
1238 reversed = true;
1239 /* fall through */
1240
1241 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001242 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001243 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001244 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001245
1246 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001247 if (reversed)
1248 p = hex_byte_pack(p, addr[5 - i]);
1249 else
1250 p = hex_byte_pack(p, addr[i]);
1251
Joe Perches8a27f7c2009-08-17 12:29:44 +00001252 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001253 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001254 }
1255 *p = '\0';
1256
Petr Mladekd529ac42019-04-17 13:53:43 +02001257 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001258}
1259
Joe Perchescf3b4292010-05-24 14:33:16 -07001260static noinline_for_stack
1261char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001262{
Harvey Harrison689afa72008-10-28 16:04:44 -07001263 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001264 bool leading_zeros = (fmt[0] == 'i');
1265 int index;
1266 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001267
Joe Perches0159f242010-01-13 20:23:30 -08001268 switch (fmt[2]) {
1269 case 'h':
1270#ifdef __BIG_ENDIAN
1271 index = 0;
1272 step = 1;
1273#else
1274 index = 3;
1275 step = -1;
1276#endif
1277 break;
1278 case 'l':
1279 index = 3;
1280 step = -1;
1281 break;
1282 case 'n':
1283 case 'b':
1284 default:
1285 index = 0;
1286 step = 1;
1287 break;
1288 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001289 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001290 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001291 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001292 if (leading_zeros) {
1293 if (digits < 3)
1294 *p++ = '0';
1295 if (digits < 2)
1296 *p++ = '0';
1297 }
1298 /* reverse the digits in the quad */
1299 while (digits--)
1300 *p++ = temp[digits];
1301 if (i < 3)
1302 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001303 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001304 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001305 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001306
Joe Perches8a27f7c2009-08-17 12:29:44 +00001307 return p;
1308}
1309
Joe Perchescf3b4292010-05-24 14:33:16 -07001310static noinline_for_stack
1311char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001312{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001313 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001314 unsigned char zerolength[8];
1315 int longest = 1;
1316 int colonpos = -1;
1317 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001318 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001319 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001320 bool useIPv4;
1321 struct in6_addr in6;
1322
1323 memcpy(&in6, addr, sizeof(struct in6_addr));
1324
1325 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001326
1327 memset(zerolength, 0, sizeof(zerolength));
1328
1329 if (useIPv4)
1330 range = 6;
1331 else
1332 range = 8;
1333
1334 /* find position of longest 0 run */
1335 for (i = 0; i < range; i++) {
1336 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001337 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001338 break;
1339 zerolength[i]++;
1340 }
1341 }
1342 for (i = 0; i < range; i++) {
1343 if (zerolength[i] > longest) {
1344 longest = zerolength[i];
1345 colonpos = i;
1346 }
1347 }
Joe Perches29cf5192011-06-09 11:23:37 -07001348 if (longest == 1) /* don't compress a single 0 */
1349 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001350
1351 /* emit address */
1352 for (i = 0; i < range; i++) {
1353 if (i == colonpos) {
1354 if (needcolon || i == 0)
1355 *p++ = ':';
1356 *p++ = ':';
1357 needcolon = false;
1358 i += longest - 1;
1359 continue;
1360 }
1361 if (needcolon) {
1362 *p++ = ':';
1363 needcolon = false;
1364 }
1365 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001366 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001367 hi = word >> 8;
1368 lo = word & 0xff;
1369 if (hi) {
1370 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001371 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001372 else
1373 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001374 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001375 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001376 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001377 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001378 else
1379 *p++ = hex_asc_lo(lo);
1380 needcolon = true;
1381 }
1382
1383 if (useIPv4) {
1384 if (needcolon)
1385 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001386 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001387 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001388 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001389
Joe Perches8a27f7c2009-08-17 12:29:44 +00001390 return p;
1391}
1392
Joe Perchescf3b4292010-05-24 14:33:16 -07001393static noinline_for_stack
1394char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001395{
1396 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001397
Harvey Harrison689afa72008-10-28 16:04:44 -07001398 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001399 p = hex_byte_pack(p, *addr++);
1400 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001401 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001402 *p++ = ':';
1403 }
1404 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001405
Joe Perches8a27f7c2009-08-17 12:29:44 +00001406 return p;
1407}
1408
Joe Perchescf3b4292010-05-24 14:33:16 -07001409static noinline_for_stack
1410char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1411 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001412{
1413 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1414
1415 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001416 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001417 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001418 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001419
Petr Mladekd529ac42019-04-17 13:53:43 +02001420 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001421}
1422
Joe Perchescf3b4292010-05-24 14:33:16 -07001423static noinline_for_stack
1424char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1425 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001426{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001427 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001428
Joe Perches0159f242010-01-13 20:23:30 -08001429 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001430
Petr Mladekd529ac42019-04-17 13:53:43 +02001431 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001432}
1433
Joe Perchescf3b4292010-05-24 14:33:16 -07001434static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001435char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1436 struct printf_spec spec, const char *fmt)
1437{
1438 bool have_p = false, have_s = false, have_f = false, have_c = false;
1439 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1440 sizeof(":12345") + sizeof("/123456789") +
1441 sizeof("%1234567890")];
1442 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1443 const u8 *addr = (const u8 *) &sa->sin6_addr;
1444 char fmt6[2] = { fmt[0], '6' };
1445 u8 off = 0;
1446
1447 fmt++;
1448 while (isalpha(*++fmt)) {
1449 switch (*fmt) {
1450 case 'p':
1451 have_p = true;
1452 break;
1453 case 'f':
1454 have_f = true;
1455 break;
1456 case 's':
1457 have_s = true;
1458 break;
1459 case 'c':
1460 have_c = true;
1461 break;
1462 }
1463 }
1464
1465 if (have_p || have_s || have_f) {
1466 *p = '[';
1467 off = 1;
1468 }
1469
1470 if (fmt6[0] == 'I' && have_c)
1471 p = ip6_compressed_string(ip6_addr + off, addr);
1472 else
1473 p = ip6_string(ip6_addr + off, addr, fmt6);
1474
1475 if (have_p || have_s || have_f)
1476 *p++ = ']';
1477
1478 if (have_p) {
1479 *p++ = ':';
1480 p = number(p, pend, ntohs(sa->sin6_port), spec);
1481 }
1482 if (have_f) {
1483 *p++ = '/';
1484 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1485 IPV6_FLOWINFO_MASK), spec);
1486 }
1487 if (have_s) {
1488 *p++ = '%';
1489 p = number(p, pend, sa->sin6_scope_id, spec);
1490 }
1491 *p = '\0';
1492
Petr Mladekd529ac42019-04-17 13:53:43 +02001493 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001494}
1495
1496static noinline_for_stack
1497char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1498 struct printf_spec spec, const char *fmt)
1499{
1500 bool have_p = false;
1501 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1502 char *pend = ip4_addr + sizeof(ip4_addr);
1503 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1504 char fmt4[3] = { fmt[0], '4', 0 };
1505
1506 fmt++;
1507 while (isalpha(*++fmt)) {
1508 switch (*fmt) {
1509 case 'p':
1510 have_p = true;
1511 break;
1512 case 'h':
1513 case 'l':
1514 case 'n':
1515 case 'b':
1516 fmt4[2] = *fmt;
1517 break;
1518 }
1519 }
1520
1521 p = ip4_string(ip4_addr, addr, fmt4);
1522 if (have_p) {
1523 *p++ = ':';
1524 p = number(p, pend, ntohs(sa->sin_port), spec);
1525 }
1526 *p = '\0';
1527
Petr Mladekd529ac42019-04-17 13:53:43 +02001528 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001529}
1530
1531static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001532char *ip_addr_string(char *buf, char *end, const void *ptr,
1533 struct printf_spec spec, const char *fmt)
1534{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001535 char *err_fmt_msg;
1536
Petr Mladek3e5903e2019-04-17 13:53:48 +02001537 if (check_pointer(&buf, end, ptr, spec))
1538 return buf;
1539
Petr Mladekf00cc102019-04-17 13:53:44 +02001540 switch (fmt[1]) {
1541 case '6':
1542 return ip6_addr_string(buf, end, ptr, spec, fmt);
1543 case '4':
1544 return ip4_addr_string(buf, end, ptr, spec, fmt);
1545 case 'S': {
1546 const union {
1547 struct sockaddr raw;
1548 struct sockaddr_in v4;
1549 struct sockaddr_in6 v6;
1550 } *sa = ptr;
1551
1552 switch (sa->raw.sa_family) {
1553 case AF_INET:
1554 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1555 case AF_INET6:
1556 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1557 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001558 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001559 }}
1560 }
1561
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001562 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001563 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001564}
1565
1566static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001567char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1568 const char *fmt)
1569{
1570 bool found = true;
1571 int count = 1;
1572 unsigned int flags = 0;
1573 int len;
1574
1575 if (spec.field_width == 0)
1576 return buf; /* nothing to print */
1577
Petr Mladek3e5903e2019-04-17 13:53:48 +02001578 if (check_pointer(&buf, end, addr, spec))
1579 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001580
1581 do {
1582 switch (fmt[count++]) {
1583 case 'a':
1584 flags |= ESCAPE_ANY;
1585 break;
1586 case 'c':
1587 flags |= ESCAPE_SPECIAL;
1588 break;
1589 case 'h':
1590 flags |= ESCAPE_HEX;
1591 break;
1592 case 'n':
1593 flags |= ESCAPE_NULL;
1594 break;
1595 case 'o':
1596 flags |= ESCAPE_OCTAL;
1597 break;
1598 case 'p':
1599 flags |= ESCAPE_NP;
1600 break;
1601 case 's':
1602 flags |= ESCAPE_SPACE;
1603 break;
1604 default:
1605 found = false;
1606 break;
1607 }
1608 } while (found);
1609
1610 if (!flags)
1611 flags = ESCAPE_ANY_NP;
1612
1613 len = spec.field_width < 0 ? 1 : spec.field_width;
1614
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001615 /*
1616 * string_escape_mem() writes as many characters as it can to
1617 * the given buffer, and returns the total size of the output
1618 * had the buffer been big enough.
1619 */
1620 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001621
1622 return buf;
1623}
1624
Petr Mladek3e5903e2019-04-17 13:53:48 +02001625static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1626 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001627{
1628 va_list va;
1629
Petr Mladek3e5903e2019-04-17 13:53:48 +02001630 if (check_pointer(&buf, end, va_fmt, spec))
1631 return buf;
1632
Petr Mladek45c3e932019-04-17 13:53:45 +02001633 va_copy(va, *va_fmt->va);
1634 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1635 va_end(va);
1636
1637 return buf;
1638}
1639
Andy Shevchenko71dca952014-10-13 15:55:18 -07001640static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001641char *uuid_string(char *buf, char *end, const u8 *addr,
1642 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001643{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001644 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001645 char *p = uuid;
1646 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001647 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001648 bool uc = false;
1649
Petr Mladek3e5903e2019-04-17 13:53:48 +02001650 if (check_pointer(&buf, end, addr, spec))
1651 return buf;
1652
Joe Perches9ac6e442009-12-14 18:01:09 -08001653 switch (*(++fmt)) {
1654 case 'L':
1655 uc = true; /* fall-through */
1656 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001657 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001658 break;
1659 case 'B':
1660 uc = true;
1661 break;
1662 }
1663
1664 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001665 if (uc)
1666 p = hex_byte_pack_upper(p, addr[index[i]]);
1667 else
1668 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001669 switch (i) {
1670 case 3:
1671 case 5:
1672 case 7:
1673 case 9:
1674 *p++ = '-';
1675 break;
1676 }
1677 }
1678
1679 *p = 0;
1680
Petr Mladekd529ac42019-04-17 13:53:43 +02001681 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001682}
1683
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001684static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001685char *netdev_bits(char *buf, char *end, const void *addr,
1686 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001687{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001688 unsigned long long num;
1689 int size;
1690
Petr Mladek3e5903e2019-04-17 13:53:48 +02001691 if (check_pointer(&buf, end, addr, spec))
1692 return buf;
1693
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001694 switch (fmt[1]) {
1695 case 'F':
1696 num = *(const netdev_features_t *)addr;
1697 size = sizeof(netdev_features_t);
1698 break;
1699 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001700 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001701 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001702
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001703 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001704}
1705
Joe Perchesaaf07622014-01-23 15:54:17 -08001706static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001707char *address_val(char *buf, char *end, const void *addr,
1708 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001709{
1710 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001711 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001712
Petr Mladek3e5903e2019-04-17 13:53:48 +02001713 if (check_pointer(&buf, end, addr, spec))
1714 return buf;
1715
Joe Perchesaaf07622014-01-23 15:54:17 -08001716 switch (fmt[1]) {
1717 case 'd':
1718 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001719 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001720 break;
1721 case 'p':
1722 default:
1723 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001724 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001725 break;
1726 }
1727
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001728 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001729}
1730
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001731static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001732char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1733{
1734 int year = tm->tm_year + (r ? 0 : 1900);
1735 int mon = tm->tm_mon + (r ? 0 : 1);
1736
1737 buf = number(buf, end, year, default_dec04_spec);
1738 if (buf < end)
1739 *buf = '-';
1740 buf++;
1741
1742 buf = number(buf, end, mon, default_dec02_spec);
1743 if (buf < end)
1744 *buf = '-';
1745 buf++;
1746
1747 return number(buf, end, tm->tm_mday, default_dec02_spec);
1748}
1749
1750static noinline_for_stack
1751char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1752{
1753 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1754 if (buf < end)
1755 *buf = ':';
1756 buf++;
1757
1758 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1759 if (buf < end)
1760 *buf = ':';
1761 buf++;
1762
1763 return number(buf, end, tm->tm_sec, default_dec02_spec);
1764}
1765
1766static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001767char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1768 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001769{
1770 bool have_t = true, have_d = true;
1771 bool raw = false;
1772 int count = 2;
1773
Petr Mladek3e5903e2019-04-17 13:53:48 +02001774 if (check_pointer(&buf, end, tm, spec))
1775 return buf;
1776
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001777 switch (fmt[count]) {
1778 case 'd':
1779 have_t = false;
1780 count++;
1781 break;
1782 case 't':
1783 have_d = false;
1784 count++;
1785 break;
1786 }
1787
1788 raw = fmt[count] == 'r';
1789
1790 if (have_d)
1791 buf = date_str(buf, end, tm, raw);
1792 if (have_d && have_t) {
1793 /* Respect ISO 8601 */
1794 if (buf < end)
1795 *buf = 'T';
1796 buf++;
1797 }
1798 if (have_t)
1799 buf = time_str(buf, end, tm, raw);
1800
1801 return buf;
1802}
1803
1804static noinline_for_stack
1805char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1806 const char *fmt)
1807{
1808 switch (fmt[1]) {
1809 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001810 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001811 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001812 return error_string(buf, end, "(%ptR?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001813 }
1814}
1815
1816static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001817char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1818 const char *fmt)
1819{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001820 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001821 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001822
Petr Mladek3e5903e2019-04-17 13:53:48 +02001823 if (check_pointer(&buf, end, clk, spec))
1824 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001825
1826 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001827 case 'n':
1828 default:
1829#ifdef CONFIG_COMMON_CLK
1830 return string(buf, end, __clk_get_name(clk), spec);
1831#else
Geert Uytterhoeven4ca96aa2019-07-01 16:00:09 +02001832 return ptr_to_id(buf, end, clk, spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001833#endif
1834 }
1835}
1836
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001837static
1838char *format_flags(char *buf, char *end, unsigned long flags,
1839 const struct trace_print_flags *names)
1840{
1841 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001842
1843 for ( ; flags && names->name; names++) {
1844 mask = names->mask;
1845 if ((flags & mask) != mask)
1846 continue;
1847
Andy Shevchenkoabd4fe622018-02-16 23:07:05 +02001848 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001849
1850 flags &= ~mask;
1851 if (flags) {
1852 if (buf < end)
1853 *buf = '|';
1854 buf++;
1855 }
1856 }
1857
1858 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001859 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001860
1861 return buf;
1862}
1863
1864static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001865char *flags_string(char *buf, char *end, void *flags_ptr,
1866 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001867{
1868 unsigned long flags;
1869 const struct trace_print_flags *names;
1870
Petr Mladek3e5903e2019-04-17 13:53:48 +02001871 if (check_pointer(&buf, end, flags_ptr, spec))
1872 return buf;
1873
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001874 switch (fmt[1]) {
1875 case 'p':
1876 flags = *(unsigned long *)flags_ptr;
1877 /* Remove zone id */
1878 flags &= (1UL << NR_PAGEFLAGS) - 1;
1879 names = pageflag_names;
1880 break;
1881 case 'v':
1882 flags = *(unsigned long *)flags_ptr;
1883 names = vmaflag_names;
1884 break;
1885 case 'g':
1886 flags = *(gfp_t *)flags_ptr;
1887 names = gfpflag_names;
1888 break;
1889 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001890 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001891 }
1892
1893 return format_flags(buf, end, flags, names);
1894}
1895
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001896static noinline_for_stack
Sakari Ailusa92eb762019-10-03 15:32:16 +03001897char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1898 char *end)
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001899{
1900 int depth;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001901
Sakari Ailusa92eb762019-10-03 15:32:16 +03001902 /* Loop starting from the root node to the current node. */
1903 for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1904 struct fwnode_handle *__fwnode =
1905 fwnode_get_nth_parent(fwnode, depth);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001906
Sakari Ailusa92eb762019-10-03 15:32:16 +03001907 buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
Andy Shevchenkoabd4fe622018-02-16 23:07:05 +02001908 default_str_spec);
Sakari Ailusa92eb762019-10-03 15:32:16 +03001909 buf = string(buf, end, fwnode_get_name(__fwnode),
1910 default_str_spec);
1911
1912 fwnode_handle_put(__fwnode);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001913 }
Sakari Ailusa92eb762019-10-03 15:32:16 +03001914
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001915 return buf;
1916}
1917
1918static noinline_for_stack
1919char *device_node_string(char *buf, char *end, struct device_node *dn,
1920 struct printf_spec spec, const char *fmt)
1921{
1922 char tbuf[sizeof("xxxx") + 1];
1923 const char *p;
1924 int ret;
1925 char *buf_start = buf;
1926 struct property *prop;
1927 bool has_mult, pass;
1928 static const struct printf_spec num_spec = {
1929 .flags = SMALL,
1930 .field_width = -1,
1931 .precision = -1,
1932 .base = 10,
1933 };
1934
1935 struct printf_spec str_spec = spec;
1936 str_spec.field_width = -1;
1937
Sakari Ailus83abc5a2019-10-03 15:32:17 +03001938 if (fmt[0] != 'F')
1939 return error_string(buf, end, "(%pO?)", spec);
1940
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001941 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001942 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001943
Petr Mladek3e5903e2019-04-17 13:53:48 +02001944 if (check_pointer(&buf, end, dn, spec))
1945 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001946
1947 /* simple case without anything any more format specifiers */
1948 fmt++;
1949 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1950 fmt = "f";
1951
1952 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001953 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001954 if (pass) {
1955 if (buf < end)
1956 *buf = ':';
1957 buf++;
1958 }
1959
1960 switch (*fmt) {
1961 case 'f': /* full_name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001962 buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
1963 end);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001964 break;
1965 case 'n': /* name */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001966 p = fwnode_get_name(of_fwnode_handle(dn));
Rob Herring6d0a70a2018-08-27 08:13:56 -05001967 precision = str_spec.precision;
1968 str_spec.precision = strchrnul(p, '@') - p;
1969 buf = string(buf, end, p, str_spec);
1970 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001971 break;
1972 case 'p': /* phandle */
1973 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1974 break;
1975 case 'P': /* path-spec */
Sakari Ailusa92eb762019-10-03 15:32:16 +03001976 p = fwnode_get_name(of_fwnode_handle(dn));
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001977 if (!p[1])
1978 p = "/";
1979 buf = string(buf, end, p, str_spec);
1980 break;
1981 case 'F': /* flags */
1982 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1983 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1984 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1985 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1986 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02001987 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001988 break;
1989 case 'c': /* major compatible string */
1990 ret = of_property_read_string(dn, "compatible", &p);
1991 if (!ret)
1992 buf = string(buf, end, p, str_spec);
1993 break;
1994 case 'C': /* full compatible string */
1995 has_mult = false;
1996 of_property_for_each_string(dn, "compatible", prop, p) {
1997 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02001998 buf = string_nocheck(buf, end, ",", str_spec);
1999 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002000 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02002001 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002002
2003 has_mult = true;
2004 }
2005 break;
2006 default:
2007 break;
2008 }
2009 }
2010
2011 return widen_string(buf, buf - buf_start, end, spec);
2012}
2013
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002014static noinline_for_stack
2015char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2016 struct printf_spec spec, const char *fmt)
Petr Mladek798cc272019-04-17 13:53:46 +02002017{
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002018 struct printf_spec str_spec = spec;
2019 char *buf_start = buf;
2020
2021 str_spec.field_width = -1;
2022
2023 if (*fmt != 'w')
2024 return error_string(buf, end, "(%pf?)", spec);
2025
2026 if (check_pointer(&buf, end, fwnode, spec))
2027 return buf;
2028
2029 fmt++;
2030
2031 switch (*fmt) {
2032 case 'P': /* name */
2033 buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
2034 break;
2035 case 'f': /* full_name */
2036 default:
2037 buf = fwnode_full_name_string(fwnode, buf, end);
2038 break;
Petr Mladek798cc272019-04-17 13:53:46 +02002039 }
2040
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002041 return widen_string(buf, buf - buf_start, end, spec);
Petr Mladek798cc272019-04-17 13:53:46 +02002042}
2043
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002044/*
2045 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2046 * by an extra set of alphanumeric characters that are extended format
2047 * specifiers.
2048 *
Joe Perches0b523762017-05-08 15:55:36 -07002049 * Please update scripts/checkpatch.pl when adding/removing conversion
2050 * characters. (Search for "check for vsprintf extension").
2051 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002052 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002053 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002054 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2055 * - 's' For symbolic direct pointers (or function descriptors) without offset
Sakari Ailus9af77062019-10-03 15:32:14 +03002056 * - '[Ss]R' as above with __builtin_extract_return_addr() translation
Sakari Ailus1586c5a2019-10-03 15:32:15 +03002057 * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
2058 * %ps and %pS. Be careful when re-using these specifiers.
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002059 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002060 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2061 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002062 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2063 * width which must be explicitly specified either as part of the
2064 * format string '%32b[l]' or through '%*b[l]', [l] selects
2065 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002066 * - 'M' For a 6-byte MAC address, it prints the address in the
2067 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002068 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002069 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002070 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002071 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002072 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2073 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2074 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002075 * [S][pfs]
2076 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2077 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002078 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2079 * IPv6 omits the colons (01020304...0f)
2080 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002081 * [S][pfs]
2082 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2083 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2084 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2085 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07002086 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002087 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2088 * of the following flags (see string_escape_mem() for the
2089 * details):
2090 * a - ESCAPE_ANY
2091 * c - ESCAPE_SPECIAL
2092 * h - ESCAPE_HEX
2093 * n - ESCAPE_NULL
2094 * o - ESCAPE_OCTAL
2095 * p - ESCAPE_NP
2096 * s - ESCAPE_SPACE
2097 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002098 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2099 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2100 * Options for %pU are:
2101 * b big endian lower case hex (default)
2102 * B big endian UPPER case hex
2103 * l little endian lower case hex
2104 * L little endian UPPER case hex
2105 * big endian output byte order is:
2106 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2107 * little endian output byte order is:
2108 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002109 * - 'V' For a struct va_format which contains a format string * and va_list *,
2110 * call vsnprintf(->format, *->va_list).
2111 * Implements a "recursive vsnprintf".
2112 * Do not use this feature without some mechanism to verify the
2113 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002114 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002115 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002116 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2117 * a certain separator (' ' by default):
2118 * C colon
2119 * D dash
2120 * N no separator
2121 * The maximum supported length is 64 bytes of the input. Consider
2122 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002123 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2124 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002125 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2126 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002127 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002128 * - 't[R][dt][r]' For time and date as represented:
2129 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002130 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2131 * (legacy clock framework) of the clock
2132 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2133 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002134 * - 'G' For flags to be printed as a collection of symbolic strings that would
2135 * construct the specific value. Supported flags given by option:
2136 * p page flags (see struct page) given as pointer to unsigned long
2137 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2138 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002139 * - 'OF[fnpPcCF]' For a device tree object
2140 * Without any optional arguments prints the full_name
2141 * f device node full_name
2142 * n device node name
2143 * p device node phandle
2144 * P device node path spec (name + @unit)
2145 * F device node flags
2146 * c major compatible string
2147 * C full compatible string
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002148 * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2149 * Without an option prints the full name of the node
2150 * f full name
2151 * P node name, including a possible unit address
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002152 * - 'x' For printing the address. Equivalent to "%lx".
2153 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002154 * ** When making changes please also update:
2155 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002156 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002157 * Note: The default behaviour (unadorned %p) is to hash the address,
2158 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002159 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002160static noinline_for_stack
2161char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2162 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002163{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002164 switch (*fmt) {
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002165 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002166 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002167 ptr = dereference_symbol_descriptor(ptr);
2168 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002169 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002170 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002171 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002172 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002173 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002174 case 'h':
2175 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002176 case 'b':
2177 switch (fmt[1]) {
2178 case 'l':
2179 return bitmap_list_string(buf, end, ptr, spec, fmt);
2180 default:
2181 return bitmap_string(buf, end, ptr, spec, fmt);
2182 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002183 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2184 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002185 /* [mM]F (FDDI) */
2186 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002187 return mac_address_string(buf, end, ptr, spec, fmt);
2188 case 'I': /* Formatted IP supported
2189 * 4: 1.2.3.4
2190 * 6: 0001:0203:...:0708
2191 * 6c: 1::708 or 1::1.2.3.4
2192 */
2193 case 'i': /* Contiguous:
2194 * 4: 001.002.003.004
2195 * 6: 000102...0f
2196 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002197 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002198 case 'E':
2199 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002200 case 'U':
2201 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002202 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002203 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002204 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002205 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002206 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002207 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002208 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002209 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002210 case 'd':
2211 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002212 case 't':
2213 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002214 case 'C':
2215 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002216 case 'D':
Jia He36594b32019-08-09 09:24:56 +08002217 return file_dentry_name(buf, end, ptr, spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002218#ifdef CONFIG_BLOCK
2219 case 'g':
2220 return bdev_name(buf, end, ptr, spec, fmt);
2221#endif
2222
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002223 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002224 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002225 case 'O':
Sakari Ailus83abc5a2019-10-03 15:32:17 +03002226 return device_node_string(buf, end, ptr, spec, fmt + 1);
Sakari Ailus3bd32d62019-10-03 15:32:18 +03002227 case 'f':
2228 return fwnode_string(buf, end, ptr, spec, fmt + 1);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002229 case 'x':
2230 return pointer_string(buf, end, ptr, spec);
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002231 case 'e':
2232 /* %pe with a non-ERR_PTR gets treated as plain %p */
2233 if (!IS_ERR(ptr))
2234 break;
2235 return err_ptr(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002236 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002237
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002238 /* default is to _not_ leak addresses, hash before printing */
2239 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002240}
2241
2242/*
2243 * Helper function to decode printf style format.
2244 * Each call decode a token from the format and return the
2245 * number of characters read (or likely the delta where it wants
2246 * to go on the next call).
2247 * The decoded token is returned through the parameters
2248 *
2249 * 'h', 'l', or 'L' for integer fields
2250 * 'z' support added 23/7/1999 S.H.
2251 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002252 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002253 * 't' added for ptrdiff_t
2254 *
2255 * @fmt: the format string
2256 * @type of the token returned
2257 * @flags: various flags such as +, -, # tokens..
2258 * @field_width: overwritten width
2259 * @base: base of the number (octal, hex, ...)
2260 * @precision: precision of a number
2261 * @qualifier: qualifier of a number (long, size_t, ...)
2262 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002263static noinline_for_stack
2264int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002265{
2266 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002267 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002268
2269 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002270 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002271 if (spec->field_width < 0) {
2272 spec->field_width = -spec->field_width;
2273 spec->flags |= LEFT;
2274 }
2275 spec->type = FORMAT_TYPE_NONE;
2276 goto precision;
2277 }
2278
2279 /* we finished early by reading the precision */
2280 if (spec->type == FORMAT_TYPE_PRECISION) {
2281 if (spec->precision < 0)
2282 spec->precision = 0;
2283
2284 spec->type = FORMAT_TYPE_NONE;
2285 goto qualifier;
2286 }
2287
2288 /* By default */
2289 spec->type = FORMAT_TYPE_NONE;
2290
2291 for (; *fmt ; ++fmt) {
2292 if (*fmt == '%')
2293 break;
2294 }
2295
2296 /* Return the current non-format string */
2297 if (fmt != start || !*fmt)
2298 return fmt - start;
2299
2300 /* Process flags */
2301 spec->flags = 0;
2302
2303 while (1) { /* this also skips first '%' */
2304 bool found = true;
2305
2306 ++fmt;
2307
2308 switch (*fmt) {
2309 case '-': spec->flags |= LEFT; break;
2310 case '+': spec->flags |= PLUS; break;
2311 case ' ': spec->flags |= SPACE; break;
2312 case '#': spec->flags |= SPECIAL; break;
2313 case '0': spec->flags |= ZEROPAD; break;
2314 default: found = false;
2315 }
2316
2317 if (!found)
2318 break;
2319 }
2320
2321 /* get field width */
2322 spec->field_width = -1;
2323
2324 if (isdigit(*fmt))
2325 spec->field_width = skip_atoi(&fmt);
2326 else if (*fmt == '*') {
2327 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002328 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002329 return ++fmt - start;
2330 }
2331
2332precision:
2333 /* get the precision */
2334 spec->precision = -1;
2335 if (*fmt == '.') {
2336 ++fmt;
2337 if (isdigit(*fmt)) {
2338 spec->precision = skip_atoi(&fmt);
2339 if (spec->precision < 0)
2340 spec->precision = 0;
2341 } else if (*fmt == '*') {
2342 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002343 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002344 return ++fmt - start;
2345 }
2346 }
2347
2348qualifier:
2349 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002350 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002351 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002352 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002353 qualifier = *fmt++;
2354 if (unlikely(qualifier == *fmt)) {
2355 if (qualifier == 'l') {
2356 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002357 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002358 } else if (qualifier == 'h') {
2359 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002360 ++fmt;
2361 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002362 }
2363 }
2364
2365 /* default base */
2366 spec->base = 10;
2367 switch (*fmt) {
2368 case 'c':
2369 spec->type = FORMAT_TYPE_CHAR;
2370 return ++fmt - start;
2371
2372 case 's':
2373 spec->type = FORMAT_TYPE_STR;
2374 return ++fmt - start;
2375
2376 case 'p':
2377 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002378 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002379
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002380 case '%':
2381 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2382 return ++fmt - start;
2383
2384 /* integer number formats - set up the flags and "break" */
2385 case 'o':
2386 spec->base = 8;
2387 break;
2388
2389 case 'x':
2390 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002391 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002392
2393 case 'X':
2394 spec->base = 16;
2395 break;
2396
2397 case 'd':
2398 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002399 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002400 case 'u':
2401 break;
2402
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002403 case 'n':
2404 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002405 * Since %n poses a greater security risk than
2406 * utility, treat it as any other invalid or
2407 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002408 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002409 /* Fall-through */
2410
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002411 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002412 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002413 spec->type = FORMAT_TYPE_INVALID;
2414 return fmt - start;
2415 }
2416
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002417 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002418 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002419 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002420 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2421 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002422 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002423 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002424 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002425 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002426 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002427 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2428 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002429 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002430 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2431 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002432 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002433 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2434 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002435 }
2436
2437 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002438}
2439
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002440static void
2441set_field_width(struct printf_spec *spec, int width)
2442{
2443 spec->field_width = width;
2444 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2445 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2446 }
2447}
2448
2449static void
2450set_precision(struct printf_spec *spec, int prec)
2451{
2452 spec->precision = prec;
2453 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2454 spec->precision = clamp(prec, 0, PRECISION_MAX);
2455 }
2456}
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458/**
2459 * vsnprintf - Format a string and place it in a buffer
2460 * @buf: The buffer to place the result into
2461 * @size: The size of the buffer, including the trailing null space
2462 * @fmt: The format string to use
2463 * @args: Arguments for the format string
2464 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002465 * This function generally follows C99 vsnprintf, but has some
2466 * extensions and a few limitations:
2467 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002468 * - ``%n`` is unsupported
2469 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002470 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002471 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002472 * extensive description.
2473 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002474 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002475 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 * The return value is the number of characters which would
2477 * be generated for the given input, excluding the trailing
2478 * '\0', as per ISO C99. If you want to have the exact
2479 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002480 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 * return is greater than or equal to @size, the resulting
2482 * string is truncated.
2483 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002484 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
2486int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002489 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002490 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002492 /* Reject out-of-range values early. Large positive sizes are
2493 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002494 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002498 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002500 /* Make sure end is always >= buf */
2501 if (end < buf) {
2502 end = ((void *)-1);
2503 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
2505
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002506 while (*fmt) {
2507 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002508 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002509
2510 fmt += read;
2511
2512 switch (spec.type) {
2513 case FORMAT_TYPE_NONE: {
2514 int copy = read;
2515 if (str < end) {
2516 if (copy > end - str)
2517 copy = end - str;
2518 memcpy(str, old_fmt, copy);
2519 }
2520 str += read;
2521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
2523
Vegard Nossumed681a92009-03-14 12:08:50 +01002524 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002525 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002528 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002529 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
André Goddard Rosad4be1512009-12-14 18:00:59 -08002532 case FORMAT_TYPE_CHAR: {
2533 char c;
2534
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002535 if (!(spec.flags & LEFT)) {
2536 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002537 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 *str = ' ';
2539 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002542 }
2543 c = (unsigned char) va_arg(args, int);
2544 if (str < end)
2545 *str = c;
2546 ++str;
2547 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002548 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002549 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002551 }
2552 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002555 case FORMAT_TYPE_STR:
2556 str = string(str, end, va_arg(args, char *), spec);
2557 break;
2558
2559 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002560 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002561 spec);
2562 while (isalnum(*fmt))
2563 fmt++;
2564 break;
2565
2566 case FORMAT_TYPE_PERCENT_CHAR:
2567 if (str < end)
2568 *str = '%';
2569 ++str;
2570 break;
2571
2572 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002573 /*
2574 * Presumably the arguments passed gcc's type
2575 * checking, but there is no safe or sane way
2576 * for us to continue parsing the format and
2577 * fetching from the va_list; the remaining
2578 * specifiers and arguments would be out of
2579 * sync.
2580 */
2581 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002582
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002583 default:
2584 switch (spec.type) {
2585 case FORMAT_TYPE_LONG_LONG:
2586 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002588 case FORMAT_TYPE_ULONG:
2589 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002591 case FORMAT_TYPE_LONG:
2592 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002594 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002595 if (spec.flags & SIGN)
2596 num = va_arg(args, ssize_t);
2597 else
2598 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002599 break;
2600 case FORMAT_TYPE_PTRDIFF:
2601 num = va_arg(args, ptrdiff_t);
2602 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002603 case FORMAT_TYPE_UBYTE:
2604 num = (unsigned char) va_arg(args, int);
2605 break;
2606 case FORMAT_TYPE_BYTE:
2607 num = (signed char) va_arg(args, int);
2608 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002609 case FORMAT_TYPE_USHORT:
2610 num = (unsigned short) va_arg(args, int);
2611 break;
2612 case FORMAT_TYPE_SHORT:
2613 num = (short) va_arg(args, int);
2614 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002615 case FORMAT_TYPE_INT:
2616 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002617 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002619 num = va_arg(args, unsigned int);
2620 }
2621
2622 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002625
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002626out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002627 if (size > 0) {
2628 if (str < end)
2629 *str = '\0';
2630 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002631 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002632 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002633
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002634 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638EXPORT_SYMBOL(vsnprintf);
2639
2640/**
2641 * vscnprintf - Format a string and place it in a buffer
2642 * @buf: The buffer to place the result into
2643 * @size: The size of the buffer, including the trailing null space
2644 * @fmt: The format string to use
2645 * @args: Arguments for the format string
2646 *
2647 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002648 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 * returns 0.
2650 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002651 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002652 *
2653 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 */
2655int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2656{
2657 int i;
2658
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002659 i = vsnprintf(buf, size, fmt, args);
2660
Anton Arapovb921c692011-01-12 16:59:49 -08002661 if (likely(i < size))
2662 return i;
2663 if (size != 0)
2664 return size - 1;
2665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667EXPORT_SYMBOL(vscnprintf);
2668
2669/**
2670 * snprintf - Format a string and place it in a buffer
2671 * @buf: The buffer to place the result into
2672 * @size: The size of the buffer, including the trailing null space
2673 * @fmt: The format string to use
2674 * @...: Arguments for the format string
2675 *
2676 * The return value is the number of characters which would be
2677 * generated for the given input, excluding the trailing null,
2678 * as per ISO C99. If the return is greater than or equal to
2679 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002680 *
2681 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002683int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 va_list args;
2686 int i;
2687
2688 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002689 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return i;
2693}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694EXPORT_SYMBOL(snprintf);
2695
2696/**
2697 * scnprintf - Format a string and place it in a buffer
2698 * @buf: The buffer to place the result into
2699 * @size: The size of the buffer, including the trailing null space
2700 * @fmt: The format string to use
2701 * @...: Arguments for the format string
2702 *
2703 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002704 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 */
2706
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002707int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
2709 va_list args;
2710 int i;
2711
2712 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002713 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002715
Anton Arapovb921c692011-01-12 16:59:49 -08002716 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718EXPORT_SYMBOL(scnprintf);
2719
2720/**
2721 * vsprintf - Format a string and place it in a buffer
2722 * @buf: The buffer to place the result into
2723 * @fmt: The format string to use
2724 * @args: Arguments for the format string
2725 *
2726 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002727 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 * buffer overflows.
2729 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002730 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002731 *
2732 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
2734int vsprintf(char *buf, const char *fmt, va_list args)
2735{
2736 return vsnprintf(buf, INT_MAX, fmt, args);
2737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738EXPORT_SYMBOL(vsprintf);
2739
2740/**
2741 * sprintf - Format a string and place it in a buffer
2742 * @buf: The buffer to place the result into
2743 * @fmt: The format string to use
2744 * @...: Arguments for the format string
2745 *
2746 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002747 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002749 *
2750 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002752int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
2754 va_list args;
2755 int i;
2756
2757 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002758 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return i;
2762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763EXPORT_SYMBOL(sprintf);
2764
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002765#ifdef CONFIG_BINARY_PRINTF
2766/*
2767 * bprintf service:
2768 * vbin_printf() - VA arguments to binary data
2769 * bstr_printf() - Binary data to text string
2770 */
2771
2772/**
2773 * vbin_printf - Parse a format string and place args' binary value in a buffer
2774 * @bin_buf: The buffer to place args' binary value
2775 * @size: The size of the buffer(by words(32bits), not characters)
2776 * @fmt: The format string to use
2777 * @args: Arguments for the format string
2778 *
2779 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002780 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002781 *
2782 * The return value is the number of words(32bits) which would be generated for
2783 * the given input.
2784 *
2785 * NOTE:
2786 * If the return value is greater than @size, the resulting bin_buf is NOT
2787 * valid for bstr_printf().
2788 */
2789int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2790{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002791 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002792 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002793 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002794
2795 str = (char *)bin_buf;
2796 end = (char *)(bin_buf + size);
2797
2798#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002799({ \
2800 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002801 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002802 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002803 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002804 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002805 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002806 *(u32 *)str = *(u32 *)&val8; \
2807 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002808 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002809 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002810 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002811 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002812 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002813 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002814 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002815 *(typeof(type) *)str = (type)(long)val4; \
2816 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002817 } \
2818 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002819 value; \
2820})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002821
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002822 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002823 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002824
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002825 fmt += read;
2826
2827 switch (spec.type) {
2828 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002829 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002830 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002831 case FORMAT_TYPE_INVALID:
2832 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002833
Vegard Nossumed681a92009-03-14 12:08:50 +01002834 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002835 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002836 width = (int)save_arg(int);
2837 /* Pointers may require the width */
2838 if (*fmt == 'p')
2839 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002840 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002841
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002842 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002843 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002844 break;
2845
2846 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002847 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002848 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002849 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002850
Petr Mladek3e5903e2019-04-17 13:53:48 +02002851 err_msg = check_pointer_msg(save_str);
2852 if (err_msg)
2853 save_str = err_msg;
2854
André Goddard Rosa6c356632009-12-14 18:00:56 -08002855 len = strlen(save_str) + 1;
2856 if (str + len < end)
2857 memcpy(str, save_str, len);
2858 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002859 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002860 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002861
2862 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002863 /* Dereferenced pointers must be done now */
2864 switch (*fmt) {
2865 /* Dereference of functions is still OK */
2866 case 'S':
2867 case 's':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002868 case 'x':
2869 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02002870 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002871 save_arg(void *);
2872 break;
2873 default:
2874 if (!isalnum(*fmt)) {
2875 save_arg(void *);
2876 break;
2877 }
2878 str = pointer(fmt, str, end, va_arg(args, void *),
2879 spec);
2880 if (str + 1 < end)
2881 *str++ = '\0';
2882 else
2883 end[-1] = '\0'; /* Must be nul terminated */
2884 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002885 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002886 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002887 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002888 break;
2889
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002890 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002891 switch (spec.type) {
2892
2893 case FORMAT_TYPE_LONG_LONG:
2894 save_arg(long long);
2895 break;
2896 case FORMAT_TYPE_ULONG:
2897 case FORMAT_TYPE_LONG:
2898 save_arg(unsigned long);
2899 break;
2900 case FORMAT_TYPE_SIZE_T:
2901 save_arg(size_t);
2902 break;
2903 case FORMAT_TYPE_PTRDIFF:
2904 save_arg(ptrdiff_t);
2905 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002906 case FORMAT_TYPE_UBYTE:
2907 case FORMAT_TYPE_BYTE:
2908 save_arg(char);
2909 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002910 case FORMAT_TYPE_USHORT:
2911 case FORMAT_TYPE_SHORT:
2912 save_arg(short);
2913 break;
2914 default:
2915 save_arg(int);
2916 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002917 }
2918 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002919
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002920out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002921 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002922#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002923}
2924EXPORT_SYMBOL_GPL(vbin_printf);
2925
2926/**
2927 * bstr_printf - Format a string from binary arguments and place it in a buffer
2928 * @buf: The buffer to place the result into
2929 * @size: The size of the buffer, including the trailing null space
2930 * @fmt: The format string to use
2931 * @bin_buf: Binary arguments for the format string
2932 *
2933 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2934 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2935 * a binary buffer that generated by vbin_printf.
2936 *
2937 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002938 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002939 *
2940 * The return value is the number of characters which would
2941 * be generated for the given input, excluding the trailing
2942 * '\0', as per ISO C99. If you want to have the exact
2943 * number of characters written into @buf as return value
2944 * (not including the trailing '\0'), use vscnprintf(). If the
2945 * return is greater than or equal to @size, the resulting
2946 * string is truncated.
2947 */
2948int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2949{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002950 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002951 char *str, *end;
2952 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002953
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002954 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002955 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002956
2957 str = buf;
2958 end = buf + size;
2959
2960#define get_arg(type) \
2961({ \
2962 typeof(type) value; \
2963 if (sizeof(type) == 8) { \
2964 args = PTR_ALIGN(args, sizeof(u32)); \
2965 *(u32 *)&value = *(u32 *)args; \
2966 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2967 } else { \
2968 args = PTR_ALIGN(args, sizeof(type)); \
2969 value = *(typeof(type) *)args; \
2970 } \
2971 args += sizeof(type); \
2972 value; \
2973})
2974
2975 /* Make sure end is always >= buf */
2976 if (end < buf) {
2977 end = ((void *)-1);
2978 size = end - buf;
2979 }
2980
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002981 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002982 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002983 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002984
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002985 fmt += read;
2986
2987 switch (spec.type) {
2988 case FORMAT_TYPE_NONE: {
2989 int copy = read;
2990 if (str < end) {
2991 if (copy > end - str)
2992 copy = end - str;
2993 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002994 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002995 str += read;
2996 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002997 }
2998
Vegard Nossumed681a92009-03-14 12:08:50 +01002999 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003000 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003001 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003002
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003003 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08003004 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003005 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003006
André Goddard Rosad4be1512009-12-14 18:00:59 -08003007 case FORMAT_TYPE_CHAR: {
3008 char c;
3009
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003010 if (!(spec.flags & LEFT)) {
3011 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003012 if (str < end)
3013 *str = ' ';
3014 ++str;
3015 }
3016 }
3017 c = (unsigned char) get_arg(char);
3018 if (str < end)
3019 *str = c;
3020 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003021 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003022 if (str < end)
3023 *str = ' ';
3024 ++str;
3025 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003026 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003027 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003028
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003029 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003030 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003031 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003032 str = string(str, end, (char *)str_arg, spec);
3033 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003034 }
3035
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003036 case FORMAT_TYPE_PTR: {
3037 bool process = false;
3038 int copy, len;
3039 /* Non function dereferences were already done */
3040 switch (*fmt) {
3041 case 'S':
3042 case 's':
3043 case 'F':
3044 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04003045 case 'x':
3046 case 'K':
Rasmus Villemoes57f56772019-10-15 21:07:05 +02003047 case 'e':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003048 process = true;
3049 break;
3050 default:
3051 if (!isalnum(*fmt)) {
3052 process = true;
3053 break;
3054 }
3055 /* Pointer dereference was already processed */
3056 if (str < end) {
3057 len = copy = strlen(args);
3058 if (copy > end - str)
3059 copy = end - str;
3060 memcpy(str, args, copy);
3061 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003062 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003063 }
3064 }
3065 if (process)
3066 str = pointer(fmt, str, end, get_arg(void *), spec);
3067
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003068 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003069 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003070 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003071 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003072
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003073 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003074 if (str < end)
3075 *str = '%';
3076 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003077 break;
3078
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003079 case FORMAT_TYPE_INVALID:
3080 goto out;
3081
André Goddard Rosad4be1512009-12-14 18:00:59 -08003082 default: {
3083 unsigned long long num;
3084
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003085 switch (spec.type) {
3086
3087 case FORMAT_TYPE_LONG_LONG:
3088 num = get_arg(long long);
3089 break;
3090 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003091 case FORMAT_TYPE_LONG:
3092 num = get_arg(unsigned long);
3093 break;
3094 case FORMAT_TYPE_SIZE_T:
3095 num = get_arg(size_t);
3096 break;
3097 case FORMAT_TYPE_PTRDIFF:
3098 num = get_arg(ptrdiff_t);
3099 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003100 case FORMAT_TYPE_UBYTE:
3101 num = get_arg(unsigned char);
3102 break;
3103 case FORMAT_TYPE_BYTE:
3104 num = get_arg(signed char);
3105 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003106 case FORMAT_TYPE_USHORT:
3107 num = get_arg(unsigned short);
3108 break;
3109 case FORMAT_TYPE_SHORT:
3110 num = get_arg(short);
3111 break;
3112 case FORMAT_TYPE_UINT:
3113 num = get_arg(unsigned int);
3114 break;
3115 default:
3116 num = get_arg(int);
3117 }
3118
3119 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003120 } /* default: */
3121 } /* switch(spec.type) */
3122 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003123
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003124out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003125 if (size > 0) {
3126 if (str < end)
3127 *str = '\0';
3128 else
3129 end[-1] = '\0';
3130 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003131
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003132#undef get_arg
3133
3134 /* the trailing null byte doesn't count towards the total */
3135 return str - buf;
3136}
3137EXPORT_SYMBOL_GPL(bstr_printf);
3138
3139/**
3140 * bprintf - Parse a format string and place args' binary value in a buffer
3141 * @bin_buf: The buffer to place args' binary value
3142 * @size: The size of the buffer(by words(32bits), not characters)
3143 * @fmt: The format string to use
3144 * @...: Arguments for the format string
3145 *
3146 * The function returns the number of words(u32) written
3147 * into @bin_buf.
3148 */
3149int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3150{
3151 va_list args;
3152 int ret;
3153
3154 va_start(args, fmt);
3155 ret = vbin_printf(bin_buf, size, fmt, args);
3156 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003157
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003158 return ret;
3159}
3160EXPORT_SYMBOL_GPL(bprintf);
3161
3162#endif /* CONFIG_BINARY_PRINTF */
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164/**
3165 * vsscanf - Unformat a buffer into a list of arguments
3166 * @buf: input buffer
3167 * @fmt: format of buffer
3168 * @args: arguments
3169 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003170int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 const char *str = buf;
3173 char *next;
3174 char digit;
3175 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003176 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003177 unsigned int base;
3178 union {
3179 long long s;
3180 unsigned long long u;
3181 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003182 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003183 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Jan Beulichda990752012-10-04 17:13:24 -07003185 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 /* skip any white space in format */
3187 /* white space in format matchs any amount of
3188 * white space, including none, in the input.
3189 */
3190 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003191 fmt = skip_spaces(++fmt);
3192 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194
3195 /* anything that is not a conversion must match exactly */
3196 if (*fmt != '%' && *fmt) {
3197 if (*fmt++ != *str++)
3198 break;
3199 continue;
3200 }
3201
3202 if (!*fmt)
3203 break;
3204 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 /* skip this conversion.
3207 * advance both strings to next white space
3208 */
3209 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003210 if (!*str)
3211 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003212 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3213 /* '%*[' not yet supported, invalid format */
3214 if (*fmt == '[')
3215 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 while (!isspace(*str) && *str)
3219 str++;
3220 continue;
3221 }
3222
3223 /* get field width */
3224 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003225 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003227 if (field_width <= 0)
3228 break;
3229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 /* get conversion qualifier */
3232 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003233 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003234 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 qualifier = *fmt++;
3236 if (unlikely(qualifier == *fmt)) {
3237 if (qualifier == 'h') {
3238 qualifier = 'H';
3239 fmt++;
3240 } else if (qualifier == 'l') {
3241 qualifier = 'L';
3242 fmt++;
3243 }
3244 }
3245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Jan Beulichda990752012-10-04 17:13:24 -07003247 if (!*fmt)
3248 break;
3249
3250 if (*fmt == 'n') {
3251 /* return number of characters read so far */
3252 *va_arg(args, int *) = str - buf;
3253 ++fmt;
3254 continue;
3255 }
3256
3257 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 break;
3259
André Goddard Rosad4be1512009-12-14 18:00:59 -08003260 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003261 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003262
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003263 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 case 'c':
3265 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003266 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 if (field_width == -1)
3268 field_width = 1;
3269 do {
3270 *s++ = *str++;
3271 } while (--field_width > 0 && *str);
3272 num++;
3273 }
3274 continue;
3275 case 's':
3276 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003277 char *s = (char *)va_arg(args, char *);
3278 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003279 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003281 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003284 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 *s = '\0';
3287 num++;
3288 }
3289 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003290 /*
3291 * Warning: This implementation of the '[' conversion specifier
3292 * deviates from its glibc counterpart in the following ways:
3293 * (1) It does NOT support ranges i.e. '-' is NOT a special
3294 * character
3295 * (2) It cannot match the closing bracket ']' itself
3296 * (3) A field width is required
3297 * (4) '%*[' (discard matching input) is currently not supported
3298 *
3299 * Example usage:
3300 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3301 * buf1, buf2, buf3);
3302 * if (ret < 3)
3303 * // etc..
3304 */
3305 case '[':
3306 {
3307 char *s = (char *)va_arg(args, char *);
3308 DECLARE_BITMAP(set, 256) = {0};
3309 unsigned int len = 0;
3310 bool negate = (*fmt == '^');
3311
3312 /* field width is required */
3313 if (field_width == -1)
3314 return num;
3315
3316 if (negate)
3317 ++fmt;
3318
3319 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3320 set_bit((u8)*fmt, set);
3321
3322 /* no ']' or no character set found */
3323 if (!*fmt || !len)
3324 return num;
3325 ++fmt;
3326
3327 if (negate) {
3328 bitmap_complement(set, set, 256);
3329 /* exclude null '\0' byte */
3330 clear_bit(0, set);
3331 }
3332
3333 /* match must be non-empty */
3334 if (!test_bit((u8)*str, set))
3335 return num;
3336
3337 while (test_bit((u8)*str, set) && field_width--)
3338 *s++ = *str++;
3339 *s = '\0';
3340 ++num;
3341 }
3342 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 case 'o':
3344 base = 8;
3345 break;
3346 case 'x':
3347 case 'X':
3348 base = 16;
3349 break;
3350 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003351 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003352 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003354 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003355 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 case 'u':
3357 break;
3358 case '%':
3359 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003360 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return num;
3362 continue;
3363 default:
3364 /* invalid format; stop here */
3365 return num;
3366 }
3367
3368 /* have some sort of integer conversion.
3369 * first, skip white space in buffer.
3370 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003371 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 digit = *str;
3374 if (is_sign && digit == '-')
3375 digit = *(str + 1);
3376
3377 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003378 || (base == 16 && !isxdigit(digit))
3379 || (base == 10 && !isdigit(digit))
3380 || (base == 8 && (!isdigit(digit) || digit > '7'))
3381 || (base == 0 && !isdigit(digit)))
3382 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Jan Beulich53809752012-12-17 16:01:31 -08003384 if (is_sign)
3385 val.s = qualifier != 'L' ?
3386 simple_strtol(str, &next, base) :
3387 simple_strtoll(str, &next, base);
3388 else
3389 val.u = qualifier != 'L' ?
3390 simple_strtoul(str, &next, base) :
3391 simple_strtoull(str, &next, base);
3392
3393 if (field_width > 0 && next - str > field_width) {
3394 if (base == 0)
3395 _parse_integer_fixup_radix(str, &base);
3396 while (next - str > field_width) {
3397 if (is_sign)
3398 val.s = div_s64(val.s, base);
3399 else
3400 val.u = div_u64(val.u, base);
3401 --next;
3402 }
3403 }
3404
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003405 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003407 if (is_sign)
3408 *va_arg(args, signed char *) = val.s;
3409 else
3410 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 break;
3412 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003413 if (is_sign)
3414 *va_arg(args, short *) = val.s;
3415 else
3416 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 break;
3418 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003419 if (is_sign)
3420 *va_arg(args, long *) = val.s;
3421 else
3422 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 break;
3424 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003425 if (is_sign)
3426 *va_arg(args, long long *) = val.s;
3427 else
3428 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003431 *va_arg(args, size_t *) = val.u;
3432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 default:
Jan Beulich53809752012-12-17 16:01:31 -08003434 if (is_sign)
3435 *va_arg(args, int *) = val.s;
3436 else
3437 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 break;
3439 }
3440 num++;
3441
3442 if (!next)
3443 break;
3444 str = next;
3445 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return num;
3448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449EXPORT_SYMBOL(vsscanf);
3450
3451/**
3452 * sscanf - Unformat a buffer into a list of arguments
3453 * @buf: input buffer
3454 * @fmt: formatting of buffer
3455 * @...: resulting arguments
3456 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003457int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 va_list args;
3460 int i;
3461
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003462 va_start(args, fmt);
3463 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return i;
3467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468EXPORT_SYMBOL(sscanf);