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