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