Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/lib/vsprintf.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | */ | |
7 | ||
8 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ | |
9 | /* | |
10 | * Wirzenius wrote this portably, Torvalds fucked it up :-) | |
11 | */ | |
12 | ||
7b9186f5 | 13 | /* |
1da177e4 LT |
14 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
15 | * - changed to provide snprintf and vsnprintf functions | |
16 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> | |
17 | * - scnprintf and vscnprintf | |
18 | */ | |
19 | ||
c0891ac1 | 20 | #include <linux/stdarg.h> |
ef27ac18 | 21 | #include <linux/build_bug.h> |
0d1d7a55 | 22 | #include <linux/clk.h> |
900cca29 | 23 | #include <linux/clk-provider.h> |
57f5677e | 24 | #include <linux/errname.h> |
8bc3bcc9 | 25 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ |
1da177e4 LT |
26 | #include <linux/types.h> |
27 | #include <linux/string.h> | |
28 | #include <linux/ctype.h> | |
29 | #include <linux/kernel.h> | |
0fe1ef24 | 30 | #include <linux/kallsyms.h> |
53809751 | 31 | #include <linux/math64.h> |
0fe1ef24 | 32 | #include <linux/uaccess.h> |
332d2e78 | 33 | #include <linux/ioport.h> |
4b6ccca7 | 34 | #include <linux/dcache.h> |
312b4e22 | 35 | #include <linux/cred.h> |
4d42c447 | 36 | #include <linux/rtc.h> |
7daac5b2 | 37 | #include <linux/time.h> |
2b1b0d66 | 38 | #include <linux/uuid.h> |
ce4fecf1 | 39 | #include <linux/of.h> |
8a27f7c9 | 40 | #include <net/addrconf.h> |
ad67b74d TH |
41 | #include <linux/siphash.h> |
42 | #include <linux/compiler.h> | |
a92eb762 | 43 | #include <linux/property.h> |
1031bc58 DM |
44 | #ifdef CONFIG_BLOCK |
45 | #include <linux/blkdev.h> | |
46 | #endif | |
1da177e4 | 47 | |
edf14cdb VB |
48 | #include "../mm/internal.h" /* For the trace_print_flags arrays */ |
49 | ||
4e57b681 | 50 | #include <asm/page.h> /* for PAGE_SIZE */ |
7c43d9a3 | 51 | #include <asm/byteorder.h> /* cpu_to_le16 */ |
d75b26f8 | 52 | #include <asm/unaligned.h> |
1da177e4 | 53 | |
71dca95d | 54 | #include <linux/string_helpers.h> |
1dff46d6 | 55 | #include "kstrtox.h" |
aa46a63e | 56 | |
84842911 CL |
57 | /* Disable pointer hashing if requested */ |
58 | bool no_hash_pointers __ro_after_init; | |
59 | EXPORT_SYMBOL_GPL(no_hash_pointers); | |
60 | ||
839b395e | 61 | static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base) |
900fdc45 RF |
62 | { |
63 | const char *cp; | |
64 | unsigned long long result = 0ULL; | |
65 | size_t prefix_chars; | |
66 | unsigned int rv; | |
67 | ||
68 | cp = _parse_integer_fixup_radix(startp, &base); | |
69 | prefix_chars = cp - startp; | |
70 | if (prefix_chars < max_chars) { | |
71 | rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars); | |
72 | /* FIXME */ | |
73 | cp += (rv & ~KSTRTOX_OVERFLOW); | |
74 | } else { | |
75 | /* Field too short for prefix + digit, skip over without converting */ | |
76 | cp = startp + max_chars; | |
77 | } | |
78 | ||
79 | if (endp) | |
80 | *endp = (char *)cp; | |
81 | ||
82 | return result; | |
83 | } | |
84 | ||
1da177e4 | 85 | /** |
922ac25c | 86 | * simple_strtoull - convert a string to an unsigned long long |
1da177e4 LT |
87 | * @cp: The start of the string |
88 | * @endp: A pointer to the end of the parsed string will be placed here | |
89 | * @base: The number base to use | |
462e4711 | 90 | * |
e8cc2b97 | 91 | * This function has caveats. Please use kstrtoull instead. |
1da177e4 | 92 | */ |
ad65dcef | 93 | noinline |
922ac25c | 94 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
1da177e4 | 95 | { |
900fdc45 | 96 | return simple_strntoull(cp, INT_MAX, endp, base); |
1da177e4 | 97 | } |
922ac25c | 98 | EXPORT_SYMBOL(simple_strtoull); |
1da177e4 LT |
99 | |
100 | /** | |
922ac25c | 101 | * simple_strtoul - convert a string to an unsigned long |
1da177e4 LT |
102 | * @cp: The start of the string |
103 | * @endp: A pointer to the end of the parsed string will be placed here | |
104 | * @base: The number base to use | |
462e4711 | 105 | * |
e8cc2b97 | 106 | * This function has caveats. Please use kstrtoul instead. |
1da177e4 | 107 | */ |
922ac25c | 108 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
1da177e4 | 109 | { |
922ac25c | 110 | return simple_strtoull(cp, endp, base); |
1da177e4 | 111 | } |
922ac25c | 112 | EXPORT_SYMBOL(simple_strtoul); |
1da177e4 LT |
113 | |
114 | /** | |
922ac25c | 115 | * simple_strtol - convert a string to a signed long |
1da177e4 LT |
116 | * @cp: The start of the string |
117 | * @endp: A pointer to the end of the parsed string will be placed here | |
118 | * @base: The number base to use | |
462e4711 | 119 | * |
e8cc2b97 | 120 | * This function has caveats. Please use kstrtol instead. |
1da177e4 | 121 | */ |
922ac25c | 122 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
1da177e4 | 123 | { |
922ac25c AGR |
124 | if (*cp == '-') |
125 | return -simple_strtoul(cp + 1, endp, base); | |
7b9186f5 | 126 | |
922ac25c | 127 | return simple_strtoul(cp, endp, base); |
1da177e4 | 128 | } |
922ac25c | 129 | EXPORT_SYMBOL(simple_strtol); |
1da177e4 | 130 | |
900fdc45 RF |
131 | static long long simple_strntoll(const char *cp, size_t max_chars, char **endp, |
132 | unsigned int base) | |
133 | { | |
134 | /* | |
135 | * simple_strntoull() safely handles receiving max_chars==0 in the | |
136 | * case cp[0] == '-' && max_chars == 1. | |
137 | * If max_chars == 0 we can drop through and pass it to simple_strntoull() | |
138 | * and the content of *cp is irrelevant. | |
139 | */ | |
140 | if (*cp == '-' && max_chars > 0) | |
141 | return -simple_strntoull(cp + 1, max_chars - 1, endp, base); | |
142 | ||
143 | return simple_strntoull(cp, max_chars, endp, base); | |
144 | } | |
145 | ||
1da177e4 LT |
146 | /** |
147 | * simple_strtoll - convert a string to a signed long long | |
148 | * @cp: The start of the string | |
149 | * @endp: A pointer to the end of the parsed string will be placed here | |
150 | * @base: The number base to use | |
462e4711 | 151 | * |
e8cc2b97 | 152 | * This function has caveats. Please use kstrtoll instead. |
1da177e4 | 153 | */ |
22d27051 | 154 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
1da177e4 | 155 | { |
900fdc45 | 156 | return simple_strntoll(cp, INT_MAX, endp, base); |
1da177e4 | 157 | } |
98d5ce0d | 158 | EXPORT_SYMBOL(simple_strtoll); |
1da177e4 | 159 | |
cf3b429b JP |
160 | static noinline_for_stack |
161 | int skip_atoi(const char **s) | |
1da177e4 | 162 | { |
7b9186f5 | 163 | int i = 0; |
1da177e4 | 164 | |
43e5b666 | 165 | do { |
1da177e4 | 166 | i = i*10 + *((*s)++) - '0'; |
43e5b666 | 167 | } while (isdigit(**s)); |
7b9186f5 | 168 | |
1da177e4 LT |
169 | return i; |
170 | } | |
171 | ||
7c43d9a3 RV |
172 | /* |
173 | * Decimal conversion is by far the most typical, and is used for | |
174 | * /proc and /sys data. This directly impacts e.g. top performance | |
175 | * with many processes running. We optimize it for speed by emitting | |
176 | * two characters at a time, using a 200 byte lookup table. This | |
177 | * roughly halves the number of multiplications compared to computing | |
178 | * the digits one at a time. Implementation strongly inspired by the | |
179 | * previous version, which in turn used ideas described at | |
180 | * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission | |
181 | * from the author, Douglas W. Jones). | |
182 | * | |
183 | * It turns out there is precisely one 26 bit fixed-point | |
184 | * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 | |
185 | * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual | |
186 | * range happens to be somewhat larger (x <= 1073741898), but that's | |
187 | * irrelevant for our purpose. | |
188 | * | |
189 | * For dividing a number in the range [10^4, 10^6-1] by 100, we still | |
190 | * need a 32x32->64 bit multiply, so we simply use the same constant. | |
191 | * | |
192 | * For dividing a number in the range [100, 10^4-1] by 100, there are | |
193 | * several options. The simplest is (x * 0x147b) >> 19, which is valid | |
194 | * for all x <= 43698. | |
133fd9f5 | 195 | */ |
4277eedd | 196 | |
7c43d9a3 RV |
197 | static const u16 decpair[100] = { |
198 | #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) | |
199 | _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), | |
200 | _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), | |
201 | _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), | |
202 | _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), | |
203 | _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), | |
204 | _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), | |
205 | _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), | |
206 | _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), | |
207 | _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), | |
208 | _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), | |
209 | #undef _ | |
210 | }; | |
211 | ||
212 | /* | |
213 | * This will print a single '0' even if r == 0, since we would | |
675cf53c RV |
214 | * immediately jump to out_r where two 0s would be written but only |
215 | * one of them accounted for in buf. This is needed by ip4_string | |
216 | * below. All other callers pass a non-zero value of r. | |
7c43d9a3 | 217 | */ |
cf3b429b | 218 | static noinline_for_stack |
7c43d9a3 | 219 | char *put_dec_trunc8(char *buf, unsigned r) |
4277eedd | 220 | { |
7c43d9a3 RV |
221 | unsigned q; |
222 | ||
223 | /* 1 <= r < 10^8 */ | |
224 | if (r < 100) | |
225 | goto out_r; | |
226 | ||
227 | /* 100 <= r < 10^8 */ | |
228 | q = (r * (u64)0x28f5c29) >> 32; | |
229 | *((u16 *)buf) = decpair[r - 100*q]; | |
230 | buf += 2; | |
231 | ||
232 | /* 1 <= q < 10^6 */ | |
233 | if (q < 100) | |
234 | goto out_q; | |
235 | ||
236 | /* 100 <= q < 10^6 */ | |
237 | r = (q * (u64)0x28f5c29) >> 32; | |
238 | *((u16 *)buf) = decpair[q - 100*r]; | |
239 | buf += 2; | |
240 | ||
241 | /* 1 <= r < 10^4 */ | |
242 | if (r < 100) | |
243 | goto out_r; | |
244 | ||
245 | /* 100 <= r < 10^4 */ | |
246 | q = (r * 0x147b) >> 19; | |
247 | *((u16 *)buf) = decpair[r - 100*q]; | |
248 | buf += 2; | |
249 | out_q: | |
250 | /* 1 <= q < 100 */ | |
251 | r = q; | |
252 | out_r: | |
253 | /* 1 <= r < 100 */ | |
254 | *((u16 *)buf) = decpair[r]; | |
675cf53c | 255 | buf += r < 10 ? 1 : 2; |
4277eedd DV |
256 | return buf; |
257 | } | |
133fd9f5 | 258 | |
7c43d9a3 | 259 | #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 |
cf3b429b | 260 | static noinline_for_stack |
7c43d9a3 | 261 | char *put_dec_full8(char *buf, unsigned r) |
4277eedd | 262 | { |
133fd9f5 DV |
263 | unsigned q; |
264 | ||
7c43d9a3 RV |
265 | /* 0 <= r < 10^8 */ |
266 | q = (r * (u64)0x28f5c29) >> 32; | |
267 | *((u16 *)buf) = decpair[r - 100*q]; | |
268 | buf += 2; | |
4277eedd | 269 | |
7c43d9a3 RV |
270 | /* 0 <= q < 10^6 */ |
271 | r = (q * (u64)0x28f5c29) >> 32; | |
272 | *((u16 *)buf) = decpair[q - 100*r]; | |
273 | buf += 2; | |
7b9186f5 | 274 | |
7c43d9a3 RV |
275 | /* 0 <= r < 10^4 */ |
276 | q = (r * 0x147b) >> 19; | |
277 | *((u16 *)buf) = decpair[r - 100*q]; | |
278 | buf += 2; | |
133fd9f5 | 279 | |
7c43d9a3 RV |
280 | /* 0 <= q < 100 */ |
281 | *((u16 *)buf) = decpair[q]; | |
282 | buf += 2; | |
283 | return buf; | |
284 | } | |
133fd9f5 | 285 | |
7c43d9a3 | 286 | static noinline_for_stack |
133fd9f5 DV |
287 | char *put_dec(char *buf, unsigned long long n) |
288 | { | |
7c43d9a3 RV |
289 | if (n >= 100*1000*1000) |
290 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); | |
291 | /* 1 <= n <= 1.6e11 */ | |
292 | if (n >= 100*1000*1000) | |
293 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); | |
294 | /* 1 <= n < 1e8 */ | |
133fd9f5 | 295 | return put_dec_trunc8(buf, n); |
4277eedd | 296 | } |
133fd9f5 | 297 | |
7c43d9a3 | 298 | #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 |
133fd9f5 | 299 | |
7c43d9a3 RV |
300 | static void |
301 | put_dec_full4(char *buf, unsigned r) | |
4277eedd | 302 | { |
7c43d9a3 RV |
303 | unsigned q; |
304 | ||
305 | /* 0 <= r < 10^4 */ | |
306 | q = (r * 0x147b) >> 19; | |
307 | *((u16 *)buf) = decpair[r - 100*q]; | |
308 | buf += 2; | |
309 | /* 0 <= q < 100 */ | |
310 | *((u16 *)buf) = decpair[q]; | |
2359172a GS |
311 | } |
312 | ||
313 | /* | |
314 | * Call put_dec_full4 on x % 10000, return x / 10000. | |
315 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 | |
316 | * holds for all x < 1,128,869,999. The largest value this | |
317 | * helper will ever be asked to convert is 1,125,520,955. | |
7c43d9a3 | 318 | * (second call in the put_dec code, assuming n is all-ones). |
2359172a | 319 | */ |
7c43d9a3 | 320 | static noinline_for_stack |
2359172a GS |
321 | unsigned put_dec_helper4(char *buf, unsigned x) |
322 | { | |
323 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; | |
324 | ||
325 | put_dec_full4(buf, x - q * 10000); | |
326 | return q; | |
4277eedd DV |
327 | } |
328 | ||
133fd9f5 DV |
329 | /* Based on code by Douglas W. Jones found at |
330 | * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> | |
331 | * (with permission from the author). | |
332 | * Performs no 64-bit division and hence should be fast on 32-bit machines. | |
333 | */ | |
334 | static | |
335 | char *put_dec(char *buf, unsigned long long n) | |
336 | { | |
337 | uint32_t d3, d2, d1, q, h; | |
338 | ||
339 | if (n < 100*1000*1000) | |
340 | return put_dec_trunc8(buf, n); | |
341 | ||
342 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ | |
343 | h = (n >> 32); | |
344 | d2 = (h ) & 0xffff; | |
345 | d3 = (h >> 16); /* implicit "& 0xffff" */ | |
346 | ||
7c43d9a3 RV |
347 | /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 |
348 | = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ | |
133fd9f5 | 349 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); |
2359172a GS |
350 | q = put_dec_helper4(buf, q); |
351 | ||
352 | q += 7671 * d3 + 9496 * d2 + 6 * d1; | |
353 | q = put_dec_helper4(buf+4, q); | |
354 | ||
355 | q += 4749 * d3 + 42 * d2; | |
356 | q = put_dec_helper4(buf+8, q); | |
133fd9f5 | 357 | |
2359172a GS |
358 | q += 281 * d3; |
359 | buf += 12; | |
360 | if (q) | |
361 | buf = put_dec_trunc8(buf, q); | |
362 | else while (buf[-1] == '0') | |
133fd9f5 DV |
363 | --buf; |
364 | ||
365 | return buf; | |
366 | } | |
367 | ||
368 | #endif | |
369 | ||
1ac101a5 KH |
370 | /* |
371 | * Convert passed number to decimal string. | |
372 | * Returns the length of string. On buffer overflow, returns 0. | |
373 | * | |
374 | * If speed is not important, use snprintf(). It's easy to read the code. | |
375 | */ | |
d1be35cb | 376 | int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) |
1ac101a5 | 377 | { |
7c43d9a3 RV |
378 | /* put_dec requires 2-byte alignment of the buffer. */ |
379 | char tmp[sizeof(num) * 3] __aligned(2); | |
1ac101a5 KH |
380 | int idx, len; |
381 | ||
133fd9f5 DV |
382 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
383 | if (num <= 9) { | |
384 | tmp[0] = '0' + num; | |
385 | len = 1; | |
386 | } else { | |
387 | len = put_dec(tmp, num) - tmp; | |
388 | } | |
1ac101a5 | 389 | |
d1be35cb | 390 | if (len > size || width > size) |
1ac101a5 | 391 | return 0; |
d1be35cb AV |
392 | |
393 | if (width > len) { | |
394 | width = width - len; | |
395 | for (idx = 0; idx < width; idx++) | |
396 | buf[idx] = ' '; | |
397 | } else { | |
398 | width = 0; | |
399 | } | |
400 | ||
1ac101a5 | 401 | for (idx = 0; idx < len; ++idx) |
d1be35cb AV |
402 | buf[idx + width] = tmp[len - idx - 1]; |
403 | ||
404 | return len + width; | |
1ac101a5 KH |
405 | } |
406 | ||
51be17df | 407 | #define SIGN 1 /* unsigned/signed, must be 1 */ |
d1c1b121 | 408 | #define LEFT 2 /* left justified */ |
1da177e4 LT |
409 | #define PLUS 4 /* show plus */ |
410 | #define SPACE 8 /* space if plus */ | |
d1c1b121 | 411 | #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ |
b89dc5d6 BH |
412 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
413 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ | |
1da177e4 | 414 | |
24a1dffb | 415 | static_assert(SIGN == 1); |
b886690d | 416 | static_assert(ZEROPAD == ('0' - ' ')); |
24a1dffb | 417 | static_assert(SMALL == ('a' ^ 'A')); |
b886690d | 418 | |
fef20d9c FW |
419 | enum format_type { |
420 | FORMAT_TYPE_NONE, /* Just a string part */ | |
ed681a91 | 421 | FORMAT_TYPE_WIDTH, |
fef20d9c FW |
422 | FORMAT_TYPE_PRECISION, |
423 | FORMAT_TYPE_CHAR, | |
424 | FORMAT_TYPE_STR, | |
425 | FORMAT_TYPE_PTR, | |
426 | FORMAT_TYPE_PERCENT_CHAR, | |
427 | FORMAT_TYPE_INVALID, | |
428 | FORMAT_TYPE_LONG_LONG, | |
429 | FORMAT_TYPE_ULONG, | |
430 | FORMAT_TYPE_LONG, | |
a4e94ef0 Z |
431 | FORMAT_TYPE_UBYTE, |
432 | FORMAT_TYPE_BYTE, | |
fef20d9c FW |
433 | FORMAT_TYPE_USHORT, |
434 | FORMAT_TYPE_SHORT, | |
435 | FORMAT_TYPE_UINT, | |
436 | FORMAT_TYPE_INT, | |
fef20d9c FW |
437 | FORMAT_TYPE_SIZE_T, |
438 | FORMAT_TYPE_PTRDIFF | |
439 | }; | |
440 | ||
441 | struct printf_spec { | |
d0484193 RV |
442 | unsigned int type:8; /* format_type enum */ |
443 | signed int field_width:24; /* width of output field */ | |
444 | unsigned int flags:8; /* flags to number() */ | |
445 | unsigned int base:8; /* number base, 8, 10 or 16 only */ | |
446 | signed int precision:16; /* # of digits/chars */ | |
447 | } __packed; | |
ef27ac18 RV |
448 | static_assert(sizeof(struct printf_spec) == 8); |
449 | ||
4d72ba01 RV |
450 | #define FIELD_WIDTH_MAX ((1 << 23) - 1) |
451 | #define PRECISION_MAX ((1 << 15) - 1) | |
fef20d9c | 452 | |
cf3b429b JP |
453 | static noinline_for_stack |
454 | char *number(char *buf, char *end, unsigned long long num, | |
455 | struct printf_spec spec) | |
1da177e4 | 456 | { |
7c43d9a3 RV |
457 | /* put_dec requires 2-byte alignment of the buffer. */ |
458 | char tmp[3 * sizeof(num)] __aligned(2); | |
9b706aee DV |
459 | char sign; |
460 | char locase; | |
fef20d9c | 461 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
1da177e4 | 462 | int i; |
7c203422 | 463 | bool is_zero = num == 0LL; |
1c7a8e62 RV |
464 | int field_width = spec.field_width; |
465 | int precision = spec.precision; | |
1da177e4 | 466 | |
9b706aee DV |
467 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
468 | * produces same digits or (maybe lowercased) letters */ | |
fef20d9c FW |
469 | locase = (spec.flags & SMALL); |
470 | if (spec.flags & LEFT) | |
471 | spec.flags &= ~ZEROPAD; | |
1da177e4 | 472 | sign = 0; |
fef20d9c | 473 | if (spec.flags & SIGN) { |
7b9186f5 | 474 | if ((signed long long)num < 0) { |
1da177e4 | 475 | sign = '-'; |
7b9186f5 | 476 | num = -(signed long long)num; |
1c7a8e62 | 477 | field_width--; |
fef20d9c | 478 | } else if (spec.flags & PLUS) { |
1da177e4 | 479 | sign = '+'; |
1c7a8e62 | 480 | field_width--; |
fef20d9c | 481 | } else if (spec.flags & SPACE) { |
1da177e4 | 482 | sign = ' '; |
1c7a8e62 | 483 | field_width--; |
1da177e4 LT |
484 | } |
485 | } | |
b39a7340 | 486 | if (need_pfx) { |
fef20d9c | 487 | if (spec.base == 16) |
1c7a8e62 | 488 | field_width -= 2; |
7c203422 | 489 | else if (!is_zero) |
1c7a8e62 | 490 | field_width--; |
1da177e4 | 491 | } |
b39a7340 DV |
492 | |
493 | /* generate full string in tmp[], in reverse order */ | |
1da177e4 | 494 | i = 0; |
133fd9f5 | 495 | if (num < spec.base) |
3ea8d440 | 496 | tmp[i++] = hex_asc_upper[num] | locase; |
fef20d9c FW |
497 | else if (spec.base != 10) { /* 8 or 16 */ |
498 | int mask = spec.base - 1; | |
b39a7340 | 499 | int shift = 3; |
7b9186f5 AGR |
500 | |
501 | if (spec.base == 16) | |
502 | shift = 4; | |
b39a7340 | 503 | do { |
3ea8d440 | 504 | tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); |
b39a7340 DV |
505 | num >>= shift; |
506 | } while (num); | |
4277eedd DV |
507 | } else { /* base 10 */ |
508 | i = put_dec(tmp, num) - tmp; | |
509 | } | |
b39a7340 DV |
510 | |
511 | /* printing 100 using %2d gives "100", not "00" */ | |
1c7a8e62 RV |
512 | if (i > precision) |
513 | precision = i; | |
b39a7340 | 514 | /* leading space padding */ |
1c7a8e62 | 515 | field_width -= precision; |
51be17df | 516 | if (!(spec.flags & (ZEROPAD | LEFT))) { |
1c7a8e62 | 517 | while (--field_width >= 0) { |
f796937a | 518 | if (buf < end) |
1da177e4 LT |
519 | *buf = ' '; |
520 | ++buf; | |
521 | } | |
522 | } | |
b39a7340 | 523 | /* sign */ |
1da177e4 | 524 | if (sign) { |
f796937a | 525 | if (buf < end) |
1da177e4 LT |
526 | *buf = sign; |
527 | ++buf; | |
528 | } | |
b39a7340 DV |
529 | /* "0x" / "0" prefix */ |
530 | if (need_pfx) { | |
7c203422 PC |
531 | if (spec.base == 16 || !is_zero) { |
532 | if (buf < end) | |
533 | *buf = '0'; | |
534 | ++buf; | |
535 | } | |
fef20d9c | 536 | if (spec.base == 16) { |
f796937a | 537 | if (buf < end) |
9b706aee | 538 | *buf = ('X' | locase); |
1da177e4 LT |
539 | ++buf; |
540 | } | |
541 | } | |
b39a7340 | 542 | /* zero or space padding */ |
fef20d9c | 543 | if (!(spec.flags & LEFT)) { |
d1c1b121 | 544 | char c = ' ' + (spec.flags & ZEROPAD); |
b886690d | 545 | |
1c7a8e62 | 546 | while (--field_width >= 0) { |
f796937a | 547 | if (buf < end) |
1da177e4 LT |
548 | *buf = c; |
549 | ++buf; | |
550 | } | |
551 | } | |
b39a7340 | 552 | /* hmm even more zero padding? */ |
1c7a8e62 | 553 | while (i <= --precision) { |
f796937a | 554 | if (buf < end) |
1da177e4 LT |
555 | *buf = '0'; |
556 | ++buf; | |
557 | } | |
b39a7340 DV |
558 | /* actual digits of result */ |
559 | while (--i >= 0) { | |
f796937a | 560 | if (buf < end) |
1da177e4 LT |
561 | *buf = tmp[i]; |
562 | ++buf; | |
563 | } | |
b39a7340 | 564 | /* trailing space padding */ |
1c7a8e62 | 565 | while (--field_width >= 0) { |
f796937a | 566 | if (buf < end) |
1da177e4 LT |
567 | *buf = ' '; |
568 | ++buf; | |
569 | } | |
7b9186f5 | 570 | |
1da177e4 LT |
571 | return buf; |
572 | } | |
573 | ||
3cab1e71 AS |
574 | static noinline_for_stack |
575 | char *special_hex_number(char *buf, char *end, unsigned long long num, int size) | |
576 | { | |
577 | struct printf_spec spec; | |
578 | ||
579 | spec.type = FORMAT_TYPE_PTR; | |
580 | spec.field_width = 2 + 2 * size; /* 0x + hex */ | |
581 | spec.flags = SPECIAL | SMALL | ZEROPAD; | |
582 | spec.base = 16; | |
583 | spec.precision = -1; | |
584 | ||
585 | return number(buf, end, num, spec); | |
586 | } | |
587 | ||
cfccde04 | 588 | static void move_right(char *buf, char *end, unsigned len, unsigned spaces) |
4b6ccca7 AV |
589 | { |
590 | size_t size; | |
591 | if (buf >= end) /* nowhere to put anything */ | |
592 | return; | |
593 | size = end - buf; | |
594 | if (size <= spaces) { | |
595 | memset(buf, ' ', size); | |
596 | return; | |
597 | } | |
598 | if (len) { | |
599 | if (len > size - spaces) | |
600 | len = size - spaces; | |
601 | memmove(buf + spaces, buf, len); | |
602 | } | |
603 | memset(buf, ' ', spaces); | |
604 | } | |
605 | ||
cfccde04 RV |
606 | /* |
607 | * Handle field width padding for a string. | |
608 | * @buf: current buffer position | |
609 | * @n: length of string | |
610 | * @end: end of output buffer | |
611 | * @spec: for field width and flags | |
612 | * Returns: new buffer position after padding. | |
613 | */ | |
614 | static noinline_for_stack | |
615 | char *widen_string(char *buf, int n, char *end, struct printf_spec spec) | |
616 | { | |
617 | unsigned spaces; | |
618 | ||
619 | if (likely(n >= spec.field_width)) | |
620 | return buf; | |
621 | /* we want to pad the sucker */ | |
622 | spaces = spec.field_width - n; | |
623 | if (!(spec.flags & LEFT)) { | |
624 | move_right(buf - n, end, n, spaces); | |
625 | return buf + spaces; | |
626 | } | |
627 | while (spaces--) { | |
628 | if (buf < end) | |
629 | *buf = ' '; | |
630 | ++buf; | |
631 | } | |
632 | return buf; | |
633 | } | |
634 | ||
d529ac41 PM |
635 | /* Handle string from a well known address. */ |
636 | static char *string_nocheck(char *buf, char *end, const char *s, | |
637 | struct printf_spec spec) | |
95508cfa | 638 | { |
34fc8b90 | 639 | int len = 0; |
b314dd49 | 640 | int lim = spec.precision; |
95508cfa | 641 | |
34fc8b90 RV |
642 | while (lim--) { |
643 | char c = *s++; | |
644 | if (!c) | |
645 | break; | |
95508cfa | 646 | if (buf < end) |
34fc8b90 | 647 | *buf = c; |
95508cfa | 648 | ++buf; |
34fc8b90 | 649 | ++len; |
95508cfa | 650 | } |
34fc8b90 | 651 | return widen_string(buf, len, end, spec); |
95508cfa RV |
652 | } |
653 | ||
57f5677e RV |
654 | static char *err_ptr(char *buf, char *end, void *ptr, |
655 | struct printf_spec spec) | |
656 | { | |
657 | int err = PTR_ERR(ptr); | |
658 | const char *sym = errname(err); | |
659 | ||
660 | if (sym) | |
661 | return string_nocheck(buf, end, sym, spec); | |
662 | ||
663 | /* | |
664 | * Somebody passed ERR_PTR(-1234) or some other non-existing | |
665 | * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to | |
666 | * printing it as its decimal representation. | |
667 | */ | |
668 | spec.flags |= SIGN; | |
669 | spec.base = 10; | |
670 | return number(buf, end, err, spec); | |
671 | } | |
672 | ||
c8c3b584 PM |
673 | /* Be careful: error messages must fit into the given buffer. */ |
674 | static char *error_string(char *buf, char *end, const char *s, | |
675 | struct printf_spec spec) | |
676 | { | |
677 | /* | |
678 | * Hard limit to avoid a completely insane messages. It actually | |
679 | * works pretty well because most error messages are in | |
680 | * the many pointer format modifiers. | |
681 | */ | |
682 | if (spec.precision == -1) | |
683 | spec.precision = 2 * sizeof(void *); | |
684 | ||
685 | return string_nocheck(buf, end, s, spec); | |
686 | } | |
687 | ||
3e5903eb | 688 | /* |
2ac5a3bf PM |
689 | * Do not call any complex external code here. Nested printk()/vsprintf() |
690 | * might cause infinite loops. Failures might break printk() and would | |
691 | * be hard to debug. | |
3e5903eb PM |
692 | */ |
693 | static const char *check_pointer_msg(const void *ptr) | |
694 | { | |
3e5903eb PM |
695 | if (!ptr) |
696 | return "(null)"; | |
697 | ||
2ac5a3bf | 698 | if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) |
3e5903eb PM |
699 | return "(efault)"; |
700 | ||
701 | return NULL; | |
702 | } | |
703 | ||
704 | static int check_pointer(char **buf, char *end, const void *ptr, | |
705 | struct printf_spec spec) | |
706 | { | |
707 | const char *err_msg; | |
708 | ||
709 | err_msg = check_pointer_msg(ptr); | |
710 | if (err_msg) { | |
c8c3b584 | 711 | *buf = error_string(*buf, end, err_msg, spec); |
3e5903eb PM |
712 | return -EFAULT; |
713 | } | |
714 | ||
715 | return 0; | |
716 | } | |
717 | ||
9073dac1 | 718 | static noinline_for_stack |
d529ac41 PM |
719 | char *string(char *buf, char *end, const char *s, |
720 | struct printf_spec spec) | |
721 | { | |
3e5903eb PM |
722 | if (check_pointer(&buf, end, s, spec)) |
723 | return buf; | |
d529ac41 PM |
724 | |
725 | return string_nocheck(buf, end, s, spec); | |
726 | } | |
727 | ||
ce9d3ece Y |
728 | static char *pointer_string(char *buf, char *end, |
729 | const void *ptr, | |
730 | struct printf_spec spec) | |
9073dac1 GU |
731 | { |
732 | spec.base = 16; | |
733 | spec.flags |= SMALL; | |
734 | if (spec.field_width == -1) { | |
735 | spec.field_width = 2 * sizeof(ptr); | |
736 | spec.flags |= ZEROPAD; | |
737 | } | |
738 | ||
739 | return number(buf, end, (unsigned long int)ptr, spec); | |
740 | } | |
741 | ||
742 | /* Make pointers available for printing early in the boot sequence. */ | |
743 | static int debug_boot_weak_hash __ro_after_init; | |
744 | ||
745 | static int __init debug_boot_weak_hash_enable(char *str) | |
746 | { | |
747 | debug_boot_weak_hash = 1; | |
748 | pr_info("debug_boot_weak_hash enabled\n"); | |
749 | return 0; | |
750 | } | |
751 | early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); | |
752 | ||
e4279b59 | 753 | static bool filled_random_ptr_key __read_mostly; |
9073dac1 | 754 | |
9073dac1 | 755 | /* Maps a pointer to a 32 bit unique identifier. */ |
e4dcad20 JFG |
756 | static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) |
757 | { | |
6701de6c | 758 | static siphash_key_t ptr_key __read_mostly; |
e4dcad20 JFG |
759 | unsigned long hashval; |
760 | ||
e4279b59 | 761 | if (!READ_ONCE(filled_random_ptr_key)) { |
6701de6c JD |
762 | static bool filled = false; |
763 | static DEFINE_SPINLOCK(filling); | |
6701de6c JD |
764 | unsigned long flags; |
765 | ||
e4279b59 | 766 | if (!rng_is_initialized() || |
6701de6c JD |
767 | !spin_trylock_irqsave(&filling, flags)) |
768 | return -EAGAIN; | |
769 | ||
770 | if (!filled) { | |
771 | get_random_bytes(&ptr_key, sizeof(ptr_key)); | |
e4279b59 SAS |
772 | /* Pairs with smp_rmb() before reading ptr_key. */ |
773 | smp_wmb(); | |
774 | WRITE_ONCE(filled_random_ptr_key, true); | |
6701de6c JD |
775 | filled = true; |
776 | } | |
777 | spin_unlock_irqrestore(&filling, flags); | |
778 | } | |
e4279b59 SAS |
779 | /* Pairs with smp_wmb() after writing ptr_key. */ |
780 | smp_rmb(); | |
e4dcad20 JFG |
781 | |
782 | #ifdef CONFIG_64BIT | |
783 | hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); | |
784 | /* | |
785 | * Mask off the first 32 bits, this makes explicit that we have | |
786 | * modified the address (and 32 bits is plenty for a unique ID). | |
787 | */ | |
788 | hashval = hashval & 0xffffffff; | |
789 | #else | |
790 | hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); | |
791 | #endif | |
792 | *hashval_out = hashval; | |
793 | return 0; | |
794 | } | |
795 | ||
796 | int ptr_to_hashval(const void *ptr, unsigned long *hashval_out) | |
797 | { | |
798 | return __ptr_to_hashval(ptr, hashval_out); | |
799 | } | |
800 | ||
9073dac1 GU |
801 | static char *ptr_to_id(char *buf, char *end, const void *ptr, |
802 | struct printf_spec spec) | |
803 | { | |
804 | const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; | |
805 | unsigned long hashval; | |
e4dcad20 | 806 | int ret; |
9073dac1 | 807 | |
7bd57fbc ID |
808 | /* |
809 | * Print the real pointer value for NULL and error pointers, | |
810 | * as they are not actual addresses. | |
811 | */ | |
812 | if (IS_ERR_OR_NULL(ptr)) | |
813 | return pointer_string(buf, end, ptr, spec); | |
814 | ||
9073dac1 GU |
815 | /* When debugging early boot use non-cryptographically secure hash. */ |
816 | if (unlikely(debug_boot_weak_hash)) { | |
817 | hashval = hash_long((unsigned long)ptr, 32); | |
818 | return pointer_string(buf, end, (const void *)hashval, spec); | |
819 | } | |
820 | ||
e4dcad20 JFG |
821 | ret = __ptr_to_hashval(ptr, &hashval); |
822 | if (ret) { | |
9073dac1 GU |
823 | spec.field_width = 2 * sizeof(ptr); |
824 | /* string length must be less than default_width */ | |
c8c3b584 | 825 | return error_string(buf, end, str, spec); |
9073dac1 GU |
826 | } |
827 | ||
9073dac1 GU |
828 | return pointer_string(buf, end, (const void *)hashval, spec); |
829 | } | |
830 | ||
84842911 CL |
831 | static char *default_pointer(char *buf, char *end, const void *ptr, |
832 | struct printf_spec spec) | |
833 | { | |
834 | /* | |
835 | * default is to _not_ leak addresses, so hash before printing, | |
836 | * unless no_hash_pointers is specified on the command line. | |
837 | */ | |
838 | if (unlikely(no_hash_pointers)) | |
839 | return pointer_string(buf, end, ptr, spec); | |
840 | ||
841 | return ptr_to_id(buf, end, ptr, spec); | |
842 | } | |
843 | ||
6eea242f PM |
844 | int kptr_restrict __read_mostly; |
845 | ||
846 | static noinline_for_stack | |
847 | char *restricted_pointer(char *buf, char *end, const void *ptr, | |
848 | struct printf_spec spec) | |
849 | { | |
850 | switch (kptr_restrict) { | |
851 | case 0: | |
1ac2f978 | 852 | /* Handle as %p, hash and do _not_ leak addresses. */ |
84842911 | 853 | return default_pointer(buf, end, ptr, spec); |
6eea242f PM |
854 | case 1: { |
855 | const struct cred *cred; | |
856 | ||
857 | /* | |
858 | * kptr_restrict==1 cannot be used in IRQ context | |
859 | * because its test for CAP_SYSLOG would be meaningless. | |
860 | */ | |
861 | if (in_irq() || in_serving_softirq() || in_nmi()) { | |
862 | if (spec.field_width == -1) | |
863 | spec.field_width = 2 * sizeof(ptr); | |
c8c3b584 | 864 | return error_string(buf, end, "pK-error", spec); |
6eea242f PM |
865 | } |
866 | ||
867 | /* | |
868 | * Only print the real pointer value if the current | |
869 | * process has CAP_SYSLOG and is running with the | |
870 | * same credentials it started with. This is because | |
871 | * access to files is checked at open() time, but %pK | |
872 | * checks permission at read() time. We don't want to | |
873 | * leak pointer values if a binary opens a file using | |
874 | * %pK and then elevates privileges before reading it. | |
875 | */ | |
876 | cred = current_cred(); | |
877 | if (!has_capability_noaudit(current, CAP_SYSLOG) || | |
878 | !uid_eq(cred->euid, cred->uid) || | |
879 | !gid_eq(cred->egid, cred->gid)) | |
880 | ptr = NULL; | |
881 | break; | |
882 | } | |
883 | case 2: | |
884 | default: | |
885 | /* Always print 0's for %pK */ | |
886 | ptr = NULL; | |
887 | break; | |
888 | } | |
889 | ||
890 | return pointer_string(buf, end, ptr, spec); | |
891 | } | |
892 | ||
4b6ccca7 AV |
893 | static noinline_for_stack |
894 | char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, | |
895 | const char *fmt) | |
896 | { | |
897 | const char *array[4], *s; | |
898 | const struct dentry *p; | |
899 | int depth; | |
900 | int i, n; | |
901 | ||
902 | switch (fmt[1]) { | |
903 | case '2': case '3': case '4': | |
904 | depth = fmt[1] - '0'; | |
905 | break; | |
906 | default: | |
907 | depth = 1; | |
908 | } | |
909 | ||
910 | rcu_read_lock(); | |
911 | for (i = 0; i < depth; i++, d = p) { | |
3e5903eb PM |
912 | if (check_pointer(&buf, end, d, spec)) { |
913 | rcu_read_unlock(); | |
914 | return buf; | |
915 | } | |
916 | ||
6aa7de05 MR |
917 | p = READ_ONCE(d->d_parent); |
918 | array[i] = READ_ONCE(d->d_name.name); | |
4b6ccca7 AV |
919 | if (p == d) { |
920 | if (i) | |
921 | array[i] = ""; | |
922 | i++; | |
923 | break; | |
924 | } | |
925 | } | |
926 | s = array[--i]; | |
927 | for (n = 0; n != spec.precision; n++, buf++) { | |
928 | char c = *s++; | |
929 | if (!c) { | |
930 | if (!i) | |
931 | break; | |
932 | c = '/'; | |
933 | s = array[--i]; | |
934 | } | |
935 | if (buf < end) | |
936 | *buf = c; | |
937 | } | |
938 | rcu_read_unlock(); | |
cfccde04 | 939 | return widen_string(buf, n, end, spec); |
4b6ccca7 AV |
940 | } |
941 | ||
36594b31 JH |
942 | static noinline_for_stack |
943 | char *file_dentry_name(char *buf, char *end, const struct file *f, | |
944 | struct printf_spec spec, const char *fmt) | |
945 | { | |
946 | if (check_pointer(&buf, end, f, spec)) | |
947 | return buf; | |
948 | ||
949 | return dentry_name(buf, end, f->f_path.dentry, spec, fmt); | |
950 | } | |
1031bc58 DM |
951 | #ifdef CONFIG_BLOCK |
952 | static noinline_for_stack | |
953 | char *bdev_name(char *buf, char *end, struct block_device *bdev, | |
954 | struct printf_spec spec, const char *fmt) | |
955 | { | |
3e5903eb PM |
956 | struct gendisk *hd; |
957 | ||
958 | if (check_pointer(&buf, end, bdev, spec)) | |
959 | return buf; | |
960 | ||
961 | hd = bdev->bd_disk; | |
1031bc58 | 962 | buf = string(buf, end, hd->disk_name, spec); |
700cd59d | 963 | if (bdev->bd_partno) { |
1031bc58 DM |
964 | if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { |
965 | if (buf < end) | |
966 | *buf = 'p'; | |
967 | buf++; | |
968 | } | |
700cd59d | 969 | buf = number(buf, end, bdev->bd_partno, spec); |
1031bc58 DM |
970 | } |
971 | return buf; | |
972 | } | |
973 | #endif | |
974 | ||
cf3b429b JP |
975 | static noinline_for_stack |
976 | char *symbol_string(char *buf, char *end, void *ptr, | |
b0d33c2b | 977 | struct printf_spec spec, const char *fmt) |
0fe1ef24 | 978 | { |
b0d33c2b | 979 | unsigned long value; |
0fe1ef24 LT |
980 | #ifdef CONFIG_KALLSYMS |
981 | char sym[KSYM_SYMBOL_LEN]; | |
b0d33c2b JP |
982 | #endif |
983 | ||
984 | if (fmt[1] == 'R') | |
985 | ptr = __builtin_extract_return_addr(ptr); | |
986 | value = (unsigned long)ptr; | |
987 | ||
988 | #ifdef CONFIG_KALLSYMS | |
9294523e SB |
989 | if (*fmt == 'B' && fmt[1] == 'b') |
990 | sprint_backtrace_build_id(sym, value); | |
991 | else if (*fmt == 'B') | |
0f77a8d3 | 992 | sprint_backtrace(sym, value); |
9294523e SB |
993 | else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b'))) |
994 | sprint_symbol_build_id(sym, value); | |
9af77064 | 995 | else if (*fmt != 's') |
0c8b946e FW |
996 | sprint_symbol(sym, value); |
997 | else | |
4796dd20 | 998 | sprint_symbol_no_offset(sym, value); |
7b9186f5 | 999 | |
d529ac41 | 1000 | return string_nocheck(buf, end, sym, spec); |
0fe1ef24 | 1001 | #else |
3cab1e71 | 1002 | return special_hex_number(buf, end, value, sizeof(void *)); |
0fe1ef24 LT |
1003 | #endif |
1004 | } | |
1005 | ||
abd4fe62 AS |
1006 | static const struct printf_spec default_str_spec = { |
1007 | .field_width = -1, | |
1008 | .precision = -1, | |
1009 | }; | |
1010 | ||
54433973 AS |
1011 | static const struct printf_spec default_flag_spec = { |
1012 | .base = 16, | |
1013 | .precision = -1, | |
1014 | .flags = SPECIAL | SMALL, | |
1015 | }; | |
1016 | ||
ce0b4910 AS |
1017 | static const struct printf_spec default_dec_spec = { |
1018 | .base = 10, | |
1019 | .precision = -1, | |
1020 | }; | |
1021 | ||
4d42c447 AS |
1022 | static const struct printf_spec default_dec02_spec = { |
1023 | .base = 10, | |
1024 | .field_width = 2, | |
1025 | .precision = -1, | |
1026 | .flags = ZEROPAD, | |
1027 | }; | |
1028 | ||
1029 | static const struct printf_spec default_dec04_spec = { | |
1030 | .base = 10, | |
1031 | .field_width = 4, | |
1032 | .precision = -1, | |
1033 | .flags = ZEROPAD, | |
1034 | }; | |
1035 | ||
cf3b429b JP |
1036 | static noinline_for_stack |
1037 | char *resource_string(char *buf, char *end, struct resource *res, | |
1038 | struct printf_spec spec, const char *fmt) | |
332d2e78 LT |
1039 | { |
1040 | #ifndef IO_RSRC_PRINTK_SIZE | |
28405372 | 1041 | #define IO_RSRC_PRINTK_SIZE 6 |
332d2e78 LT |
1042 | #endif |
1043 | ||
1044 | #ifndef MEM_RSRC_PRINTK_SIZE | |
28405372 | 1045 | #define MEM_RSRC_PRINTK_SIZE 10 |
332d2e78 | 1046 | #endif |
4da0b66c | 1047 | static const struct printf_spec io_spec = { |
fef20d9c | 1048 | .base = 16, |
4da0b66c | 1049 | .field_width = IO_RSRC_PRINTK_SIZE, |
fef20d9c FW |
1050 | .precision = -1, |
1051 | .flags = SPECIAL | SMALL | ZEROPAD, | |
1052 | }; | |
4da0b66c BH |
1053 | static const struct printf_spec mem_spec = { |
1054 | .base = 16, | |
1055 | .field_width = MEM_RSRC_PRINTK_SIZE, | |
1056 | .precision = -1, | |
1057 | .flags = SPECIAL | SMALL | ZEROPAD, | |
1058 | }; | |
0f4050c7 BH |
1059 | static const struct printf_spec bus_spec = { |
1060 | .base = 16, | |
1061 | .field_width = 2, | |
1062 | .precision = -1, | |
1063 | .flags = SMALL | ZEROPAD, | |
1064 | }; | |
4da0b66c | 1065 | static const struct printf_spec str_spec = { |
fd95541e BH |
1066 | .field_width = -1, |
1067 | .precision = 10, | |
1068 | .flags = LEFT, | |
1069 | }; | |
c7dabef8 BH |
1070 | |
1071 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) | |
1072 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ | |
1073 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) | |
1074 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) | |
9d7cca04 | 1075 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
c7dabef8 BH |
1076 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
1077 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, | |
1078 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; | |
1079 | ||
332d2e78 | 1080 | char *p = sym, *pend = sym + sizeof(sym); |
c7dabef8 | 1081 | int decode = (fmt[0] == 'R') ? 1 : 0; |
4da0b66c | 1082 | const struct printf_spec *specp; |
332d2e78 | 1083 | |
3e5903eb PM |
1084 | if (check_pointer(&buf, end, res, spec)) |
1085 | return buf; | |
1086 | ||
332d2e78 | 1087 | *p++ = '['; |
4da0b66c | 1088 | if (res->flags & IORESOURCE_IO) { |
d529ac41 | 1089 | p = string_nocheck(p, pend, "io ", str_spec); |
4da0b66c BH |
1090 | specp = &io_spec; |
1091 | } else if (res->flags & IORESOURCE_MEM) { | |
d529ac41 | 1092 | p = string_nocheck(p, pend, "mem ", str_spec); |
4da0b66c BH |
1093 | specp = &mem_spec; |
1094 | } else if (res->flags & IORESOURCE_IRQ) { | |
d529ac41 | 1095 | p = string_nocheck(p, pend, "irq ", str_spec); |
ce0b4910 | 1096 | specp = &default_dec_spec; |
4da0b66c | 1097 | } else if (res->flags & IORESOURCE_DMA) { |
d529ac41 | 1098 | p = string_nocheck(p, pend, "dma ", str_spec); |
ce0b4910 | 1099 | specp = &default_dec_spec; |
0f4050c7 | 1100 | } else if (res->flags & IORESOURCE_BUS) { |
d529ac41 | 1101 | p = string_nocheck(p, pend, "bus ", str_spec); |
0f4050c7 | 1102 | specp = &bus_spec; |
4da0b66c | 1103 | } else { |
d529ac41 | 1104 | p = string_nocheck(p, pend, "??? ", str_spec); |
4da0b66c | 1105 | specp = &mem_spec; |
c7dabef8 | 1106 | decode = 0; |
fd95541e | 1107 | } |
d19cb803 | 1108 | if (decode && res->flags & IORESOURCE_UNSET) { |
d529ac41 | 1109 | p = string_nocheck(p, pend, "size ", str_spec); |
d19cb803 BH |
1110 | p = number(p, pend, resource_size(res), *specp); |
1111 | } else { | |
1112 | p = number(p, pend, res->start, *specp); | |
1113 | if (res->start != res->end) { | |
1114 | *p++ = '-'; | |
1115 | p = number(p, pend, res->end, *specp); | |
1116 | } | |
c91d3376 | 1117 | } |
c7dabef8 | 1118 | if (decode) { |
fd95541e | 1119 | if (res->flags & IORESOURCE_MEM_64) |
d529ac41 | 1120 | p = string_nocheck(p, pend, " 64bit", str_spec); |
fd95541e | 1121 | if (res->flags & IORESOURCE_PREFETCH) |
d529ac41 | 1122 | p = string_nocheck(p, pend, " pref", str_spec); |
9d7cca04 | 1123 | if (res->flags & IORESOURCE_WINDOW) |
d529ac41 | 1124 | p = string_nocheck(p, pend, " window", str_spec); |
fd95541e | 1125 | if (res->flags & IORESOURCE_DISABLED) |
d529ac41 | 1126 | p = string_nocheck(p, pend, " disabled", str_spec); |
c7dabef8 | 1127 | } else { |
d529ac41 | 1128 | p = string_nocheck(p, pend, " flags ", str_spec); |
54433973 | 1129 | p = number(p, pend, res->flags, default_flag_spec); |
fd95541e | 1130 | } |
332d2e78 | 1131 | *p++ = ']'; |
c7dabef8 | 1132 | *p = '\0'; |
332d2e78 | 1133 | |
d529ac41 | 1134 | return string_nocheck(buf, end, sym, spec); |
332d2e78 LT |
1135 | } |
1136 | ||
31550a16 AS |
1137 | static noinline_for_stack |
1138 | char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, | |
1139 | const char *fmt) | |
1140 | { | |
360603a1 | 1141 | int i, len = 1; /* if we pass '%ph[CDN]', field width remains |
31550a16 AS |
1142 | negative value, fallback to the default */ |
1143 | char separator; | |
1144 | ||
1145 | if (spec.field_width == 0) | |
1146 | /* nothing to print */ | |
1147 | return buf; | |
1148 | ||
3e5903eb PM |
1149 | if (check_pointer(&buf, end, addr, spec)) |
1150 | return buf; | |
31550a16 AS |
1151 | |
1152 | switch (fmt[1]) { | |
1153 | case 'C': | |
1154 | separator = ':'; | |
1155 | break; | |
1156 | case 'D': | |
1157 | separator = '-'; | |
1158 | break; | |
1159 | case 'N': | |
1160 | separator = 0; | |
1161 | break; | |
1162 | default: | |
1163 | separator = ' '; | |
1164 | break; | |
1165 | } | |
1166 | ||
1167 | if (spec.field_width > 0) | |
1168 | len = min_t(int, spec.field_width, 64); | |
1169 | ||
9c98f235 RV |
1170 | for (i = 0; i < len; ++i) { |
1171 | if (buf < end) | |
1172 | *buf = hex_asc_hi(addr[i]); | |
1173 | ++buf; | |
1174 | if (buf < end) | |
1175 | *buf = hex_asc_lo(addr[i]); | |
1176 | ++buf; | |
31550a16 | 1177 | |
9c98f235 RV |
1178 | if (separator && i != len - 1) { |
1179 | if (buf < end) | |
1180 | *buf = separator; | |
1181 | ++buf; | |
1182 | } | |
31550a16 AS |
1183 | } |
1184 | ||
1185 | return buf; | |
1186 | } | |
1187 | ||
dbc760bc TH |
1188 | static noinline_for_stack |
1189 | char *bitmap_string(char *buf, char *end, unsigned long *bitmap, | |
1190 | struct printf_spec spec, const char *fmt) | |
1191 | { | |
1192 | const int CHUNKSZ = 32; | |
1193 | int nr_bits = max_t(int, spec.field_width, 0); | |
1194 | int i, chunksz; | |
1195 | bool first = true; | |
1196 | ||
3e5903eb PM |
1197 | if (check_pointer(&buf, end, bitmap, spec)) |
1198 | return buf; | |
1199 | ||
dbc760bc TH |
1200 | /* reused to print numbers */ |
1201 | spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; | |
1202 | ||
1203 | chunksz = nr_bits & (CHUNKSZ - 1); | |
1204 | if (chunksz == 0) | |
1205 | chunksz = CHUNKSZ; | |
1206 | ||
1207 | i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; | |
1208 | for (; i >= 0; i -= CHUNKSZ) { | |
1209 | u32 chunkmask, val; | |
1210 | int word, bit; | |
1211 | ||
1212 | chunkmask = ((1ULL << chunksz) - 1); | |
1213 | word = i / BITS_PER_LONG; | |
1214 | bit = i % BITS_PER_LONG; | |
1215 | val = (bitmap[word] >> bit) & chunkmask; | |
1216 | ||
1217 | if (!first) { | |
1218 | if (buf < end) | |
1219 | *buf = ','; | |
1220 | buf++; | |
1221 | } | |
1222 | first = false; | |
1223 | ||
1224 | spec.field_width = DIV_ROUND_UP(chunksz, 4); | |
1225 | buf = number(buf, end, val, spec); | |
1226 | ||
1227 | chunksz = CHUNKSZ; | |
1228 | } | |
1229 | return buf; | |
1230 | } | |
1231 | ||
1232 | static noinline_for_stack | |
1233 | char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, | |
1234 | struct printf_spec spec, const char *fmt) | |
1235 | { | |
1236 | int nr_bits = max_t(int, spec.field_width, 0); | |
dbc760bc | 1237 | bool first = true; |
15325b4f | 1238 | int rbot, rtop; |
dbc760bc | 1239 | |
3e5903eb PM |
1240 | if (check_pointer(&buf, end, bitmap, spec)) |
1241 | return buf; | |
1242 | ||
15325b4f | 1243 | for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) { |
dbc760bc TH |
1244 | if (!first) { |
1245 | if (buf < end) | |
1246 | *buf = ','; | |
1247 | buf++; | |
1248 | } | |
1249 | first = false; | |
1250 | ||
ce0b4910 | 1251 | buf = number(buf, end, rbot, default_dec_spec); |
15325b4f YN |
1252 | if (rtop == rbot + 1) |
1253 | continue; | |
dbc760bc | 1254 | |
15325b4f YN |
1255 | if (buf < end) |
1256 | *buf = '-'; | |
1257 | buf = number(++buf, end, rtop - 1, default_dec_spec); | |
dbc760bc TH |
1258 | } |
1259 | return buf; | |
1260 | } | |
1261 | ||
cf3b429b JP |
1262 | static noinline_for_stack |
1263 | char *mac_address_string(char *buf, char *end, u8 *addr, | |
1264 | struct printf_spec spec, const char *fmt) | |
dd45c9cf | 1265 | { |
8a27f7c9 | 1266 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
dd45c9cf HH |
1267 | char *p = mac_addr; |
1268 | int i; | |
bc7259a2 | 1269 | char separator; |
76597ff9 | 1270 | bool reversed = false; |
bc7259a2 | 1271 | |
3e5903eb PM |
1272 | if (check_pointer(&buf, end, addr, spec)) |
1273 | return buf; | |
1274 | ||
76597ff9 AE |
1275 | switch (fmt[1]) { |
1276 | case 'F': | |
bc7259a2 | 1277 | separator = '-'; |
76597ff9 AE |
1278 | break; |
1279 | ||
1280 | case 'R': | |
1281 | reversed = true; | |
4c1ca831 | 1282 | fallthrough; |
76597ff9 AE |
1283 | |
1284 | default: | |
bc7259a2 | 1285 | separator = ':'; |
76597ff9 | 1286 | break; |
bc7259a2 | 1287 | } |
dd45c9cf HH |
1288 | |
1289 | for (i = 0; i < 6; i++) { | |
76597ff9 AE |
1290 | if (reversed) |
1291 | p = hex_byte_pack(p, addr[5 - i]); | |
1292 | else | |
1293 | p = hex_byte_pack(p, addr[i]); | |
1294 | ||
8a27f7c9 | 1295 | if (fmt[0] == 'M' && i != 5) |
bc7259a2 | 1296 | *p++ = separator; |
dd45c9cf HH |
1297 | } |
1298 | *p = '\0'; | |
1299 | ||
d529ac41 | 1300 | return string_nocheck(buf, end, mac_addr, spec); |
dd45c9cf HH |
1301 | } |
1302 | ||
cf3b429b JP |
1303 | static noinline_for_stack |
1304 | char *ip4_string(char *p, const u8 *addr, const char *fmt) | |
8a27f7c9 JP |
1305 | { |
1306 | int i; | |
0159f24e JP |
1307 | bool leading_zeros = (fmt[0] == 'i'); |
1308 | int index; | |
1309 | int step; | |
1310 | ||
1311 | switch (fmt[2]) { | |
1312 | case 'h': | |
1313 | #ifdef __BIG_ENDIAN | |
1314 | index = 0; | |
1315 | step = 1; | |
1316 | #else | |
1317 | index = 3; | |
1318 | step = -1; | |
1319 | #endif | |
1320 | break; | |
1321 | case 'l': | |
1322 | index = 3; | |
1323 | step = -1; | |
1324 | break; | |
1325 | case 'n': | |
1326 | case 'b': | |
1327 | default: | |
1328 | index = 0; | |
1329 | step = 1; | |
1330 | break; | |
1331 | } | |
8a27f7c9 | 1332 | for (i = 0; i < 4; i++) { |
7c43d9a3 | 1333 | char temp[4] __aligned(2); /* hold each IP quad in reverse order */ |
133fd9f5 | 1334 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
8a27f7c9 JP |
1335 | if (leading_zeros) { |
1336 | if (digits < 3) | |
1337 | *p++ = '0'; | |
1338 | if (digits < 2) | |
1339 | *p++ = '0'; | |
1340 | } | |
1341 | /* reverse the digits in the quad */ | |
1342 | while (digits--) | |
1343 | *p++ = temp[digits]; | |
1344 | if (i < 3) | |
1345 | *p++ = '.'; | |
0159f24e | 1346 | index += step; |
8a27f7c9 | 1347 | } |
8a27f7c9 | 1348 | *p = '\0'; |
7b9186f5 | 1349 | |
8a27f7c9 JP |
1350 | return p; |
1351 | } | |
1352 | ||
cf3b429b JP |
1353 | static noinline_for_stack |
1354 | char *ip6_compressed_string(char *p, const char *addr) | |
689afa7d | 1355 | { |
7b9186f5 | 1356 | int i, j, range; |
8a27f7c9 JP |
1357 | unsigned char zerolength[8]; |
1358 | int longest = 1; | |
1359 | int colonpos = -1; | |
1360 | u16 word; | |
7b9186f5 | 1361 | u8 hi, lo; |
8a27f7c9 | 1362 | bool needcolon = false; |
eb78cd26 JP |
1363 | bool useIPv4; |
1364 | struct in6_addr in6; | |
1365 | ||
1366 | memcpy(&in6, addr, sizeof(struct in6_addr)); | |
1367 | ||
1368 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); | |
8a27f7c9 JP |
1369 | |
1370 | memset(zerolength, 0, sizeof(zerolength)); | |
1371 | ||
1372 | if (useIPv4) | |
1373 | range = 6; | |
1374 | else | |
1375 | range = 8; | |
1376 | ||
1377 | /* find position of longest 0 run */ | |
1378 | for (i = 0; i < range; i++) { | |
1379 | for (j = i; j < range; j++) { | |
eb78cd26 | 1380 | if (in6.s6_addr16[j] != 0) |
8a27f7c9 JP |
1381 | break; |
1382 | zerolength[i]++; | |
1383 | } | |
1384 | } | |
1385 | for (i = 0; i < range; i++) { | |
1386 | if (zerolength[i] > longest) { | |
1387 | longest = zerolength[i]; | |
1388 | colonpos = i; | |
1389 | } | |
1390 | } | |
29cf519e JP |
1391 | if (longest == 1) /* don't compress a single 0 */ |
1392 | colonpos = -1; | |
689afa7d | 1393 | |
8a27f7c9 JP |
1394 | /* emit address */ |
1395 | for (i = 0; i < range; i++) { | |
1396 | if (i == colonpos) { | |
1397 | if (needcolon || i == 0) | |
1398 | *p++ = ':'; | |
1399 | *p++ = ':'; | |
1400 | needcolon = false; | |
1401 | i += longest - 1; | |
1402 | continue; | |
1403 | } | |
1404 | if (needcolon) { | |
1405 | *p++ = ':'; | |
1406 | needcolon = false; | |
1407 | } | |
1408 | /* hex u16 without leading 0s */ | |
eb78cd26 | 1409 | word = ntohs(in6.s6_addr16[i]); |
8a27f7c9 JP |
1410 | hi = word >> 8; |
1411 | lo = word & 0xff; | |
1412 | if (hi) { | |
1413 | if (hi > 0x0f) | |
55036ba7 | 1414 | p = hex_byte_pack(p, hi); |
8a27f7c9 JP |
1415 | else |
1416 | *p++ = hex_asc_lo(hi); | |
55036ba7 | 1417 | p = hex_byte_pack(p, lo); |
8a27f7c9 | 1418 | } |
b5ff992b | 1419 | else if (lo > 0x0f) |
55036ba7 | 1420 | p = hex_byte_pack(p, lo); |
8a27f7c9 JP |
1421 | else |
1422 | *p++ = hex_asc_lo(lo); | |
1423 | needcolon = true; | |
1424 | } | |
1425 | ||
1426 | if (useIPv4) { | |
1427 | if (needcolon) | |
1428 | *p++ = ':'; | |
0159f24e | 1429 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
8a27f7c9 | 1430 | } |
8a27f7c9 | 1431 | *p = '\0'; |
7b9186f5 | 1432 | |
8a27f7c9 JP |
1433 | return p; |
1434 | } | |
1435 | ||
cf3b429b JP |
1436 | static noinline_for_stack |
1437 | char *ip6_string(char *p, const char *addr, const char *fmt) | |
8a27f7c9 JP |
1438 | { |
1439 | int i; | |
7b9186f5 | 1440 | |
689afa7d | 1441 | for (i = 0; i < 8; i++) { |
55036ba7 AS |
1442 | p = hex_byte_pack(p, *addr++); |
1443 | p = hex_byte_pack(p, *addr++); | |
8a27f7c9 | 1444 | if (fmt[0] == 'I' && i != 7) |
689afa7d HH |
1445 | *p++ = ':'; |
1446 | } | |
1447 | *p = '\0'; | |
7b9186f5 | 1448 | |
8a27f7c9 JP |
1449 | return p; |
1450 | } | |
1451 | ||
cf3b429b JP |
1452 | static noinline_for_stack |
1453 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, | |
1454 | struct printf_spec spec, const char *fmt) | |
8a27f7c9 JP |
1455 | { |
1456 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; | |
1457 | ||
1458 | if (fmt[0] == 'I' && fmt[2] == 'c') | |
eb78cd26 | 1459 | ip6_compressed_string(ip6_addr, addr); |
8a27f7c9 | 1460 | else |
eb78cd26 | 1461 | ip6_string(ip6_addr, addr, fmt); |
689afa7d | 1462 | |
d529ac41 | 1463 | return string_nocheck(buf, end, ip6_addr, spec); |
689afa7d HH |
1464 | } |
1465 | ||
cf3b429b JP |
1466 | static noinline_for_stack |
1467 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, | |
1468 | struct printf_spec spec, const char *fmt) | |
4aa99606 | 1469 | { |
8a27f7c9 | 1470 | char ip4_addr[sizeof("255.255.255.255")]; |
4aa99606 | 1471 | |
0159f24e | 1472 | ip4_string(ip4_addr, addr, fmt); |
4aa99606 | 1473 | |
d529ac41 | 1474 | return string_nocheck(buf, end, ip4_addr, spec); |
4aa99606 HH |
1475 | } |
1476 | ||
10679643 DB |
1477 | static noinline_for_stack |
1478 | char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, | |
1479 | struct printf_spec spec, const char *fmt) | |
1480 | { | |
1481 | bool have_p = false, have_s = false, have_f = false, have_c = false; | |
1482 | char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + | |
1483 | sizeof(":12345") + sizeof("/123456789") + | |
1484 | sizeof("%1234567890")]; | |
1485 | char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); | |
1486 | const u8 *addr = (const u8 *) &sa->sin6_addr; | |
1487 | char fmt6[2] = { fmt[0], '6' }; | |
1488 | u8 off = 0; | |
1489 | ||
1490 | fmt++; | |
1491 | while (isalpha(*++fmt)) { | |
1492 | switch (*fmt) { | |
1493 | case 'p': | |
1494 | have_p = true; | |
1495 | break; | |
1496 | case 'f': | |
1497 | have_f = true; | |
1498 | break; | |
1499 | case 's': | |
1500 | have_s = true; | |
1501 | break; | |
1502 | case 'c': | |
1503 | have_c = true; | |
1504 | break; | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | if (have_p || have_s || have_f) { | |
1509 | *p = '['; | |
1510 | off = 1; | |
1511 | } | |
1512 | ||
1513 | if (fmt6[0] == 'I' && have_c) | |
1514 | p = ip6_compressed_string(ip6_addr + off, addr); | |
1515 | else | |
1516 | p = ip6_string(ip6_addr + off, addr, fmt6); | |
1517 | ||
1518 | if (have_p || have_s || have_f) | |
1519 | *p++ = ']'; | |
1520 | ||
1521 | if (have_p) { | |
1522 | *p++ = ':'; | |
1523 | p = number(p, pend, ntohs(sa->sin6_port), spec); | |
1524 | } | |
1525 | if (have_f) { | |
1526 | *p++ = '/'; | |
1527 | p = number(p, pend, ntohl(sa->sin6_flowinfo & | |
1528 | IPV6_FLOWINFO_MASK), spec); | |
1529 | } | |
1530 | if (have_s) { | |
1531 | *p++ = '%'; | |
1532 | p = number(p, pend, sa->sin6_scope_id, spec); | |
1533 | } | |
1534 | *p = '\0'; | |
1535 | ||
d529ac41 | 1536 | return string_nocheck(buf, end, ip6_addr, spec); |
10679643 DB |
1537 | } |
1538 | ||
1539 | static noinline_for_stack | |
1540 | char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, | |
1541 | struct printf_spec spec, const char *fmt) | |
1542 | { | |
1543 | bool have_p = false; | |
1544 | char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; | |
1545 | char *pend = ip4_addr + sizeof(ip4_addr); | |
1546 | const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; | |
1547 | char fmt4[3] = { fmt[0], '4', 0 }; | |
1548 | ||
1549 | fmt++; | |
1550 | while (isalpha(*++fmt)) { | |
1551 | switch (*fmt) { | |
1552 | case 'p': | |
1553 | have_p = true; | |
1554 | break; | |
1555 | case 'h': | |
1556 | case 'l': | |
1557 | case 'n': | |
1558 | case 'b': | |
1559 | fmt4[2] = *fmt; | |
1560 | break; | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | p = ip4_string(ip4_addr, addr, fmt4); | |
1565 | if (have_p) { | |
1566 | *p++ = ':'; | |
1567 | p = number(p, pend, ntohs(sa->sin_port), spec); | |
1568 | } | |
1569 | *p = '\0'; | |
1570 | ||
d529ac41 | 1571 | return string_nocheck(buf, end, ip4_addr, spec); |
10679643 DB |
1572 | } |
1573 | ||
f00cc102 PM |
1574 | static noinline_for_stack |
1575 | char *ip_addr_string(char *buf, char *end, const void *ptr, | |
1576 | struct printf_spec spec, const char *fmt) | |
1577 | { | |
0b74d4d7 PM |
1578 | char *err_fmt_msg; |
1579 | ||
3e5903eb PM |
1580 | if (check_pointer(&buf, end, ptr, spec)) |
1581 | return buf; | |
1582 | ||
f00cc102 PM |
1583 | switch (fmt[1]) { |
1584 | case '6': | |
1585 | return ip6_addr_string(buf, end, ptr, spec, fmt); | |
1586 | case '4': | |
1587 | return ip4_addr_string(buf, end, ptr, spec, fmt); | |
1588 | case 'S': { | |
1589 | const union { | |
1590 | struct sockaddr raw; | |
1591 | struct sockaddr_in v4; | |
1592 | struct sockaddr_in6 v6; | |
1593 | } *sa = ptr; | |
1594 | ||
1595 | switch (sa->raw.sa_family) { | |
1596 | case AF_INET: | |
1597 | return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); | |
1598 | case AF_INET6: | |
1599 | return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); | |
1600 | default: | |
c8c3b584 | 1601 | return error_string(buf, end, "(einval)", spec); |
f00cc102 PM |
1602 | }} |
1603 | } | |
1604 | ||
0b74d4d7 | 1605 | err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; |
c8c3b584 | 1606 | return error_string(buf, end, err_fmt_msg, spec); |
f00cc102 PM |
1607 | } |
1608 | ||
71dca95d AS |
1609 | static noinline_for_stack |
1610 | char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, | |
1611 | const char *fmt) | |
1612 | { | |
1613 | bool found = true; | |
1614 | int count = 1; | |
1615 | unsigned int flags = 0; | |
1616 | int len; | |
1617 | ||
1618 | if (spec.field_width == 0) | |
1619 | return buf; /* nothing to print */ | |
1620 | ||
3e5903eb PM |
1621 | if (check_pointer(&buf, end, addr, spec)) |
1622 | return buf; | |
71dca95d AS |
1623 | |
1624 | do { | |
1625 | switch (fmt[count++]) { | |
1626 | case 'a': | |
1627 | flags |= ESCAPE_ANY; | |
1628 | break; | |
1629 | case 'c': | |
1630 | flags |= ESCAPE_SPECIAL; | |
1631 | break; | |
1632 | case 'h': | |
1633 | flags |= ESCAPE_HEX; | |
1634 | break; | |
1635 | case 'n': | |
1636 | flags |= ESCAPE_NULL; | |
1637 | break; | |
1638 | case 'o': | |
1639 | flags |= ESCAPE_OCTAL; | |
1640 | break; | |
1641 | case 'p': | |
1642 | flags |= ESCAPE_NP; | |
1643 | break; | |
1644 | case 's': | |
1645 | flags |= ESCAPE_SPACE; | |
1646 | break; | |
1647 | default: | |
1648 | found = false; | |
1649 | break; | |
1650 | } | |
1651 | } while (found); | |
1652 | ||
1653 | if (!flags) | |
1654 | flags = ESCAPE_ANY_NP; | |
1655 | ||
1656 | len = spec.field_width < 0 ? 1 : spec.field_width; | |
1657 | ||
41416f23 RV |
1658 | /* |
1659 | * string_escape_mem() writes as many characters as it can to | |
1660 | * the given buffer, and returns the total size of the output | |
1661 | * had the buffer been big enough. | |
1662 | */ | |
1663 | buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); | |
71dca95d AS |
1664 | |
1665 | return buf; | |
1666 | } | |
1667 | ||
3e5903eb PM |
1668 | static char *va_format(char *buf, char *end, struct va_format *va_fmt, |
1669 | struct printf_spec spec, const char *fmt) | |
45c3e93d PM |
1670 | { |
1671 | va_list va; | |
1672 | ||
3e5903eb PM |
1673 | if (check_pointer(&buf, end, va_fmt, spec)) |
1674 | return buf; | |
1675 | ||
45c3e93d PM |
1676 | va_copy(va, *va_fmt->va); |
1677 | buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); | |
1678 | va_end(va); | |
1679 | ||
1680 | return buf; | |
1681 | } | |
1682 | ||
cf3b429b JP |
1683 | static noinline_for_stack |
1684 | char *uuid_string(char *buf, char *end, const u8 *addr, | |
1685 | struct printf_spec spec, const char *fmt) | |
9ac6e44e | 1686 | { |
2b1b0d66 | 1687 | char uuid[UUID_STRING_LEN + 1]; |
9ac6e44e JP |
1688 | char *p = uuid; |
1689 | int i; | |
f9727a17 | 1690 | const u8 *index = uuid_index; |
9ac6e44e JP |
1691 | bool uc = false; |
1692 | ||
3e5903eb PM |
1693 | if (check_pointer(&buf, end, addr, spec)) |
1694 | return buf; | |
1695 | ||
9ac6e44e JP |
1696 | switch (*(++fmt)) { |
1697 | case 'L': | |
df561f66 | 1698 | uc = true; |
4c1ca831 | 1699 | fallthrough; |
9ac6e44e | 1700 | case 'l': |
f9727a17 | 1701 | index = guid_index; |
9ac6e44e JP |
1702 | break; |
1703 | case 'B': | |
1704 | uc = true; | |
1705 | break; | |
1706 | } | |
1707 | ||
1708 | for (i = 0; i < 16; i++) { | |
aa4ea1c3 AS |
1709 | if (uc) |
1710 | p = hex_byte_pack_upper(p, addr[index[i]]); | |
1711 | else | |
1712 | p = hex_byte_pack(p, addr[index[i]]); | |
9ac6e44e JP |
1713 | switch (i) { |
1714 | case 3: | |
1715 | case 5: | |
1716 | case 7: | |
1717 | case 9: | |
1718 | *p++ = '-'; | |
1719 | break; | |
1720 | } | |
1721 | } | |
1722 | ||
1723 | *p = 0; | |
1724 | ||
d529ac41 | 1725 | return string_nocheck(buf, end, uuid, spec); |
9ac6e44e JP |
1726 | } |
1727 | ||
5b17aecf | 1728 | static noinline_for_stack |
431bca24 GU |
1729 | char *netdev_bits(char *buf, char *end, const void *addr, |
1730 | struct printf_spec spec, const char *fmt) | |
c8f44aff | 1731 | { |
5b17aecf AS |
1732 | unsigned long long num; |
1733 | int size; | |
1734 | ||
3e5903eb PM |
1735 | if (check_pointer(&buf, end, addr, spec)) |
1736 | return buf; | |
1737 | ||
5b17aecf AS |
1738 | switch (fmt[1]) { |
1739 | case 'F': | |
1740 | num = *(const netdev_features_t *)addr; | |
1741 | size = sizeof(netdev_features_t); | |
1742 | break; | |
1743 | default: | |
c8c3b584 | 1744 | return error_string(buf, end, "(%pN?)", spec); |
5b17aecf | 1745 | } |
c8f44aff | 1746 | |
3cab1e71 | 1747 | return special_hex_number(buf, end, num, size); |
c8f44aff MM |
1748 | } |
1749 | ||
af612e43 SA |
1750 | static noinline_for_stack |
1751 | char *fourcc_string(char *buf, char *end, const u32 *fourcc, | |
1752 | struct printf_spec spec, const char *fmt) | |
1753 | { | |
1754 | char output[sizeof("0123 little-endian (0x01234567)")]; | |
1755 | char *p = output; | |
1756 | unsigned int i; | |
d75b26f8 | 1757 | u32 orig, val; |
af612e43 SA |
1758 | |
1759 | if (fmt[1] != 'c' || fmt[2] != 'c') | |
1760 | return error_string(buf, end, "(%p4?)", spec); | |
1761 | ||
1762 | if (check_pointer(&buf, end, fourcc, spec)) | |
1763 | return buf; | |
1764 | ||
d75b26f8 AS |
1765 | orig = get_unaligned(fourcc); |
1766 | val = orig & ~BIT(31); | |
af612e43 | 1767 | |
d75b26f8 | 1768 | for (i = 0; i < sizeof(u32); i++) { |
af612e43 SA |
1769 | unsigned char c = val >> (i * 8); |
1770 | ||
1771 | /* Print non-control ASCII characters as-is, dot otherwise */ | |
1772 | *p++ = isascii(c) && isprint(c) ? c : '.'; | |
1773 | } | |
1774 | ||
f74a08fc AS |
1775 | *p++ = ' '; |
1776 | strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian"); | |
af612e43 SA |
1777 | p += strlen(p); |
1778 | ||
1779 | *p++ = ' '; | |
1780 | *p++ = '('; | |
d75b26f8 | 1781 | p = special_hex_number(p, output + sizeof(output) - 2, orig, sizeof(u32)); |
af612e43 SA |
1782 | *p++ = ')'; |
1783 | *p = '\0'; | |
1784 | ||
1785 | return string(buf, end, output, spec); | |
1786 | } | |
1787 | ||
aaf07621 | 1788 | static noinline_for_stack |
3e5903eb PM |
1789 | char *address_val(char *buf, char *end, const void *addr, |
1790 | struct printf_spec spec, const char *fmt) | |
aaf07621 JP |
1791 | { |
1792 | unsigned long long num; | |
3cab1e71 | 1793 | int size; |
aaf07621 | 1794 | |
3e5903eb PM |
1795 | if (check_pointer(&buf, end, addr, spec)) |
1796 | return buf; | |
1797 | ||
aaf07621 JP |
1798 | switch (fmt[1]) { |
1799 | case 'd': | |
1800 | num = *(const dma_addr_t *)addr; | |
3cab1e71 | 1801 | size = sizeof(dma_addr_t); |
aaf07621 JP |
1802 | break; |
1803 | case 'p': | |
1804 | default: | |
1805 | num = *(const phys_addr_t *)addr; | |
3cab1e71 | 1806 | size = sizeof(phys_addr_t); |
aaf07621 JP |
1807 | break; |
1808 | } | |
1809 | ||
3cab1e71 | 1810 | return special_hex_number(buf, end, num, size); |
aaf07621 JP |
1811 | } |
1812 | ||
4d42c447 AS |
1813 | static noinline_for_stack |
1814 | char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) | |
1815 | { | |
1816 | int year = tm->tm_year + (r ? 0 : 1900); | |
1817 | int mon = tm->tm_mon + (r ? 0 : 1); | |
1818 | ||
1819 | buf = number(buf, end, year, default_dec04_spec); | |
1820 | if (buf < end) | |
1821 | *buf = '-'; | |
1822 | buf++; | |
1823 | ||
1824 | buf = number(buf, end, mon, default_dec02_spec); | |
1825 | if (buf < end) | |
1826 | *buf = '-'; | |
1827 | buf++; | |
1828 | ||
1829 | return number(buf, end, tm->tm_mday, default_dec02_spec); | |
1830 | } | |
1831 | ||
1832 | static noinline_for_stack | |
1833 | char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) | |
1834 | { | |
1835 | buf = number(buf, end, tm->tm_hour, default_dec02_spec); | |
1836 | if (buf < end) | |
1837 | *buf = ':'; | |
1838 | buf++; | |
1839 | ||
1840 | buf = number(buf, end, tm->tm_min, default_dec02_spec); | |
1841 | if (buf < end) | |
1842 | *buf = ':'; | |
1843 | buf++; | |
1844 | ||
1845 | return number(buf, end, tm->tm_sec, default_dec02_spec); | |
1846 | } | |
1847 | ||
1848 | static noinline_for_stack | |
3e5903eb PM |
1849 | char *rtc_str(char *buf, char *end, const struct rtc_time *tm, |
1850 | struct printf_spec spec, const char *fmt) | |
4d42c447 AS |
1851 | { |
1852 | bool have_t = true, have_d = true; | |
20bc8c1e AS |
1853 | bool raw = false, iso8601_separator = true; |
1854 | bool found = true; | |
4d42c447 AS |
1855 | int count = 2; |
1856 | ||
3e5903eb PM |
1857 | if (check_pointer(&buf, end, tm, spec)) |
1858 | return buf; | |
1859 | ||
4d42c447 AS |
1860 | switch (fmt[count]) { |
1861 | case 'd': | |
1862 | have_t = false; | |
1863 | count++; | |
1864 | break; | |
1865 | case 't': | |
1866 | have_d = false; | |
1867 | count++; | |
1868 | break; | |
1869 | } | |
1870 | ||
20bc8c1e AS |
1871 | do { |
1872 | switch (fmt[count++]) { | |
1873 | case 'r': | |
1874 | raw = true; | |
1875 | break; | |
1876 | case 's': | |
1877 | iso8601_separator = false; | |
1878 | break; | |
1879 | default: | |
1880 | found = false; | |
1881 | break; | |
1882 | } | |
1883 | } while (found); | |
4d42c447 AS |
1884 | |
1885 | if (have_d) | |
1886 | buf = date_str(buf, end, tm, raw); | |
1887 | if (have_d && have_t) { | |
4d42c447 | 1888 | if (buf < end) |
20bc8c1e | 1889 | *buf = iso8601_separator ? 'T' : ' '; |
4d42c447 AS |
1890 | buf++; |
1891 | } | |
1892 | if (have_t) | |
1893 | buf = time_str(buf, end, tm, raw); | |
1894 | ||
1895 | return buf; | |
1896 | } | |
1897 | ||
7daac5b2 AS |
1898 | static noinline_for_stack |
1899 | char *time64_str(char *buf, char *end, const time64_t time, | |
1900 | struct printf_spec spec, const char *fmt) | |
1901 | { | |
1902 | struct rtc_time rtc_time; | |
1903 | struct tm tm; | |
1904 | ||
1905 | time64_to_tm(time, 0, &tm); | |
1906 | ||
1907 | rtc_time.tm_sec = tm.tm_sec; | |
1908 | rtc_time.tm_min = tm.tm_min; | |
1909 | rtc_time.tm_hour = tm.tm_hour; | |
1910 | rtc_time.tm_mday = tm.tm_mday; | |
1911 | rtc_time.tm_mon = tm.tm_mon; | |
1912 | rtc_time.tm_year = tm.tm_year; | |
1913 | rtc_time.tm_wday = tm.tm_wday; | |
1914 | rtc_time.tm_yday = tm.tm_yday; | |
1915 | ||
1916 | rtc_time.tm_isdst = 0; | |
1917 | ||
1918 | return rtc_str(buf, end, &rtc_time, spec, fmt); | |
1919 | } | |
1920 | ||
4d42c447 AS |
1921 | static noinline_for_stack |
1922 | char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, | |
1923 | const char *fmt) | |
1924 | { | |
1925 | switch (fmt[1]) { | |
1926 | case 'R': | |
3e5903eb | 1927 | return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); |
7daac5b2 AS |
1928 | case 'T': |
1929 | return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); | |
4d42c447 | 1930 | default: |
7daac5b2 | 1931 | return error_string(buf, end, "(%pt?)", spec); |
4d42c447 AS |
1932 | } |
1933 | } | |
1934 | ||
900cca29 GU |
1935 | static noinline_for_stack |
1936 | char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, | |
1937 | const char *fmt) | |
1938 | { | |
0b74d4d7 | 1939 | if (!IS_ENABLED(CONFIG_HAVE_CLK)) |
c8c3b584 | 1940 | return error_string(buf, end, "(%pC?)", spec); |
0b74d4d7 | 1941 | |
3e5903eb PM |
1942 | if (check_pointer(&buf, end, clk, spec)) |
1943 | return buf; | |
900cca29 GU |
1944 | |
1945 | switch (fmt[1]) { | |
900cca29 GU |
1946 | case 'n': |
1947 | default: | |
1948 | #ifdef CONFIG_COMMON_CLK | |
1949 | return string(buf, end, __clk_get_name(clk), spec); | |
1950 | #else | |
4ca96aa9 | 1951 | return ptr_to_id(buf, end, clk, spec); |
900cca29 GU |
1952 | #endif |
1953 | } | |
1954 | } | |
1955 | ||
edf14cdb VB |
1956 | static |
1957 | char *format_flags(char *buf, char *end, unsigned long flags, | |
1958 | const struct trace_print_flags *names) | |
1959 | { | |
1960 | unsigned long mask; | |
edf14cdb VB |
1961 | |
1962 | for ( ; flags && names->name; names++) { | |
1963 | mask = names->mask; | |
1964 | if ((flags & mask) != mask) | |
1965 | continue; | |
1966 | ||
abd4fe62 | 1967 | buf = string(buf, end, names->name, default_str_spec); |
edf14cdb VB |
1968 | |
1969 | flags &= ~mask; | |
1970 | if (flags) { | |
1971 | if (buf < end) | |
1972 | *buf = '|'; | |
1973 | buf++; | |
1974 | } | |
1975 | } | |
1976 | ||
1977 | if (flags) | |
54433973 | 1978 | buf = number(buf, end, flags, default_flag_spec); |
edf14cdb VB |
1979 | |
1980 | return buf; | |
1981 | } | |
1982 | ||
c244297a YS |
1983 | struct page_flags_fields { |
1984 | int width; | |
1985 | int shift; | |
1986 | int mask; | |
1987 | const struct printf_spec *spec; | |
1988 | const char *name; | |
1989 | }; | |
1990 | ||
1991 | static const struct page_flags_fields pff[] = { | |
1992 | {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, | |
1993 | &default_dec_spec, "section"}, | |
1994 | {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, | |
1995 | &default_dec_spec, "node"}, | |
1996 | {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, | |
1997 | &default_dec_spec, "zone"}, | |
1998 | {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, | |
1999 | &default_flag_spec, "lastcpupid"}, | |
2000 | {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, | |
2001 | &default_flag_spec, "kasantag"}, | |
2002 | }; | |
2003 | ||
2004 | static | |
2005 | char *format_page_flags(char *buf, char *end, unsigned long flags) | |
2006 | { | |
41c961b9 | 2007 | unsigned long main_flags = flags & PAGEFLAGS_MASK; |
c244297a YS |
2008 | bool append = false; |
2009 | int i; | |
2010 | ||
23efd080 MWO |
2011 | buf = number(buf, end, flags, default_flag_spec); |
2012 | if (buf < end) | |
2013 | *buf = '('; | |
2014 | buf++; | |
2015 | ||
c244297a YS |
2016 | /* Page flags from the main area. */ |
2017 | if (main_flags) { | |
2018 | buf = format_flags(buf, end, main_flags, pageflag_names); | |
2019 | append = true; | |
2020 | } | |
2021 | ||
2022 | /* Page flags from the fields area */ | |
2023 | for (i = 0; i < ARRAY_SIZE(pff); i++) { | |
2024 | /* Skip undefined fields. */ | |
2025 | if (!pff[i].width) | |
2026 | continue; | |
2027 | ||
2028 | /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */ | |
2029 | if (append) { | |
2030 | if (buf < end) | |
2031 | *buf = '|'; | |
2032 | buf++; | |
2033 | } | |
2034 | ||
2035 | buf = string(buf, end, pff[i].name, default_str_spec); | |
2036 | if (buf < end) | |
2037 | *buf = '='; | |
2038 | buf++; | |
2039 | buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask, | |
2040 | *pff[i].spec); | |
2041 | ||
2042 | append = true; | |
2043 | } | |
23efd080 MWO |
2044 | if (buf < end) |
2045 | *buf = ')'; | |
2046 | buf++; | |
c244297a YS |
2047 | |
2048 | return buf; | |
2049 | } | |
2050 | ||
edf14cdb | 2051 | static noinline_for_stack |
0b74d4d7 PM |
2052 | char *flags_string(char *buf, char *end, void *flags_ptr, |
2053 | struct printf_spec spec, const char *fmt) | |
edf14cdb VB |
2054 | { |
2055 | unsigned long flags; | |
2056 | const struct trace_print_flags *names; | |
2057 | ||
3e5903eb PM |
2058 | if (check_pointer(&buf, end, flags_ptr, spec)) |
2059 | return buf; | |
2060 | ||
edf14cdb VB |
2061 | switch (fmt[1]) { |
2062 | case 'p': | |
c244297a | 2063 | return format_page_flags(buf, end, *(unsigned long *)flags_ptr); |
edf14cdb VB |
2064 | case 'v': |
2065 | flags = *(unsigned long *)flags_ptr; | |
2066 | names = vmaflag_names; | |
2067 | break; | |
2068 | case 'g': | |
30d497a0 | 2069 | flags = (__force unsigned long)(*(gfp_t *)flags_ptr); |
edf14cdb VB |
2070 | names = gfpflag_names; |
2071 | break; | |
2072 | default: | |
c8c3b584 | 2073 | return error_string(buf, end, "(%pG?)", spec); |
edf14cdb VB |
2074 | } |
2075 | ||
2076 | return format_flags(buf, end, flags, names); | |
2077 | } | |
2078 | ||
ce4fecf1 | 2079 | static noinline_for_stack |
a92eb762 SA |
2080 | char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, |
2081 | char *end) | |
ce4fecf1 PA |
2082 | { |
2083 | int depth; | |
ce4fecf1 | 2084 | |
a92eb762 SA |
2085 | /* Loop starting from the root node to the current node. */ |
2086 | for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { | |
2087 | struct fwnode_handle *__fwnode = | |
2088 | fwnode_get_nth_parent(fwnode, depth); | |
ce4fecf1 | 2089 | |
a92eb762 | 2090 | buf = string(buf, end, fwnode_get_name_prefix(__fwnode), |
abd4fe62 | 2091 | default_str_spec); |
a92eb762 | 2092 | buf = string(buf, end, fwnode_get_name(__fwnode), |
abd4fe62 | 2093 | default_str_spec); |
a92eb762 SA |
2094 | |
2095 | fwnode_handle_put(__fwnode); | |
ce4fecf1 | 2096 | } |
a92eb762 | 2097 | |
ce4fecf1 PA |
2098 | return buf; |
2099 | } | |
2100 | ||
2101 | static noinline_for_stack | |
2102 | char *device_node_string(char *buf, char *end, struct device_node *dn, | |
2103 | struct printf_spec spec, const char *fmt) | |
2104 | { | |
2105 | char tbuf[sizeof("xxxx") + 1]; | |
2106 | const char *p; | |
2107 | int ret; | |
2108 | char *buf_start = buf; | |
2109 | struct property *prop; | |
2110 | bool has_mult, pass; | |
ce4fecf1 PA |
2111 | |
2112 | struct printf_spec str_spec = spec; | |
2113 | str_spec.field_width = -1; | |
2114 | ||
83abc5a7 SA |
2115 | if (fmt[0] != 'F') |
2116 | return error_string(buf, end, "(%pO?)", spec); | |
2117 | ||
ce4fecf1 | 2118 | if (!IS_ENABLED(CONFIG_OF)) |
c8c3b584 | 2119 | return error_string(buf, end, "(%pOF?)", spec); |
ce4fecf1 | 2120 | |
3e5903eb PM |
2121 | if (check_pointer(&buf, end, dn, spec)) |
2122 | return buf; | |
ce4fecf1 PA |
2123 | |
2124 | /* simple case without anything any more format specifiers */ | |
2125 | fmt++; | |
2126 | if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) | |
2127 | fmt = "f"; | |
2128 | ||
2129 | for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { | |
6d0a70a2 | 2130 | int precision; |
ce4fecf1 PA |
2131 | if (pass) { |
2132 | if (buf < end) | |
2133 | *buf = ':'; | |
2134 | buf++; | |
2135 | } | |
2136 | ||
2137 | switch (*fmt) { | |
2138 | case 'f': /* full_name */ | |
a92eb762 SA |
2139 | buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, |
2140 | end); | |
ce4fecf1 PA |
2141 | break; |
2142 | case 'n': /* name */ | |
a92eb762 | 2143 | p = fwnode_get_name(of_fwnode_handle(dn)); |
6d0a70a2 RH |
2144 | precision = str_spec.precision; |
2145 | str_spec.precision = strchrnul(p, '@') - p; | |
2146 | buf = string(buf, end, p, str_spec); | |
2147 | str_spec.precision = precision; | |
ce4fecf1 PA |
2148 | break; |
2149 | case 'p': /* phandle */ | |
09ceb8d7 | 2150 | buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); |
ce4fecf1 PA |
2151 | break; |
2152 | case 'P': /* path-spec */ | |
a92eb762 | 2153 | p = fwnode_get_name(of_fwnode_handle(dn)); |
ce4fecf1 PA |
2154 | if (!p[1]) |
2155 | p = "/"; | |
2156 | buf = string(buf, end, p, str_spec); | |
2157 | break; | |
2158 | case 'F': /* flags */ | |
2159 | tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; | |
2160 | tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; | |
2161 | tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; | |
2162 | tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; | |
2163 | tbuf[4] = 0; | |
d529ac41 | 2164 | buf = string_nocheck(buf, end, tbuf, str_spec); |
ce4fecf1 PA |
2165 | break; |
2166 | case 'c': /* major compatible string */ | |
2167 | ret = of_property_read_string(dn, "compatible", &p); | |
2168 | if (!ret) | |
2169 | buf = string(buf, end, p, str_spec); | |
2170 | break; | |
2171 | case 'C': /* full compatible string */ | |
2172 | has_mult = false; | |
2173 | of_property_for_each_string(dn, "compatible", prop, p) { | |
2174 | if (has_mult) | |
d529ac41 PM |
2175 | buf = string_nocheck(buf, end, ",", str_spec); |
2176 | buf = string_nocheck(buf, end, "\"", str_spec); | |
ce4fecf1 | 2177 | buf = string(buf, end, p, str_spec); |
d529ac41 | 2178 | buf = string_nocheck(buf, end, "\"", str_spec); |
ce4fecf1 PA |
2179 | |
2180 | has_mult = true; | |
2181 | } | |
2182 | break; | |
2183 | default: | |
2184 | break; | |
2185 | } | |
2186 | } | |
2187 | ||
2188 | return widen_string(buf, buf - buf_start, end, spec); | |
2189 | } | |
2190 | ||
3bd32d6a SA |
2191 | static noinline_for_stack |
2192 | char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, | |
2193 | struct printf_spec spec, const char *fmt) | |
798cc27a | 2194 | { |
3bd32d6a SA |
2195 | struct printf_spec str_spec = spec; |
2196 | char *buf_start = buf; | |
2197 | ||
2198 | str_spec.field_width = -1; | |
2199 | ||
2200 | if (*fmt != 'w') | |
2201 | return error_string(buf, end, "(%pf?)", spec); | |
2202 | ||
2203 | if (check_pointer(&buf, end, fwnode, spec)) | |
2204 | return buf; | |
2205 | ||
2206 | fmt++; | |
2207 | ||
2208 | switch (*fmt) { | |
2209 | case 'P': /* name */ | |
2210 | buf = string(buf, end, fwnode_get_name(fwnode), str_spec); | |
2211 | break; | |
2212 | case 'f': /* full_name */ | |
2213 | default: | |
2214 | buf = fwnode_full_name_string(fwnode, buf, end); | |
2215 | break; | |
798cc27a PM |
2216 | } |
2217 | ||
3bd32d6a | 2218 | return widen_string(buf, buf - buf_start, end, spec); |
798cc27a PM |
2219 | } |
2220 | ||
79270291 | 2221 | int __init no_hash_pointers_enable(char *str) |
5ead723a | 2222 | { |
9f961c2e ME |
2223 | if (no_hash_pointers) |
2224 | return 0; | |
2225 | ||
5ead723a TT |
2226 | no_hash_pointers = true; |
2227 | ||
2228 | pr_warn("**********************************************************\n"); | |
2229 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); | |
2230 | pr_warn("** **\n"); | |
2231 | pr_warn("** This system shows unhashed kernel memory addresses **\n"); | |
2232 | pr_warn("** via the console, logs, and other interfaces. This **\n"); | |
2233 | pr_warn("** might reduce the security of your system. **\n"); | |
2234 | pr_warn("** **\n"); | |
2235 | pr_warn("** If you see this message and you are not debugging **\n"); | |
2236 | pr_warn("** the kernel, report this immediately to your system **\n"); | |
2237 | pr_warn("** administrator! **\n"); | |
2238 | pr_warn("** **\n"); | |
2239 | pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); | |
2240 | pr_warn("**********************************************************\n"); | |
2241 | ||
2242 | return 0; | |
2243 | } | |
2244 | early_param("no_hash_pointers", no_hash_pointers_enable); | |
2245 | ||
4d8a743c LT |
2246 | /* |
2247 | * Show a '%p' thing. A kernel extension is that the '%p' is followed | |
2248 | * by an extra set of alphanumeric characters that are extended format | |
2249 | * specifiers. | |
2250 | * | |
0b523769 JP |
2251 | * Please update scripts/checkpatch.pl when adding/removing conversion |
2252 | * characters. (Search for "check for vsprintf extension"). | |
2253 | * | |
332d2e78 LT |
2254 | * Right now we handle: |
2255 | * | |
cdb7e52d SS |
2256 | * - 'S' For symbolic direct pointers (or function descriptors) with offset |
2257 | * - 's' For symbolic direct pointers (or function descriptors) without offset | |
9af77064 | 2258 | * - '[Ss]R' as above with __builtin_extract_return_addr() translation |
9294523e | 2259 | * - 'S[R]b' as above with module build ID (for use in backtraces) |
1586c5ae SA |
2260 | * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of |
2261 | * %ps and %pS. Be careful when re-using these specifiers. | |
0f77a8d3 | 2262 | * - 'B' For backtraced symbolic direct pointers with offset |
9294523e | 2263 | * - 'Bb' as above with module build ID (for use in backtraces) |
c7dabef8 BH |
2264 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
2265 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] | |
dbc760bc TH |
2266 | * - 'b[l]' For a bitmap, the number of bits is determined by the field |
2267 | * width which must be explicitly specified either as part of the | |
2268 | * format string '%32b[l]' or through '%*b[l]', [l] selects | |
2269 | * range-list format instead of hex format | |
dd45c9cf HH |
2270 | * - 'M' For a 6-byte MAC address, it prints the address in the |
2271 | * usual colon-separated hex notation | |
8a27f7c9 | 2272 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
bc7259a2 | 2273 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
c8e00060 | 2274 | * with a dash-separated hex notation |
7c59154e | 2275 | * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) |
8a27f7c9 JP |
2276 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
2277 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) | |
2278 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's | |
10679643 DB |
2279 | * [S][pfs] |
2280 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to | |
2281 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] | |
8a27f7c9 JP |
2282 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
2283 | * IPv6 omits the colons (01020304...0f) | |
2284 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) | |
10679643 DB |
2285 | * [S][pfs] |
2286 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to | |
2287 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] | |
2288 | * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order | |
2289 | * - 'I[6S]c' for IPv6 addresses printed as specified by | |
8eda94bd | 2290 | * https://tools.ietf.org/html/rfc5952 |
71dca95d AS |
2291 | * - 'E[achnops]' For an escaped buffer, where rules are defined by combination |
2292 | * of the following flags (see string_escape_mem() for the | |
2293 | * details): | |
2294 | * a - ESCAPE_ANY | |
2295 | * c - ESCAPE_SPECIAL | |
2296 | * h - ESCAPE_HEX | |
2297 | * n - ESCAPE_NULL | |
2298 | * o - ESCAPE_OCTAL | |
2299 | * p - ESCAPE_NP | |
2300 | * s - ESCAPE_SPACE | |
2301 | * By default ESCAPE_ANY_NP is used. | |
9ac6e44e JP |
2302 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
2303 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
2304 | * Options for %pU are: | |
2305 | * b big endian lower case hex (default) | |
2306 | * B big endian UPPER case hex | |
2307 | * l little endian lower case hex | |
2308 | * L little endian UPPER case hex | |
2309 | * big endian output byte order is: | |
2310 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] | |
2311 | * little endian output byte order is: | |
2312 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] | |
7db6f5fb JP |
2313 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
2314 | * call vsnprintf(->format, *->va_list). | |
2315 | * Implements a "recursive vsnprintf". | |
2316 | * Do not use this feature without some mechanism to verify the | |
2317 | * correctness of the format string and va_list arguments. | |
a48849e2 VB |
2318 | * - 'K' For a kernel pointer that should be hidden from unprivileged users. |
2319 | * Use only for procfs, sysfs and similar files, not printk(); please | |
2320 | * read the documentation (path below) first. | |
c8f44aff | 2321 | * - 'NF' For a netdev_features_t |
af612e43 | 2322 | * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value. |
31550a16 AS |
2323 | * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with |
2324 | * a certain separator (' ' by default): | |
2325 | * C colon | |
2326 | * D dash | |
2327 | * N no separator | |
2328 | * The maximum supported length is 64 bytes of the input. Consider | |
2329 | * to use print_hex_dump() for the larger input. | |
aaf07621 JP |
2330 | * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives |
2331 | * (default assumed to be phys_addr_t, passed by reference) | |
c0d92a57 OJ |
2332 | * - 'd[234]' For a dentry name (optionally 2-4 last components) |
2333 | * - 'D[234]' Same as 'd' but for a struct file | |
1031bc58 | 2334 | * - 'g' For block_device name (gendisk + partition number) |
20bc8c1e | 2335 | * - 't[RT][dt][r][s]' For time and date as represented by: |
4d42c447 | 2336 | * R struct rtc_time |
7daac5b2 | 2337 | * T time64_t |
900cca29 GU |
2338 | * - 'C' For a clock, it prints the name (Common Clock Framework) or address |
2339 | * (legacy clock framework) of the clock | |
2340 | * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address | |
2341 | * (legacy clock framework) of the clock | |
edf14cdb VB |
2342 | * - 'G' For flags to be printed as a collection of symbolic strings that would |
2343 | * construct the specific value. Supported flags given by option: | |
2344 | * p page flags (see struct page) given as pointer to unsigned long | |
2345 | * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t | |
2346 | * v vma flags (VM_*) given as pointer to unsigned long | |
94ac8f20 GU |
2347 | * - 'OF[fnpPcCF]' For a device tree object |
2348 | * Without any optional arguments prints the full_name | |
2349 | * f device node full_name | |
2350 | * n device node name | |
2351 | * p device node phandle | |
2352 | * P device node path spec (name + @unit) | |
2353 | * F device node flags | |
2354 | * c major compatible string | |
2355 | * C full compatible string | |
3bd32d6a SA |
2356 | * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer |
2357 | * Without an option prints the full name of the node | |
2358 | * f full name | |
2359 | * P node name, including a possible unit address | |
a48849e2 VB |
2360 | * - 'x' For printing the address unmodified. Equivalent to "%lx". |
2361 | * Please read the documentation (path below) before using! | |
b2a5212f DB |
2362 | * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of |
2363 | * bpf_trace_printk() where [ku] prefix specifies either kernel (k) | |
2364 | * or user (u) memory to probe, and: | |
2365 | * s a string, equivalent to "%s" on direct vsnprintf() use | |
7b1924a1 | 2366 | * |
b3ed2321 TH |
2367 | * ** When making changes please also update: |
2368 | * Documentation/core-api/printk-formats.rst | |
9ac6e44e | 2369 | * |
ad67b74d TH |
2370 | * Note: The default behaviour (unadorned %p) is to hash the address, |
2371 | * rendering it useful as a unique identifier. | |
4d8a743c | 2372 | */ |
cf3b429b JP |
2373 | static noinline_for_stack |
2374 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, | |
2375 | struct printf_spec spec) | |
78a8bf69 | 2376 | { |
0fe1ef24 | 2377 | switch (*fmt) { |
0fe1ef24 | 2378 | case 'S': |
9ac6e44e | 2379 | case 's': |
04b8eb7a | 2380 | ptr = dereference_symbol_descriptor(ptr); |
4c1ca831 | 2381 | fallthrough; |
0f77a8d3 | 2382 | case 'B': |
b0d33c2b | 2383 | return symbol_string(buf, end, ptr, spec, fmt); |
332d2e78 | 2384 | case 'R': |
c7dabef8 | 2385 | case 'r': |
fd95541e | 2386 | return resource_string(buf, end, ptr, spec, fmt); |
31550a16 AS |
2387 | case 'h': |
2388 | return hex_string(buf, end, ptr, spec, fmt); | |
dbc760bc TH |
2389 | case 'b': |
2390 | switch (fmt[1]) { | |
2391 | case 'l': | |
2392 | return bitmap_list_string(buf, end, ptr, spec, fmt); | |
2393 | default: | |
2394 | return bitmap_string(buf, end, ptr, spec, fmt); | |
2395 | } | |
8a27f7c9 JP |
2396 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
2397 | case 'm': /* Contiguous: 000102030405 */ | |
76597ff9 AE |
2398 | /* [mM]F (FDDI) */ |
2399 | /* [mM]R (Reverse order; Bluetooth) */ | |
8a27f7c9 JP |
2400 | return mac_address_string(buf, end, ptr, spec, fmt); |
2401 | case 'I': /* Formatted IP supported | |
2402 | * 4: 1.2.3.4 | |
2403 | * 6: 0001:0203:...:0708 | |
2404 | * 6c: 1::708 or 1::1.2.3.4 | |
2405 | */ | |
2406 | case 'i': /* Contiguous: | |
2407 | * 4: 001.002.003.004 | |
2408 | * 6: 000102...0f | |
2409 | */ | |
f00cc102 | 2410 | return ip_addr_string(buf, end, ptr, spec, fmt); |
71dca95d AS |
2411 | case 'E': |
2412 | return escaped_string(buf, end, ptr, spec, fmt); | |
9ac6e44e JP |
2413 | case 'U': |
2414 | return uuid_string(buf, end, ptr, spec, fmt); | |
7db6f5fb | 2415 | case 'V': |
3e5903eb | 2416 | return va_format(buf, end, ptr, spec, fmt); |
455cd5ab | 2417 | case 'K': |
57e73442 | 2418 | return restricted_pointer(buf, end, ptr, spec); |
c8f44aff | 2419 | case 'N': |
431bca24 | 2420 | return netdev_bits(buf, end, ptr, spec, fmt); |
af612e43 SA |
2421 | case '4': |
2422 | return fourcc_string(buf, end, ptr, spec, fmt); | |
7d799210 | 2423 | case 'a': |
3e5903eb | 2424 | return address_val(buf, end, ptr, spec, fmt); |
4b6ccca7 AV |
2425 | case 'd': |
2426 | return dentry_name(buf, end, ptr, spec, fmt); | |
4d42c447 AS |
2427 | case 't': |
2428 | return time_and_date(buf, end, ptr, spec, fmt); | |
900cca29 GU |
2429 | case 'C': |
2430 | return clock(buf, end, ptr, spec, fmt); | |
4b6ccca7 | 2431 | case 'D': |
36594b31 | 2432 | return file_dentry_name(buf, end, ptr, spec, fmt); |
1031bc58 DM |
2433 | #ifdef CONFIG_BLOCK |
2434 | case 'g': | |
2435 | return bdev_name(buf, end, ptr, spec, fmt); | |
2436 | #endif | |
2437 | ||
edf14cdb | 2438 | case 'G': |
0b74d4d7 | 2439 | return flags_string(buf, end, ptr, spec, fmt); |
ce4fecf1 | 2440 | case 'O': |
83abc5a7 | 2441 | return device_node_string(buf, end, ptr, spec, fmt + 1); |
3bd32d6a SA |
2442 | case 'f': |
2443 | return fwnode_string(buf, end, ptr, spec, fmt + 1); | |
7b1924a1 TH |
2444 | case 'x': |
2445 | return pointer_string(buf, end, ptr, spec); | |
57f5677e RV |
2446 | case 'e': |
2447 | /* %pe with a non-ERR_PTR gets treated as plain %p */ | |
2448 | if (!IS_ERR(ptr)) | |
84842911 | 2449 | return default_pointer(buf, end, ptr, spec); |
57f5677e | 2450 | return err_ptr(buf, end, ptr, spec); |
b2a5212f DB |
2451 | case 'u': |
2452 | case 'k': | |
2453 | switch (fmt[1]) { | |
2454 | case 's': | |
2455 | return string(buf, end, ptr, spec); | |
2456 | default: | |
2457 | return error_string(buf, end, "(einval)", spec); | |
2458 | } | |
84842911 CL |
2459 | default: |
2460 | return default_pointer(buf, end, ptr, spec); | |
fef20d9c | 2461 | } |
fef20d9c FW |
2462 | } |
2463 | ||
2464 | /* | |
2465 | * Helper function to decode printf style format. | |
2466 | * Each call decode a token from the format and return the | |
2467 | * number of characters read (or likely the delta where it wants | |
2468 | * to go on the next call). | |
2469 | * The decoded token is returned through the parameters | |
2470 | * | |
2471 | * 'h', 'l', or 'L' for integer fields | |
2472 | * 'z' support added 23/7/1999 S.H. | |
2473 | * 'z' changed to 'Z' --davidm 1/25/99 | |
5b5e0928 | 2474 | * 'Z' changed to 'z' --adobriyan 2017-01-25 |
fef20d9c FW |
2475 | * 't' added for ptrdiff_t |
2476 | * | |
2477 | * @fmt: the format string | |
2478 | * @type of the token returned | |
2479 | * @flags: various flags such as +, -, # tokens.. | |
2480 | * @field_width: overwritten width | |
2481 | * @base: base of the number (octal, hex, ...) | |
2482 | * @precision: precision of a number | |
2483 | * @qualifier: qualifier of a number (long, size_t, ...) | |
2484 | */ | |
cf3b429b JP |
2485 | static noinline_for_stack |
2486 | int format_decode(const char *fmt, struct printf_spec *spec) | |
fef20d9c FW |
2487 | { |
2488 | const char *start = fmt; | |
d0484193 | 2489 | char qualifier; |
fef20d9c FW |
2490 | |
2491 | /* we finished early by reading the field width */ | |
ed681a91 | 2492 | if (spec->type == FORMAT_TYPE_WIDTH) { |
fef20d9c FW |
2493 | if (spec->field_width < 0) { |
2494 | spec->field_width = -spec->field_width; | |
2495 | spec->flags |= LEFT; | |
2496 | } | |
2497 | spec->type = FORMAT_TYPE_NONE; | |
2498 | goto precision; | |
2499 | } | |
2500 | ||
2501 | /* we finished early by reading the precision */ | |
2502 | if (spec->type == FORMAT_TYPE_PRECISION) { | |
2503 | if (spec->precision < 0) | |
2504 | spec->precision = 0; | |
2505 | ||
2506 | spec->type = FORMAT_TYPE_NONE; | |
2507 | goto qualifier; | |
2508 | } | |
2509 | ||
2510 | /* By default */ | |
2511 | spec->type = FORMAT_TYPE_NONE; | |
2512 | ||
2513 | for (; *fmt ; ++fmt) { | |
2514 | if (*fmt == '%') | |
2515 | break; | |
2516 | } | |
2517 | ||
2518 | /* Return the current non-format string */ | |
2519 | if (fmt != start || !*fmt) | |
2520 | return fmt - start; | |
2521 | ||
2522 | /* Process flags */ | |
2523 | spec->flags = 0; | |
2524 | ||
2525 | while (1) { /* this also skips first '%' */ | |
2526 | bool found = true; | |
2527 | ||
2528 | ++fmt; | |
2529 | ||
2530 | switch (*fmt) { | |
2531 | case '-': spec->flags |= LEFT; break; | |
2532 | case '+': spec->flags |= PLUS; break; | |
2533 | case ' ': spec->flags |= SPACE; break; | |
2534 | case '#': spec->flags |= SPECIAL; break; | |
2535 | case '0': spec->flags |= ZEROPAD; break; | |
2536 | default: found = false; | |
2537 | } | |
2538 | ||
2539 | if (!found) | |
2540 | break; | |
2541 | } | |
2542 | ||
2543 | /* get field width */ | |
2544 | spec->field_width = -1; | |
2545 | ||
2546 | if (isdigit(*fmt)) | |
2547 | spec->field_width = skip_atoi(&fmt); | |
2548 | else if (*fmt == '*') { | |
2549 | /* it's the next argument */ | |
ed681a91 | 2550 | spec->type = FORMAT_TYPE_WIDTH; |
fef20d9c FW |
2551 | return ++fmt - start; |
2552 | } | |
2553 | ||
2554 | precision: | |
2555 | /* get the precision */ | |
2556 | spec->precision = -1; | |
2557 | if (*fmt == '.') { | |
2558 | ++fmt; | |
2559 | if (isdigit(*fmt)) { | |
2560 | spec->precision = skip_atoi(&fmt); | |
2561 | if (spec->precision < 0) | |
2562 | spec->precision = 0; | |
2563 | } else if (*fmt == '*') { | |
2564 | /* it's the next argument */ | |
adf26f84 | 2565 | spec->type = FORMAT_TYPE_PRECISION; |
fef20d9c FW |
2566 | return ++fmt - start; |
2567 | } | |
2568 | } | |
2569 | ||
2570 | qualifier: | |
2571 | /* get the conversion qualifier */ | |
d0484193 | 2572 | qualifier = 0; |
75fb8f26 | 2573 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
5b5e0928 | 2574 | *fmt == 'z' || *fmt == 't') { |
d0484193 RV |
2575 | qualifier = *fmt++; |
2576 | if (unlikely(qualifier == *fmt)) { | |
2577 | if (qualifier == 'l') { | |
2578 | qualifier = 'L'; | |
a4e94ef0 | 2579 | ++fmt; |
d0484193 RV |
2580 | } else if (qualifier == 'h') { |
2581 | qualifier = 'H'; | |
a4e94ef0 Z |
2582 | ++fmt; |
2583 | } | |
fef20d9c FW |
2584 | } |
2585 | } | |
2586 | ||
2587 | /* default base */ | |
2588 | spec->base = 10; | |
2589 | switch (*fmt) { | |
2590 | case 'c': | |
2591 | spec->type = FORMAT_TYPE_CHAR; | |
2592 | return ++fmt - start; | |
2593 | ||
2594 | case 's': | |
2595 | spec->type = FORMAT_TYPE_STR; | |
2596 | return ++fmt - start; | |
2597 | ||
2598 | case 'p': | |
2599 | spec->type = FORMAT_TYPE_PTR; | |
ffbfed03 | 2600 | return ++fmt - start; |
fef20d9c | 2601 | |
fef20d9c FW |
2602 | case '%': |
2603 | spec->type = FORMAT_TYPE_PERCENT_CHAR; | |
2604 | return ++fmt - start; | |
2605 | ||
2606 | /* integer number formats - set up the flags and "break" */ | |
2607 | case 'o': | |
2608 | spec->base = 8; | |
2609 | break; | |
2610 | ||
2611 | case 'x': | |
2612 | spec->flags |= SMALL; | |
4c1ca831 | 2613 | fallthrough; |
fef20d9c FW |
2614 | |
2615 | case 'X': | |
2616 | spec->base = 16; | |
2617 | break; | |
2618 | ||
2619 | case 'd': | |
2620 | case 'i': | |
39e874f8 | 2621 | spec->flags |= SIGN; |
36f9ff9e | 2622 | break; |
fef20d9c | 2623 | case 'u': |
4aa99606 | 2624 | break; |
fef20d9c | 2625 | |
708d96fd RM |
2626 | case 'n': |
2627 | /* | |
b006f19b RV |
2628 | * Since %n poses a greater security risk than |
2629 | * utility, treat it as any other invalid or | |
2630 | * unsupported format specifier. | |
708d96fd | 2631 | */ |
4c1ca831 | 2632 | fallthrough; |
708d96fd | 2633 | |
fef20d9c | 2634 | default: |
b006f19b | 2635 | WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); |
fef20d9c FW |
2636 | spec->type = FORMAT_TYPE_INVALID; |
2637 | return fmt - start; | |
0fe1ef24 | 2638 | } |
fef20d9c | 2639 | |
d0484193 | 2640 | if (qualifier == 'L') |
fef20d9c | 2641 | spec->type = FORMAT_TYPE_LONG_LONG; |
d0484193 | 2642 | else if (qualifier == 'l') { |
51be17df RV |
2643 | BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); |
2644 | spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); | |
5b5e0928 | 2645 | } else if (qualifier == 'z') { |
fef20d9c | 2646 | spec->type = FORMAT_TYPE_SIZE_T; |
d0484193 | 2647 | } else if (qualifier == 't') { |
fef20d9c | 2648 | spec->type = FORMAT_TYPE_PTRDIFF; |
d0484193 | 2649 | } else if (qualifier == 'H') { |
51be17df RV |
2650 | BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); |
2651 | spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); | |
d0484193 | 2652 | } else if (qualifier == 'h') { |
51be17df RV |
2653 | BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); |
2654 | spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); | |
fef20d9c | 2655 | } else { |
51be17df RV |
2656 | BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); |
2657 | spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); | |
78a8bf69 | 2658 | } |
fef20d9c FW |
2659 | |
2660 | return ++fmt - start; | |
78a8bf69 LT |
2661 | } |
2662 | ||
4d72ba01 RV |
2663 | static void |
2664 | set_field_width(struct printf_spec *spec, int width) | |
2665 | { | |
2666 | spec->field_width = width; | |
2667 | if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { | |
2668 | spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); | |
2669 | } | |
2670 | } | |
2671 | ||
2672 | static void | |
2673 | set_precision(struct printf_spec *spec, int prec) | |
2674 | { | |
2675 | spec->precision = prec; | |
2676 | if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { | |
2677 | spec->precision = clamp(prec, 0, PRECISION_MAX); | |
2678 | } | |
2679 | } | |
2680 | ||
1da177e4 LT |
2681 | /** |
2682 | * vsnprintf - Format a string and place it in a buffer | |
2683 | * @buf: The buffer to place the result into | |
2684 | * @size: The size of the buffer, including the trailing null space | |
2685 | * @fmt: The format string to use | |
2686 | * @args: Arguments for the format string | |
2687 | * | |
d7ec9a05 RV |
2688 | * This function generally follows C99 vsnprintf, but has some |
2689 | * extensions and a few limitations: | |
2690 | * | |
6cc89134 | 2691 | * - ``%n`` is unsupported |
2692 | * - ``%p*`` is handled by pointer() | |
5e4ee7b1 | 2693 | * |
27e7c0e8 | 2694 | * See pointer() or Documentation/core-api/printk-formats.rst for more |
5e4ee7b1 | 2695 | * extensive description. |
20036fdc | 2696 | * |
6cc89134 | 2697 | * **Please update the documentation in both places when making changes** |
80f548e0 | 2698 | * |
1da177e4 LT |
2699 | * The return value is the number of characters which would |
2700 | * be generated for the given input, excluding the trailing | |
2701 | * '\0', as per ISO C99. If you want to have the exact | |
2702 | * number of characters written into @buf as return value | |
72fd4a35 | 2703 | * (not including the trailing '\0'), use vscnprintf(). If the |
1da177e4 LT |
2704 | * return is greater than or equal to @size, the resulting |
2705 | * string is truncated. | |
2706 | * | |
ba1835eb | 2707 | * If you're not already dealing with a va_list consider using snprintf(). |
1da177e4 LT |
2708 | */ |
2709 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) | |
2710 | { | |
1da177e4 | 2711 | unsigned long long num; |
d4be151b | 2712 | char *str, *end; |
fef20d9c | 2713 | struct printf_spec spec = {0}; |
1da177e4 | 2714 | |
f796937a JF |
2715 | /* Reject out-of-range values early. Large positive sizes are |
2716 | used for unknown buffer sizes. */ | |
2aa2f9e2 | 2717 | if (WARN_ON_ONCE(size > INT_MAX)) |
1da177e4 | 2718 | return 0; |
1da177e4 LT |
2719 | |
2720 | str = buf; | |
f796937a | 2721 | end = buf + size; |
1da177e4 | 2722 | |
f796937a JF |
2723 | /* Make sure end is always >= buf */ |
2724 | if (end < buf) { | |
2725 | end = ((void *)-1); | |
2726 | size = end - buf; | |
1da177e4 LT |
2727 | } |
2728 | ||
fef20d9c FW |
2729 | while (*fmt) { |
2730 | const char *old_fmt = fmt; | |
d4be151b | 2731 | int read = format_decode(fmt, &spec); |
1da177e4 | 2732 | |
fef20d9c | 2733 | fmt += read; |
1da177e4 | 2734 | |
fef20d9c FW |
2735 | switch (spec.type) { |
2736 | case FORMAT_TYPE_NONE: { | |
2737 | int copy = read; | |
2738 | if (str < end) { | |
2739 | if (copy > end - str) | |
2740 | copy = end - str; | |
2741 | memcpy(str, old_fmt, copy); | |
1da177e4 | 2742 | } |
fef20d9c FW |
2743 | str += read; |
2744 | break; | |
1da177e4 LT |
2745 | } |
2746 | ||
ed681a91 | 2747 | case FORMAT_TYPE_WIDTH: |
4d72ba01 | 2748 | set_field_width(&spec, va_arg(args, int)); |
fef20d9c | 2749 | break; |
1da177e4 | 2750 | |
fef20d9c | 2751 | case FORMAT_TYPE_PRECISION: |
4d72ba01 | 2752 | set_precision(&spec, va_arg(args, int)); |
fef20d9c | 2753 | break; |
1da177e4 | 2754 | |
d4be151b AGR |
2755 | case FORMAT_TYPE_CHAR: { |
2756 | char c; | |
2757 | ||
fef20d9c FW |
2758 | if (!(spec.flags & LEFT)) { |
2759 | while (--spec.field_width > 0) { | |
f796937a | 2760 | if (str < end) |
1da177e4 LT |
2761 | *str = ' '; |
2762 | ++str; | |
1da177e4 | 2763 | |
fef20d9c FW |
2764 | } |
2765 | } | |
2766 | c = (unsigned char) va_arg(args, int); | |
2767 | if (str < end) | |
2768 | *str = c; | |
2769 | ++str; | |
2770 | while (--spec.field_width > 0) { | |
f796937a | 2771 | if (str < end) |
fef20d9c | 2772 | *str = ' '; |
1da177e4 | 2773 | ++str; |
fef20d9c FW |
2774 | } |
2775 | break; | |
d4be151b | 2776 | } |
1da177e4 | 2777 | |
fef20d9c FW |
2778 | case FORMAT_TYPE_STR: |
2779 | str = string(str, end, va_arg(args, char *), spec); | |
2780 | break; | |
1da177e4 | 2781 | |
fef20d9c | 2782 | case FORMAT_TYPE_PTR: |
ffbfed03 | 2783 | str = pointer(fmt, str, end, va_arg(args, void *), |
fef20d9c FW |
2784 | spec); |
2785 | while (isalnum(*fmt)) | |
2786 | fmt++; | |
2787 | break; | |
1da177e4 | 2788 | |
fef20d9c FW |
2789 | case FORMAT_TYPE_PERCENT_CHAR: |
2790 | if (str < end) | |
2791 | *str = '%'; | |
2792 | ++str; | |
2793 | break; | |
1da177e4 | 2794 | |
fef20d9c | 2795 | case FORMAT_TYPE_INVALID: |
b006f19b RV |
2796 | /* |
2797 | * Presumably the arguments passed gcc's type | |
2798 | * checking, but there is no safe or sane way | |
2799 | * for us to continue parsing the format and | |
2800 | * fetching from the va_list; the remaining | |
2801 | * specifiers and arguments would be out of | |
2802 | * sync. | |
2803 | */ | |
2804 | goto out; | |
fef20d9c | 2805 | |
fef20d9c FW |
2806 | default: |
2807 | switch (spec.type) { | |
2808 | case FORMAT_TYPE_LONG_LONG: | |
2809 | num = va_arg(args, long long); | |
2810 | break; | |
2811 | case FORMAT_TYPE_ULONG: | |
2812 | num = va_arg(args, unsigned long); | |
2813 | break; | |
2814 | case FORMAT_TYPE_LONG: | |
2815 | num = va_arg(args, long); | |
2816 | break; | |
2817 | case FORMAT_TYPE_SIZE_T: | |
ef124960 JG |
2818 | if (spec.flags & SIGN) |
2819 | num = va_arg(args, ssize_t); | |
2820 | else | |
2821 | num = va_arg(args, size_t); | |
fef20d9c FW |
2822 | break; |
2823 | case FORMAT_TYPE_PTRDIFF: | |
2824 | num = va_arg(args, ptrdiff_t); | |
2825 | break; | |
a4e94ef0 Z |
2826 | case FORMAT_TYPE_UBYTE: |
2827 | num = (unsigned char) va_arg(args, int); | |
2828 | break; | |
2829 | case FORMAT_TYPE_BYTE: | |
2830 | num = (signed char) va_arg(args, int); | |
2831 | break; | |
fef20d9c FW |
2832 | case FORMAT_TYPE_USHORT: |
2833 | num = (unsigned short) va_arg(args, int); | |
2834 | break; | |
2835 | case FORMAT_TYPE_SHORT: | |
2836 | num = (short) va_arg(args, int); | |
2837 | break; | |
39e874f8 FW |
2838 | case FORMAT_TYPE_INT: |
2839 | num = (int) va_arg(args, int); | |
fef20d9c FW |
2840 | break; |
2841 | default: | |
2842 | num = va_arg(args, unsigned int); | |
2843 | } | |
2844 | ||
2845 | str = number(str, end, num, spec); | |
1da177e4 | 2846 | } |
1da177e4 | 2847 | } |
fef20d9c | 2848 | |
b006f19b | 2849 | out: |
f796937a JF |
2850 | if (size > 0) { |
2851 | if (str < end) | |
2852 | *str = '\0'; | |
2853 | else | |
0a6047ee | 2854 | end[-1] = '\0'; |
f796937a | 2855 | } |
fef20d9c | 2856 | |
f796937a | 2857 | /* the trailing null byte doesn't count towards the total */ |
1da177e4 | 2858 | return str-buf; |
fef20d9c | 2859 | |
1da177e4 | 2860 | } |
1da177e4 LT |
2861 | EXPORT_SYMBOL(vsnprintf); |
2862 | ||
2863 | /** | |
2864 | * vscnprintf - Format a string and place it in a buffer | |
2865 | * @buf: The buffer to place the result into | |
2866 | * @size: The size of the buffer, including the trailing null space | |
2867 | * @fmt: The format string to use | |
2868 | * @args: Arguments for the format string | |
2869 | * | |
2870 | * The return value is the number of characters which have been written into | |
b921c69f | 2871 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
1da177e4 LT |
2872 | * returns 0. |
2873 | * | |
ba1835eb | 2874 | * If you're not already dealing with a va_list consider using scnprintf(). |
20036fdc AK |
2875 | * |
2876 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 LT |
2877 | */ |
2878 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) | |
2879 | { | |
2880 | int i; | |
2881 | ||
ef62c8ff WL |
2882 | if (unlikely(!size)) |
2883 | return 0; | |
2884 | ||
7b9186f5 AGR |
2885 | i = vsnprintf(buf, size, fmt, args); |
2886 | ||
b921c69f AA |
2887 | if (likely(i < size)) |
2888 | return i; | |
ef62c8ff WL |
2889 | |
2890 | return size - 1; | |
1da177e4 | 2891 | } |
1da177e4 LT |
2892 | EXPORT_SYMBOL(vscnprintf); |
2893 | ||
2894 | /** | |
2895 | * snprintf - Format a string and place it in a buffer | |
2896 | * @buf: The buffer to place the result into | |
2897 | * @size: The size of the buffer, including the trailing null space | |
2898 | * @fmt: The format string to use | |
2899 | * @...: Arguments for the format string | |
2900 | * | |
2901 | * The return value is the number of characters which would be | |
2902 | * generated for the given input, excluding the trailing null, | |
2903 | * as per ISO C99. If the return is greater than or equal to | |
2904 | * @size, the resulting string is truncated. | |
20036fdc AK |
2905 | * |
2906 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 | 2907 | */ |
7b9186f5 | 2908 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
1da177e4 LT |
2909 | { |
2910 | va_list args; | |
2911 | int i; | |
2912 | ||
2913 | va_start(args, fmt); | |
7b9186f5 | 2914 | i = vsnprintf(buf, size, fmt, args); |
1da177e4 | 2915 | va_end(args); |
7b9186f5 | 2916 | |
1da177e4 LT |
2917 | return i; |
2918 | } | |
1da177e4 LT |
2919 | EXPORT_SYMBOL(snprintf); |
2920 | ||
2921 | /** | |
2922 | * scnprintf - Format a string and place it in a buffer | |
2923 | * @buf: The buffer to place the result into | |
2924 | * @size: The size of the buffer, including the trailing null space | |
2925 | * @fmt: The format string to use | |
2926 | * @...: Arguments for the format string | |
2927 | * | |
2928 | * The return value is the number of characters written into @buf not including | |
b903c0b8 | 2929 | * the trailing '\0'. If @size is == 0 the function returns 0. |
1da177e4 LT |
2930 | */ |
2931 | ||
7b9186f5 | 2932 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
1da177e4 LT |
2933 | { |
2934 | va_list args; | |
2935 | int i; | |
2936 | ||
2937 | va_start(args, fmt); | |
b921c69f | 2938 | i = vscnprintf(buf, size, fmt, args); |
1da177e4 | 2939 | va_end(args); |
7b9186f5 | 2940 | |
b921c69f | 2941 | return i; |
1da177e4 LT |
2942 | } |
2943 | EXPORT_SYMBOL(scnprintf); | |
2944 | ||
2945 | /** | |
2946 | * vsprintf - Format a string and place it in a buffer | |
2947 | * @buf: The buffer to place the result into | |
2948 | * @fmt: The format string to use | |
2949 | * @args: Arguments for the format string | |
2950 | * | |
2951 | * The function returns the number of characters written | |
72fd4a35 | 2952 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
1da177e4 LT |
2953 | * buffer overflows. |
2954 | * | |
ba1835eb | 2955 | * If you're not already dealing with a va_list consider using sprintf(). |
20036fdc AK |
2956 | * |
2957 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 LT |
2958 | */ |
2959 | int vsprintf(char *buf, const char *fmt, va_list args) | |
2960 | { | |
2961 | return vsnprintf(buf, INT_MAX, fmt, args); | |
2962 | } | |
1da177e4 LT |
2963 | EXPORT_SYMBOL(vsprintf); |
2964 | ||
2965 | /** | |
2966 | * sprintf - Format a string and place it in a buffer | |
2967 | * @buf: The buffer to place the result into | |
2968 | * @fmt: The format string to use | |
2969 | * @...: Arguments for the format string | |
2970 | * | |
2971 | * The function returns the number of characters written | |
72fd4a35 | 2972 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
1da177e4 | 2973 | * buffer overflows. |
20036fdc AK |
2974 | * |
2975 | * See the vsnprintf() documentation for format string extensions over C99. | |
1da177e4 | 2976 | */ |
7b9186f5 | 2977 | int sprintf(char *buf, const char *fmt, ...) |
1da177e4 LT |
2978 | { |
2979 | va_list args; | |
2980 | int i; | |
2981 | ||
2982 | va_start(args, fmt); | |
7b9186f5 | 2983 | i = vsnprintf(buf, INT_MAX, fmt, args); |
1da177e4 | 2984 | va_end(args); |
7b9186f5 | 2985 | |
1da177e4 LT |
2986 | return i; |
2987 | } | |
1da177e4 LT |
2988 | EXPORT_SYMBOL(sprintf); |
2989 | ||
4370aa4a LJ |
2990 | #ifdef CONFIG_BINARY_PRINTF |
2991 | /* | |
2992 | * bprintf service: | |
2993 | * vbin_printf() - VA arguments to binary data | |
2994 | * bstr_printf() - Binary data to text string | |
2995 | */ | |
2996 | ||
2997 | /** | |
2998 | * vbin_printf - Parse a format string and place args' binary value in a buffer | |
2999 | * @bin_buf: The buffer to place args' binary value | |
3000 | * @size: The size of the buffer(by words(32bits), not characters) | |
3001 | * @fmt: The format string to use | |
3002 | * @args: Arguments for the format string | |
3003 | * | |
3004 | * The format follows C99 vsnprintf, except %n is ignored, and its argument | |
da3dae54 | 3005 | * is skipped. |
4370aa4a LJ |
3006 | * |
3007 | * The return value is the number of words(32bits) which would be generated for | |
3008 | * the given input. | |
3009 | * | |
3010 | * NOTE: | |
3011 | * If the return value is greater than @size, the resulting bin_buf is NOT | |
3012 | * valid for bstr_printf(). | |
3013 | */ | |
3014 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) | |
3015 | { | |
fef20d9c | 3016 | struct printf_spec spec = {0}; |
4370aa4a | 3017 | char *str, *end; |
841a915d | 3018 | int width; |
4370aa4a LJ |
3019 | |
3020 | str = (char *)bin_buf; | |
3021 | end = (char *)(bin_buf + size); | |
3022 | ||
3023 | #define save_arg(type) \ | |
841a915d SRV |
3024 | ({ \ |
3025 | unsigned long long value; \ | |
4370aa4a | 3026 | if (sizeof(type) == 8) { \ |
841a915d | 3027 | unsigned long long val8; \ |
4370aa4a | 3028 | str = PTR_ALIGN(str, sizeof(u32)); \ |
841a915d | 3029 | val8 = va_arg(args, unsigned long long); \ |
4370aa4a | 3030 | if (str + sizeof(type) <= end) { \ |
841a915d SRV |
3031 | *(u32 *)str = *(u32 *)&val8; \ |
3032 | *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ | |
4370aa4a | 3033 | } \ |
841a915d | 3034 | value = val8; \ |
4370aa4a | 3035 | } else { \ |
841a915d | 3036 | unsigned int val4; \ |
4370aa4a | 3037 | str = PTR_ALIGN(str, sizeof(type)); \ |
841a915d | 3038 | val4 = va_arg(args, int); \ |
4370aa4a | 3039 | if (str + sizeof(type) <= end) \ |
841a915d SRV |
3040 | *(typeof(type) *)str = (type)(long)val4; \ |
3041 | value = (unsigned long long)val4; \ | |
4370aa4a LJ |
3042 | } \ |
3043 | str += sizeof(type); \ | |
841a915d SRV |
3044 | value; \ |
3045 | }) | |
4370aa4a | 3046 | |
fef20d9c | 3047 | while (*fmt) { |
d4be151b | 3048 | int read = format_decode(fmt, &spec); |
4370aa4a | 3049 | |
fef20d9c | 3050 | fmt += read; |
4370aa4a | 3051 | |
fef20d9c FW |
3052 | switch (spec.type) { |
3053 | case FORMAT_TYPE_NONE: | |
d4be151b | 3054 | case FORMAT_TYPE_PERCENT_CHAR: |
fef20d9c | 3055 | break; |
b006f19b RV |
3056 | case FORMAT_TYPE_INVALID: |
3057 | goto out; | |
fef20d9c | 3058 | |
ed681a91 | 3059 | case FORMAT_TYPE_WIDTH: |
fef20d9c | 3060 | case FORMAT_TYPE_PRECISION: |
841a915d SRV |
3061 | width = (int)save_arg(int); |
3062 | /* Pointers may require the width */ | |
3063 | if (*fmt == 'p') | |
3064 | set_field_width(&spec, width); | |
fef20d9c FW |
3065 | break; |
3066 | ||
3067 | case FORMAT_TYPE_CHAR: | |
4370aa4a | 3068 | save_arg(char); |
fef20d9c FW |
3069 | break; |
3070 | ||
3071 | case FORMAT_TYPE_STR: { | |
4370aa4a | 3072 | const char *save_str = va_arg(args, char *); |
3e5903eb | 3073 | const char *err_msg; |
4370aa4a | 3074 | size_t len; |
6c356634 | 3075 | |
3e5903eb PM |
3076 | err_msg = check_pointer_msg(save_str); |
3077 | if (err_msg) | |
3078 | save_str = err_msg; | |
3079 | ||
6c356634 AGR |
3080 | len = strlen(save_str) + 1; |
3081 | if (str + len < end) | |
3082 | memcpy(str, save_str, len); | |
3083 | str += len; | |
fef20d9c | 3084 | break; |
4370aa4a | 3085 | } |
fef20d9c FW |
3086 | |
3087 | case FORMAT_TYPE_PTR: | |
841a915d SRV |
3088 | /* Dereferenced pointers must be done now */ |
3089 | switch (*fmt) { | |
3090 | /* Dereference of functions is still OK */ | |
3091 | case 'S': | |
3092 | case 's': | |
1e6338cf SRV |
3093 | case 'x': |
3094 | case 'K': | |
57f5677e | 3095 | case 'e': |
841a915d SRV |
3096 | save_arg(void *); |
3097 | break; | |
3098 | default: | |
3099 | if (!isalnum(*fmt)) { | |
3100 | save_arg(void *); | |
3101 | break; | |
3102 | } | |
3103 | str = pointer(fmt, str, end, va_arg(args, void *), | |
3104 | spec); | |
3105 | if (str + 1 < end) | |
3106 | *str++ = '\0'; | |
3107 | else | |
3108 | end[-1] = '\0'; /* Must be nul terminated */ | |
3109 | } | |
4370aa4a | 3110 | /* skip all alphanumeric pointer suffixes */ |
fef20d9c | 3111 | while (isalnum(*fmt)) |
4370aa4a | 3112 | fmt++; |
fef20d9c FW |
3113 | break; |
3114 | ||
fef20d9c FW |
3115 | default: |
3116 | switch (spec.type) { | |
3117 | ||
3118 | case FORMAT_TYPE_LONG_LONG: | |
4370aa4a | 3119 | save_arg(long long); |
fef20d9c FW |
3120 | break; |
3121 | case FORMAT_TYPE_ULONG: | |
3122 | case FORMAT_TYPE_LONG: | |
4370aa4a | 3123 | save_arg(unsigned long); |
fef20d9c FW |
3124 | break; |
3125 | case FORMAT_TYPE_SIZE_T: | |
4370aa4a | 3126 | save_arg(size_t); |
fef20d9c FW |
3127 | break; |
3128 | case FORMAT_TYPE_PTRDIFF: | |
4370aa4a | 3129 | save_arg(ptrdiff_t); |
fef20d9c | 3130 | break; |
a4e94ef0 Z |
3131 | case FORMAT_TYPE_UBYTE: |
3132 | case FORMAT_TYPE_BYTE: | |
3133 | save_arg(char); | |
3134 | break; | |
fef20d9c FW |
3135 | case FORMAT_TYPE_USHORT: |
3136 | case FORMAT_TYPE_SHORT: | |
4370aa4a | 3137 | save_arg(short); |
fef20d9c FW |
3138 | break; |
3139 | default: | |
4370aa4a | 3140 | save_arg(int); |
fef20d9c | 3141 | } |
4370aa4a LJ |
3142 | } |
3143 | } | |
fef20d9c | 3144 | |
b006f19b | 3145 | out: |
7b9186f5 | 3146 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
fef20d9c | 3147 | #undef save_arg |
4370aa4a LJ |
3148 | } |
3149 | EXPORT_SYMBOL_GPL(vbin_printf); | |
3150 | ||
3151 | /** | |
3152 | * bstr_printf - Format a string from binary arguments and place it in a buffer | |
3153 | * @buf: The buffer to place the result into | |
3154 | * @size: The size of the buffer, including the trailing null space | |
3155 | * @fmt: The format string to use | |
3156 | * @bin_buf: Binary arguments for the format string | |
3157 | * | |
3158 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets | |
3159 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is | |
3160 | * a binary buffer that generated by vbin_printf. | |
3161 | * | |
3162 | * The format follows C99 vsnprintf, but has some extensions: | |
0efb4d20 | 3163 | * see vsnprintf comment for details. |
4370aa4a LJ |
3164 | * |
3165 | * The return value is the number of characters which would | |
3166 | * be generated for the given input, excluding the trailing | |
3167 | * '\0', as per ISO C99. If you want to have the exact | |
3168 | * number of characters written into @buf as return value | |
3169 | * (not including the trailing '\0'), use vscnprintf(). If the | |
3170 | * return is greater than or equal to @size, the resulting | |
3171 | * string is truncated. | |
3172 | */ | |
3173 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) | |
3174 | { | |
fef20d9c | 3175 | struct printf_spec spec = {0}; |
d4be151b AGR |
3176 | char *str, *end; |
3177 | const char *args = (const char *)bin_buf; | |
4370aa4a | 3178 | |
762abb51 | 3179 | if (WARN_ON_ONCE(size > INT_MAX)) |
4370aa4a | 3180 | return 0; |
4370aa4a LJ |
3181 | |
3182 | str = buf; | |
3183 | end = buf + size; | |
3184 | ||
3185 | #define get_arg(type) \ | |
3186 | ({ \ | |
3187 | typeof(type) value; \ | |
3188 | if (sizeof(type) == 8) { \ | |
3189 | args = PTR_ALIGN(args, sizeof(u32)); \ | |
3190 | *(u32 *)&value = *(u32 *)args; \ | |
3191 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ | |
3192 | } else { \ | |
3193 | args = PTR_ALIGN(args, sizeof(type)); \ | |
3194 | value = *(typeof(type) *)args; \ | |
3195 | } \ | |
3196 | args += sizeof(type); \ | |
3197 | value; \ | |
3198 | }) | |
3199 | ||
3200 | /* Make sure end is always >= buf */ | |
3201 | if (end < buf) { | |
3202 | end = ((void *)-1); | |
3203 | size = end - buf; | |
3204 | } | |
3205 | ||
fef20d9c | 3206 | while (*fmt) { |
fef20d9c | 3207 | const char *old_fmt = fmt; |
d4be151b | 3208 | int read = format_decode(fmt, &spec); |
4370aa4a | 3209 | |
fef20d9c | 3210 | fmt += read; |
4370aa4a | 3211 | |
fef20d9c FW |
3212 | switch (spec.type) { |
3213 | case FORMAT_TYPE_NONE: { | |
3214 | int copy = read; | |
3215 | if (str < end) { | |
3216 | if (copy > end - str) | |
3217 | copy = end - str; | |
3218 | memcpy(str, old_fmt, copy); | |
4370aa4a | 3219 | } |
fef20d9c FW |
3220 | str += read; |
3221 | break; | |
4370aa4a LJ |
3222 | } |
3223 | ||
ed681a91 | 3224 | case FORMAT_TYPE_WIDTH: |
4d72ba01 | 3225 | set_field_width(&spec, get_arg(int)); |
fef20d9c | 3226 | break; |
4370aa4a | 3227 | |
fef20d9c | 3228 | case FORMAT_TYPE_PRECISION: |
4d72ba01 | 3229 | set_precision(&spec, get_arg(int)); |
fef20d9c | 3230 | break; |
4370aa4a | 3231 | |
d4be151b AGR |
3232 | case FORMAT_TYPE_CHAR: { |
3233 | char c; | |
3234 | ||
fef20d9c FW |
3235 | if (!(spec.flags & LEFT)) { |
3236 | while (--spec.field_width > 0) { | |
4370aa4a LJ |
3237 | if (str < end) |
3238 | *str = ' '; | |
3239 | ++str; | |
3240 | } | |
3241 | } | |
3242 | c = (unsigned char) get_arg(char); | |
3243 | if (str < end) | |
3244 | *str = c; | |
3245 | ++str; | |
fef20d9c | 3246 | while (--spec.field_width > 0) { |
4370aa4a LJ |
3247 | if (str < end) |
3248 | *str = ' '; | |
3249 | ++str; | |
3250 | } | |
fef20d9c | 3251 | break; |
d4be151b | 3252 | } |
4370aa4a | 3253 | |
fef20d9c | 3254 | case FORMAT_TYPE_STR: { |
4370aa4a | 3255 | const char *str_arg = args; |
d4be151b | 3256 | args += strlen(str_arg) + 1; |
fef20d9c FW |
3257 | str = string(str, end, (char *)str_arg, spec); |
3258 | break; | |
4370aa4a LJ |
3259 | } |
3260 | ||
841a915d SRV |
3261 | case FORMAT_TYPE_PTR: { |
3262 | bool process = false; | |
3263 | int copy, len; | |
3264 | /* Non function dereferences were already done */ | |
3265 | switch (*fmt) { | |
3266 | case 'S': | |
3267 | case 's': | |
1e6338cf SRV |
3268 | case 'x': |
3269 | case 'K': | |
57f5677e | 3270 | case 'e': |
841a915d SRV |
3271 | process = true; |
3272 | break; | |
3273 | default: | |
3274 | if (!isalnum(*fmt)) { | |
3275 | process = true; | |
3276 | break; | |
3277 | } | |
3278 | /* Pointer dereference was already processed */ | |
3279 | if (str < end) { | |
3280 | len = copy = strlen(args); | |
3281 | if (copy > end - str) | |
3282 | copy = end - str; | |
3283 | memcpy(str, args, copy); | |
3284 | str += len; | |
62165600 | 3285 | args += len + 1; |
841a915d SRV |
3286 | } |
3287 | } | |
3288 | if (process) | |
3289 | str = pointer(fmt, str, end, get_arg(void *), spec); | |
3290 | ||
fef20d9c | 3291 | while (isalnum(*fmt)) |
4370aa4a | 3292 | fmt++; |
fef20d9c | 3293 | break; |
841a915d | 3294 | } |
4370aa4a | 3295 | |
fef20d9c | 3296 | case FORMAT_TYPE_PERCENT_CHAR: |
4370aa4a LJ |
3297 | if (str < end) |
3298 | *str = '%'; | |
3299 | ++str; | |
fef20d9c FW |
3300 | break; |
3301 | ||
b006f19b RV |
3302 | case FORMAT_TYPE_INVALID: |
3303 | goto out; | |
3304 | ||
d4be151b AGR |
3305 | default: { |
3306 | unsigned long long num; | |
3307 | ||
fef20d9c FW |
3308 | switch (spec.type) { |
3309 | ||
3310 | case FORMAT_TYPE_LONG_LONG: | |
3311 | num = get_arg(long long); | |
3312 | break; | |
3313 | case FORMAT_TYPE_ULONG: | |
fef20d9c FW |
3314 | case FORMAT_TYPE_LONG: |
3315 | num = get_arg(unsigned long); | |
3316 | break; | |
3317 | case FORMAT_TYPE_SIZE_T: | |
3318 | num = get_arg(size_t); | |
3319 | break; | |
3320 | case FORMAT_TYPE_PTRDIFF: | |
3321 | num = get_arg(ptrdiff_t); | |
3322 | break; | |
a4e94ef0 Z |
3323 | case FORMAT_TYPE_UBYTE: |
3324 | num = get_arg(unsigned char); | |
3325 | break; | |
3326 | case FORMAT_TYPE_BYTE: | |
3327 | num = get_arg(signed char); | |
3328 | break; | |
fef20d9c FW |
3329 | case FORMAT_TYPE_USHORT: |
3330 | num = get_arg(unsigned short); | |
3331 | break; | |
3332 | case FORMAT_TYPE_SHORT: | |
3333 | num = get_arg(short); | |
3334 | break; | |
3335 | case FORMAT_TYPE_UINT: | |
3336 | num = get_arg(unsigned int); | |
3337 | break; | |
3338 | default: | |
3339 | num = get_arg(int); | |
3340 | } | |
3341 | ||
3342 | str = number(str, end, num, spec); | |
d4be151b AGR |
3343 | } /* default: */ |
3344 | } /* switch(spec.type) */ | |
3345 | } /* while(*fmt) */ | |
fef20d9c | 3346 | |
b006f19b | 3347 | out: |
4370aa4a LJ |
3348 | if (size > 0) { |
3349 | if (str < end) | |
3350 | *str = '\0'; | |
3351 | else | |
3352 | end[-1] = '\0'; | |
3353 | } | |
fef20d9c | 3354 | |
4370aa4a LJ |
3355 | #undef get_arg |
3356 | ||
3357 | /* the trailing null byte doesn't count towards the total */ | |
3358 | return str - buf; | |
3359 | } | |
3360 | EXPORT_SYMBOL_GPL(bstr_printf); | |
3361 | ||
3362 | /** | |
3363 | * bprintf - Parse a format string and place args' binary value in a buffer | |
3364 | * @bin_buf: The buffer to place args' binary value | |
3365 | * @size: The size of the buffer(by words(32bits), not characters) | |
3366 | * @fmt: The format string to use | |
3367 | * @...: Arguments for the format string | |
3368 | * | |
3369 | * The function returns the number of words(u32) written | |
3370 | * into @bin_buf. | |
3371 | */ | |
3372 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) | |
3373 | { | |
3374 | va_list args; | |
3375 | int ret; | |
3376 | ||
3377 | va_start(args, fmt); | |
3378 | ret = vbin_printf(bin_buf, size, fmt, args); | |
3379 | va_end(args); | |
7b9186f5 | 3380 | |
4370aa4a LJ |
3381 | return ret; |
3382 | } | |
3383 | EXPORT_SYMBOL_GPL(bprintf); | |
3384 | ||
3385 | #endif /* CONFIG_BINARY_PRINTF */ | |
3386 | ||
1da177e4 LT |
3387 | /** |
3388 | * vsscanf - Unformat a buffer into a list of arguments | |
3389 | * @buf: input buffer | |
3390 | * @fmt: format of buffer | |
3391 | * @args: arguments | |
3392 | */ | |
7b9186f5 | 3393 | int vsscanf(const char *buf, const char *fmt, va_list args) |
1da177e4 LT |
3394 | { |
3395 | const char *str = buf; | |
3396 | char *next; | |
3397 | char digit; | |
3398 | int num = 0; | |
ef0658f3 | 3399 | u8 qualifier; |
53809751 JB |
3400 | unsigned int base; |
3401 | union { | |
3402 | long long s; | |
3403 | unsigned long long u; | |
3404 | } val; | |
ef0658f3 | 3405 | s16 field_width; |
d4be151b | 3406 | bool is_sign; |
1da177e4 | 3407 | |
da99075c | 3408 | while (*fmt) { |
1da177e4 | 3409 | /* skip any white space in format */ |
9dbbc3b9 | 3410 | /* white space in format matches any amount of |
1da177e4 LT |
3411 | * white space, including none, in the input. |
3412 | */ | |
3413 | if (isspace(*fmt)) { | |
e7d2860b AGR |
3414 | fmt = skip_spaces(++fmt); |
3415 | str = skip_spaces(str); | |
1da177e4 LT |
3416 | } |
3417 | ||
3418 | /* anything that is not a conversion must match exactly */ | |
3419 | if (*fmt != '%' && *fmt) { | |
3420 | if (*fmt++ != *str++) | |
3421 | break; | |
3422 | continue; | |
3423 | } | |
3424 | ||
3425 | if (!*fmt) | |
3426 | break; | |
3427 | ++fmt; | |
7b9186f5 | 3428 | |
1da177e4 LT |
3429 | /* skip this conversion. |
3430 | * advance both strings to next white space | |
3431 | */ | |
3432 | if (*fmt == '*') { | |
da99075c JB |
3433 | if (!*str) |
3434 | break; | |
f9310b2f JY |
3435 | while (!isspace(*fmt) && *fmt != '%' && *fmt) { |
3436 | /* '%*[' not yet supported, invalid format */ | |
3437 | if (*fmt == '[') | |
3438 | return num; | |
1da177e4 | 3439 | fmt++; |
f9310b2f | 3440 | } |
1da177e4 LT |
3441 | while (!isspace(*str) && *str) |
3442 | str++; | |
3443 | continue; | |
3444 | } | |
3445 | ||
3446 | /* get field width */ | |
3447 | field_width = -1; | |
53809751 | 3448 | if (isdigit(*fmt)) { |
1da177e4 | 3449 | field_width = skip_atoi(&fmt); |
53809751 JB |
3450 | if (field_width <= 0) |
3451 | break; | |
3452 | } | |
1da177e4 LT |
3453 | |
3454 | /* get conversion qualifier */ | |
3455 | qualifier = -1; | |
75fb8f26 | 3456 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
5b5e0928 | 3457 | *fmt == 'z') { |
1da177e4 LT |
3458 | qualifier = *fmt++; |
3459 | if (unlikely(qualifier == *fmt)) { | |
3460 | if (qualifier == 'h') { | |
3461 | qualifier = 'H'; | |
3462 | fmt++; | |
3463 | } else if (qualifier == 'l') { | |
3464 | qualifier = 'L'; | |
3465 | fmt++; | |
3466 | } | |
3467 | } | |
3468 | } | |
1da177e4 | 3469 | |
da99075c JB |
3470 | if (!*fmt) |
3471 | break; | |
3472 | ||
3473 | if (*fmt == 'n') { | |
3474 | /* return number of characters read so far */ | |
3475 | *va_arg(args, int *) = str - buf; | |
3476 | ++fmt; | |
3477 | continue; | |
3478 | } | |
3479 | ||
3480 | if (!*str) | |
1da177e4 LT |
3481 | break; |
3482 | ||
d4be151b | 3483 | base = 10; |
3f623eba | 3484 | is_sign = false; |
d4be151b | 3485 | |
7b9186f5 | 3486 | switch (*fmt++) { |
1da177e4 LT |
3487 | case 'c': |
3488 | { | |
7b9186f5 | 3489 | char *s = (char *)va_arg(args, char*); |
1da177e4 LT |
3490 | if (field_width == -1) |
3491 | field_width = 1; | |
3492 | do { | |
3493 | *s++ = *str++; | |
3494 | } while (--field_width > 0 && *str); | |
3495 | num++; | |
3496 | } | |
3497 | continue; | |
3498 | case 's': | |
3499 | { | |
7b9186f5 AGR |
3500 | char *s = (char *)va_arg(args, char *); |
3501 | if (field_width == -1) | |
4be929be | 3502 | field_width = SHRT_MAX; |
1da177e4 | 3503 | /* first, skip leading white space in buffer */ |
e7d2860b | 3504 | str = skip_spaces(str); |
1da177e4 LT |
3505 | |
3506 | /* now copy until next white space */ | |
7b9186f5 | 3507 | while (*str && !isspace(*str) && field_width--) |
1da177e4 | 3508 | *s++ = *str++; |
1da177e4 LT |
3509 | *s = '\0'; |
3510 | num++; | |
3511 | } | |
3512 | continue; | |
f9310b2f JY |
3513 | /* |
3514 | * Warning: This implementation of the '[' conversion specifier | |
3515 | * deviates from its glibc counterpart in the following ways: | |
3516 | * (1) It does NOT support ranges i.e. '-' is NOT a special | |
3517 | * character | |
3518 | * (2) It cannot match the closing bracket ']' itself | |
3519 | * (3) A field width is required | |
3520 | * (4) '%*[' (discard matching input) is currently not supported | |
3521 | * | |
3522 | * Example usage: | |
3523 | * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", | |
3524 | * buf1, buf2, buf3); | |
3525 | * if (ret < 3) | |
3526 | * // etc.. | |
3527 | */ | |
3528 | case '[': | |
3529 | { | |
3530 | char *s = (char *)va_arg(args, char *); | |
3531 | DECLARE_BITMAP(set, 256) = {0}; | |
3532 | unsigned int len = 0; | |
3533 | bool negate = (*fmt == '^'); | |
3534 | ||
3535 | /* field width is required */ | |
3536 | if (field_width == -1) | |
3537 | return num; | |
3538 | ||
3539 | if (negate) | |
3540 | ++fmt; | |
3541 | ||
3542 | for ( ; *fmt && *fmt != ']'; ++fmt, ++len) | |
52e68cd6 | 3543 | __set_bit((u8)*fmt, set); |
f9310b2f JY |
3544 | |
3545 | /* no ']' or no character set found */ | |
3546 | if (!*fmt || !len) | |
3547 | return num; | |
3548 | ++fmt; | |
3549 | ||
3550 | if (negate) { | |
3551 | bitmap_complement(set, set, 256); | |
3552 | /* exclude null '\0' byte */ | |
52e68cd6 | 3553 | __clear_bit(0, set); |
f9310b2f JY |
3554 | } |
3555 | ||
3556 | /* match must be non-empty */ | |
3557 | if (!test_bit((u8)*str, set)) | |
3558 | return num; | |
3559 | ||
3560 | while (test_bit((u8)*str, set) && field_width--) | |
3561 | *s++ = *str++; | |
3562 | *s = '\0'; | |
3563 | ++num; | |
3564 | } | |
3565 | continue; | |
1da177e4 LT |
3566 | case 'o': |
3567 | base = 8; | |
3568 | break; | |
3569 | case 'x': | |
3570 | case 'X': | |
3571 | base = 16; | |
3572 | break; | |
3573 | case 'i': | |
7b9186f5 | 3574 | base = 0; |
4c1ca831 | 3575 | fallthrough; |
1da177e4 | 3576 | case 'd': |
3f623eba | 3577 | is_sign = true; |
4c1ca831 | 3578 | fallthrough; |
1da177e4 LT |
3579 | case 'u': |
3580 | break; | |
3581 | case '%': | |
3582 | /* looking for '%' in str */ | |
7b9186f5 | 3583 | if (*str++ != '%') |
1da177e4 LT |
3584 | return num; |
3585 | continue; | |
3586 | default: | |
3587 | /* invalid format; stop here */ | |
3588 | return num; | |
3589 | } | |
3590 | ||
3591 | /* have some sort of integer conversion. | |
3592 | * first, skip white space in buffer. | |
3593 | */ | |
e7d2860b | 3594 | str = skip_spaces(str); |
1da177e4 LT |
3595 | |
3596 | digit = *str; | |
11b3dda5 RF |
3597 | if (is_sign && digit == '-') { |
3598 | if (field_width == 1) | |
3599 | break; | |
3600 | ||
1da177e4 | 3601 | digit = *(str + 1); |
11b3dda5 | 3602 | } |
1da177e4 LT |
3603 | |
3604 | if (!digit | |
7b9186f5 AGR |
3605 | || (base == 16 && !isxdigit(digit)) |
3606 | || (base == 10 && !isdigit(digit)) | |
3607 | || (base == 8 && (!isdigit(digit) || digit > '7')) | |
3608 | || (base == 0 && !isdigit(digit))) | |
3609 | break; | |
1da177e4 | 3610 | |
53809751 | 3611 | if (is_sign) |
900fdc45 RF |
3612 | val.s = simple_strntoll(str, |
3613 | field_width >= 0 ? field_width : INT_MAX, | |
3614 | &next, base); | |
53809751 | 3615 | else |
900fdc45 RF |
3616 | val.u = simple_strntoull(str, |
3617 | field_width >= 0 ? field_width : INT_MAX, | |
3618 | &next, base); | |
53809751 | 3619 | |
7b9186f5 | 3620 | switch (qualifier) { |
1da177e4 | 3621 | case 'H': /* that's 'hh' in format */ |
53809751 JB |
3622 | if (is_sign) |
3623 | *va_arg(args, signed char *) = val.s; | |
3624 | else | |
3625 | *va_arg(args, unsigned char *) = val.u; | |
1da177e4 LT |
3626 | break; |
3627 | case 'h': | |
53809751 JB |
3628 | if (is_sign) |
3629 | *va_arg(args, short *) = val.s; | |
3630 | else | |
3631 | *va_arg(args, unsigned short *) = val.u; | |
1da177e4 LT |
3632 | break; |
3633 | case 'l': | |
53809751 JB |
3634 | if (is_sign) |
3635 | *va_arg(args, long *) = val.s; | |
3636 | else | |
3637 | *va_arg(args, unsigned long *) = val.u; | |
1da177e4 LT |
3638 | break; |
3639 | case 'L': | |
53809751 JB |
3640 | if (is_sign) |
3641 | *va_arg(args, long long *) = val.s; | |
3642 | else | |
3643 | *va_arg(args, unsigned long long *) = val.u; | |
1da177e4 | 3644 | break; |
1da177e4 | 3645 | case 'z': |
53809751 JB |
3646 | *va_arg(args, size_t *) = val.u; |
3647 | break; | |
1da177e4 | 3648 | default: |
53809751 JB |
3649 | if (is_sign) |
3650 | *va_arg(args, int *) = val.s; | |
3651 | else | |
3652 | *va_arg(args, unsigned int *) = val.u; | |
1da177e4 LT |
3653 | break; |
3654 | } | |
3655 | num++; | |
3656 | ||
3657 | if (!next) | |
3658 | break; | |
3659 | str = next; | |
3660 | } | |
c6b40d16 | 3661 | |
1da177e4 LT |
3662 | return num; |
3663 | } | |
1da177e4 LT |
3664 | EXPORT_SYMBOL(vsscanf); |
3665 | ||
3666 | /** | |
3667 | * sscanf - Unformat a buffer into a list of arguments | |
3668 | * @buf: input buffer | |
3669 | * @fmt: formatting of buffer | |
3670 | * @...: resulting arguments | |
3671 | */ | |
7b9186f5 | 3672 | int sscanf(const char *buf, const char *fmt, ...) |
1da177e4 LT |
3673 | { |
3674 | va_list args; | |
3675 | int i; | |
3676 | ||
7b9186f5 AGR |
3677 | va_start(args, fmt); |
3678 | i = vsscanf(buf, fmt, args); | |
1da177e4 | 3679 | va_end(args); |
7b9186f5 | 3680 | |
1da177e4 LT |
3681 | return i; |
3682 | } | |
1da177e4 | 3683 | EXPORT_SYMBOL(sscanf); |