blob: 1f367f3a7e2b7230f54a12923878f0615ef0cbd2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
Rasmus Villemoesef27ac12019-03-07 16:27:03 -080020#include <linux/build_bug.h>
Stephen Boyd0d1d7a52015-06-19 15:00:46 -070021#include <linux/clk.h>
Geert Uytterhoeven900cca22015-04-15 16:17:20 -070022#include <linux/clk-provider.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050023#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/ctype.h>
27#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070028#include <linux/kallsyms.h>
Jan Beulich53809752012-12-17 16:01:31 -080029#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070030#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110031#include <linux/ioport.h>
Al Viro4b6ccca2013-09-03 12:00:44 -040032#include <linux/dcache.h>
Ryan Mallon312b4e22013-11-12 15:08:51 -080033#include <linux/cred.h>
Andy Shevchenko4d42c442018-12-04 23:23:11 +020034#include <linux/rtc.h>
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -070035#include <linux/uuid.h>
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +020036#include <linux/of.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000037#include <net/addrconf.h>
Tobin C. Hardingad67b742017-11-01 15:32:23 +110038#include <linux/siphash.h>
39#include <linux/compiler.h>
Dmitry Monakhov1031bc52015-04-13 16:31:35 +040040#ifdef CONFIG_BLOCK
41#include <linux/blkdev.h>
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -070044#include "../mm/internal.h" /* For the trace_print_flags arrays */
45
Tim Schmielau4e57b682005-10-30 15:03:48 -080046#include <asm/page.h> /* for PAGE_SIZE */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -070047#include <asm/byteorder.h> /* cpu_to_le16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andy Shevchenko71dca952014-10-13 15:55:18 -070049#include <linux/string_helpers.h>
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070050#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * simple_strtoull - convert a string to an unsigned long long
54 * @cp: The start of the string
55 * @endp: A pointer to the end of the parsed string will be placed here
56 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080057 *
58 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Harvey Harrison22d27052008-10-16 13:40:35 -070060unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070062 unsigned long long result;
63 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070065 cp = _parse_integer_fixup_radix(cp, &base);
66 rv = _parse_integer(cp, base, &result);
67 /* FIXME */
68 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (endp)
71 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return result;
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075EXPORT_SYMBOL(simple_strtoull);
76
77/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080078 * simple_strtoul - convert a string to an unsigned long
79 * @cp: The start of the string
80 * @endp: A pointer to the end of the parsed string will be placed here
81 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080082 *
83 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080084 */
85unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
86{
87 return simple_strtoull(cp, endp, base);
88}
89EXPORT_SYMBOL(simple_strtoul);
90
91/**
92 * simple_strtol - convert a string to a signed long
93 * @cp: The start of the string
94 * @endp: A pointer to the end of the parsed string will be placed here
95 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080096 *
97 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080098 */
99long simple_strtol(const char *cp, char **endp, unsigned int base)
100{
101 if (*cp == '-')
102 return -simple_strtoul(cp + 1, endp, base);
103
104 return simple_strtoul(cp, endp, base);
105}
106EXPORT_SYMBOL(simple_strtol);
107
108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -0800113 *
114 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700116long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800118 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700119 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800120
Harvey Harrison22d27052008-10-16 13:40:35 -0700121 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400123EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Joe Perchescf3b4292010-05-24 14:33:16 -0700125static noinline_for_stack
126int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800128 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800130 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 i = i*10 + *((*s)++) - '0';
Rasmus Villemoes43e5b662015-02-12 15:01:42 -0800132 } while (isdigit(**s));
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return i;
135}
136
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700137/*
138 * Decimal conversion is by far the most typical, and is used for
139 * /proc and /sys data. This directly impacts e.g. top performance
140 * with many processes running. We optimize it for speed by emitting
141 * two characters at a time, using a 200 byte lookup table. This
142 * roughly halves the number of multiplications compared to computing
143 * the digits one at a time. Implementation strongly inspired by the
144 * previous version, which in turn used ideas described at
145 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
146 * from the author, Douglas W. Jones).
147 *
148 * It turns out there is precisely one 26 bit fixed-point
149 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
150 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
151 * range happens to be somewhat larger (x <= 1073741898), but that's
152 * irrelevant for our purpose.
153 *
154 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
155 * need a 32x32->64 bit multiply, so we simply use the same constant.
156 *
157 * For dividing a number in the range [100, 10^4-1] by 100, there are
158 * several options. The simplest is (x * 0x147b) >> 19, which is valid
159 * for all x <= 43698.
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700160 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700161
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700162static const u16 decpair[100] = {
163#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
164 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
165 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
166 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
167 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
168 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
169 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
170 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
171 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
172 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
173 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
174#undef _
175};
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700176
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700177/*
178 * This will print a single '0' even if r == 0, since we would
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700179 * immediately jump to out_r where two 0s would be written but only
180 * one of them accounted for in buf. This is needed by ip4_string
181 * below. All other callers pass a non-zero value of r.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700182*/
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700183static noinline_for_stack
184char *put_dec_trunc8(char *buf, unsigned r)
185{
186 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700187
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700188 /* 1 <= r < 10^8 */
189 if (r < 100)
190 goto out_r;
George Spelvincb239d02012-10-04 17:12:30 -0700191
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700192 /* 100 <= r < 10^8 */
193 q = (r * (u64)0x28f5c29) >> 32;
194 *((u16 *)buf) = decpair[r - 100*q];
195 buf += 2;
196
197 /* 1 <= q < 10^6 */
198 if (q < 100)
199 goto out_q;
200
201 /* 100 <= q < 10^6 */
202 r = (q * (u64)0x28f5c29) >> 32;
203 *((u16 *)buf) = decpair[q - 100*r];
204 buf += 2;
205
206 /* 1 <= r < 10^4 */
207 if (r < 100)
208 goto out_r;
209
210 /* 100 <= r < 10^4 */
211 q = (r * 0x147b) >> 19;
212 *((u16 *)buf) = decpair[r - 100*q];
213 buf += 2;
214out_q:
215 /* 1 <= q < 100 */
216 r = q;
217out_r:
218 /* 1 <= r < 100 */
219 *((u16 *)buf) = decpair[r];
Rasmus Villemoes675cf532015-04-16 12:43:42 -0700220 buf += r < 10 ? 1 : 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700221 return buf;
222}
223
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700224#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
225static noinline_for_stack
226char *put_dec_full8(char *buf, unsigned r)
227{
228 unsigned q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700229
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700230 /* 0 <= r < 10^8 */
231 q = (r * (u64)0x28f5c29) >> 32;
232 *((u16 *)buf) = decpair[r - 100*q];
233 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700234
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700235 /* 0 <= q < 10^6 */
236 r = (q * (u64)0x28f5c29) >> 32;
237 *((u16 *)buf) = decpair[q - 100*r];
238 buf += 2;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700239
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700240 /* 0 <= r < 10^4 */
241 q = (r * 0x147b) >> 19;
242 *((u16 *)buf) = decpair[r - 100*q];
243 buf += 2;
244
245 /* 0 <= q < 100 */
246 *((u16 *)buf) = decpair[q];
247 buf += 2;
248 return buf;
249}
250
251static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700252char *put_dec(char *buf, unsigned long long n)
253{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700254 if (n >= 100*1000*1000)
255 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
256 /* 1 <= n <= 1.6e11 */
257 if (n >= 100*1000*1000)
258 buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
259 /* 1 <= n < 1e8 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700260 return put_dec_trunc8(buf, n);
261}
262
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700263#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700264
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700265static void
266put_dec_full4(char *buf, unsigned r)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700267{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700268 unsigned q;
269
270 /* 0 <= r < 10^4 */
271 q = (r * 0x147b) >> 19;
272 *((u16 *)buf) = decpair[r - 100*q];
273 buf += 2;
274 /* 0 <= q < 100 */
275 *((u16 *)buf) = decpair[q];
George Spelvin23591722012-10-04 17:12:29 -0700276}
277
278/*
279 * Call put_dec_full4 on x % 10000, return x / 10000.
280 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
281 * holds for all x < 1,128,869,999. The largest value this
282 * helper will ever be asked to convert is 1,125,520,955.
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700283 * (second call in the put_dec code, assuming n is all-ones).
George Spelvin23591722012-10-04 17:12:29 -0700284 */
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700285static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700286unsigned put_dec_helper4(char *buf, unsigned x)
287{
288 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
289
290 put_dec_full4(buf, x - q * 10000);
291 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700292}
293
294/* Based on code by Douglas W. Jones found at
295 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
296 * (with permission from the author).
297 * Performs no 64-bit division and hence should be fast on 32-bit machines.
298 */
299static
300char *put_dec(char *buf, unsigned long long n)
301{
302 uint32_t d3, d2, d1, q, h;
303
304 if (n < 100*1000*1000)
305 return put_dec_trunc8(buf, n);
306
307 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
308 h = (n >> 32);
309 d2 = (h ) & 0xffff;
310 d3 = (h >> 16); /* implicit "& 0xffff" */
311
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700312 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
313 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700314 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700315 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700316
George Spelvin23591722012-10-04 17:12:29 -0700317 q += 7671 * d3 + 9496 * d2 + 6 * d1;
318 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700319
George Spelvin23591722012-10-04 17:12:29 -0700320 q += 4749 * d3 + 42 * d2;
321 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700322
George Spelvin23591722012-10-04 17:12:29 -0700323 q += 281 * d3;
324 buf += 12;
325 if (q)
326 buf = put_dec_trunc8(buf, q);
327 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700328 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800329
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700330 return buf;
331}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700332
333#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700334
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700335/*
336 * Convert passed number to decimal string.
337 * Returns the length of string. On buffer overflow, returns 0.
338 *
339 * If speed is not important, use snprintf(). It's easy to read the code.
340 */
Andrei Vagind1be35c2018-04-10 16:31:16 -0700341int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700342{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700343 /* put_dec requires 2-byte alignment of the buffer. */
344 char tmp[sizeof(num) * 3] __aligned(2);
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700345 int idx, len;
346
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700347 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
348 if (num <= 9) {
349 tmp[0] = '0' + num;
350 len = 1;
351 } else {
352 len = put_dec(tmp, num) - tmp;
353 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700354
Andrei Vagind1be35c2018-04-10 16:31:16 -0700355 if (len > size || width > size)
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700356 return 0;
Andrei Vagind1be35c2018-04-10 16:31:16 -0700357
358 if (width > len) {
359 width = width - len;
360 for (idx = 0; idx < width; idx++)
361 buf[idx] = ' ';
362 } else {
363 width = 0;
364 }
365
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700366 for (idx = 0; idx < len; ++idx)
Andrei Vagind1be35c2018-04-10 16:31:16 -0700367 buf[idx + width] = tmp[len - idx - 1];
368
369 return len + width;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700370}
371
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700372#define SIGN 1 /* unsigned/signed, must be 1 */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700373#define LEFT 2 /* left justified */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#define PLUS 4 /* show plus */
375#define SPACE 8 /* space if plus */
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700376#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700377#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
378#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100380enum format_type {
381 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100382 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100383 FORMAT_TYPE_PRECISION,
384 FORMAT_TYPE_CHAR,
385 FORMAT_TYPE_STR,
386 FORMAT_TYPE_PTR,
387 FORMAT_TYPE_PERCENT_CHAR,
388 FORMAT_TYPE_INVALID,
389 FORMAT_TYPE_LONG_LONG,
390 FORMAT_TYPE_ULONG,
391 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800392 FORMAT_TYPE_UBYTE,
393 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100394 FORMAT_TYPE_USHORT,
395 FORMAT_TYPE_SHORT,
396 FORMAT_TYPE_UINT,
397 FORMAT_TYPE_INT,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100398 FORMAT_TYPE_SIZE_T,
399 FORMAT_TYPE_PTRDIFF
400};
401
402struct printf_spec {
Rasmus Villemoesd0484192016-01-15 16:58:37 -0800403 unsigned int type:8; /* format_type enum */
404 signed int field_width:24; /* width of output field */
405 unsigned int flags:8; /* flags to number() */
406 unsigned int base:8; /* number base, 8, 10 or 16 only */
407 signed int precision:16; /* # of digits/chars */
408} __packed;
Rasmus Villemoesef27ac12019-03-07 16:27:03 -0800409static_assert(sizeof(struct printf_spec) == 8);
410
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -0800411#define FIELD_WIDTH_MAX ((1 << 23) - 1)
412#define PRECISION_MAX ((1 << 15) - 1)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100413
Joe Perchescf3b4292010-05-24 14:33:16 -0700414static noinline_for_stack
415char *number(char *buf, char *end, unsigned long long num,
416 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -0700418 /* put_dec requires 2-byte alignment of the buffer. */
419 char tmp[3 * sizeof(num)] __aligned(2);
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100420 char sign;
421 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100422 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700424 bool is_zero = num == 0LL;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800425 int field_width = spec.field_width;
426 int precision = spec.precision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100428 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
429 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100430 locase = (spec.flags & SMALL);
431 if (spec.flags & LEFT)
432 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100434 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800435 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800437 num = -(signed long long)num;
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800438 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100439 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sign = '+';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800441 field_width--;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100442 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sign = ' ';
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800444 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700447 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100448 if (spec.base == 16)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800449 field_width -= 2;
Pierre Carrier7c203422012-05-29 15:07:35 -0700450 else if (!is_zero)
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800451 field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700453
454 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700456 if (num < spec.base)
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700457 tmp[i++] = hex_asc_upper[num] | locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100458 else if (spec.base != 10) { /* 8 or 16 */
459 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800461
462 if (spec.base == 16)
463 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700464 do {
Rasmus Villemoes3ea8d442015-04-15 16:17:08 -0700465 tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700466 num >>= shift;
467 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700468 } else { /* base 10 */
469 i = put_dec(tmp, num) - tmp;
470 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700471
472 /* printing 100 using %2d gives "100", not "00" */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800473 if (i > precision)
474 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700475 /* leading space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800476 field_width -= precision;
Rasmus Villemoes51be17d2015-04-15 16:17:02 -0700477 if (!(spec.flags & (ZEROPAD | LEFT))) {
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800478 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700479 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *buf = ' ';
481 ++buf;
482 }
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700486 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *buf = sign;
488 ++buf;
489 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700490 /* "0x" / "0" prefix */
491 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700492 if (spec.base == 16 || !is_zero) {
493 if (buf < end)
494 *buf = '0';
495 ++buf;
496 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100497 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700498 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100499 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ++buf;
501 }
502 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700503 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100504 if (!(spec.flags & LEFT)) {
Rasmus Villemoesd1c1b122015-04-15 16:17:11 -0700505 char c = ' ' + (spec.flags & ZEROPAD);
506 BUILD_BUG_ON(' ' + ZEROPAD != '0');
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800507 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700508 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buf = c;
510 ++buf;
511 }
512 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700513 /* hmm even more zero padding? */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800514 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700515 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *buf = '0';
517 ++buf;
518 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700519 /* actual digits of result */
520 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700521 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *buf = tmp[i];
523 ++buf;
524 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700525 /* trailing space padding */
Rasmus Villemoes1c7a8e62016-01-15 16:58:41 -0800526 while (--field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700527 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *buf = ' ';
529 ++buf;
530 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return buf;
533}
534
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800535static noinline_for_stack
536char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
537{
538 struct printf_spec spec;
539
540 spec.type = FORMAT_TYPE_PTR;
541 spec.field_width = 2 + 2 * size; /* 0x + hex */
542 spec.flags = SPECIAL | SMALL | ZEROPAD;
543 spec.base = 16;
544 spec.precision = -1;
545
546 return number(buf, end, num, spec);
547}
548
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800549static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
Al Viro4b6ccca2013-09-03 12:00:44 -0400550{
551 size_t size;
552 if (buf >= end) /* nowhere to put anything */
553 return;
554 size = end - buf;
555 if (size <= spaces) {
556 memset(buf, ' ', size);
557 return;
558 }
559 if (len) {
560 if (len > size - spaces)
561 len = size - spaces;
562 memmove(buf + spaces, buf, len);
563 }
564 memset(buf, ' ', spaces);
565}
566
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800567/*
568 * Handle field width padding for a string.
569 * @buf: current buffer position
570 * @n: length of string
571 * @end: end of output buffer
572 * @spec: for field width and flags
573 * Returns: new buffer position after padding.
574 */
575static noinline_for_stack
576char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
577{
578 unsigned spaces;
579
580 if (likely(n >= spec.field_width))
581 return buf;
582 /* we want to pad the sucker */
583 spaces = spec.field_width - n;
584 if (!(spec.flags & LEFT)) {
585 move_right(buf - n, end, n, spaces);
586 return buf + spaces;
587 }
588 while (spaces--) {
589 if (buf < end)
590 *buf = ' ';
591 ++buf;
592 }
593 return buf;
594}
595
Petr Mladekd529ac42019-04-17 13:53:43 +0200596/* Handle string from a well known address. */
597static char *string_nocheck(char *buf, char *end, const char *s,
598 struct printf_spec spec)
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800599{
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800600 int len = 0;
601 size_t lim = spec.precision;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800602
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800603 while (lim--) {
604 char c = *s++;
605 if (!c)
606 break;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800607 if (buf < end)
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800608 *buf = c;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800609 ++buf;
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800610 ++len;
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800611 }
Rasmus Villemoes34fc8b92016-01-15 16:58:34 -0800612 return widen_string(buf, len, end, spec);
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800613}
614
Petr Mladekc8c3b582019-04-17 13:53:50 +0200615/* Be careful: error messages must fit into the given buffer. */
616static char *error_string(char *buf, char *end, const char *s,
617 struct printf_spec spec)
618{
619 /*
620 * Hard limit to avoid a completely insane messages. It actually
621 * works pretty well because most error messages are in
622 * the many pointer format modifiers.
623 */
624 if (spec.precision == -1)
625 spec.precision = 2 * sizeof(void *);
626
627 return string_nocheck(buf, end, s, spec);
628}
629
Petr Mladek3e5903e2019-04-17 13:53:48 +0200630/*
631 * This is not a fool-proof test. 99% of the time that this will fault is
632 * due to a bad pointer, not one that crosses into bad memory. Just test
633 * the address to make sure it doesn't fault due to a poorly added printk
634 * during debugging.
635 */
636static const char *check_pointer_msg(const void *ptr)
637{
638 char byte;
639
640 if (!ptr)
641 return "(null)";
642
643 if (probe_kernel_address(ptr, byte))
644 return "(efault)";
645
646 return NULL;
647}
648
649static int check_pointer(char **buf, char *end, const void *ptr,
650 struct printf_spec spec)
651{
652 const char *err_msg;
653
654 err_msg = check_pointer_msg(ptr);
655 if (err_msg) {
Petr Mladekc8c3b582019-04-17 13:53:50 +0200656 *buf = error_string(*buf, end, err_msg, spec);
Petr Mladek3e5903e2019-04-17 13:53:48 +0200657 return -EFAULT;
658 }
659
660 return 0;
661}
662
Rasmus Villemoes95508cf2016-01-15 16:58:31 -0800663static noinline_for_stack
Petr Mladekd529ac42019-04-17 13:53:43 +0200664char *string(char *buf, char *end, const char *s,
665 struct printf_spec spec)
666{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200667 if (check_pointer(&buf, end, s, spec))
668 return buf;
Petr Mladekd529ac42019-04-17 13:53:43 +0200669
670 return string_nocheck(buf, end, s, spec);
671}
672
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200673char *pointer_string(char *buf, char *end, const void *ptr,
674 struct printf_spec spec)
675{
676 spec.base = 16;
677 spec.flags |= SMALL;
678 if (spec.field_width == -1) {
679 spec.field_width = 2 * sizeof(ptr);
680 spec.flags |= ZEROPAD;
681 }
682
683 return number(buf, end, (unsigned long int)ptr, spec);
684}
685
686/* Make pointers available for printing early in the boot sequence. */
687static int debug_boot_weak_hash __ro_after_init;
688
689static int __init debug_boot_weak_hash_enable(char *str)
690{
691 debug_boot_weak_hash = 1;
692 pr_info("debug_boot_weak_hash enabled\n");
693 return 0;
694}
695early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
696
697static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
698static siphash_key_t ptr_key __read_mostly;
699
700static void enable_ptr_key_workfn(struct work_struct *work)
701{
702 get_random_bytes(&ptr_key, sizeof(ptr_key));
703 /* Needs to run from preemptible context */
704 static_branch_disable(&not_filled_random_ptr_key);
705}
706
707static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
708
709static void fill_random_ptr_key(struct random_ready_callback *unused)
710{
711 /* This may be in an interrupt handler. */
712 queue_work(system_unbound_wq, &enable_ptr_key_work);
713}
714
715static struct random_ready_callback random_ready = {
716 .func = fill_random_ptr_key
717};
718
719static int __init initialize_ptr_random(void)
720{
721 int key_size = sizeof(ptr_key);
722 int ret;
723
724 /* Use hw RNG if available. */
725 if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
726 static_branch_disable(&not_filled_random_ptr_key);
727 return 0;
728 }
729
730 ret = add_random_ready_callback(&random_ready);
731 if (!ret) {
732 return 0;
733 } else if (ret == -EALREADY) {
734 /* This is in preemptible context */
735 enable_ptr_key_workfn(&enable_ptr_key_work);
736 return 0;
737 }
738
739 return ret;
740}
741early_initcall(initialize_ptr_random);
742
743/* Maps a pointer to a 32 bit unique identifier. */
744static char *ptr_to_id(char *buf, char *end, const void *ptr,
745 struct printf_spec spec)
746{
747 const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
748 unsigned long hashval;
749
750 /* When debugging early boot use non-cryptographically secure hash. */
751 if (unlikely(debug_boot_weak_hash)) {
752 hashval = hash_long((unsigned long)ptr, 32);
753 return pointer_string(buf, end, (const void *)hashval, spec);
754 }
755
756 if (static_branch_unlikely(&not_filled_random_ptr_key)) {
757 spec.field_width = 2 * sizeof(ptr);
758 /* string length must be less than default_width */
Petr Mladekc8c3b582019-04-17 13:53:50 +0200759 return error_string(buf, end, str, spec);
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200760 }
761
762#ifdef CONFIG_64BIT
763 hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
764 /*
765 * Mask off the first 32 bits, this makes explicit that we have
766 * modified the address (and 32 bits is plenty for a unique ID).
767 */
768 hashval = hashval & 0xffffffff;
769#else
770 hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
771#endif
772 return pointer_string(buf, end, (const void *)hashval, spec);
773}
774
Petr Mladek6eea2422019-04-17 13:53:41 +0200775int kptr_restrict __read_mostly;
776
777static noinline_for_stack
778char *restricted_pointer(char *buf, char *end, const void *ptr,
779 struct printf_spec spec)
780{
781 switch (kptr_restrict) {
782 case 0:
Petr Mladek1ac2f972019-04-17 13:53:42 +0200783 /* Handle as %p, hash and do _not_ leak addresses. */
784 return ptr_to_id(buf, end, ptr, spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200785 case 1: {
786 const struct cred *cred;
787
788 /*
789 * kptr_restrict==1 cannot be used in IRQ context
790 * because its test for CAP_SYSLOG would be meaningless.
791 */
792 if (in_irq() || in_serving_softirq() || in_nmi()) {
793 if (spec.field_width == -1)
794 spec.field_width = 2 * sizeof(ptr);
Petr Mladekc8c3b582019-04-17 13:53:50 +0200795 return error_string(buf, end, "pK-error", spec);
Petr Mladek6eea2422019-04-17 13:53:41 +0200796 }
797
798 /*
799 * Only print the real pointer value if the current
800 * process has CAP_SYSLOG and is running with the
801 * same credentials it started with. This is because
802 * access to files is checked at open() time, but %pK
803 * checks permission at read() time. We don't want to
804 * leak pointer values if a binary opens a file using
805 * %pK and then elevates privileges before reading it.
806 */
807 cred = current_cred();
808 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
809 !uid_eq(cred->euid, cred->uid) ||
810 !gid_eq(cred->egid, cred->gid))
811 ptr = NULL;
812 break;
813 }
814 case 2:
815 default:
816 /* Always print 0's for %pK */
817 ptr = NULL;
818 break;
819 }
820
821 return pointer_string(buf, end, ptr, spec);
822}
823
Geert Uytterhoeven9073dac2018-10-11 10:42:47 +0200824static noinline_for_stack
Al Viro4b6ccca2013-09-03 12:00:44 -0400825char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
826 const char *fmt)
827{
828 const char *array[4], *s;
829 const struct dentry *p;
830 int depth;
831 int i, n;
832
833 switch (fmt[1]) {
834 case '2': case '3': case '4':
835 depth = fmt[1] - '0';
836 break;
837 default:
838 depth = 1;
839 }
840
841 rcu_read_lock();
842 for (i = 0; i < depth; i++, d = p) {
Petr Mladek3e5903e2019-04-17 13:53:48 +0200843 if (check_pointer(&buf, end, d, spec)) {
844 rcu_read_unlock();
845 return buf;
846 }
847
Mark Rutland6aa7de02017-10-23 14:07:29 -0700848 p = READ_ONCE(d->d_parent);
849 array[i] = READ_ONCE(d->d_name.name);
Al Viro4b6ccca2013-09-03 12:00:44 -0400850 if (p == d) {
851 if (i)
852 array[i] = "";
853 i++;
854 break;
855 }
856 }
857 s = array[--i];
858 for (n = 0; n != spec.precision; n++, buf++) {
859 char c = *s++;
860 if (!c) {
861 if (!i)
862 break;
863 c = '/';
864 s = array[--i];
865 }
866 if (buf < end)
867 *buf = c;
868 }
869 rcu_read_unlock();
Rasmus Villemoescfccde02016-01-15 16:58:28 -0800870 return widen_string(buf, n, end, spec);
Al Viro4b6ccca2013-09-03 12:00:44 -0400871}
872
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400873#ifdef CONFIG_BLOCK
874static noinline_for_stack
875char *bdev_name(char *buf, char *end, struct block_device *bdev,
876 struct printf_spec spec, const char *fmt)
877{
Petr Mladek3e5903e2019-04-17 13:53:48 +0200878 struct gendisk *hd;
879
880 if (check_pointer(&buf, end, bdev, spec))
881 return buf;
882
883 hd = bdev->bd_disk;
Dmitry Monakhov1031bc52015-04-13 16:31:35 +0400884 buf = string(buf, end, hd->disk_name, spec);
885 if (bdev->bd_part->partno) {
886 if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
887 if (buf < end)
888 *buf = 'p';
889 buf++;
890 }
891 buf = number(buf, end, bdev->bd_part->partno, spec);
892 }
893 return buf;
894}
895#endif
896
Joe Perchescf3b4292010-05-24 14:33:16 -0700897static noinline_for_stack
898char *symbol_string(char *buf, char *end, void *ptr,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800899 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700900{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800901 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700902#ifdef CONFIG_KALLSYMS
903 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800904#endif
905
906 if (fmt[1] == 'R')
907 ptr = __builtin_extract_return_addr(ptr);
908 value = (unsigned long)ptr;
909
910#ifdef CONFIG_KALLSYMS
911 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900912 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800913 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200914 sprint_symbol(sym, value);
915 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700916 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800917
Petr Mladekd529ac42019-04-17 13:53:43 +0200918 return string_nocheck(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700919#else
Andy Shevchenko3cab1e72016-01-15 16:59:18 -0800920 return special_hex_number(buf, end, value, sizeof(void *));
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700921#endif
922}
923
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +0200924static const struct printf_spec default_str_spec = {
925 .field_width = -1,
926 .precision = -1,
927};
928
Andy Shevchenko54433972018-02-16 23:07:06 +0200929static const struct printf_spec default_flag_spec = {
930 .base = 16,
931 .precision = -1,
932 .flags = SPECIAL | SMALL,
933};
934
Andy Shevchenkoce0b4912018-02-16 23:07:04 +0200935static const struct printf_spec default_dec_spec = {
936 .base = 10,
937 .precision = -1,
938};
939
Andy Shevchenko4d42c442018-12-04 23:23:11 +0200940static const struct printf_spec default_dec02_spec = {
941 .base = 10,
942 .field_width = 2,
943 .precision = -1,
944 .flags = ZEROPAD,
945};
946
947static const struct printf_spec default_dec04_spec = {
948 .base = 10,
949 .field_width = 4,
950 .precision = -1,
951 .flags = ZEROPAD,
952};
953
Joe Perchescf3b4292010-05-24 14:33:16 -0700954static noinline_for_stack
955char *resource_string(char *buf, char *end, struct resource *res,
956 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100957{
958#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600959#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100960#endif
961
962#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600963#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100964#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700965 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100966 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700967 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100968 .precision = -1,
969 .flags = SPECIAL | SMALL | ZEROPAD,
970 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700971 static const struct printf_spec mem_spec = {
972 .base = 16,
973 .field_width = MEM_RSRC_PRINTK_SIZE,
974 .precision = -1,
975 .flags = SPECIAL | SMALL | ZEROPAD,
976 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700977 static const struct printf_spec bus_spec = {
978 .base = 16,
979 .field_width = 2,
980 .precision = -1,
981 .flags = SMALL | ZEROPAD,
982 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700983 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600984 .field_width = -1,
985 .precision = 10,
986 .flags = LEFT,
987 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600988
989 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
990 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
991#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
992#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700993#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600994#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
995 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
996 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
997
Linus Torvalds332d2e72008-10-20 15:07:34 +1100998 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600999 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001000 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +11001001
Petr Mladek3e5903e2019-04-17 13:53:48 +02001002 if (check_pointer(&buf, end, res, spec))
1003 return buf;
1004
Linus Torvalds332d2e72008-10-20 15:07:34 +11001005 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001006 if (res->flags & IORESOURCE_IO) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001007 p = string_nocheck(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001008 specp = &io_spec;
1009 } else if (res->flags & IORESOURCE_MEM) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001010 p = string_nocheck(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001011 specp = &mem_spec;
1012 } else if (res->flags & IORESOURCE_IRQ) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001013 p = string_nocheck(p, pend, "irq ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001014 specp = &default_dec_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001015 } else if (res->flags & IORESOURCE_DMA) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001016 p = string_nocheck(p, pend, "dma ", str_spec);
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001017 specp = &default_dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001018 } else if (res->flags & IORESOURCE_BUS) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001019 p = string_nocheck(p, pend, "bus ", str_spec);
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -07001020 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001021 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001022 p = string_nocheck(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -07001023 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001024 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001025 }
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001026 if (decode && res->flags & IORESOURCE_UNSET) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001027 p = string_nocheck(p, pend, "size ", str_spec);
Bjorn Helgaasd19cb802014-02-26 11:25:56 -07001028 p = number(p, pend, resource_size(res), *specp);
1029 } else {
1030 p = number(p, pend, res->start, *specp);
1031 if (res->start != res->end) {
1032 *p++ = '-';
1033 p = number(p, pend, res->end, *specp);
1034 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -06001035 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001036 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001037 if (res->flags & IORESOURCE_MEM_64)
Petr Mladekd529ac42019-04-17 13:53:43 +02001038 p = string_nocheck(p, pend, " 64bit", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001039 if (res->flags & IORESOURCE_PREFETCH)
Petr Mladekd529ac42019-04-17 13:53:43 +02001040 p = string_nocheck(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -07001041 if (res->flags & IORESOURCE_WINDOW)
Petr Mladekd529ac42019-04-17 13:53:43 +02001042 p = string_nocheck(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001043 if (res->flags & IORESOURCE_DISABLED)
Petr Mladekd529ac42019-04-17 13:53:43 +02001044 p = string_nocheck(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001045 } else {
Petr Mladekd529ac42019-04-17 13:53:43 +02001046 p = string_nocheck(p, pend, " flags ", str_spec);
Andy Shevchenko54433972018-02-16 23:07:06 +02001047 p = number(p, pend, res->flags, default_flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001048 }
Linus Torvalds332d2e72008-10-20 15:07:34 +11001049 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001050 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +11001051
Petr Mladekd529ac42019-04-17 13:53:43 +02001052 return string_nocheck(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001053}
1054
Joe Perchescf3b4292010-05-24 14:33:16 -07001055static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -07001056char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1057 const char *fmt)
1058{
Steven Rostedt360603a2013-05-28 15:47:39 -04001059 int i, len = 1; /* if we pass '%ph[CDN]', field width remains
Andy Shevchenko31550a12012-07-30 14:40:27 -07001060 negative value, fallback to the default */
1061 char separator;
1062
1063 if (spec.field_width == 0)
1064 /* nothing to print */
1065 return buf;
1066
Petr Mladek3e5903e2019-04-17 13:53:48 +02001067 if (check_pointer(&buf, end, addr, spec))
1068 return buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001069
1070 switch (fmt[1]) {
1071 case 'C':
1072 separator = ':';
1073 break;
1074 case 'D':
1075 separator = '-';
1076 break;
1077 case 'N':
1078 separator = 0;
1079 break;
1080 default:
1081 separator = ' ';
1082 break;
1083 }
1084
1085 if (spec.field_width > 0)
1086 len = min_t(int, spec.field_width, 64);
1087
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001088 for (i = 0; i < len; ++i) {
1089 if (buf < end)
1090 *buf = hex_asc_hi(addr[i]);
1091 ++buf;
1092 if (buf < end)
1093 *buf = hex_asc_lo(addr[i]);
1094 ++buf;
Andy Shevchenko31550a12012-07-30 14:40:27 -07001095
Rasmus Villemoes9c98f232015-04-15 16:17:23 -07001096 if (separator && i != len - 1) {
1097 if (buf < end)
1098 *buf = separator;
1099 ++buf;
1100 }
Andy Shevchenko31550a12012-07-30 14:40:27 -07001101 }
1102
1103 return buf;
1104}
1105
1106static noinline_for_stack
Tejun Heodbc760b2015-02-13 14:36:53 -08001107char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1108 struct printf_spec spec, const char *fmt)
1109{
1110 const int CHUNKSZ = 32;
1111 int nr_bits = max_t(int, spec.field_width, 0);
1112 int i, chunksz;
1113 bool first = true;
1114
Petr Mladek3e5903e2019-04-17 13:53:48 +02001115 if (check_pointer(&buf, end, bitmap, spec))
1116 return buf;
1117
Tejun Heodbc760b2015-02-13 14:36:53 -08001118 /* reused to print numbers */
1119 spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1120
1121 chunksz = nr_bits & (CHUNKSZ - 1);
1122 if (chunksz == 0)
1123 chunksz = CHUNKSZ;
1124
1125 i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1126 for (; i >= 0; i -= CHUNKSZ) {
1127 u32 chunkmask, val;
1128 int word, bit;
1129
1130 chunkmask = ((1ULL << chunksz) - 1);
1131 word = i / BITS_PER_LONG;
1132 bit = i % BITS_PER_LONG;
1133 val = (bitmap[word] >> bit) & chunkmask;
1134
1135 if (!first) {
1136 if (buf < end)
1137 *buf = ',';
1138 buf++;
1139 }
1140 first = false;
1141
1142 spec.field_width = DIV_ROUND_UP(chunksz, 4);
1143 buf = number(buf, end, val, spec);
1144
1145 chunksz = CHUNKSZ;
1146 }
1147 return buf;
1148}
1149
1150static noinline_for_stack
1151char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1152 struct printf_spec spec, const char *fmt)
1153{
1154 int nr_bits = max_t(int, spec.field_width, 0);
1155 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
1156 int cur, rbot, rtop;
1157 bool first = true;
1158
Petr Mladek3e5903e2019-04-17 13:53:48 +02001159 if (check_pointer(&buf, end, bitmap, spec))
1160 return buf;
1161
Tejun Heodbc760b2015-02-13 14:36:53 -08001162 rbot = cur = find_first_bit(bitmap, nr_bits);
1163 while (cur < nr_bits) {
1164 rtop = cur;
1165 cur = find_next_bit(bitmap, nr_bits, cur + 1);
1166 if (cur < nr_bits && cur <= rtop + 1)
1167 continue;
1168
1169 if (!first) {
1170 if (buf < end)
1171 *buf = ',';
1172 buf++;
1173 }
1174 first = false;
1175
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001176 buf = number(buf, end, rbot, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001177 if (rbot < rtop) {
1178 if (buf < end)
1179 *buf = '-';
1180 buf++;
1181
Andy Shevchenkoce0b4912018-02-16 23:07:04 +02001182 buf = number(buf, end, rtop, default_dec_spec);
Tejun Heodbc760b2015-02-13 14:36:53 -08001183 }
1184
1185 rbot = cur;
1186 }
1187 return buf;
1188}
1189
1190static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001191char *mac_address_string(char *buf, char *end, u8 *addr,
1192 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001193{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001194 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001195 char *p = mac_addr;
1196 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001197 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001198 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001199
Petr Mladek3e5903e2019-04-17 13:53:48 +02001200 if (check_pointer(&buf, end, addr, spec))
1201 return buf;
1202
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001203 switch (fmt[1]) {
1204 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +00001205 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001206 break;
1207
1208 case 'R':
1209 reversed = true;
1210 /* fall through */
1211
1212 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +00001213 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001214 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +00001215 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001216
1217 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001218 if (reversed)
1219 p = hex_byte_pack(p, addr[5 - i]);
1220 else
1221 p = hex_byte_pack(p, addr[i]);
1222
Joe Perches8a27f7c2009-08-17 12:29:44 +00001223 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +00001224 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001225 }
1226 *p = '\0';
1227
Petr Mladekd529ac42019-04-17 13:53:43 +02001228 return string_nocheck(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001229}
1230
Joe Perchescf3b4292010-05-24 14:33:16 -07001231static noinline_for_stack
1232char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -07001233{
Harvey Harrison689afa72008-10-28 16:04:44 -07001234 int i;
Joe Perches0159f242010-01-13 20:23:30 -08001235 bool leading_zeros = (fmt[0] == 'i');
1236 int index;
1237 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -07001238
Joe Perches0159f242010-01-13 20:23:30 -08001239 switch (fmt[2]) {
1240 case 'h':
1241#ifdef __BIG_ENDIAN
1242 index = 0;
1243 step = 1;
1244#else
1245 index = 3;
1246 step = -1;
1247#endif
1248 break;
1249 case 'l':
1250 index = 3;
1251 step = -1;
1252 break;
1253 case 'n':
1254 case 'b':
1255 default:
1256 index = 0;
1257 step = 1;
1258 break;
1259 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001260 for (i = 0; i < 4; i++) {
Rasmus Villemoes7c43d9a2015-04-16 12:43:22 -07001261 char temp[4] __aligned(2); /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -07001262 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001263 if (leading_zeros) {
1264 if (digits < 3)
1265 *p++ = '0';
1266 if (digits < 2)
1267 *p++ = '0';
1268 }
1269 /* reverse the digits in the quad */
1270 while (digits--)
1271 *p++ = temp[digits];
1272 if (i < 3)
1273 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -08001274 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001275 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001276 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001277
Joe Perches8a27f7c2009-08-17 12:29:44 +00001278 return p;
1279}
1280
Joe Perchescf3b4292010-05-24 14:33:16 -07001281static noinline_for_stack
1282char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001283{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001284 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001285 unsigned char zerolength[8];
1286 int longest = 1;
1287 int colonpos = -1;
1288 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001289 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001290 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +00001291 bool useIPv4;
1292 struct in6_addr in6;
1293
1294 memcpy(&in6, addr, sizeof(struct in6_addr));
1295
1296 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001297
1298 memset(zerolength, 0, sizeof(zerolength));
1299
1300 if (useIPv4)
1301 range = 6;
1302 else
1303 range = 8;
1304
1305 /* find position of longest 0 run */
1306 for (i = 0; i < range; i++) {
1307 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +00001308 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001309 break;
1310 zerolength[i]++;
1311 }
1312 }
1313 for (i = 0; i < range; i++) {
1314 if (zerolength[i] > longest) {
1315 longest = zerolength[i];
1316 colonpos = i;
1317 }
1318 }
Joe Perches29cf5192011-06-09 11:23:37 -07001319 if (longest == 1) /* don't compress a single 0 */
1320 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +00001321
1322 /* emit address */
1323 for (i = 0; i < range; i++) {
1324 if (i == colonpos) {
1325 if (needcolon || i == 0)
1326 *p++ = ':';
1327 *p++ = ':';
1328 needcolon = false;
1329 i += longest - 1;
1330 continue;
1331 }
1332 if (needcolon) {
1333 *p++ = ':';
1334 needcolon = false;
1335 }
1336 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +00001337 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001338 hi = word >> 8;
1339 lo = word & 0xff;
1340 if (hi) {
1341 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001342 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001343 else
1344 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001345 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001346 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -08001347 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001348 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001349 else
1350 *p++ = hex_asc_lo(lo);
1351 needcolon = true;
1352 }
1353
1354 if (useIPv4) {
1355 if (needcolon)
1356 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -08001357 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +00001358 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00001359 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001360
Joe Perches8a27f7c2009-08-17 12:29:44 +00001361 return p;
1362}
1363
Joe Perchescf3b4292010-05-24 14:33:16 -07001364static noinline_for_stack
1365char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001366{
1367 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001368
Harvey Harrison689afa72008-10-28 16:04:44 -07001369 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -07001370 p = hex_byte_pack(p, *addr++);
1371 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001372 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -07001373 *p++ = ':';
1374 }
1375 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001376
Joe Perches8a27f7c2009-08-17 12:29:44 +00001377 return p;
1378}
1379
Joe Perchescf3b4292010-05-24 14:33:16 -07001380static noinline_for_stack
1381char *ip6_addr_string(char *buf, char *end, const u8 *addr,
1382 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001383{
1384 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1385
1386 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +00001387 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001388 else
Joe Percheseb78cd22009-09-18 13:04:06 +00001389 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -07001390
Petr Mladekd529ac42019-04-17 13:53:43 +02001391 return string_nocheck(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -07001392}
1393
Joe Perchescf3b4292010-05-24 14:33:16 -07001394static noinline_for_stack
1395char *ip4_addr_string(char *buf, char *end, const u8 *addr,
1396 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -07001397{
Joe Perches8a27f7c2009-08-17 12:29:44 +00001398 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -07001399
Joe Perches0159f242010-01-13 20:23:30 -08001400 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001401
Petr Mladekd529ac42019-04-17 13:53:43 +02001402 return string_nocheck(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -07001403}
1404
Joe Perchescf3b4292010-05-24 14:33:16 -07001405static noinline_for_stack
Daniel Borkmann10679642013-06-28 19:49:39 +02001406char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
1407 struct printf_spec spec, const char *fmt)
1408{
1409 bool have_p = false, have_s = false, have_f = false, have_c = false;
1410 char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1411 sizeof(":12345") + sizeof("/123456789") +
1412 sizeof("%1234567890")];
1413 char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
1414 const u8 *addr = (const u8 *) &sa->sin6_addr;
1415 char fmt6[2] = { fmt[0], '6' };
1416 u8 off = 0;
1417
1418 fmt++;
1419 while (isalpha(*++fmt)) {
1420 switch (*fmt) {
1421 case 'p':
1422 have_p = true;
1423 break;
1424 case 'f':
1425 have_f = true;
1426 break;
1427 case 's':
1428 have_s = true;
1429 break;
1430 case 'c':
1431 have_c = true;
1432 break;
1433 }
1434 }
1435
1436 if (have_p || have_s || have_f) {
1437 *p = '[';
1438 off = 1;
1439 }
1440
1441 if (fmt6[0] == 'I' && have_c)
1442 p = ip6_compressed_string(ip6_addr + off, addr);
1443 else
1444 p = ip6_string(ip6_addr + off, addr, fmt6);
1445
1446 if (have_p || have_s || have_f)
1447 *p++ = ']';
1448
1449 if (have_p) {
1450 *p++ = ':';
1451 p = number(p, pend, ntohs(sa->sin6_port), spec);
1452 }
1453 if (have_f) {
1454 *p++ = '/';
1455 p = number(p, pend, ntohl(sa->sin6_flowinfo &
1456 IPV6_FLOWINFO_MASK), spec);
1457 }
1458 if (have_s) {
1459 *p++ = '%';
1460 p = number(p, pend, sa->sin6_scope_id, spec);
1461 }
1462 *p = '\0';
1463
Petr Mladekd529ac42019-04-17 13:53:43 +02001464 return string_nocheck(buf, end, ip6_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001465}
1466
1467static noinline_for_stack
1468char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
1469 struct printf_spec spec, const char *fmt)
1470{
1471 bool have_p = false;
1472 char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
1473 char *pend = ip4_addr + sizeof(ip4_addr);
1474 const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
1475 char fmt4[3] = { fmt[0], '4', 0 };
1476
1477 fmt++;
1478 while (isalpha(*++fmt)) {
1479 switch (*fmt) {
1480 case 'p':
1481 have_p = true;
1482 break;
1483 case 'h':
1484 case 'l':
1485 case 'n':
1486 case 'b':
1487 fmt4[2] = *fmt;
1488 break;
1489 }
1490 }
1491
1492 p = ip4_string(ip4_addr, addr, fmt4);
1493 if (have_p) {
1494 *p++ = ':';
1495 p = number(p, pend, ntohs(sa->sin_port), spec);
1496 }
1497 *p = '\0';
1498
Petr Mladekd529ac42019-04-17 13:53:43 +02001499 return string_nocheck(buf, end, ip4_addr, spec);
Daniel Borkmann10679642013-06-28 19:49:39 +02001500}
1501
1502static noinline_for_stack
Petr Mladekf00cc102019-04-17 13:53:44 +02001503char *ip_addr_string(char *buf, char *end, const void *ptr,
1504 struct printf_spec spec, const char *fmt)
1505{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001506 char *err_fmt_msg;
1507
Petr Mladek3e5903e2019-04-17 13:53:48 +02001508 if (check_pointer(&buf, end, ptr, spec))
1509 return buf;
1510
Petr Mladekf00cc102019-04-17 13:53:44 +02001511 switch (fmt[1]) {
1512 case '6':
1513 return ip6_addr_string(buf, end, ptr, spec, fmt);
1514 case '4':
1515 return ip4_addr_string(buf, end, ptr, spec, fmt);
1516 case 'S': {
1517 const union {
1518 struct sockaddr raw;
1519 struct sockaddr_in v4;
1520 struct sockaddr_in6 v6;
1521 } *sa = ptr;
1522
1523 switch (sa->raw.sa_family) {
1524 case AF_INET:
1525 return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1526 case AF_INET6:
1527 return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1528 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001529 return error_string(buf, end, "(einval)", spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001530 }}
1531 }
1532
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001533 err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
Petr Mladekc8c3b582019-04-17 13:53:50 +02001534 return error_string(buf, end, err_fmt_msg, spec);
Petr Mladekf00cc102019-04-17 13:53:44 +02001535}
1536
1537static noinline_for_stack
Andy Shevchenko71dca952014-10-13 15:55:18 -07001538char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
1539 const char *fmt)
1540{
1541 bool found = true;
1542 int count = 1;
1543 unsigned int flags = 0;
1544 int len;
1545
1546 if (spec.field_width == 0)
1547 return buf; /* nothing to print */
1548
Petr Mladek3e5903e2019-04-17 13:53:48 +02001549 if (check_pointer(&buf, end, addr, spec))
1550 return buf;
Andy Shevchenko71dca952014-10-13 15:55:18 -07001551
1552 do {
1553 switch (fmt[count++]) {
1554 case 'a':
1555 flags |= ESCAPE_ANY;
1556 break;
1557 case 'c':
1558 flags |= ESCAPE_SPECIAL;
1559 break;
1560 case 'h':
1561 flags |= ESCAPE_HEX;
1562 break;
1563 case 'n':
1564 flags |= ESCAPE_NULL;
1565 break;
1566 case 'o':
1567 flags |= ESCAPE_OCTAL;
1568 break;
1569 case 'p':
1570 flags |= ESCAPE_NP;
1571 break;
1572 case 's':
1573 flags |= ESCAPE_SPACE;
1574 break;
1575 default:
1576 found = false;
1577 break;
1578 }
1579 } while (found);
1580
1581 if (!flags)
1582 flags = ESCAPE_ANY_NP;
1583
1584 len = spec.field_width < 0 ? 1 : spec.field_width;
1585
Rasmus Villemoes41416f22015-04-15 16:17:28 -07001586 /*
1587 * string_escape_mem() writes as many characters as it can to
1588 * the given buffer, and returns the total size of the output
1589 * had the buffer been big enough.
1590 */
1591 buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
Andy Shevchenko71dca952014-10-13 15:55:18 -07001592
1593 return buf;
1594}
1595
Petr Mladek3e5903e2019-04-17 13:53:48 +02001596static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1597 struct printf_spec spec, const char *fmt)
Petr Mladek45c3e932019-04-17 13:53:45 +02001598{
1599 va_list va;
1600
Petr Mladek3e5903e2019-04-17 13:53:48 +02001601 if (check_pointer(&buf, end, va_fmt, spec))
1602 return buf;
1603
Petr Mladek45c3e932019-04-17 13:53:45 +02001604 va_copy(va, *va_fmt->va);
1605 buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
1606 va_end(va);
1607
1608 return buf;
1609}
1610
Andy Shevchenko71dca952014-10-13 15:55:18 -07001611static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -07001612char *uuid_string(char *buf, char *end, const u8 *addr,
1613 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -08001614{
Andy Shevchenko2b1b0d62016-05-20 17:01:04 -07001615 char uuid[UUID_STRING_LEN + 1];
Joe Perches9ac6e442009-12-14 18:01:09 -08001616 char *p = uuid;
1617 int i;
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001618 const u8 *index = uuid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001619 bool uc = false;
1620
Petr Mladek3e5903e2019-04-17 13:53:48 +02001621 if (check_pointer(&buf, end, addr, spec))
1622 return buf;
1623
Joe Perches9ac6e442009-12-14 18:01:09 -08001624 switch (*(++fmt)) {
1625 case 'L':
1626 uc = true; /* fall-through */
1627 case 'l':
Christoph Hellwigf9727a12017-05-17 10:02:48 +02001628 index = guid_index;
Joe Perches9ac6e442009-12-14 18:01:09 -08001629 break;
1630 case 'B':
1631 uc = true;
1632 break;
1633 }
1634
1635 for (i = 0; i < 16; i++) {
Andy Shevchenkoaa4ea1c2016-05-20 17:00:54 -07001636 if (uc)
1637 p = hex_byte_pack_upper(p, addr[index[i]]);
1638 else
1639 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -08001640 switch (i) {
1641 case 3:
1642 case 5:
1643 case 7:
1644 case 9:
1645 *p++ = '-';
1646 break;
1647 }
1648 }
1649
1650 *p = 0;
1651
Petr Mladekd529ac42019-04-17 13:53:43 +02001652 return string_nocheck(buf, end, uuid, spec);
Joe Perches9ac6e442009-12-14 18:01:09 -08001653}
1654
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001655static noinline_for_stack
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02001656char *netdev_bits(char *buf, char *end, const void *addr,
1657 struct printf_spec spec, const char *fmt)
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001658{
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001659 unsigned long long num;
1660 int size;
1661
Petr Mladek3e5903e2019-04-17 13:53:48 +02001662 if (check_pointer(&buf, end, addr, spec))
1663 return buf;
1664
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001665 switch (fmt[1]) {
1666 case 'F':
1667 num = *(const netdev_features_t *)addr;
1668 size = sizeof(netdev_features_t);
1669 break;
1670 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001671 return error_string(buf, end, "(%pN?)", spec);
Andy Shevchenko5b17aec2016-01-15 16:59:20 -08001672 }
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001673
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001674 return special_hex_number(buf, end, num, size);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001675}
1676
Joe Perchesaaf07622014-01-23 15:54:17 -08001677static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001678char *address_val(char *buf, char *end, const void *addr,
1679 struct printf_spec spec, const char *fmt)
Joe Perchesaaf07622014-01-23 15:54:17 -08001680{
1681 unsigned long long num;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001682 int size;
Joe Perchesaaf07622014-01-23 15:54:17 -08001683
Petr Mladek3e5903e2019-04-17 13:53:48 +02001684 if (check_pointer(&buf, end, addr, spec))
1685 return buf;
1686
Joe Perchesaaf07622014-01-23 15:54:17 -08001687 switch (fmt[1]) {
1688 case 'd':
1689 num = *(const dma_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001690 size = sizeof(dma_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001691 break;
1692 case 'p':
1693 default:
1694 num = *(const phys_addr_t *)addr;
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001695 size = sizeof(phys_addr_t);
Joe Perchesaaf07622014-01-23 15:54:17 -08001696 break;
1697 }
1698
Andy Shevchenko3cab1e72016-01-15 16:59:18 -08001699 return special_hex_number(buf, end, num, size);
Joe Perchesaaf07622014-01-23 15:54:17 -08001700}
1701
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001702static noinline_for_stack
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001703char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1704{
1705 int year = tm->tm_year + (r ? 0 : 1900);
1706 int mon = tm->tm_mon + (r ? 0 : 1);
1707
1708 buf = number(buf, end, year, default_dec04_spec);
1709 if (buf < end)
1710 *buf = '-';
1711 buf++;
1712
1713 buf = number(buf, end, mon, default_dec02_spec);
1714 if (buf < end)
1715 *buf = '-';
1716 buf++;
1717
1718 return number(buf, end, tm->tm_mday, default_dec02_spec);
1719}
1720
1721static noinline_for_stack
1722char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1723{
1724 buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1725 if (buf < end)
1726 *buf = ':';
1727 buf++;
1728
1729 buf = number(buf, end, tm->tm_min, default_dec02_spec);
1730 if (buf < end)
1731 *buf = ':';
1732 buf++;
1733
1734 return number(buf, end, tm->tm_sec, default_dec02_spec);
1735}
1736
1737static noinline_for_stack
Petr Mladek3e5903e2019-04-17 13:53:48 +02001738char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
1739 struct printf_spec spec, const char *fmt)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001740{
1741 bool have_t = true, have_d = true;
1742 bool raw = false;
1743 int count = 2;
1744
Petr Mladek3e5903e2019-04-17 13:53:48 +02001745 if (check_pointer(&buf, end, tm, spec))
1746 return buf;
1747
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001748 switch (fmt[count]) {
1749 case 'd':
1750 have_t = false;
1751 count++;
1752 break;
1753 case 't':
1754 have_d = false;
1755 count++;
1756 break;
1757 }
1758
1759 raw = fmt[count] == 'r';
1760
1761 if (have_d)
1762 buf = date_str(buf, end, tm, raw);
1763 if (have_d && have_t) {
1764 /* Respect ISO 8601 */
1765 if (buf < end)
1766 *buf = 'T';
1767 buf++;
1768 }
1769 if (have_t)
1770 buf = time_str(buf, end, tm, raw);
1771
1772 return buf;
1773}
1774
1775static noinline_for_stack
1776char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1777 const char *fmt)
1778{
1779 switch (fmt[1]) {
1780 case 'R':
Petr Mladek3e5903e2019-04-17 13:53:48 +02001781 return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001782 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001783 return error_string(buf, end, "(%ptR?)", spec);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02001784 }
1785}
1786
1787static noinline_for_stack
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001788char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1789 const char *fmt)
1790{
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001791 if (!IS_ENABLED(CONFIG_HAVE_CLK))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001792 return error_string(buf, end, "(%pC?)", spec);
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001793
Petr Mladek3e5903e2019-04-17 13:53:48 +02001794 if (check_pointer(&buf, end, clk, spec))
1795 return buf;
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001796
1797 switch (fmt[1]) {
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001798 case 'n':
1799 default:
1800#ifdef CONFIG_COMMON_CLK
1801 return string(buf, end, __clk_get_name(clk), spec);
1802#else
Petr Mladekc8c3b582019-04-17 13:53:50 +02001803 return error_string(buf, end, "(%pC?)", spec);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07001804#endif
1805 }
1806}
1807
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001808static
1809char *format_flags(char *buf, char *end, unsigned long flags,
1810 const struct trace_print_flags *names)
1811{
1812 unsigned long mask;
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001813
1814 for ( ; flags && names->name; names++) {
1815 mask = names->mask;
1816 if ((flags & mask) != mask)
1817 continue;
1818
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001819 buf = string(buf, end, names->name, default_str_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001820
1821 flags &= ~mask;
1822 if (flags) {
1823 if (buf < end)
1824 *buf = '|';
1825 buf++;
1826 }
1827 }
1828
1829 if (flags)
Andy Shevchenko54433972018-02-16 23:07:06 +02001830 buf = number(buf, end, flags, default_flag_spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001831
1832 return buf;
1833}
1834
1835static noinline_for_stack
Petr Mladek0b74d4d2019-04-17 13:53:47 +02001836char *flags_string(char *buf, char *end, void *flags_ptr,
1837 struct printf_spec spec, const char *fmt)
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001838{
1839 unsigned long flags;
1840 const struct trace_print_flags *names;
1841
Petr Mladek3e5903e2019-04-17 13:53:48 +02001842 if (check_pointer(&buf, end, flags_ptr, spec))
1843 return buf;
1844
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001845 switch (fmt[1]) {
1846 case 'p':
1847 flags = *(unsigned long *)flags_ptr;
1848 /* Remove zone id */
1849 flags &= (1UL << NR_PAGEFLAGS) - 1;
1850 names = pageflag_names;
1851 break;
1852 case 'v':
1853 flags = *(unsigned long *)flags_ptr;
1854 names = vmaflag_names;
1855 break;
1856 case 'g':
1857 flags = *(gfp_t *)flags_ptr;
1858 names = gfpflag_names;
1859 break;
1860 default:
Petr Mladekc8c3b582019-04-17 13:53:50 +02001861 return error_string(buf, end, "(%pG?)", spec);
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07001862 }
1863
1864 return format_flags(buf, end, flags, names);
1865}
1866
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001867static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1868{
1869 for ( ; np && depth; depth--)
1870 np = np->parent;
1871
1872 return kbasename(np->full_name);
1873}
1874
1875static noinline_for_stack
1876char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1877{
1878 int depth;
1879 const struct device_node *parent = np->parent;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001880
1881 /* special case for root node */
1882 if (!parent)
Petr Mladekd529ac42019-04-17 13:53:43 +02001883 return string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001884
1885 for (depth = 0; parent->parent; depth++)
1886 parent = parent->parent;
1887
1888 for ( ; depth >= 0; depth--) {
Petr Mladekd529ac42019-04-17 13:53:43 +02001889 buf = string_nocheck(buf, end, "/", default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001890 buf = string(buf, end, device_node_name_for_depth(np, depth),
Andy Shevchenkoabd4fe62018-02-16 23:07:05 +02001891 default_str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001892 }
1893 return buf;
1894}
1895
1896static noinline_for_stack
1897char *device_node_string(char *buf, char *end, struct device_node *dn,
1898 struct printf_spec spec, const char *fmt)
1899{
1900 char tbuf[sizeof("xxxx") + 1];
1901 const char *p;
1902 int ret;
1903 char *buf_start = buf;
1904 struct property *prop;
1905 bool has_mult, pass;
1906 static const struct printf_spec num_spec = {
1907 .flags = SMALL,
1908 .field_width = -1,
1909 .precision = -1,
1910 .base = 10,
1911 };
1912
1913 struct printf_spec str_spec = spec;
1914 str_spec.field_width = -1;
1915
1916 if (!IS_ENABLED(CONFIG_OF))
Petr Mladekc8c3b582019-04-17 13:53:50 +02001917 return error_string(buf, end, "(%pOF?)", spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001918
Petr Mladek3e5903e2019-04-17 13:53:48 +02001919 if (check_pointer(&buf, end, dn, spec))
1920 return buf;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001921
1922 /* simple case without anything any more format specifiers */
1923 fmt++;
1924 if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1925 fmt = "f";
1926
1927 for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
Rob Herring6d0a70a2018-08-27 08:13:56 -05001928 int precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001929 if (pass) {
1930 if (buf < end)
1931 *buf = ':';
1932 buf++;
1933 }
1934
1935 switch (*fmt) {
1936 case 'f': /* full_name */
1937 buf = device_node_gen_full_name(dn, buf, end);
1938 break;
1939 case 'n': /* name */
Rob Herring6d0a70a2018-08-27 08:13:56 -05001940 p = kbasename(of_node_full_name(dn));
1941 precision = str_spec.precision;
1942 str_spec.precision = strchrnul(p, '@') - p;
1943 buf = string(buf, end, p, str_spec);
1944 str_spec.precision = precision;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001945 break;
1946 case 'p': /* phandle */
1947 buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1948 break;
1949 case 'P': /* path-spec */
1950 p = kbasename(of_node_full_name(dn));
1951 if (!p[1])
1952 p = "/";
1953 buf = string(buf, end, p, str_spec);
1954 break;
1955 case 'F': /* flags */
1956 tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1957 tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1958 tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1959 tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1960 tbuf[4] = 0;
Petr Mladekd529ac42019-04-17 13:53:43 +02001961 buf = string_nocheck(buf, end, tbuf, str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001962 break;
1963 case 'c': /* major compatible string */
1964 ret = of_property_read_string(dn, "compatible", &p);
1965 if (!ret)
1966 buf = string(buf, end, p, str_spec);
1967 break;
1968 case 'C': /* full compatible string */
1969 has_mult = false;
1970 of_property_for_each_string(dn, "compatible", prop, p) {
1971 if (has_mult)
Petr Mladekd529ac42019-04-17 13:53:43 +02001972 buf = string_nocheck(buf, end, ",", str_spec);
1973 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001974 buf = string(buf, end, p, str_spec);
Petr Mladekd529ac42019-04-17 13:53:43 +02001975 buf = string_nocheck(buf, end, "\"", str_spec);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02001976
1977 has_mult = true;
1978 }
1979 break;
1980 default:
1981 break;
1982 }
1983 }
1984
1985 return widen_string(buf, buf - buf_start, end, spec);
1986}
1987
Petr Mladek798cc272019-04-17 13:53:46 +02001988static char *kobject_string(char *buf, char *end, void *ptr,
1989 struct printf_spec spec, const char *fmt)
1990{
1991 switch (fmt[1]) {
1992 case 'F':
1993 return device_node_string(buf, end, ptr, spec, fmt + 1);
1994 }
1995
Petr Mladekc8c3b582019-04-17 13:53:50 +02001996 return error_string(buf, end, "(%pO?)", spec);
Petr Mladek798cc272019-04-17 13:53:46 +02001997}
1998
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001999/*
2000 * Show a '%p' thing. A kernel extension is that the '%p' is followed
2001 * by an extra set of alphanumeric characters that are extended format
2002 * specifiers.
2003 *
Joe Perches0b523762017-05-08 15:55:36 -07002004 * Please update scripts/checkpatch.pl when adding/removing conversion
2005 * characters. (Search for "check for vsprintf extension").
2006 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11002007 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002008 *
Sergey Senozhatskycdb7e522018-04-14 12:00:05 +09002009 * - 'S' For symbolic direct pointers (or function descriptors) with offset
2010 * - 's' For symbolic direct pointers (or function descriptors) without offset
2011 * - 'F' Same as 'S'
2012 * - 'f' Same as 's'
Joe Perchesb0d33c22012-12-12 10:18:50 -08002013 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002014 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002015 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2016 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Tejun Heodbc760b2015-02-13 14:36:53 -08002017 * - 'b[l]' For a bitmap, the number of bits is determined by the field
2018 * width which must be explicitly specified either as part of the
2019 * format string '%32b[l]' or through '%*b[l]', [l] selects
2020 * range-list format instead of hex format
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07002021 * - 'M' For a 6-byte MAC address, it prints the address in the
2022 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00002023 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00002024 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08002025 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07002026 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00002027 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2028 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
2029 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
Daniel Borkmann10679642013-06-28 19:49:39 +02002030 * [S][pfs]
2031 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2032 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
Joe Perches8a27f7c2009-08-17 12:29:44 +00002033 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
2034 * IPv6 omits the colons (01020304...0f)
2035 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Daniel Borkmann10679642013-06-28 19:49:39 +02002036 * [S][pfs]
2037 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
2038 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
2039 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
2040 * - 'I[6S]c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07002041 * http://tools.ietf.org/html/rfc5952
Andy Shevchenko71dca952014-10-13 15:55:18 -07002042 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
2043 * of the following flags (see string_escape_mem() for the
2044 * details):
2045 * a - ESCAPE_ANY
2046 * c - ESCAPE_SPECIAL
2047 * h - ESCAPE_HEX
2048 * n - ESCAPE_NULL
2049 * o - ESCAPE_OCTAL
2050 * p - ESCAPE_NP
2051 * s - ESCAPE_SPACE
2052 * By default ESCAPE_ANY_NP is used.
Joe Perches9ac6e442009-12-14 18:01:09 -08002053 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
2054 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
2055 * Options for %pU are:
2056 * b big endian lower case hex (default)
2057 * B big endian UPPER case hex
2058 * l little endian lower case hex
2059 * L little endian UPPER case hex
2060 * big endian output byte order is:
2061 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
2062 * little endian output byte order is:
2063 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00002064 * - 'V' For a struct va_format which contains a format string * and va_list *,
2065 * call vsnprintf(->format, *->va_list).
2066 * Implements a "recursive vsnprintf".
2067 * Do not use this feature without some mechanism to verify the
2068 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002069 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002070 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07002071 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2072 * a certain separator (' ' by default):
2073 * C colon
2074 * D dash
2075 * N no separator
2076 * The maximum supported length is 64 bytes of the input. Consider
2077 * to use print_hex_dump() for the larger input.
Joe Perchesaaf07622014-01-23 15:54:17 -08002078 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2079 * (default assumed to be phys_addr_t, passed by reference)
Olof Johanssonc0d92a52013-11-12 15:09:50 -08002080 * - 'd[234]' For a dentry name (optionally 2-4 last components)
2081 * - 'D[234]' Same as 'd' but for a struct file
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002082 * - 'g' For block_device name (gendisk + partition number)
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002083 * - 't[R][dt][r]' For time and date as represented:
2084 * R struct rtc_time
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002085 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2086 * (legacy clock framework) of the clock
2087 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2088 * (legacy clock framework) of the clock
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002089 * - 'G' For flags to be printed as a collection of symbolic strings that would
2090 * construct the specific value. Supported flags given by option:
2091 * p page flags (see struct page) given as pointer to unsigned long
2092 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2093 * v vma flags (VM_*) given as pointer to unsigned long
Geert Uytterhoeven94ac8f22018-10-08 13:08:48 +02002094 * - 'OF[fnpPcCF]' For a device tree object
2095 * Without any optional arguments prints the full_name
2096 * f device node full_name
2097 * n device node name
2098 * p device node phandle
2099 * P device node path spec (name + @unit)
2100 * F device node flags
2101 * c major compatible string
2102 * C full compatible string
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002103 * - 'x' For printing the address. Equivalent to "%lx".
2104 *
Tobin C. Hardingb3ed2322017-12-20 08:17:15 +11002105 * ** When making changes please also update:
2106 * Documentation/core-api/printk-formats.rst
Joe Perches9ac6e442009-12-14 18:01:09 -08002107 *
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002108 * Note: The default behaviour (unadorned %p) is to hash the address,
2109 * rendering it useful as a unique identifier.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07002110 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002111static noinline_for_stack
2112char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2113 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002114{
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002115 switch (*fmt) {
2116 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02002117 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002118 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08002119 case 's':
Sergey Senozhatsky04b8eb72017-12-06 13:36:49 +09002120 ptr = dereference_symbol_descriptor(ptr);
2121 /* Fallthrough */
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09002122 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08002123 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11002124 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06002125 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06002126 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07002127 case 'h':
2128 return hex_string(buf, end, ptr, spec, fmt);
Tejun Heodbc760b2015-02-13 14:36:53 -08002129 case 'b':
2130 switch (fmt[1]) {
2131 case 'l':
2132 return bitmap_list_string(buf, end, ptr, spec, fmt);
2133 default:
2134 return bitmap_string(buf, end, ptr, spec, fmt);
2135 }
Joe Perches8a27f7c2009-08-17 12:29:44 +00002136 case 'M': /* Colon separated: 00:01:02:03:04:05 */
2137 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07002138 /* [mM]F (FDDI) */
2139 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00002140 return mac_address_string(buf, end, ptr, spec, fmt);
2141 case 'I': /* Formatted IP supported
2142 * 4: 1.2.3.4
2143 * 6: 0001:0203:...:0708
2144 * 6c: 1::708 or 1::1.2.3.4
2145 */
2146 case 'i': /* Contiguous:
2147 * 4: 001.002.003.004
2148 * 6: 000102...0f
2149 */
Petr Mladekf00cc102019-04-17 13:53:44 +02002150 return ip_addr_string(buf, end, ptr, spec, fmt);
Andy Shevchenko71dca952014-10-13 15:55:18 -07002151 case 'E':
2152 return escaped_string(buf, end, ptr, spec, fmt);
Joe Perches9ac6e442009-12-14 18:01:09 -08002153 case 'U':
2154 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00002155 case 'V':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002156 return va_format(buf, end, ptr, spec, fmt);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08002157 case 'K':
Tobin C. Harding57e73442017-11-23 10:56:39 +11002158 return restricted_pointer(buf, end, ptr, spec);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002159 case 'N':
Geert Uytterhoeven431bca22018-10-11 10:42:49 +02002160 return netdev_bits(buf, end, ptr, spec, fmt);
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08002161 case 'a':
Petr Mladek3e5903e2019-04-17 13:53:48 +02002162 return address_val(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002163 case 'd':
2164 return dentry_name(buf, end, ptr, spec, fmt);
Andy Shevchenko4d42c442018-12-04 23:23:11 +02002165 case 't':
2166 return time_and_date(buf, end, ptr, spec, fmt);
Geert Uytterhoeven900cca22015-04-15 16:17:20 -07002167 case 'C':
2168 return clock(buf, end, ptr, spec, fmt);
Al Viro4b6ccca2013-09-03 12:00:44 -04002169 case 'D':
2170 return dentry_name(buf, end,
2171 ((const struct file *)ptr)->f_path.dentry,
2172 spec, fmt);
Dmitry Monakhov1031bc52015-04-13 16:31:35 +04002173#ifdef CONFIG_BLOCK
2174 case 'g':
2175 return bdev_name(buf, end, ptr, spec, fmt);
2176#endif
2177
Vlastimil Babkaedf14cd2016-03-15 14:55:56 -07002178 case 'G':
Petr Mladek0b74d4d2019-04-17 13:53:47 +02002179 return flags_string(buf, end, ptr, spec, fmt);
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02002180 case 'O':
Petr Mladek798cc272019-04-17 13:53:46 +02002181 return kobject_string(buf, end, ptr, spec, fmt);
Tobin C. Harding7b1924a2017-11-23 10:59:45 +11002182 case 'x':
2183 return pointer_string(buf, end, ptr, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07002184 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002185
Tobin C. Hardingad67b742017-11-01 15:32:23 +11002186 /* default is to _not_ leak addresses, hash before printing */
2187 return ptr_to_id(buf, end, ptr, spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002188}
2189
2190/*
2191 * Helper function to decode printf style format.
2192 * Each call decode a token from the format and return the
2193 * number of characters read (or likely the delta where it wants
2194 * to go on the next call).
2195 * The decoded token is returned through the parameters
2196 *
2197 * 'h', 'l', or 'L' for integer fields
2198 * 'z' support added 23/7/1999 S.H.
2199 * 'z' changed to 'Z' --davidm 1/25/99
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002200 * 'Z' changed to 'z' --adobriyan 2017-01-25
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002201 * 't' added for ptrdiff_t
2202 *
2203 * @fmt: the format string
2204 * @type of the token returned
2205 * @flags: various flags such as +, -, # tokens..
2206 * @field_width: overwritten width
2207 * @base: base of the number (octal, hex, ...)
2208 * @precision: precision of a number
2209 * @qualifier: qualifier of a number (long, size_t, ...)
2210 */
Joe Perchescf3b4292010-05-24 14:33:16 -07002211static noinline_for_stack
2212int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002213{
2214 const char *start = fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002215 char qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002216
2217 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01002218 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002219 if (spec->field_width < 0) {
2220 spec->field_width = -spec->field_width;
2221 spec->flags |= LEFT;
2222 }
2223 spec->type = FORMAT_TYPE_NONE;
2224 goto precision;
2225 }
2226
2227 /* we finished early by reading the precision */
2228 if (spec->type == FORMAT_TYPE_PRECISION) {
2229 if (spec->precision < 0)
2230 spec->precision = 0;
2231
2232 spec->type = FORMAT_TYPE_NONE;
2233 goto qualifier;
2234 }
2235
2236 /* By default */
2237 spec->type = FORMAT_TYPE_NONE;
2238
2239 for (; *fmt ; ++fmt) {
2240 if (*fmt == '%')
2241 break;
2242 }
2243
2244 /* Return the current non-format string */
2245 if (fmt != start || !*fmt)
2246 return fmt - start;
2247
2248 /* Process flags */
2249 spec->flags = 0;
2250
2251 while (1) { /* this also skips first '%' */
2252 bool found = true;
2253
2254 ++fmt;
2255
2256 switch (*fmt) {
2257 case '-': spec->flags |= LEFT; break;
2258 case '+': spec->flags |= PLUS; break;
2259 case ' ': spec->flags |= SPACE; break;
2260 case '#': spec->flags |= SPECIAL; break;
2261 case '0': spec->flags |= ZEROPAD; break;
2262 default: found = false;
2263 }
2264
2265 if (!found)
2266 break;
2267 }
2268
2269 /* get field width */
2270 spec->field_width = -1;
2271
2272 if (isdigit(*fmt))
2273 spec->field_width = skip_atoi(&fmt);
2274 else if (*fmt == '*') {
2275 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01002276 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002277 return ++fmt - start;
2278 }
2279
2280precision:
2281 /* get the precision */
2282 spec->precision = -1;
2283 if (*fmt == '.') {
2284 ++fmt;
2285 if (isdigit(*fmt)) {
2286 spec->precision = skip_atoi(&fmt);
2287 if (spec->precision < 0)
2288 spec->precision = 0;
2289 } else if (*fmt == '*') {
2290 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01002291 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002292 return ++fmt - start;
2293 }
2294 }
2295
2296qualifier:
2297 /* get the conversion qualifier */
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002298 qualifier = 0;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002299 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002300 *fmt == 'z' || *fmt == 't') {
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002301 qualifier = *fmt++;
2302 if (unlikely(qualifier == *fmt)) {
2303 if (qualifier == 'l') {
2304 qualifier = 'L';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002305 ++fmt;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002306 } else if (qualifier == 'h') {
2307 qualifier = 'H';
Zhaoleia4e94ef2009-03-27 17:07:05 +08002308 ++fmt;
2309 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002310 }
2311 }
2312
2313 /* default base */
2314 spec->base = 10;
2315 switch (*fmt) {
2316 case 'c':
2317 spec->type = FORMAT_TYPE_CHAR;
2318 return ++fmt - start;
2319
2320 case 's':
2321 spec->type = FORMAT_TYPE_STR;
2322 return ++fmt - start;
2323
2324 case 'p':
2325 spec->type = FORMAT_TYPE_PTR;
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002326 return ++fmt - start;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002327
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002328 case '%':
2329 spec->type = FORMAT_TYPE_PERCENT_CHAR;
2330 return ++fmt - start;
2331
2332 /* integer number formats - set up the flags and "break" */
2333 case 'o':
2334 spec->base = 8;
2335 break;
2336
2337 case 'x':
2338 spec->flags |= SMALL;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02002339 /* fall through */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002340
2341 case 'X':
2342 spec->base = 16;
2343 break;
2344
2345 case 'd':
2346 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002347 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002348 case 'u':
2349 break;
2350
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002351 case 'n':
2352 /*
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002353 * Since %n poses a greater security risk than
2354 * utility, treat it as any other invalid or
2355 * unsupported format specifier.
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002356 */
Ryan Mallon708d96fd2014-04-03 14:48:37 -07002357 /* Fall-through */
2358
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002359 default:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002360 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002361 spec->type = FORMAT_TYPE_INVALID;
2362 return fmt - start;
2363 }
2364
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002365 if (qualifier == 'L')
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002366 spec->type = FORMAT_TYPE_LONG_LONG;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002367 else if (qualifier == 'l') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002368 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
2369 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002370 } else if (qualifier == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002371 spec->type = FORMAT_TYPE_SIZE_T;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002372 } else if (qualifier == 't') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002373 spec->type = FORMAT_TYPE_PTRDIFF;
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002374 } else if (qualifier == 'H') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002375 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
2376 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
Rasmus Villemoesd0484192016-01-15 16:58:37 -08002377 } else if (qualifier == 'h') {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002378 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
2379 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002380 } else {
Rasmus Villemoes51be17d2015-04-15 16:17:02 -07002381 BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
2382 spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002383 }
2384
2385 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07002386}
2387
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002388static void
2389set_field_width(struct printf_spec *spec, int width)
2390{
2391 spec->field_width = width;
2392 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
2393 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
2394 }
2395}
2396
2397static void
2398set_precision(struct printf_spec *spec, int prec)
2399{
2400 spec->precision = prec;
2401 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
2402 spec->precision = clamp(prec, 0, PRECISION_MAX);
2403 }
2404}
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406/**
2407 * vsnprintf - Format a string and place it in a buffer
2408 * @buf: The buffer to place the result into
2409 * @size: The size of the buffer, including the trailing null space
2410 * @fmt: The format string to use
2411 * @args: Arguments for the format string
2412 *
Rasmus Villemoesd7ec9a02015-11-06 16:30:35 -08002413 * This function generally follows C99 vsnprintf, but has some
2414 * extensions and a few limitations:
2415 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002416 * - ``%n`` is unsupported
2417 * - ``%p*`` is handled by pointer()
Andi Kleen20036fd2008-10-15 22:02:02 -07002418 *
Jonathan Corbet27e7c0e2017-12-21 12:39:45 -07002419 * See pointer() or Documentation/core-api/printk-formats.rst for more
Martin Kletzander5e4ee7b2015-11-06 16:30:17 -08002420 * extensive description.
2421 *
mchehab@s-opensource.com6cc89132017-03-30 17:11:33 -03002422 * **Please update the documentation in both places when making changes**
Andrew Morton80f548e2012-07-30 14:40:25 -07002423 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 * The return value is the number of characters which would
2425 * be generated for the given input, excluding the trailing
2426 * '\0', as per ISO C99. If you want to have the exact
2427 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002428 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 * return is greater than or equal to @size, the resulting
2430 * string is truncated.
2431 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002432 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 */
2434int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
2435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002437 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002438 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002440 /* Reject out-of-range values early. Large positive sizes are
2441 used for unknown buffer sizes. */
Rasmus Villemoes2aa2f9e2015-02-12 15:01:39 -08002442 if (WARN_ON_ONCE(size > INT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002446 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002448 /* Make sure end is always >= buf */
2449 if (end < buf) {
2450 end = ((void *)-1);
2451 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 }
2453
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002454 while (*fmt) {
2455 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002456 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002457
2458 fmt += read;
2459
2460 switch (spec.type) {
2461 case FORMAT_TYPE_NONE: {
2462 int copy = read;
2463 if (str < end) {
2464 if (copy > end - str)
2465 copy = end - str;
2466 memcpy(str, old_fmt, copy);
2467 }
2468 str += read;
2469 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471
Vegard Nossumed681a92009-03-14 12:08:50 +01002472 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002473 set_field_width(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002476 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002477 set_precision(&spec, va_arg(args, int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
André Goddard Rosad4be1512009-12-14 18:00:59 -08002480 case FORMAT_TYPE_CHAR: {
2481 char c;
2482
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002483 if (!(spec.flags & LEFT)) {
2484 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002485 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 *str = ' ';
2487 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002490 }
2491 c = (unsigned char) va_arg(args, int);
2492 if (str < end)
2493 *str = c;
2494 ++str;
2495 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002496 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002497 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002499 }
2500 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002503 case FORMAT_TYPE_STR:
2504 str = string(str, end, va_arg(args, char *), spec);
2505 break;
2506
2507 case FORMAT_TYPE_PTR:
Rasmus Villemoesffbfed02015-02-12 15:01:37 -08002508 str = pointer(fmt, str, end, va_arg(args, void *),
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002509 spec);
2510 while (isalnum(*fmt))
2511 fmt++;
2512 break;
2513
2514 case FORMAT_TYPE_PERCENT_CHAR:
2515 if (str < end)
2516 *str = '%';
2517 ++str;
2518 break;
2519
2520 case FORMAT_TYPE_INVALID:
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002521 /*
2522 * Presumably the arguments passed gcc's type
2523 * checking, but there is no safe or sane way
2524 * for us to continue parsing the format and
2525 * fetching from the va_list; the remaining
2526 * specifiers and arguments would be out of
2527 * sync.
2528 */
2529 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002530
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002531 default:
2532 switch (spec.type) {
2533 case FORMAT_TYPE_LONG_LONG:
2534 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002536 case FORMAT_TYPE_ULONG:
2537 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002539 case FORMAT_TYPE_LONG:
2540 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002542 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08002543 if (spec.flags & SIGN)
2544 num = va_arg(args, ssize_t);
2545 else
2546 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002547 break;
2548 case FORMAT_TYPE_PTRDIFF:
2549 num = va_arg(args, ptrdiff_t);
2550 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002551 case FORMAT_TYPE_UBYTE:
2552 num = (unsigned char) va_arg(args, int);
2553 break;
2554 case FORMAT_TYPE_BYTE:
2555 num = (signed char) va_arg(args, int);
2556 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002557 case FORMAT_TYPE_USHORT:
2558 num = (unsigned short) va_arg(args, int);
2559 break;
2560 case FORMAT_TYPE_SHORT:
2561 num = (short) va_arg(args, int);
2562 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01002563 case FORMAT_TYPE_INT:
2564 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002567 num = va_arg(args, unsigned int);
2568 }
2569
2570 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002573
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002574out:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002575 if (size > 0) {
2576 if (str < end)
2577 *str = '\0';
2578 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07002579 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002580 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002581
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07002582 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586EXPORT_SYMBOL(vsnprintf);
2587
2588/**
2589 * vscnprintf - Format a string and place it in a buffer
2590 * @buf: The buffer to place the result into
2591 * @size: The size of the buffer, including the trailing null space
2592 * @fmt: The format string to use
2593 * @args: Arguments for the format string
2594 *
2595 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08002596 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 * returns 0.
2598 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002599 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002600 *
2601 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 */
2603int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
2604{
2605 int i;
2606
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002607 i = vsnprintf(buf, size, fmt, args);
2608
Anton Arapovb921c692011-01-12 16:59:49 -08002609 if (likely(i < size))
2610 return i;
2611 if (size != 0)
2612 return size - 1;
2613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615EXPORT_SYMBOL(vscnprintf);
2616
2617/**
2618 * snprintf - Format a string and place it in a buffer
2619 * @buf: The buffer to place the result into
2620 * @size: The size of the buffer, including the trailing null space
2621 * @fmt: The format string to use
2622 * @...: Arguments for the format string
2623 *
2624 * The return value is the number of characters which would be
2625 * generated for the given input, excluding the trailing null,
2626 * as per ISO C99. If the return is greater than or equal to
2627 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07002628 *
2629 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002631int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
2633 va_list args;
2634 int i;
2635
2636 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002637 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return i;
2641}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642EXPORT_SYMBOL(snprintf);
2643
2644/**
2645 * scnprintf - Format a string and place it in a buffer
2646 * @buf: The buffer to place the result into
2647 * @size: The size of the buffer, including the trailing null space
2648 * @fmt: The format string to use
2649 * @...: Arguments for the format string
2650 *
2651 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07002652 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 */
2654
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002655int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 va_list args;
2658 int i;
2659
2660 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08002661 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002663
Anton Arapovb921c692011-01-12 16:59:49 -08002664 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666EXPORT_SYMBOL(scnprintf);
2667
2668/**
2669 * vsprintf - Format a string and place it in a buffer
2670 * @buf: The buffer to place the result into
2671 * @fmt: The format string to use
2672 * @args: Arguments for the format string
2673 *
2674 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002675 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 * buffer overflows.
2677 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07002678 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07002679 *
2680 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 */
2682int vsprintf(char *buf, const char *fmt, va_list args)
2683{
2684 return vsnprintf(buf, INT_MAX, fmt, args);
2685}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686EXPORT_SYMBOL(vsprintf);
2687
2688/**
2689 * sprintf - Format a string and place it in a buffer
2690 * @buf: The buffer to place the result into
2691 * @fmt: The format string to use
2692 * @...: Arguments for the format string
2693 *
2694 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002695 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07002697 *
2698 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002700int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
2702 va_list args;
2703 int i;
2704
2705 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002706 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 return i;
2710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711EXPORT_SYMBOL(sprintf);
2712
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002713#ifdef CONFIG_BINARY_PRINTF
2714/*
2715 * bprintf service:
2716 * vbin_printf() - VA arguments to binary data
2717 * bstr_printf() - Binary data to text string
2718 */
2719
2720/**
2721 * vbin_printf - Parse a format string and place args' binary value in a buffer
2722 * @bin_buf: The buffer to place args' binary value
2723 * @size: The size of the buffer(by words(32bits), not characters)
2724 * @fmt: The format string to use
2725 * @args: Arguments for the format string
2726 *
2727 * The format follows C99 vsnprintf, except %n is ignored, and its argument
Masanari Iidada3dae52014-09-09 01:27:23 +09002728 * is skipped.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002729 *
2730 * The return value is the number of words(32bits) which would be generated for
2731 * the given input.
2732 *
2733 * NOTE:
2734 * If the return value is greater than @size, the resulting bin_buf is NOT
2735 * valid for bstr_printf().
2736 */
2737int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
2738{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002739 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002740 char *str, *end;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002741 int width;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002742
2743 str = (char *)bin_buf;
2744 end = (char *)(bin_buf + size);
2745
2746#define save_arg(type) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002747({ \
2748 unsigned long long value; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002749 if (sizeof(type) == 8) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002750 unsigned long long val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002751 str = PTR_ALIGN(str, sizeof(u32)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002752 val8 = va_arg(args, unsigned long long); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002753 if (str + sizeof(type) <= end) { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002754 *(u32 *)str = *(u32 *)&val8; \
2755 *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002756 } \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002757 value = val8; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002758 } else { \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002759 unsigned int val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002760 str = PTR_ALIGN(str, sizeof(type)); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002761 val4 = va_arg(args, int); \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002762 if (str + sizeof(type) <= end) \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002763 *(typeof(type) *)str = (type)(long)val4; \
2764 value = (unsigned long long)val4; \
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002765 } \
2766 str += sizeof(type); \
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002767 value; \
2768})
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002769
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002770 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08002771 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002772
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002773 fmt += read;
2774
2775 switch (spec.type) {
2776 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08002777 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002778 break;
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002779 case FORMAT_TYPE_INVALID:
2780 goto out;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002781
Vegard Nossumed681a92009-03-14 12:08:50 +01002782 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002783 case FORMAT_TYPE_PRECISION:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002784 width = (int)save_arg(int);
2785 /* Pointers may require the width */
2786 if (*fmt == 'p')
2787 set_field_width(&spec, width);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002788 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002789
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002790 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002791 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002792 break;
2793
2794 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002795 const char *save_str = va_arg(args, char *);
Petr Mladek3e5903e2019-04-17 13:53:48 +02002796 const char *err_msg;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002797 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08002798
Petr Mladek3e5903e2019-04-17 13:53:48 +02002799 err_msg = check_pointer_msg(save_str);
2800 if (err_msg)
2801 save_str = err_msg;
2802
André Goddard Rosa6c356632009-12-14 18:00:56 -08002803 len = strlen(save_str) + 1;
2804 if (str + len < end)
2805 memcpy(str, save_str, len);
2806 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002807 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002808 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002809
2810 case FORMAT_TYPE_PTR:
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002811 /* Dereferenced pointers must be done now */
2812 switch (*fmt) {
2813 /* Dereference of functions is still OK */
2814 case 'S':
2815 case 's':
2816 case 'F':
2817 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002818 case 'x':
2819 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002820 save_arg(void *);
2821 break;
2822 default:
2823 if (!isalnum(*fmt)) {
2824 save_arg(void *);
2825 break;
2826 }
2827 str = pointer(fmt, str, end, va_arg(args, void *),
2828 spec);
2829 if (str + 1 < end)
2830 *str++ = '\0';
2831 else
2832 end[-1] = '\0'; /* Must be nul terminated */
2833 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002834 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002835 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002836 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002837 break;
2838
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002839 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002840 switch (spec.type) {
2841
2842 case FORMAT_TYPE_LONG_LONG:
2843 save_arg(long long);
2844 break;
2845 case FORMAT_TYPE_ULONG:
2846 case FORMAT_TYPE_LONG:
2847 save_arg(unsigned long);
2848 break;
2849 case FORMAT_TYPE_SIZE_T:
2850 save_arg(size_t);
2851 break;
2852 case FORMAT_TYPE_PTRDIFF:
2853 save_arg(ptrdiff_t);
2854 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08002855 case FORMAT_TYPE_UBYTE:
2856 case FORMAT_TYPE_BYTE:
2857 save_arg(char);
2858 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002859 case FORMAT_TYPE_USHORT:
2860 case FORMAT_TYPE_SHORT:
2861 save_arg(short);
2862 break;
2863 default:
2864 save_arg(int);
2865 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002866 }
2867 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002868
Rasmus Villemoesb006f192015-11-06 16:30:20 -08002869out:
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002870 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002871#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002872}
2873EXPORT_SYMBOL_GPL(vbin_printf);
2874
2875/**
2876 * bstr_printf - Format a string from binary arguments and place it in a buffer
2877 * @buf: The buffer to place the result into
2878 * @size: The size of the buffer, including the trailing null space
2879 * @fmt: The format string to use
2880 * @bin_buf: Binary arguments for the format string
2881 *
2882 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2883 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2884 * a binary buffer that generated by vbin_printf.
2885 *
2886 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04002887 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002888 *
2889 * The return value is the number of characters which would
2890 * be generated for the given input, excluding the trailing
2891 * '\0', as per ISO C99. If you want to have the exact
2892 * number of characters written into @buf as return value
2893 * (not including the trailing '\0'), use vscnprintf(). If the
2894 * return is greater than or equal to @size, the resulting
2895 * string is truncated.
2896 */
2897int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2898{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002899 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08002900 char *str, *end;
2901 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002902
Rasmus Villemoes762abb52015-11-06 16:30:23 -08002903 if (WARN_ON_ONCE(size > INT_MAX))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002904 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002905
2906 str = buf;
2907 end = buf + size;
2908
2909#define get_arg(type) \
2910({ \
2911 typeof(type) value; \
2912 if (sizeof(type) == 8) { \
2913 args = PTR_ALIGN(args, sizeof(u32)); \
2914 *(u32 *)&value = *(u32 *)args; \
2915 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2916 } else { \
2917 args = PTR_ALIGN(args, sizeof(type)); \
2918 value = *(typeof(type) *)args; \
2919 } \
2920 args += sizeof(type); \
2921 value; \
2922})
2923
2924 /* Make sure end is always >= buf */
2925 if (end < buf) {
2926 end = ((void *)-1);
2927 size = end - buf;
2928 }
2929
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002930 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002931 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002932 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002933
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002934 fmt += read;
2935
2936 switch (spec.type) {
2937 case FORMAT_TYPE_NONE: {
2938 int copy = read;
2939 if (str < end) {
2940 if (copy > end - str)
2941 copy = end - str;
2942 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002943 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002944 str += read;
2945 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002946 }
2947
Vegard Nossumed681a92009-03-14 12:08:50 +01002948 case FORMAT_TYPE_WIDTH:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002949 set_field_width(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002950 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002951
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002952 case FORMAT_TYPE_PRECISION:
Rasmus Villemoes4d72ba02016-01-15 16:58:44 -08002953 set_precision(&spec, get_arg(int));
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002954 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002955
André Goddard Rosad4be1512009-12-14 18:00:59 -08002956 case FORMAT_TYPE_CHAR: {
2957 char c;
2958
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002959 if (!(spec.flags & LEFT)) {
2960 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002961 if (str < end)
2962 *str = ' ';
2963 ++str;
2964 }
2965 }
2966 c = (unsigned char) get_arg(char);
2967 if (str < end)
2968 *str = c;
2969 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002970 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002971 if (str < end)
2972 *str = ' ';
2973 ++str;
2974 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002975 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002976 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002977
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002978 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002979 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002980 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01002981 str = string(str, end, (char *)str_arg, spec);
2982 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002983 }
2984
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002985 case FORMAT_TYPE_PTR: {
2986 bool process = false;
2987 int copy, len;
2988 /* Non function dereferences were already done */
2989 switch (*fmt) {
2990 case 'S':
2991 case 's':
2992 case 'F':
2993 case 'f':
Steven Rostedt (VMware)1e6338c2018-04-03 14:38:53 -04002994 case 'x':
2995 case 'K':
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05002996 process = true;
2997 break;
2998 default:
2999 if (!isalnum(*fmt)) {
3000 process = true;
3001 break;
3002 }
3003 /* Pointer dereference was already processed */
3004 if (str < end) {
3005 len = copy = strlen(args);
3006 if (copy > end - str)
3007 copy = end - str;
3008 memcpy(str, args, copy);
3009 str += len;
Steven Rostedt (VMware)62165602018-10-05 10:08:03 -04003010 args += len + 1;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003011 }
3012 }
3013 if (process)
3014 str = pointer(fmt, str, end, get_arg(void *), spec);
3015
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003016 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003017 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003018 break;
Steven Rostedt (VMware)841a9152017-12-28 20:40:25 -05003019 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003020
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003021 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003022 if (str < end)
3023 *str = '%';
3024 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003025 break;
3026
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003027 case FORMAT_TYPE_INVALID:
3028 goto out;
3029
André Goddard Rosad4be1512009-12-14 18:00:59 -08003030 default: {
3031 unsigned long long num;
3032
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003033 switch (spec.type) {
3034
3035 case FORMAT_TYPE_LONG_LONG:
3036 num = get_arg(long long);
3037 break;
3038 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003039 case FORMAT_TYPE_LONG:
3040 num = get_arg(unsigned long);
3041 break;
3042 case FORMAT_TYPE_SIZE_T:
3043 num = get_arg(size_t);
3044 break;
3045 case FORMAT_TYPE_PTRDIFF:
3046 num = get_arg(ptrdiff_t);
3047 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08003048 case FORMAT_TYPE_UBYTE:
3049 num = get_arg(unsigned char);
3050 break;
3051 case FORMAT_TYPE_BYTE:
3052 num = get_arg(signed char);
3053 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003054 case FORMAT_TYPE_USHORT:
3055 num = get_arg(unsigned short);
3056 break;
3057 case FORMAT_TYPE_SHORT:
3058 num = get_arg(short);
3059 break;
3060 case FORMAT_TYPE_UINT:
3061 num = get_arg(unsigned int);
3062 break;
3063 default:
3064 num = get_arg(int);
3065 }
3066
3067 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08003068 } /* default: */
3069 } /* switch(spec.type) */
3070 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003071
Rasmus Villemoesb006f192015-11-06 16:30:20 -08003072out:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003073 if (size > 0) {
3074 if (str < end)
3075 *str = '\0';
3076 else
3077 end[-1] = '\0';
3078 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01003079
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003080#undef get_arg
3081
3082 /* the trailing null byte doesn't count towards the total */
3083 return str - buf;
3084}
3085EXPORT_SYMBOL_GPL(bstr_printf);
3086
3087/**
3088 * bprintf - Parse a format string and place args' binary value in a buffer
3089 * @bin_buf: The buffer to place args' binary value
3090 * @size: The size of the buffer(by words(32bits), not characters)
3091 * @fmt: The format string to use
3092 * @...: Arguments for the format string
3093 *
3094 * The function returns the number of words(u32) written
3095 * into @bin_buf.
3096 */
3097int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
3098{
3099 va_list args;
3100 int ret;
3101
3102 va_start(args, fmt);
3103 ret = vbin_printf(bin_buf, size, fmt, args);
3104 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003105
Lai Jiangshan4370aa42009-03-06 17:21:46 +01003106 return ret;
3107}
3108EXPORT_SYMBOL_GPL(bprintf);
3109
3110#endif /* CONFIG_BINARY_PRINTF */
3111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112/**
3113 * vsscanf - Unformat a buffer into a list of arguments
3114 * @buf: input buffer
3115 * @fmt: format of buffer
3116 * @args: arguments
3117 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003118int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 const char *str = buf;
3121 char *next;
3122 char digit;
3123 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08003124 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08003125 unsigned int base;
3126 union {
3127 long long s;
3128 unsigned long long u;
3129 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08003130 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003131 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Jan Beulichda990752012-10-04 17:13:24 -07003133 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 /* skip any white space in format */
3135 /* white space in format matchs any amount of
3136 * white space, including none, in the input.
3137 */
3138 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08003139 fmt = skip_spaces(++fmt);
3140 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
3142
3143 /* anything that is not a conversion must match exactly */
3144 if (*fmt != '%' && *fmt) {
3145 if (*fmt++ != *str++)
3146 break;
3147 continue;
3148 }
3149
3150 if (!*fmt)
3151 break;
3152 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 /* skip this conversion.
3155 * advance both strings to next white space
3156 */
3157 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07003158 if (!*str)
3159 break;
Jessica Yuf9310b22016-03-17 14:23:07 -07003160 while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3161 /* '%*[' not yet supported, invalid format */
3162 if (*fmt == '[')
3163 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 fmt++;
Jessica Yuf9310b22016-03-17 14:23:07 -07003165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 while (!isspace(*str) && *str)
3167 str++;
3168 continue;
3169 }
3170
3171 /* get field width */
3172 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08003173 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08003175 if (field_width <= 0)
3176 break;
3177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 /* get conversion qualifier */
3180 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07003181 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003182 *fmt == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 qualifier = *fmt++;
3184 if (unlikely(qualifier == *fmt)) {
3185 if (qualifier == 'h') {
3186 qualifier = 'H';
3187 fmt++;
3188 } else if (qualifier == 'l') {
3189 qualifier = 'L';
3190 fmt++;
3191 }
3192 }
3193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Jan Beulichda990752012-10-04 17:13:24 -07003195 if (!*fmt)
3196 break;
3197
3198 if (*fmt == 'n') {
3199 /* return number of characters read so far */
3200 *va_arg(args, int *) = str - buf;
3201 ++fmt;
3202 continue;
3203 }
3204
3205 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 break;
3207
André Goddard Rosad4be1512009-12-14 18:00:59 -08003208 base = 10;
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003209 is_sign = false;
André Goddard Rosad4be1512009-12-14 18:00:59 -08003210
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003211 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 case 'c':
3213 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003214 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (field_width == -1)
3216 field_width = 1;
3217 do {
3218 *s++ = *str++;
3219 } while (--field_width > 0 && *str);
3220 num++;
3221 }
3222 continue;
3223 case 's':
3224 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003225 char *s = (char *)va_arg(args, char *);
3226 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003227 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003229 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003232 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 *s = '\0';
3235 num++;
3236 }
3237 continue;
Jessica Yuf9310b22016-03-17 14:23:07 -07003238 /*
3239 * Warning: This implementation of the '[' conversion specifier
3240 * deviates from its glibc counterpart in the following ways:
3241 * (1) It does NOT support ranges i.e. '-' is NOT a special
3242 * character
3243 * (2) It cannot match the closing bracket ']' itself
3244 * (3) A field width is required
3245 * (4) '%*[' (discard matching input) is currently not supported
3246 *
3247 * Example usage:
3248 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3249 * buf1, buf2, buf3);
3250 * if (ret < 3)
3251 * // etc..
3252 */
3253 case '[':
3254 {
3255 char *s = (char *)va_arg(args, char *);
3256 DECLARE_BITMAP(set, 256) = {0};
3257 unsigned int len = 0;
3258 bool negate = (*fmt == '^');
3259
3260 /* field width is required */
3261 if (field_width == -1)
3262 return num;
3263
3264 if (negate)
3265 ++fmt;
3266
3267 for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3268 set_bit((u8)*fmt, set);
3269
3270 /* no ']' or no character set found */
3271 if (!*fmt || !len)
3272 return num;
3273 ++fmt;
3274
3275 if (negate) {
3276 bitmap_complement(set, set, 256);
3277 /* exclude null '\0' byte */
3278 clear_bit(0, set);
3279 }
3280
3281 /* match must be non-empty */
3282 if (!test_bit((u8)*str, set))
3283 return num;
3284
3285 while (test_bit((u8)*str, set) && field_width--)
3286 *s++ = *str++;
3287 *s = '\0';
3288 ++num;
3289 }
3290 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 case 'o':
3292 base = 8;
3293 break;
3294 case 'x':
3295 case 'X':
3296 base = 16;
3297 break;
3298 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003299 base = 0;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003300 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 case 'd':
Fabian Frederick3f623eb2014-06-04 16:11:52 -07003302 is_sign = true;
Andy Shevchenko7e6bd6f2018-02-16 23:07:11 +02003303 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 case 'u':
3305 break;
3306 case '%':
3307 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003308 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return num;
3310 continue;
3311 default:
3312 /* invalid format; stop here */
3313 return num;
3314 }
3315
3316 /* have some sort of integer conversion.
3317 * first, skip white space in buffer.
3318 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08003319 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 digit = *str;
3322 if (is_sign && digit == '-')
3323 digit = *(str + 1);
3324
3325 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003326 || (base == 16 && !isxdigit(digit))
3327 || (base == 10 && !isdigit(digit))
3328 || (base == 8 && (!isdigit(digit) || digit > '7'))
3329 || (base == 0 && !isdigit(digit)))
3330 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Jan Beulich53809752012-12-17 16:01:31 -08003332 if (is_sign)
3333 val.s = qualifier != 'L' ?
3334 simple_strtol(str, &next, base) :
3335 simple_strtoll(str, &next, base);
3336 else
3337 val.u = qualifier != 'L' ?
3338 simple_strtoul(str, &next, base) :
3339 simple_strtoull(str, &next, base);
3340
3341 if (field_width > 0 && next - str > field_width) {
3342 if (base == 0)
3343 _parse_integer_fixup_radix(str, &base);
3344 while (next - str > field_width) {
3345 if (is_sign)
3346 val.s = div_s64(val.s, base);
3347 else
3348 val.u = div_u64(val.u, base);
3349 --next;
3350 }
3351 }
3352
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003353 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08003355 if (is_sign)
3356 *va_arg(args, signed char *) = val.s;
3357 else
3358 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 break;
3360 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08003361 if (is_sign)
3362 *va_arg(args, short *) = val.s;
3363 else
3364 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 break;
3366 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08003367 if (is_sign)
3368 *va_arg(args, long *) = val.s;
3369 else
3370 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 break;
3372 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08003373 if (is_sign)
3374 *va_arg(args, long long *) = val.s;
3375 else
3376 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08003379 *va_arg(args, size_t *) = val.u;
3380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 default:
Jan Beulich53809752012-12-17 16:01:31 -08003382 if (is_sign)
3383 *va_arg(args, int *) = val.s;
3384 else
3385 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 break;
3387 }
3388 num++;
3389
3390 if (!next)
3391 break;
3392 str = next;
3393 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return num;
3396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397EXPORT_SYMBOL(vsscanf);
3398
3399/**
3400 * sscanf - Unformat a buffer into a list of arguments
3401 * @buf: input buffer
3402 * @fmt: formatting of buffer
3403 * @...: resulting arguments
3404 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003405int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 va_list args;
3408 int i;
3409
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003410 va_start(args, fmt);
3411 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08003413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return i;
3415}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416EXPORT_SYMBOL(sscanf);