11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/lib/vsprintf.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Wirzenius wrote this portably, Torvalds fucked it up :-) 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 141da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions 151da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 161da177e4SLinus Torvalds * - scnprintf and vscnprintf 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <stdarg.h> 208bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 211da177e4SLinus Torvalds #include <linux/types.h> 221da177e4SLinus Torvalds #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/ctype.h> 241da177e4SLinus Torvalds #include <linux/kernel.h> 250fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 2653809751SJan Beulich #include <linux/math64.h> 270fe1ef24SLinus Torvalds #include <linux/uaccess.h> 28332d2e78SLinus Torvalds #include <linux/ioport.h> 294b6ccca7SAl Viro #include <linux/dcache.h> 30*312b4e22SRyan Mallon #include <linux/cred.h> 318a27f7c9SJoe Perches #include <net/addrconf.h> 321da177e4SLinus Torvalds 334e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 34deac93dfSJames Bottomley #include <asm/sections.h> /* for dereference_function_descriptor() */ 351da177e4SLinus Torvalds 361dff46d6SAlexey Dobriyan #include "kstrtox.h" 37aa46a63eSHarvey Harrison 381da177e4SLinus Torvalds /** 391da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 401da177e4SLinus Torvalds * @cp: The start of the string 411da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 421da177e4SLinus Torvalds * @base: The number base to use 43462e4711SEldad Zack * 44462e4711SEldad Zack * This function is obsolete. Please use kstrtoull instead. 451da177e4SLinus Torvalds */ 461da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 471da177e4SLinus Torvalds { 481dff46d6SAlexey Dobriyan unsigned long long result; 491dff46d6SAlexey Dobriyan unsigned int rv; 501da177e4SLinus Torvalds 511dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 521dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 531dff46d6SAlexey Dobriyan /* FIXME */ 541dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 55aa46a63eSHarvey Harrison 561da177e4SLinus Torvalds if (endp) 571da177e4SLinus Torvalds *endp = (char *)cp; 587b9186f5SAndré Goddard Rosa 591da177e4SLinus Torvalds return result; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /** 64922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 65922ac25cSAndré Goddard Rosa * @cp: The start of the string 66922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 67922ac25cSAndré Goddard Rosa * @base: The number base to use 68462e4711SEldad Zack * 69462e4711SEldad Zack * This function is obsolete. Please use kstrtoul instead. 70922ac25cSAndré Goddard Rosa */ 71922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 72922ac25cSAndré Goddard Rosa { 73922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 74922ac25cSAndré Goddard Rosa } 75922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 76922ac25cSAndré Goddard Rosa 77922ac25cSAndré Goddard Rosa /** 78922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 79922ac25cSAndré Goddard Rosa * @cp: The start of the string 80922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 81922ac25cSAndré Goddard Rosa * @base: The number base to use 82462e4711SEldad Zack * 83462e4711SEldad Zack * This function is obsolete. Please use kstrtol instead. 84922ac25cSAndré Goddard Rosa */ 85922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 86922ac25cSAndré Goddard Rosa { 87922ac25cSAndré Goddard Rosa if (*cp == '-') 88922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 89922ac25cSAndré Goddard Rosa 90922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 91922ac25cSAndré Goddard Rosa } 92922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 93922ac25cSAndré Goddard Rosa 94922ac25cSAndré Goddard Rosa /** 951da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 961da177e4SLinus Torvalds * @cp: The start of the string 971da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 981da177e4SLinus Torvalds * @base: The number base to use 99462e4711SEldad Zack * 100462e4711SEldad Zack * This function is obsolete. Please use kstrtoll instead. 1011da177e4SLinus Torvalds */ 1021da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds if (*cp == '-') 1051da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1067b9186f5SAndré Goddard Rosa 1071da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1081da177e4SLinus Torvalds } 10998d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1101da177e4SLinus Torvalds 111cf3b429bSJoe Perches static noinline_for_stack 112cf3b429bSJoe Perches int skip_atoi(const char **s) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds int i = 0; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds while (isdigit(**s)) 1171da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 1187b9186f5SAndré Goddard Rosa 1191da177e4SLinus Torvalds return i; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1224277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used 1234277eeddSDenis Vlasenko * for /proc and /sys data. This directly impacts e.g. top performance 1244277eeddSDenis Vlasenko * with many processes running. We optimize it for speed 125133fd9f5SDenys Vlasenko * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html> 126133fd9f5SDenys Vlasenko * (with permission from the author, Douglas W. Jones). 127133fd9f5SDenys Vlasenko */ 1284277eeddSDenis Vlasenko 129133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 130133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */ 131cf3b429bSJoe Perches static noinline_for_stack 132133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q) 1334277eeddSDenis Vlasenko { 134133fd9f5SDenys Vlasenko unsigned r; 1354277eeddSDenis Vlasenko 1367b9186f5SAndré Goddard Rosa /* 1377b9186f5SAndré Goddard Rosa * Possible ways to approx. divide by 10 138133fd9f5SDenys Vlasenko * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) 139133fd9f5SDenys Vlasenko * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) 140133fd9f5SDenys Vlasenko * (x * 0x6667) >> 18 x < 43699 141133fd9f5SDenys Vlasenko * (x * 0x3334) >> 17 x < 16389 142133fd9f5SDenys Vlasenko * (x * 0x199a) >> 16 x < 16389 143133fd9f5SDenys Vlasenko * (x * 0x0ccd) >> 15 x < 16389 144133fd9f5SDenys Vlasenko * (x * 0x0667) >> 14 x < 2739 145133fd9f5SDenys Vlasenko * (x * 0x0334) >> 13 x < 1029 146133fd9f5SDenys Vlasenko * (x * 0x019a) >> 12 x < 1029 147133fd9f5SDenys Vlasenko * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) 148133fd9f5SDenys Vlasenko * (x * 0x0067) >> 10 x < 179 149133fd9f5SDenys Vlasenko * (x * 0x0034) >> 9 x < 69 same 150133fd9f5SDenys Vlasenko * (x * 0x001a) >> 8 x < 69 same 151133fd9f5SDenys Vlasenko * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) 152133fd9f5SDenys Vlasenko * (x * 0x0007) >> 6 x < 19 153133fd9f5SDenys Vlasenko * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html> 1547b9186f5SAndré Goddard Rosa */ 155133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 156133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 1 */ 157133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 158133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 2 */ 159133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 160133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 3 */ 161133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 162133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 4 */ 163133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 164133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 5 */ 165133fd9f5SDenys Vlasenko /* Now value is under 10000, can avoid 64-bit multiply */ 166133fd9f5SDenys Vlasenko q = (r * 0x199a) >> 16; 167133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 6 */ 168133fd9f5SDenys Vlasenko r = (q * 0xcd) >> 11; 169133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 7 */ 170133fd9f5SDenys Vlasenko q = (r * 0xcd) >> 11; 171133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 8 */ 172133fd9f5SDenys Vlasenko *buf++ = q + '0'; /* 9 */ 173133fd9f5SDenys Vlasenko return buf; 174133fd9f5SDenys Vlasenko } 175133fd9f5SDenys Vlasenko #endif 1764277eeddSDenis Vlasenko 177133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros. 178133fd9f5SDenys Vlasenko * Code can be easily arranged to print 9 digits too, but our callers 179133fd9f5SDenys Vlasenko * always call put_dec_full9() instead when the number has 9 decimal digits. 180133fd9f5SDenys Vlasenko */ 181133fd9f5SDenys Vlasenko static noinline_for_stack 182133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 183133fd9f5SDenys Vlasenko { 184133fd9f5SDenys Vlasenko unsigned q; 1854277eeddSDenis Vlasenko 186133fd9f5SDenys Vlasenko /* Copy of previous function's body with added early returns */ 187cb239d0aSGeorge Spelvin while (r >= 10000) { 188cb239d0aSGeorge Spelvin q = r + '0'; 189cb239d0aSGeorge Spelvin r = (r * (uint64_t)0x1999999a) >> 32; 190cb239d0aSGeorge Spelvin *buf++ = q - 10*r; 191cb239d0aSGeorge Spelvin } 192cb239d0aSGeorge Spelvin 193f4000516SGeorge Spelvin q = (r * 0x199a) >> 16; /* r <= 9999 */ 194f4000516SGeorge Spelvin *buf++ = (r - 10 * q) + '0'; 195133fd9f5SDenys Vlasenko if (q == 0) 196133fd9f5SDenys Vlasenko return buf; 197f4000516SGeorge Spelvin r = (q * 0xcd) >> 11; /* q <= 999 */ 198f4000516SGeorge Spelvin *buf++ = (q - 10 * r) + '0'; 199133fd9f5SDenys Vlasenko if (r == 0) 200133fd9f5SDenys Vlasenko return buf; 201f4000516SGeorge Spelvin q = (r * 0xcd) >> 11; /* r <= 99 */ 202f4000516SGeorge Spelvin *buf++ = (r - 10 * q) + '0'; 203133fd9f5SDenys Vlasenko if (q == 0) 204133fd9f5SDenys Vlasenko return buf; 205f4000516SGeorge Spelvin *buf++ = q + '0'; /* q <= 9 */ 206133fd9f5SDenys Vlasenko return buf; 207133fd9f5SDenys Vlasenko } 208133fd9f5SDenys Vlasenko 209133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers. 210133fd9f5SDenys Vlasenko * One is generic: divide by 1000000000 and repeatedly print 211133fd9f5SDenys Vlasenko * groups of (up to) 9 digits. It's conceptually simple, 212133fd9f5SDenys Vlasenko * but requires a (unsigned long long) / 1000000000 division. 213133fd9f5SDenys Vlasenko * 214133fd9f5SDenys Vlasenko * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, 215133fd9f5SDenys Vlasenko * manipulates them cleverly and generates groups of 4 decimal digits. 216133fd9f5SDenys Vlasenko * It so happens that it does NOT require long long division. 217133fd9f5SDenys Vlasenko * 218133fd9f5SDenys Vlasenko * If long is > 32 bits, division of 64-bit values is relatively easy, 219133fd9f5SDenys Vlasenko * and we will use the first algorithm. 220133fd9f5SDenys Vlasenko * If long long is > 64 bits (strange architecture with VERY large long long), 221133fd9f5SDenys Vlasenko * second algorithm can't be used, and we again use the first one. 222133fd9f5SDenys Vlasenko * 223133fd9f5SDenys Vlasenko * Else (if long is 32 bits and long long is 64 bits) we use second one. 224133fd9f5SDenys Vlasenko */ 225133fd9f5SDenys Vlasenko 226133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 227133fd9f5SDenys Vlasenko 228133fd9f5SDenys Vlasenko /* First algorithm: generic */ 229133fd9f5SDenys Vlasenko 230133fd9f5SDenys Vlasenko static 231133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 232133fd9f5SDenys Vlasenko { 233133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) { 234133fd9f5SDenys Vlasenko while (n >= 1000*1000*1000) 235133fd9f5SDenys Vlasenko buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); 236133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 237133fd9f5SDenys Vlasenko return put_dec_full9(buf, n); 238133fd9f5SDenys Vlasenko } 239133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 240133fd9f5SDenys Vlasenko } 241133fd9f5SDenys Vlasenko 242133fd9f5SDenys Vlasenko #else 243133fd9f5SDenys Vlasenko 244133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */ 245133fd9f5SDenys Vlasenko 246e49317d4SGeorge Spelvin /* See comment in put_dec_full9 for choice of constants */ 247133fd9f5SDenys Vlasenko static noinline_for_stack 2482359172aSGeorge Spelvin void put_dec_full4(char *buf, unsigned q) 249133fd9f5SDenys Vlasenko { 250133fd9f5SDenys Vlasenko unsigned r; 251e49317d4SGeorge Spelvin r = (q * 0xccd) >> 15; 2522359172aSGeorge Spelvin buf[0] = (q - 10 * r) + '0'; 253e49317d4SGeorge Spelvin q = (r * 0xcd) >> 11; 2542359172aSGeorge Spelvin buf[1] = (r - 10 * q) + '0'; 255133fd9f5SDenys Vlasenko r = (q * 0xcd) >> 11; 2562359172aSGeorge Spelvin buf[2] = (q - 10 * r) + '0'; 2572359172aSGeorge Spelvin buf[3] = r + '0'; 2582359172aSGeorge Spelvin } 2592359172aSGeorge Spelvin 2602359172aSGeorge Spelvin /* 2612359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2622359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2632359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2642359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2652359172aSGeorge Spelvin * (d1 in the put_dec code, assuming n is all-ones). 2662359172aSGeorge Spelvin */ 2672359172aSGeorge Spelvin static 2682359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2692359172aSGeorge Spelvin { 2702359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2712359172aSGeorge Spelvin 2722359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2732359172aSGeorge Spelvin return q; 274133fd9f5SDenys Vlasenko } 275133fd9f5SDenys Vlasenko 276133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 277133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 278133fd9f5SDenys Vlasenko * (with permission from the author). 279133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 280133fd9f5SDenys Vlasenko */ 281133fd9f5SDenys Vlasenko static 282133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 283133fd9f5SDenys Vlasenko { 284133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 285133fd9f5SDenys Vlasenko 286133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 287133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 288133fd9f5SDenys Vlasenko 289133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 290133fd9f5SDenys Vlasenko h = (n >> 32); 291133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 292133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 293133fd9f5SDenys Vlasenko 294133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 2952359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 296133fd9f5SDenys Vlasenko 2972359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 2982359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 299133fd9f5SDenys Vlasenko 3002359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3012359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 302133fd9f5SDenys Vlasenko 3032359172aSGeorge Spelvin q += 281 * d3; 3042359172aSGeorge Spelvin buf += 12; 3052359172aSGeorge Spelvin if (q) 3062359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3072359172aSGeorge Spelvin else while (buf[-1] == '0') 308133fd9f5SDenys Vlasenko --buf; 3097b9186f5SAndré Goddard Rosa 3104277eeddSDenis Vlasenko return buf; 3114277eeddSDenis Vlasenko } 312133fd9f5SDenys Vlasenko 313133fd9f5SDenys Vlasenko #endif 3144277eeddSDenis Vlasenko 3151ac101a5SKAMEZAWA Hiroyuki /* 3161ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3171ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3181ac101a5SKAMEZAWA Hiroyuki * 3191ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3201ac101a5SKAMEZAWA Hiroyuki */ 3211ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num) 3221ac101a5SKAMEZAWA Hiroyuki { 323133fd9f5SDenys Vlasenko char tmp[sizeof(num) * 3]; 3241ac101a5SKAMEZAWA Hiroyuki int idx, len; 3251ac101a5SKAMEZAWA Hiroyuki 326133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 327133fd9f5SDenys Vlasenko if (num <= 9) { 328133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 329133fd9f5SDenys Vlasenko len = 1; 330133fd9f5SDenys Vlasenko } else { 3311ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 332133fd9f5SDenys Vlasenko } 3331ac101a5SKAMEZAWA Hiroyuki 3341ac101a5SKAMEZAWA Hiroyuki if (len > size) 3351ac101a5SKAMEZAWA Hiroyuki return 0; 3361ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 3371ac101a5SKAMEZAWA Hiroyuki buf[idx] = tmp[len - idx - 1]; 3381ac101a5SKAMEZAWA Hiroyuki return len; 3391ac101a5SKAMEZAWA Hiroyuki } 3401ac101a5SKAMEZAWA Hiroyuki 3411da177e4SLinus Torvalds #define ZEROPAD 1 /* pad with zero */ 3421da177e4SLinus Torvalds #define SIGN 2 /* unsigned/signed long */ 3431da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3441da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 3451da177e4SLinus Torvalds #define LEFT 16 /* left justified */ 346b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 347b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3481da177e4SLinus Torvalds 349fef20d9cSFrederic Weisbecker enum format_type { 350fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 351ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 352fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 353fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 354fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 355fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 356fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 357fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 358fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 359fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 360fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 361a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 362a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 363fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 364fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 365fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 366fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 367fef20d9cSFrederic Weisbecker FORMAT_TYPE_NRCHARS, 368fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 369fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 370fef20d9cSFrederic Weisbecker }; 371fef20d9cSFrederic Weisbecker 372fef20d9cSFrederic Weisbecker struct printf_spec { 3734e310fdaSJoe Perches u8 type; /* format_type enum */ 374ef0658f3SJoe Perches u8 flags; /* flags to number() */ 3754e310fdaSJoe Perches u8 base; /* number base, 8, 10 or 16 only */ 3764e310fdaSJoe Perches u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ 3774e310fdaSJoe Perches s16 field_width; /* width of output field */ 3784e310fdaSJoe Perches s16 precision; /* # of digits/chars */ 379fef20d9cSFrederic Weisbecker }; 380fef20d9cSFrederic Weisbecker 381cf3b429bSJoe Perches static noinline_for_stack 382cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 383fef20d9cSFrederic Weisbecker struct printf_spec spec) 3841da177e4SLinus Torvalds { 3859b706aeeSDenys Vlasenko /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ 3869b706aeeSDenys Vlasenko static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ 3879b706aeeSDenys Vlasenko 3889b706aeeSDenys Vlasenko char tmp[66]; 3899b706aeeSDenys Vlasenko char sign; 3909b706aeeSDenys Vlasenko char locase; 391fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 3921da177e4SLinus Torvalds int i; 3937c203422SPierre Carrier bool is_zero = num == 0LL; 3941da177e4SLinus Torvalds 3959b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 3969b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 397fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 398fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 399fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4001da177e4SLinus Torvalds sign = 0; 401fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4021da177e4SLinus Torvalds if ((signed long long)num < 0) { 4031da177e4SLinus Torvalds sign = '-'; 4041da177e4SLinus Torvalds num = -(signed long long)num; 405fef20d9cSFrederic Weisbecker spec.field_width--; 406fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4071da177e4SLinus Torvalds sign = '+'; 408fef20d9cSFrederic Weisbecker spec.field_width--; 409fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4101da177e4SLinus Torvalds sign = ' '; 411fef20d9cSFrederic Weisbecker spec.field_width--; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds } 414b39a7340SDenis Vlasenko if (need_pfx) { 415fef20d9cSFrederic Weisbecker if (spec.base == 16) 4167c203422SPierre Carrier spec.field_width -= 2; 4177c203422SPierre Carrier else if (!is_zero) 418fef20d9cSFrederic Weisbecker spec.field_width--; 4191da177e4SLinus Torvalds } 420b39a7340SDenis Vlasenko 421b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4221da177e4SLinus Torvalds i = 0; 423133fd9f5SDenys Vlasenko if (num < spec.base) 424133fd9f5SDenys Vlasenko tmp[i++] = digits[num] | locase; 4254277eeddSDenis Vlasenko /* Generic code, for any base: 4264277eeddSDenis Vlasenko else do { 4279b706aeeSDenys Vlasenko tmp[i++] = (digits[do_div(num,base)] | locase); 4284277eeddSDenis Vlasenko } while (num != 0); 4294277eeddSDenis Vlasenko */ 430fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 431fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 432b39a7340SDenis Vlasenko int shift = 3; 4337b9186f5SAndré Goddard Rosa 4347b9186f5SAndré Goddard Rosa if (spec.base == 16) 4357b9186f5SAndré Goddard Rosa shift = 4; 436b39a7340SDenis Vlasenko do { 4379b706aeeSDenys Vlasenko tmp[i++] = (digits[((unsigned char)num) & mask] | locase); 438b39a7340SDenis Vlasenko num >>= shift; 439b39a7340SDenis Vlasenko } while (num); 4404277eeddSDenis Vlasenko } else { /* base 10 */ 4414277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4424277eeddSDenis Vlasenko } 443b39a7340SDenis Vlasenko 444b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 445fef20d9cSFrederic Weisbecker if (i > spec.precision) 446fef20d9cSFrederic Weisbecker spec.precision = i; 447b39a7340SDenis Vlasenko /* leading space padding */ 448fef20d9cSFrederic Weisbecker spec.field_width -= spec.precision; 449fef20d9cSFrederic Weisbecker if (!(spec.flags & (ZEROPAD+LEFT))) { 450fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 451f796937aSJeremy Fitzhardinge if (buf < end) 4521da177e4SLinus Torvalds *buf = ' '; 4531da177e4SLinus Torvalds ++buf; 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds } 456b39a7340SDenis Vlasenko /* sign */ 4571da177e4SLinus Torvalds if (sign) { 458f796937aSJeremy Fitzhardinge if (buf < end) 4591da177e4SLinus Torvalds *buf = sign; 4601da177e4SLinus Torvalds ++buf; 4611da177e4SLinus Torvalds } 462b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 463b39a7340SDenis Vlasenko if (need_pfx) { 4647c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 465f796937aSJeremy Fitzhardinge if (buf < end) 4661da177e4SLinus Torvalds *buf = '0'; 4671da177e4SLinus Torvalds ++buf; 4687c203422SPierre Carrier } 469fef20d9cSFrederic Weisbecker if (spec.base == 16) { 470f796937aSJeremy Fitzhardinge if (buf < end) 4719b706aeeSDenys Vlasenko *buf = ('X' | locase); 4721da177e4SLinus Torvalds ++buf; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds } 475b39a7340SDenis Vlasenko /* zero or space padding */ 476fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 477fef20d9cSFrederic Weisbecker char c = (spec.flags & ZEROPAD) ? '0' : ' '; 478fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 479f796937aSJeremy Fitzhardinge if (buf < end) 4801da177e4SLinus Torvalds *buf = c; 4811da177e4SLinus Torvalds ++buf; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds } 484b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 485fef20d9cSFrederic Weisbecker while (i <= --spec.precision) { 486f796937aSJeremy Fitzhardinge if (buf < end) 4871da177e4SLinus Torvalds *buf = '0'; 4881da177e4SLinus Torvalds ++buf; 4891da177e4SLinus Torvalds } 490b39a7340SDenis Vlasenko /* actual digits of result */ 491b39a7340SDenis Vlasenko while (--i >= 0) { 492f796937aSJeremy Fitzhardinge if (buf < end) 4931da177e4SLinus Torvalds *buf = tmp[i]; 4941da177e4SLinus Torvalds ++buf; 4951da177e4SLinus Torvalds } 496b39a7340SDenis Vlasenko /* trailing space padding */ 497fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 498f796937aSJeremy Fitzhardinge if (buf < end) 4991da177e4SLinus Torvalds *buf = ' '; 5001da177e4SLinus Torvalds ++buf; 5011da177e4SLinus Torvalds } 5027b9186f5SAndré Goddard Rosa 5031da177e4SLinus Torvalds return buf; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 506cf3b429bSJoe Perches static noinline_for_stack 507cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec) 5080f9bfa56SLinus Torvalds { 5090f9bfa56SLinus Torvalds int len, i; 5100f9bfa56SLinus Torvalds 5110f9bfa56SLinus Torvalds if ((unsigned long)s < PAGE_SIZE) 5120f4f81dcSAndré Goddard Rosa s = "(null)"; 5130f9bfa56SLinus Torvalds 514fef20d9cSFrederic Weisbecker len = strnlen(s, spec.precision); 5150f9bfa56SLinus Torvalds 516fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 517fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5180f9bfa56SLinus Torvalds if (buf < end) 5190f9bfa56SLinus Torvalds *buf = ' '; 5200f9bfa56SLinus Torvalds ++buf; 5210f9bfa56SLinus Torvalds } 5220f9bfa56SLinus Torvalds } 5230f9bfa56SLinus Torvalds for (i = 0; i < len; ++i) { 5240f9bfa56SLinus Torvalds if (buf < end) 5250f9bfa56SLinus Torvalds *buf = *s; 5260f9bfa56SLinus Torvalds ++buf; ++s; 5270f9bfa56SLinus Torvalds } 528fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5290f9bfa56SLinus Torvalds if (buf < end) 5300f9bfa56SLinus Torvalds *buf = ' '; 5310f9bfa56SLinus Torvalds ++buf; 5320f9bfa56SLinus Torvalds } 5337b9186f5SAndré Goddard Rosa 5340f9bfa56SLinus Torvalds return buf; 5350f9bfa56SLinus Torvalds } 5360f9bfa56SLinus Torvalds 5374b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces) 5384b6ccca7SAl Viro { 5394b6ccca7SAl Viro size_t size; 5404b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5414b6ccca7SAl Viro return; 5424b6ccca7SAl Viro size = end - buf; 5434b6ccca7SAl Viro if (size <= spaces) { 5444b6ccca7SAl Viro memset(buf, ' ', size); 5454b6ccca7SAl Viro return; 5464b6ccca7SAl Viro } 5474b6ccca7SAl Viro if (len) { 5484b6ccca7SAl Viro if (len > size - spaces) 5494b6ccca7SAl Viro len = size - spaces; 5504b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5514b6ccca7SAl Viro } 5524b6ccca7SAl Viro memset(buf, ' ', spaces); 5534b6ccca7SAl Viro } 5544b6ccca7SAl Viro 5554b6ccca7SAl Viro static noinline_for_stack 5564b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 5574b6ccca7SAl Viro const char *fmt) 5584b6ccca7SAl Viro { 5594b6ccca7SAl Viro const char *array[4], *s; 5604b6ccca7SAl Viro const struct dentry *p; 5614b6ccca7SAl Viro int depth; 5624b6ccca7SAl Viro int i, n; 5634b6ccca7SAl Viro 5644b6ccca7SAl Viro switch (fmt[1]) { 5654b6ccca7SAl Viro case '2': case '3': case '4': 5664b6ccca7SAl Viro depth = fmt[1] - '0'; 5674b6ccca7SAl Viro break; 5684b6ccca7SAl Viro default: 5694b6ccca7SAl Viro depth = 1; 5704b6ccca7SAl Viro } 5714b6ccca7SAl Viro 5724b6ccca7SAl Viro rcu_read_lock(); 5734b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 5744b6ccca7SAl Viro p = ACCESS_ONCE(d->d_parent); 5754b6ccca7SAl Viro array[i] = ACCESS_ONCE(d->d_name.name); 5764b6ccca7SAl Viro if (p == d) { 5774b6ccca7SAl Viro if (i) 5784b6ccca7SAl Viro array[i] = ""; 5794b6ccca7SAl Viro i++; 5804b6ccca7SAl Viro break; 5814b6ccca7SAl Viro } 5824b6ccca7SAl Viro } 5834b6ccca7SAl Viro s = array[--i]; 5844b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 5854b6ccca7SAl Viro char c = *s++; 5864b6ccca7SAl Viro if (!c) { 5874b6ccca7SAl Viro if (!i) 5884b6ccca7SAl Viro break; 5894b6ccca7SAl Viro c = '/'; 5904b6ccca7SAl Viro s = array[--i]; 5914b6ccca7SAl Viro } 5924b6ccca7SAl Viro if (buf < end) 5934b6ccca7SAl Viro *buf = c; 5944b6ccca7SAl Viro } 5954b6ccca7SAl Viro rcu_read_unlock(); 5964b6ccca7SAl Viro if (n < spec.field_width) { 5974b6ccca7SAl Viro /* we want to pad the sucker */ 5984b6ccca7SAl Viro unsigned spaces = spec.field_width - n; 5994b6ccca7SAl Viro if (!(spec.flags & LEFT)) { 6004b6ccca7SAl Viro widen(buf - n, end, n, spaces); 6014b6ccca7SAl Viro return buf + spaces; 6024b6ccca7SAl Viro } 6034b6ccca7SAl Viro while (spaces--) { 6044b6ccca7SAl Viro if (buf < end) 6054b6ccca7SAl Viro *buf = ' '; 6064b6ccca7SAl Viro ++buf; 6074b6ccca7SAl Viro } 6084b6ccca7SAl Viro } 6094b6ccca7SAl Viro return buf; 6104b6ccca7SAl Viro } 6114b6ccca7SAl Viro 612cf3b429bSJoe Perches static noinline_for_stack 613cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 614b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 6150fe1ef24SLinus Torvalds { 616b0d33c2bSJoe Perches unsigned long value; 6170fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 6180fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 619b0d33c2bSJoe Perches #endif 620b0d33c2bSJoe Perches 621b0d33c2bSJoe Perches if (fmt[1] == 'R') 622b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 623b0d33c2bSJoe Perches value = (unsigned long)ptr; 624b0d33c2bSJoe Perches 625b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 626b0d33c2bSJoe Perches if (*fmt == 'B') 6270f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 628b0d33c2bSJoe Perches else if (*fmt != 'f' && *fmt != 's') 6290fe1ef24SLinus Torvalds sprint_symbol(sym, value); 6300c8b946eSFrederic Weisbecker else 6314796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 6327b9186f5SAndré Goddard Rosa 633fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 6340fe1ef24SLinus Torvalds #else 635fef20d9cSFrederic Weisbecker spec.field_width = 2 * sizeof(void *); 636fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL | SMALL | ZEROPAD; 637fef20d9cSFrederic Weisbecker spec.base = 16; 6387b9186f5SAndré Goddard Rosa 639fef20d9cSFrederic Weisbecker return number(buf, end, value, spec); 6400fe1ef24SLinus Torvalds #endif 6410fe1ef24SLinus Torvalds } 6420fe1ef24SLinus Torvalds 643cf3b429bSJoe Perches static noinline_for_stack 644cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 645fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 646332d2e78SLinus Torvalds { 647332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 64828405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 649332d2e78SLinus Torvalds #endif 650332d2e78SLinus Torvalds 651332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 65228405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 653332d2e78SLinus Torvalds #endif 6544da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 655fef20d9cSFrederic Weisbecker .base = 16, 6564da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 657fef20d9cSFrederic Weisbecker .precision = -1, 658fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 659fef20d9cSFrederic Weisbecker }; 6604da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 6614da0b66cSBjorn Helgaas .base = 16, 6624da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 6634da0b66cSBjorn Helgaas .precision = -1, 6644da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 6654da0b66cSBjorn Helgaas }; 6660f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 6670f4050c7SBjorn Helgaas .base = 16, 6680f4050c7SBjorn Helgaas .field_width = 2, 6690f4050c7SBjorn Helgaas .precision = -1, 6700f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 6710f4050c7SBjorn Helgaas }; 6724da0b66cSBjorn Helgaas static const struct printf_spec dec_spec = { 673c91d3376SBjorn Helgaas .base = 10, 674c91d3376SBjorn Helgaas .precision = -1, 675c91d3376SBjorn Helgaas .flags = 0, 676c91d3376SBjorn Helgaas }; 6774da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 678fd95541eSBjorn Helgaas .field_width = -1, 679fd95541eSBjorn Helgaas .precision = 10, 680fd95541eSBjorn Helgaas .flags = LEFT, 681fd95541eSBjorn Helgaas }; 6824da0b66cSBjorn Helgaas static const struct printf_spec flag_spec = { 683fd95541eSBjorn Helgaas .base = 16, 684fd95541eSBjorn Helgaas .precision = -1, 685fd95541eSBjorn Helgaas .flags = SPECIAL | SMALL, 686fd95541eSBjorn Helgaas }; 687c7dabef8SBjorn Helgaas 688c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 689c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 690c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 691c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 6929d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 693c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 694c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 695c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 696c7dabef8SBjorn Helgaas 697332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 698c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 6994da0b66cSBjorn Helgaas const struct printf_spec *specp; 700332d2e78SLinus Torvalds 701332d2e78SLinus Torvalds *p++ = '['; 7024da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 703fd95541eSBjorn Helgaas p = string(p, pend, "io ", str_spec); 7044da0b66cSBjorn Helgaas specp = &io_spec; 7054da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 706fd95541eSBjorn Helgaas p = string(p, pend, "mem ", str_spec); 7074da0b66cSBjorn Helgaas specp = &mem_spec; 7084da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 709fd95541eSBjorn Helgaas p = string(p, pend, "irq ", str_spec); 7104da0b66cSBjorn Helgaas specp = &dec_spec; 7114da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 712fd95541eSBjorn Helgaas p = string(p, pend, "dma ", str_spec); 7134da0b66cSBjorn Helgaas specp = &dec_spec; 7140f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 7150f4050c7SBjorn Helgaas p = string(p, pend, "bus ", str_spec); 7160f4050c7SBjorn Helgaas specp = &bus_spec; 7174da0b66cSBjorn Helgaas } else { 718c7dabef8SBjorn Helgaas p = string(p, pend, "??? ", str_spec); 7194da0b66cSBjorn Helgaas specp = &mem_spec; 720c7dabef8SBjorn Helgaas decode = 0; 721fd95541eSBjorn Helgaas } 7224da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 723c91d3376SBjorn Helgaas if (res->start != res->end) { 724332d2e78SLinus Torvalds *p++ = '-'; 7254da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 726c91d3376SBjorn Helgaas } 727c7dabef8SBjorn Helgaas if (decode) { 728fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 729fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 730fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 731fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 7329d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 7339d7cca04SBjorn Helgaas p = string(p, pend, " window", str_spec); 734fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 735fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 736c7dabef8SBjorn Helgaas } else { 737fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 738c7dabef8SBjorn Helgaas p = number(p, pend, res->flags, flag_spec); 739fd95541eSBjorn Helgaas } 740332d2e78SLinus Torvalds *p++ = ']'; 741c7dabef8SBjorn Helgaas *p = '\0'; 742332d2e78SLinus Torvalds 743fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 744332d2e78SLinus Torvalds } 745332d2e78SLinus Torvalds 746cf3b429bSJoe Perches static noinline_for_stack 74731550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 74831550a16SAndy Shevchenko const char *fmt) 74931550a16SAndy Shevchenko { 750360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 75131550a16SAndy Shevchenko negative value, fallback to the default */ 75231550a16SAndy Shevchenko char separator; 75331550a16SAndy Shevchenko 75431550a16SAndy Shevchenko if (spec.field_width == 0) 75531550a16SAndy Shevchenko /* nothing to print */ 75631550a16SAndy Shevchenko return buf; 75731550a16SAndy Shevchenko 75831550a16SAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 75931550a16SAndy Shevchenko /* NULL pointer */ 76031550a16SAndy Shevchenko return string(buf, end, NULL, spec); 76131550a16SAndy Shevchenko 76231550a16SAndy Shevchenko switch (fmt[1]) { 76331550a16SAndy Shevchenko case 'C': 76431550a16SAndy Shevchenko separator = ':'; 76531550a16SAndy Shevchenko break; 76631550a16SAndy Shevchenko case 'D': 76731550a16SAndy Shevchenko separator = '-'; 76831550a16SAndy Shevchenko break; 76931550a16SAndy Shevchenko case 'N': 77031550a16SAndy Shevchenko separator = 0; 77131550a16SAndy Shevchenko break; 77231550a16SAndy Shevchenko default: 77331550a16SAndy Shevchenko separator = ' '; 77431550a16SAndy Shevchenko break; 77531550a16SAndy Shevchenko } 77631550a16SAndy Shevchenko 77731550a16SAndy Shevchenko if (spec.field_width > 0) 77831550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 77931550a16SAndy Shevchenko 78031550a16SAndy Shevchenko for (i = 0; i < len && buf < end - 1; i++) { 78131550a16SAndy Shevchenko buf = hex_byte_pack(buf, addr[i]); 78231550a16SAndy Shevchenko 78331550a16SAndy Shevchenko if (buf < end && separator && i != len - 1) 78431550a16SAndy Shevchenko *buf++ = separator; 78531550a16SAndy Shevchenko } 78631550a16SAndy Shevchenko 78731550a16SAndy Shevchenko return buf; 78831550a16SAndy Shevchenko } 78931550a16SAndy Shevchenko 79031550a16SAndy Shevchenko static noinline_for_stack 791cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 7928a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 793dd45c9cfSHarvey Harrison { 7948a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 795dd45c9cfSHarvey Harrison char *p = mac_addr; 796dd45c9cfSHarvey Harrison int i; 797bc7259a2SJoe Perches char separator; 79876597ff9SAndrei Emeltchenko bool reversed = false; 799bc7259a2SJoe Perches 80076597ff9SAndrei Emeltchenko switch (fmt[1]) { 80176597ff9SAndrei Emeltchenko case 'F': 802bc7259a2SJoe Perches separator = '-'; 80376597ff9SAndrei Emeltchenko break; 80476597ff9SAndrei Emeltchenko 80576597ff9SAndrei Emeltchenko case 'R': 80676597ff9SAndrei Emeltchenko reversed = true; 80776597ff9SAndrei Emeltchenko /* fall through */ 80876597ff9SAndrei Emeltchenko 80976597ff9SAndrei Emeltchenko default: 810bc7259a2SJoe Perches separator = ':'; 81176597ff9SAndrei Emeltchenko break; 812bc7259a2SJoe Perches } 813dd45c9cfSHarvey Harrison 814dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 81576597ff9SAndrei Emeltchenko if (reversed) 81676597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 81776597ff9SAndrei Emeltchenko else 81855036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 81976597ff9SAndrei Emeltchenko 8208a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 821bc7259a2SJoe Perches *p++ = separator; 822dd45c9cfSHarvey Harrison } 823dd45c9cfSHarvey Harrison *p = '\0'; 824dd45c9cfSHarvey Harrison 825fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 826dd45c9cfSHarvey Harrison } 827dd45c9cfSHarvey Harrison 828cf3b429bSJoe Perches static noinline_for_stack 829cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 830689afa7dSHarvey Harrison { 831689afa7dSHarvey Harrison int i; 8320159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 8330159f24eSJoe Perches int index; 8340159f24eSJoe Perches int step; 835689afa7dSHarvey Harrison 8360159f24eSJoe Perches switch (fmt[2]) { 8370159f24eSJoe Perches case 'h': 8380159f24eSJoe Perches #ifdef __BIG_ENDIAN 8390159f24eSJoe Perches index = 0; 8400159f24eSJoe Perches step = 1; 8410159f24eSJoe Perches #else 8420159f24eSJoe Perches index = 3; 8430159f24eSJoe Perches step = -1; 8440159f24eSJoe Perches #endif 8450159f24eSJoe Perches break; 8460159f24eSJoe Perches case 'l': 8470159f24eSJoe Perches index = 3; 8480159f24eSJoe Perches step = -1; 8490159f24eSJoe Perches break; 8500159f24eSJoe Perches case 'n': 8510159f24eSJoe Perches case 'b': 8520159f24eSJoe Perches default: 8530159f24eSJoe Perches index = 0; 8540159f24eSJoe Perches step = 1; 8550159f24eSJoe Perches break; 8560159f24eSJoe Perches } 8578a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 8588a27f7c9SJoe Perches char temp[3]; /* hold each IP quad in reverse order */ 859133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 8608a27f7c9SJoe Perches if (leading_zeros) { 8618a27f7c9SJoe Perches if (digits < 3) 8628a27f7c9SJoe Perches *p++ = '0'; 8638a27f7c9SJoe Perches if (digits < 2) 8648a27f7c9SJoe Perches *p++ = '0'; 8658a27f7c9SJoe Perches } 8668a27f7c9SJoe Perches /* reverse the digits in the quad */ 8678a27f7c9SJoe Perches while (digits--) 8688a27f7c9SJoe Perches *p++ = temp[digits]; 8698a27f7c9SJoe Perches if (i < 3) 8708a27f7c9SJoe Perches *p++ = '.'; 8710159f24eSJoe Perches index += step; 8728a27f7c9SJoe Perches } 8738a27f7c9SJoe Perches *p = '\0'; 8747b9186f5SAndré Goddard Rosa 8758a27f7c9SJoe Perches return p; 8768a27f7c9SJoe Perches } 8778a27f7c9SJoe Perches 878cf3b429bSJoe Perches static noinline_for_stack 879cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 8808a27f7c9SJoe Perches { 8817b9186f5SAndré Goddard Rosa int i, j, range; 8828a27f7c9SJoe Perches unsigned char zerolength[8]; 8838a27f7c9SJoe Perches int longest = 1; 8848a27f7c9SJoe Perches int colonpos = -1; 8858a27f7c9SJoe Perches u16 word; 8867b9186f5SAndré Goddard Rosa u8 hi, lo; 8878a27f7c9SJoe Perches bool needcolon = false; 888eb78cd26SJoe Perches bool useIPv4; 889eb78cd26SJoe Perches struct in6_addr in6; 890eb78cd26SJoe Perches 891eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 892eb78cd26SJoe Perches 893eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 8948a27f7c9SJoe Perches 8958a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 8968a27f7c9SJoe Perches 8978a27f7c9SJoe Perches if (useIPv4) 8988a27f7c9SJoe Perches range = 6; 8998a27f7c9SJoe Perches else 9008a27f7c9SJoe Perches range = 8; 9018a27f7c9SJoe Perches 9028a27f7c9SJoe Perches /* find position of longest 0 run */ 9038a27f7c9SJoe Perches for (i = 0; i < range; i++) { 9048a27f7c9SJoe Perches for (j = i; j < range; j++) { 905eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 9068a27f7c9SJoe Perches break; 9078a27f7c9SJoe Perches zerolength[i]++; 9088a27f7c9SJoe Perches } 9098a27f7c9SJoe Perches } 9108a27f7c9SJoe Perches for (i = 0; i < range; i++) { 9118a27f7c9SJoe Perches if (zerolength[i] > longest) { 9128a27f7c9SJoe Perches longest = zerolength[i]; 9138a27f7c9SJoe Perches colonpos = i; 9148a27f7c9SJoe Perches } 9158a27f7c9SJoe Perches } 91629cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 91729cf519eSJoe Perches colonpos = -1; 9188a27f7c9SJoe Perches 9198a27f7c9SJoe Perches /* emit address */ 9208a27f7c9SJoe Perches for (i = 0; i < range; i++) { 9218a27f7c9SJoe Perches if (i == colonpos) { 9228a27f7c9SJoe Perches if (needcolon || i == 0) 9238a27f7c9SJoe Perches *p++ = ':'; 9248a27f7c9SJoe Perches *p++ = ':'; 9258a27f7c9SJoe Perches needcolon = false; 9268a27f7c9SJoe Perches i += longest - 1; 9278a27f7c9SJoe Perches continue; 9288a27f7c9SJoe Perches } 9298a27f7c9SJoe Perches if (needcolon) { 9308a27f7c9SJoe Perches *p++ = ':'; 9318a27f7c9SJoe Perches needcolon = false; 9328a27f7c9SJoe Perches } 9338a27f7c9SJoe Perches /* hex u16 without leading 0s */ 934eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 9358a27f7c9SJoe Perches hi = word >> 8; 9368a27f7c9SJoe Perches lo = word & 0xff; 9378a27f7c9SJoe Perches if (hi) { 9388a27f7c9SJoe Perches if (hi > 0x0f) 93955036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 9408a27f7c9SJoe Perches else 9418a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 94255036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 9438a27f7c9SJoe Perches } 944b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 94555036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 9468a27f7c9SJoe Perches else 9478a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 9488a27f7c9SJoe Perches needcolon = true; 9498a27f7c9SJoe Perches } 9508a27f7c9SJoe Perches 9518a27f7c9SJoe Perches if (useIPv4) { 9528a27f7c9SJoe Perches if (needcolon) 9538a27f7c9SJoe Perches *p++ = ':'; 9540159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 9558a27f7c9SJoe Perches } 9568a27f7c9SJoe Perches *p = '\0'; 9577b9186f5SAndré Goddard Rosa 9588a27f7c9SJoe Perches return p; 9598a27f7c9SJoe Perches } 9608a27f7c9SJoe Perches 961cf3b429bSJoe Perches static noinline_for_stack 962cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 9638a27f7c9SJoe Perches { 9648a27f7c9SJoe Perches int i; 9657b9186f5SAndré Goddard Rosa 966689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 96755036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 96855036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 9698a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 970689afa7dSHarvey Harrison *p++ = ':'; 971689afa7dSHarvey Harrison } 972689afa7dSHarvey Harrison *p = '\0'; 9737b9186f5SAndré Goddard Rosa 9748a27f7c9SJoe Perches return p; 9758a27f7c9SJoe Perches } 9768a27f7c9SJoe Perches 977cf3b429bSJoe Perches static noinline_for_stack 978cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 9798a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 9808a27f7c9SJoe Perches { 9818a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 9828a27f7c9SJoe Perches 9838a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 984eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 9858a27f7c9SJoe Perches else 986eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 987689afa7dSHarvey Harrison 988fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 989689afa7dSHarvey Harrison } 990689afa7dSHarvey Harrison 991cf3b429bSJoe Perches static noinline_for_stack 992cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 9938a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 9944aa99606SHarvey Harrison { 9958a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 9964aa99606SHarvey Harrison 9970159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 9984aa99606SHarvey Harrison 999fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 10004aa99606SHarvey Harrison } 10014aa99606SHarvey Harrison 1002cf3b429bSJoe Perches static noinline_for_stack 100310679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 100410679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 100510679643SDaniel Borkmann { 100610679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 100710679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 100810679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 100910679643SDaniel Borkmann sizeof("%1234567890")]; 101010679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 101110679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 101210679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 101310679643SDaniel Borkmann u8 off = 0; 101410679643SDaniel Borkmann 101510679643SDaniel Borkmann fmt++; 101610679643SDaniel Borkmann while (isalpha(*++fmt)) { 101710679643SDaniel Borkmann switch (*fmt) { 101810679643SDaniel Borkmann case 'p': 101910679643SDaniel Borkmann have_p = true; 102010679643SDaniel Borkmann break; 102110679643SDaniel Borkmann case 'f': 102210679643SDaniel Borkmann have_f = true; 102310679643SDaniel Borkmann break; 102410679643SDaniel Borkmann case 's': 102510679643SDaniel Borkmann have_s = true; 102610679643SDaniel Borkmann break; 102710679643SDaniel Borkmann case 'c': 102810679643SDaniel Borkmann have_c = true; 102910679643SDaniel Borkmann break; 103010679643SDaniel Borkmann } 103110679643SDaniel Borkmann } 103210679643SDaniel Borkmann 103310679643SDaniel Borkmann if (have_p || have_s || have_f) { 103410679643SDaniel Borkmann *p = '['; 103510679643SDaniel Borkmann off = 1; 103610679643SDaniel Borkmann } 103710679643SDaniel Borkmann 103810679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 103910679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 104010679643SDaniel Borkmann else 104110679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 104210679643SDaniel Borkmann 104310679643SDaniel Borkmann if (have_p || have_s || have_f) 104410679643SDaniel Borkmann *p++ = ']'; 104510679643SDaniel Borkmann 104610679643SDaniel Borkmann if (have_p) { 104710679643SDaniel Borkmann *p++ = ':'; 104810679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 104910679643SDaniel Borkmann } 105010679643SDaniel Borkmann if (have_f) { 105110679643SDaniel Borkmann *p++ = '/'; 105210679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 105310679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 105410679643SDaniel Borkmann } 105510679643SDaniel Borkmann if (have_s) { 105610679643SDaniel Borkmann *p++ = '%'; 105710679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 105810679643SDaniel Borkmann } 105910679643SDaniel Borkmann *p = '\0'; 106010679643SDaniel Borkmann 106110679643SDaniel Borkmann return string(buf, end, ip6_addr, spec); 106210679643SDaniel Borkmann } 106310679643SDaniel Borkmann 106410679643SDaniel Borkmann static noinline_for_stack 106510679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 106610679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 106710679643SDaniel Borkmann { 106810679643SDaniel Borkmann bool have_p = false; 106910679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 107010679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 107110679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 107210679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 107310679643SDaniel Borkmann 107410679643SDaniel Borkmann fmt++; 107510679643SDaniel Borkmann while (isalpha(*++fmt)) { 107610679643SDaniel Borkmann switch (*fmt) { 107710679643SDaniel Borkmann case 'p': 107810679643SDaniel Borkmann have_p = true; 107910679643SDaniel Borkmann break; 108010679643SDaniel Borkmann case 'h': 108110679643SDaniel Borkmann case 'l': 108210679643SDaniel Borkmann case 'n': 108310679643SDaniel Borkmann case 'b': 108410679643SDaniel Borkmann fmt4[2] = *fmt; 108510679643SDaniel Borkmann break; 108610679643SDaniel Borkmann } 108710679643SDaniel Borkmann } 108810679643SDaniel Borkmann 108910679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 109010679643SDaniel Borkmann if (have_p) { 109110679643SDaniel Borkmann *p++ = ':'; 109210679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 109310679643SDaniel Borkmann } 109410679643SDaniel Borkmann *p = '\0'; 109510679643SDaniel Borkmann 109610679643SDaniel Borkmann return string(buf, end, ip4_addr, spec); 109710679643SDaniel Borkmann } 109810679643SDaniel Borkmann 109910679643SDaniel Borkmann static noinline_for_stack 1100cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 11019ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 11029ac6e44eSJoe Perches { 11039ac6e44eSJoe Perches char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; 11049ac6e44eSJoe Perches char *p = uuid; 11059ac6e44eSJoe Perches int i; 11069ac6e44eSJoe Perches static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 11079ac6e44eSJoe Perches static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; 11089ac6e44eSJoe Perches const u8 *index = be; 11099ac6e44eSJoe Perches bool uc = false; 11109ac6e44eSJoe Perches 11119ac6e44eSJoe Perches switch (*(++fmt)) { 11129ac6e44eSJoe Perches case 'L': 11139ac6e44eSJoe Perches uc = true; /* fall-through */ 11149ac6e44eSJoe Perches case 'l': 11159ac6e44eSJoe Perches index = le; 11169ac6e44eSJoe Perches break; 11179ac6e44eSJoe Perches case 'B': 11189ac6e44eSJoe Perches uc = true; 11199ac6e44eSJoe Perches break; 11209ac6e44eSJoe Perches } 11219ac6e44eSJoe Perches 11229ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 112355036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 11249ac6e44eSJoe Perches switch (i) { 11259ac6e44eSJoe Perches case 3: 11269ac6e44eSJoe Perches case 5: 11279ac6e44eSJoe Perches case 7: 11289ac6e44eSJoe Perches case 9: 11299ac6e44eSJoe Perches *p++ = '-'; 11309ac6e44eSJoe Perches break; 11319ac6e44eSJoe Perches } 11329ac6e44eSJoe Perches } 11339ac6e44eSJoe Perches 11349ac6e44eSJoe Perches *p = 0; 11359ac6e44eSJoe Perches 11369ac6e44eSJoe Perches if (uc) { 11379ac6e44eSJoe Perches p = uuid; 11389ac6e44eSJoe Perches do { 11399ac6e44eSJoe Perches *p = toupper(*p); 11409ac6e44eSJoe Perches } while (*(++p)); 11419ac6e44eSJoe Perches } 11429ac6e44eSJoe Perches 11439ac6e44eSJoe Perches return string(buf, end, uuid, spec); 11449ac6e44eSJoe Perches } 11459ac6e44eSJoe Perches 1146c8f44affSMichał Mirosław static 1147c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr, 1148c8f44affSMichał Mirosław struct printf_spec spec) 1149c8f44affSMichał Mirosław { 1150c8f44affSMichał Mirosław spec.flags |= SPECIAL | SMALL | ZEROPAD; 1151c8f44affSMichał Mirosław if (spec.field_width == -1) 1152c8f44affSMichał Mirosław spec.field_width = 2 + 2 * sizeof(netdev_features_t); 1153c8f44affSMichał Mirosław spec.base = 16; 1154c8f44affSMichał Mirosław 1155c8f44affSMichał Mirosław return number(buf, end, *(const netdev_features_t *)addr, spec); 1156c8f44affSMichał Mirosław } 1157c8f44affSMichał Mirosław 1158411f05f1SIngo Molnar int kptr_restrict __read_mostly; 1159455cd5abSDan Rosenberg 11604d8a743cSLinus Torvalds /* 11614d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 11624d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 11634d8a743cSLinus Torvalds * specifiers. 11644d8a743cSLinus Torvalds * 1165332d2e78SLinus Torvalds * Right now we handle: 11660fe1ef24SLinus Torvalds * 11670c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 11680c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 11690efb4d20SSteven Rostedt * - 'S' For symbolic direct pointers with offset 11700efb4d20SSteven Rostedt * - 's' For symbolic direct pointers without offset 1171b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 11720f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 1173c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1174c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1175dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 1176dd45c9cfSHarvey Harrison * usual colon-separated hex notation 11778a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1178bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 1179c8e00060SJoe Perches * with a dash-separated hex notation 11807c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 11818a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 11828a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 11838a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 118410679643SDaniel Borkmann * [S][pfs] 118510679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 118610679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 11878a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 11888a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 11898a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 119010679643SDaniel Borkmann * [S][pfs] 119110679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 119210679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 119310679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 119410679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 119529cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 11969ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 11979ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 11989ac6e44eSJoe Perches * Options for %pU are: 11999ac6e44eSJoe Perches * b big endian lower case hex (default) 12009ac6e44eSJoe Perches * B big endian UPPER case hex 12019ac6e44eSJoe Perches * l little endian lower case hex 12029ac6e44eSJoe Perches * L little endian UPPER case hex 12039ac6e44eSJoe Perches * big endian output byte order is: 12049ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 12059ac6e44eSJoe Perches * little endian output byte order is: 12069ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 12077db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 12087db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 12097db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 12107db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 12117db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 1212455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 1213c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 121431550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 121531550a16SAndy Shevchenko * a certain separator (' ' by default): 121631550a16SAndy Shevchenko * C colon 121731550a16SAndy Shevchenko * D dash 121831550a16SAndy Shevchenko * N no separator 121931550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 122031550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 12217d799210SStepan Moskovchenko * - 'a' For a phys_addr_t type and its derivative types (passed by reference) 12229ac6e44eSJoe Perches * 1223332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1224332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 1225332d2e78SLinus Torvalds * pointer to the real address. 12264d8a743cSLinus Torvalds */ 1227cf3b429bSJoe Perches static noinline_for_stack 1228cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1229fef20d9cSFrederic Weisbecker struct printf_spec spec) 123078a8bf69SLinus Torvalds { 1231725fe002SGrant Likely int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); 1232725fe002SGrant Likely 12339f36e2c4SKees Cook if (!ptr && *fmt != 'K') { 12345e057981SJoe Perches /* 12355e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 12365e057981SJoe Perches * tabular output look nice. 12375e057981SJoe Perches */ 12385e057981SJoe Perches if (spec.field_width == -1) 1239725fe002SGrant Likely spec.field_width = default_width; 1240fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 12415e057981SJoe Perches } 1242d97106abSLinus Torvalds 12430fe1ef24SLinus Torvalds switch (*fmt) { 12440fe1ef24SLinus Torvalds case 'F': 12450c8b946eSFrederic Weisbecker case 'f': 12460fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 12470fe1ef24SLinus Torvalds /* Fallthrough */ 12480fe1ef24SLinus Torvalds case 'S': 12499ac6e44eSJoe Perches case 's': 12500f77a8d3SNamhyung Kim case 'B': 1251b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1252332d2e78SLinus Torvalds case 'R': 1253c7dabef8SBjorn Helgaas case 'r': 1254fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 125531550a16SAndy Shevchenko case 'h': 125631550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 12578a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 12588a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 125976597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 126076597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 12618a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 12628a27f7c9SJoe Perches case 'I': /* Formatted IP supported 12638a27f7c9SJoe Perches * 4: 1.2.3.4 12648a27f7c9SJoe Perches * 6: 0001:0203:...:0708 12658a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 12668a27f7c9SJoe Perches */ 12678a27f7c9SJoe Perches case 'i': /* Contiguous: 12688a27f7c9SJoe Perches * 4: 001.002.003.004 12698a27f7c9SJoe Perches * 6: 000102...0f 12708a27f7c9SJoe Perches */ 12718a27f7c9SJoe Perches switch (fmt[1]) { 12728a27f7c9SJoe Perches case '6': 12738a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 12748a27f7c9SJoe Perches case '4': 12758a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 127610679643SDaniel Borkmann case 'S': { 127710679643SDaniel Borkmann const union { 127810679643SDaniel Borkmann struct sockaddr raw; 127910679643SDaniel Borkmann struct sockaddr_in v4; 128010679643SDaniel Borkmann struct sockaddr_in6 v6; 128110679643SDaniel Borkmann } *sa = ptr; 128210679643SDaniel Borkmann 128310679643SDaniel Borkmann switch (sa->raw.sa_family) { 128410679643SDaniel Borkmann case AF_INET: 128510679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 128610679643SDaniel Borkmann case AF_INET6: 128710679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 128810679643SDaniel Borkmann default: 128910679643SDaniel Borkmann return string(buf, end, "(invalid address)", spec); 129010679643SDaniel Borkmann }} 12918a27f7c9SJoe Perches } 12924aa99606SHarvey Harrison break; 12939ac6e44eSJoe Perches case 'U': 12949ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 12957db6f5fbSJoe Perches case 'V': 12965756b76eSJan Beulich { 12975756b76eSJan Beulich va_list va; 12985756b76eSJan Beulich 12995756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 13005756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 13015756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 13025756b76eSJan Beulich va_end(va); 13035756b76eSJan Beulich return buf; 13045756b76eSJan Beulich } 1305455cd5abSDan Rosenberg case 'K': 1306455cd5abSDan Rosenberg /* 1307455cd5abSDan Rosenberg * %pK cannot be used in IRQ context because its test 1308455cd5abSDan Rosenberg * for CAP_SYSLOG would be meaningless. 1309455cd5abSDan Rosenberg */ 13103715c530SDan Rosenberg if (kptr_restrict && (in_irq() || in_serving_softirq() || 13113715c530SDan Rosenberg in_nmi())) { 1312455cd5abSDan Rosenberg if (spec.field_width == -1) 1313725fe002SGrant Likely spec.field_width = default_width; 1314455cd5abSDan Rosenberg return string(buf, end, "pK-error", spec); 1315455cd5abSDan Rosenberg } 1316*312b4e22SRyan Mallon 1317*312b4e22SRyan Mallon switch (kptr_restrict) { 1318*312b4e22SRyan Mallon case 0: 1319*312b4e22SRyan Mallon /* Always print %pK values */ 1320*312b4e22SRyan Mallon break; 1321*312b4e22SRyan Mallon case 1: { 1322*312b4e22SRyan Mallon /* 1323*312b4e22SRyan Mallon * Only print the real pointer value if the current 1324*312b4e22SRyan Mallon * process has CAP_SYSLOG and is running with the 1325*312b4e22SRyan Mallon * same credentials it started with. This is because 1326*312b4e22SRyan Mallon * access to files is checked at open() time, but %pK 1327*312b4e22SRyan Mallon * checks permission at read() time. We don't want to 1328*312b4e22SRyan Mallon * leak pointer values if a binary opens a file using 1329*312b4e22SRyan Mallon * %pK and then elevates privileges before reading it. 1330*312b4e22SRyan Mallon */ 1331*312b4e22SRyan Mallon const struct cred *cred = current_cred(); 1332*312b4e22SRyan Mallon 1333*312b4e22SRyan Mallon if (!has_capability_noaudit(current, CAP_SYSLOG) || 1334*312b4e22SRyan Mallon !uid_eq(cred->euid, cred->uid) || 1335*312b4e22SRyan Mallon !gid_eq(cred->egid, cred->gid)) 133626297607SJoe Perches ptr = NULL; 133726297607SJoe Perches break; 1338*312b4e22SRyan Mallon } 1339*312b4e22SRyan Mallon case 2: 1340*312b4e22SRyan Mallon default: 1341*312b4e22SRyan Mallon /* Always print 0's for %pK */ 1342*312b4e22SRyan Mallon ptr = NULL; 1343*312b4e22SRyan Mallon break; 1344*312b4e22SRyan Mallon } 1345*312b4e22SRyan Mallon break; 1346*312b4e22SRyan Mallon 1347c8f44affSMichał Mirosław case 'N': 1348c8f44affSMichał Mirosław switch (fmt[1]) { 1349c8f44affSMichał Mirosław case 'F': 1350c8f44affSMichał Mirosław return netdev_feature_string(buf, end, ptr, spec); 1351c8f44affSMichał Mirosław } 1352c8f44affSMichał Mirosław break; 13537d799210SStepan Moskovchenko case 'a': 13547d799210SStepan Moskovchenko spec.flags |= SPECIAL | SMALL | ZEROPAD; 13557d799210SStepan Moskovchenko spec.field_width = sizeof(phys_addr_t) * 2 + 2; 13567d799210SStepan Moskovchenko spec.base = 16; 13577d799210SStepan Moskovchenko return number(buf, end, 13587d799210SStepan Moskovchenko (unsigned long long) *((phys_addr_t *)ptr), spec); 13594b6ccca7SAl Viro case 'd': 13604b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 13614b6ccca7SAl Viro case 'D': 13624b6ccca7SAl Viro return dentry_name(buf, end, 13634b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 13644b6ccca7SAl Viro spec, fmt); 13650fe1ef24SLinus Torvalds } 1366fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 1367fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 1368725fe002SGrant Likely spec.field_width = default_width; 1369fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 137078a8bf69SLinus Torvalds } 1371fef20d9cSFrederic Weisbecker spec.base = 16; 1372fef20d9cSFrederic Weisbecker 1373fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 1374fef20d9cSFrederic Weisbecker } 1375fef20d9cSFrederic Weisbecker 1376fef20d9cSFrederic Weisbecker /* 1377fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1378fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1379fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1380fef20d9cSFrederic Weisbecker * to go on the next call). 1381fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1382fef20d9cSFrederic Weisbecker * 1383fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1384fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1385fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 1386fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1387fef20d9cSFrederic Weisbecker * 1388fef20d9cSFrederic Weisbecker * @fmt: the format string 1389fef20d9cSFrederic Weisbecker * @type of the token returned 1390fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1391fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1392fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1393fef20d9cSFrederic Weisbecker * @precision: precision of a number 1394fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1395fef20d9cSFrederic Weisbecker */ 1396cf3b429bSJoe Perches static noinline_for_stack 1397cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 1398fef20d9cSFrederic Weisbecker { 1399fef20d9cSFrederic Weisbecker const char *start = fmt; 1400fef20d9cSFrederic Weisbecker 1401fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 1402ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 1403fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 1404fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 1405fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 1406fef20d9cSFrederic Weisbecker } 1407fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1408fef20d9cSFrederic Weisbecker goto precision; 1409fef20d9cSFrederic Weisbecker } 1410fef20d9cSFrederic Weisbecker 1411fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 1412fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 1413fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1414fef20d9cSFrederic Weisbecker spec->precision = 0; 1415fef20d9cSFrederic Weisbecker 1416fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1417fef20d9cSFrederic Weisbecker goto qualifier; 1418fef20d9cSFrederic Weisbecker } 1419fef20d9cSFrederic Weisbecker 1420fef20d9cSFrederic Weisbecker /* By default */ 1421fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1422fef20d9cSFrederic Weisbecker 1423fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 1424fef20d9cSFrederic Weisbecker if (*fmt == '%') 1425fef20d9cSFrederic Weisbecker break; 1426fef20d9cSFrederic Weisbecker } 1427fef20d9cSFrederic Weisbecker 1428fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 1429fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 1430fef20d9cSFrederic Weisbecker return fmt - start; 1431fef20d9cSFrederic Weisbecker 1432fef20d9cSFrederic Weisbecker /* Process flags */ 1433fef20d9cSFrederic Weisbecker spec->flags = 0; 1434fef20d9cSFrederic Weisbecker 1435fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 1436fef20d9cSFrederic Weisbecker bool found = true; 1437fef20d9cSFrederic Weisbecker 1438fef20d9cSFrederic Weisbecker ++fmt; 1439fef20d9cSFrederic Weisbecker 1440fef20d9cSFrederic Weisbecker switch (*fmt) { 1441fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 1442fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 1443fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 1444fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 1445fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 1446fef20d9cSFrederic Weisbecker default: found = false; 1447fef20d9cSFrederic Weisbecker } 1448fef20d9cSFrederic Weisbecker 1449fef20d9cSFrederic Weisbecker if (!found) 1450fef20d9cSFrederic Weisbecker break; 1451fef20d9cSFrederic Weisbecker } 1452fef20d9cSFrederic Weisbecker 1453fef20d9cSFrederic Weisbecker /* get field width */ 1454fef20d9cSFrederic Weisbecker spec->field_width = -1; 1455fef20d9cSFrederic Weisbecker 1456fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1457fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1458fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1459fef20d9cSFrederic Weisbecker /* it's the next argument */ 1460ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1461fef20d9cSFrederic Weisbecker return ++fmt - start; 1462fef20d9cSFrederic Weisbecker } 1463fef20d9cSFrederic Weisbecker 1464fef20d9cSFrederic Weisbecker precision: 1465fef20d9cSFrederic Weisbecker /* get the precision */ 1466fef20d9cSFrederic Weisbecker spec->precision = -1; 1467fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1468fef20d9cSFrederic Weisbecker ++fmt; 1469fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1470fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1471fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1472fef20d9cSFrederic Weisbecker spec->precision = 0; 1473fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1474fef20d9cSFrederic Weisbecker /* it's the next argument */ 1475adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1476fef20d9cSFrederic Weisbecker return ++fmt - start; 1477fef20d9cSFrederic Weisbecker } 1478fef20d9cSFrederic Weisbecker } 1479fef20d9cSFrederic Weisbecker 1480fef20d9cSFrederic Weisbecker qualifier: 1481fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1482fef20d9cSFrederic Weisbecker spec->qualifier = -1; 148375fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 148475fb8f26SAndy Shevchenko _tolower(*fmt) == 'z' || *fmt == 't') { 1485a4e94ef0SZhaolei spec->qualifier = *fmt++; 1486a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 1487a4e94ef0SZhaolei if (spec->qualifier == 'l') { 1488fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 1489fef20d9cSFrederic Weisbecker ++fmt; 1490a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 1491a4e94ef0SZhaolei spec->qualifier = 'H'; 1492a4e94ef0SZhaolei ++fmt; 1493a4e94ef0SZhaolei } 1494fef20d9cSFrederic Weisbecker } 1495fef20d9cSFrederic Weisbecker } 1496fef20d9cSFrederic Weisbecker 1497fef20d9cSFrederic Weisbecker /* default base */ 1498fef20d9cSFrederic Weisbecker spec->base = 10; 1499fef20d9cSFrederic Weisbecker switch (*fmt) { 1500fef20d9cSFrederic Weisbecker case 'c': 1501fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1502fef20d9cSFrederic Weisbecker return ++fmt - start; 1503fef20d9cSFrederic Weisbecker 1504fef20d9cSFrederic Weisbecker case 's': 1505fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1506fef20d9cSFrederic Weisbecker return ++fmt - start; 1507fef20d9cSFrederic Weisbecker 1508fef20d9cSFrederic Weisbecker case 'p': 1509fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1510fef20d9cSFrederic Weisbecker return fmt - start; 1511fef20d9cSFrederic Weisbecker /* skip alnum */ 1512fef20d9cSFrederic Weisbecker 1513fef20d9cSFrederic Weisbecker case 'n': 1514fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NRCHARS; 1515fef20d9cSFrederic Weisbecker return ++fmt - start; 1516fef20d9cSFrederic Weisbecker 1517fef20d9cSFrederic Weisbecker case '%': 1518fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1519fef20d9cSFrederic Weisbecker return ++fmt - start; 1520fef20d9cSFrederic Weisbecker 1521fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1522fef20d9cSFrederic Weisbecker case 'o': 1523fef20d9cSFrederic Weisbecker spec->base = 8; 1524fef20d9cSFrederic Weisbecker break; 1525fef20d9cSFrederic Weisbecker 1526fef20d9cSFrederic Weisbecker case 'x': 1527fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1528fef20d9cSFrederic Weisbecker 1529fef20d9cSFrederic Weisbecker case 'X': 1530fef20d9cSFrederic Weisbecker spec->base = 16; 1531fef20d9cSFrederic Weisbecker break; 1532fef20d9cSFrederic Weisbecker 1533fef20d9cSFrederic Weisbecker case 'd': 1534fef20d9cSFrederic Weisbecker case 'i': 153539e874f8SFrederic Weisbecker spec->flags |= SIGN; 1536fef20d9cSFrederic Weisbecker case 'u': 1537fef20d9cSFrederic Weisbecker break; 1538fef20d9cSFrederic Weisbecker 1539fef20d9cSFrederic Weisbecker default: 1540fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1541fef20d9cSFrederic Weisbecker return fmt - start; 1542fef20d9cSFrederic Weisbecker } 1543fef20d9cSFrederic Weisbecker 1544fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 1545fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1546fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 154739e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1548fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG; 1549fef20d9cSFrederic Weisbecker else 1550fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_ULONG; 155175fb8f26SAndy Shevchenko } else if (_tolower(spec->qualifier) == 'z') { 1552fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1553fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 1554fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1555a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 1556a4e94ef0SZhaolei if (spec->flags & SIGN) 1557a4e94ef0SZhaolei spec->type = FORMAT_TYPE_BYTE; 1558a4e94ef0SZhaolei else 1559a4e94ef0SZhaolei spec->type = FORMAT_TYPE_UBYTE; 1560fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 156139e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1562fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SHORT; 1563fef20d9cSFrederic Weisbecker else 1564fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_USHORT; 1565fef20d9cSFrederic Weisbecker } else { 156639e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1567fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INT; 1568fef20d9cSFrederic Weisbecker else 1569fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_UINT; 1570fef20d9cSFrederic Weisbecker } 1571fef20d9cSFrederic Weisbecker 1572fef20d9cSFrederic Weisbecker return ++fmt - start; 157378a8bf69SLinus Torvalds } 157478a8bf69SLinus Torvalds 15751da177e4SLinus Torvalds /** 15761da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 15771da177e4SLinus Torvalds * @buf: The buffer to place the result into 15781da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 15791da177e4SLinus Torvalds * @fmt: The format string to use 15801da177e4SLinus Torvalds * @args: Arguments for the format string 15811da177e4SLinus Torvalds * 158220036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 158391adcd2cSSteven Rostedt * %pS output the name of a text symbol with offset 158491adcd2cSSteven Rostedt * %ps output the name of a text symbol without offset 15850c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 15860c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 15870f77a8d3SNamhyung Kim * %pB output the name of a backtrace symbol with its offset 15888a79503aSUwe Kleine-König * %pR output the address range in a struct resource with decoded flags 15898a79503aSUwe Kleine-König * %pr output the address range in a struct resource with raw flags 15908a79503aSUwe Kleine-König * %pM output a 6-byte MAC address with colons 15917c59154eSAndy Shevchenko * %pMR output a 6-byte MAC address with colons in reversed order 15927c59154eSAndy Shevchenko * %pMF output a 6-byte MAC address with dashes 15938a79503aSUwe Kleine-König * %pm output a 6-byte MAC address without colons 15947c59154eSAndy Shevchenko * %pmR output a 6-byte MAC address without colons in reversed order 15958a79503aSUwe Kleine-König * %pI4 print an IPv4 address without leading zeros 15968a79503aSUwe Kleine-König * %pi4 print an IPv4 address with leading zeros 15978a79503aSUwe Kleine-König * %pI6 print an IPv6 address with colons 15988a79503aSUwe Kleine-König * %pi6 print an IPv6 address without colons 1599f996f208SJan Engelhardt * %pI6c print an IPv6 address as specified by RFC 5952 160010679643SDaniel Borkmann * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 160110679643SDaniel Borkmann * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 16028a79503aSUwe Kleine-König * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper 16038a79503aSUwe Kleine-König * case. 160431550a16SAndy Shevchenko * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 160531550a16SAndy Shevchenko * bytes of the input) 16060efb4d20SSteven Rostedt * %n is ignored 160720036fdcSAndi Kleen * 160880f548e0SAndrew Morton * ** Please update Documentation/printk-formats.txt when making changes ** 160980f548e0SAndrew Morton * 16101da177e4SLinus Torvalds * The return value is the number of characters which would 16111da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 16121da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 16131da177e4SLinus Torvalds * number of characters written into @buf as return value 161472fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 16151da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 16161da177e4SLinus Torvalds * string is truncated. 16171da177e4SLinus Torvalds * 1618ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 16191da177e4SLinus Torvalds */ 16201da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 16211da177e4SLinus Torvalds { 16221da177e4SLinus Torvalds unsigned long long num; 1623d4be151bSAndré Goddard Rosa char *str, *end; 1624fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 16251da177e4SLinus Torvalds 1626f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1627f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 16282f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 16291da177e4SLinus Torvalds return 0; 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds str = buf; 1632f796937aSJeremy Fitzhardinge end = buf + size; 16331da177e4SLinus Torvalds 1634f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1635f796937aSJeremy Fitzhardinge if (end < buf) { 16361da177e4SLinus Torvalds end = ((void *)-1); 1637f796937aSJeremy Fitzhardinge size = end - buf; 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 1640fef20d9cSFrederic Weisbecker while (*fmt) { 1641fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1642d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 1643fef20d9cSFrederic Weisbecker 1644fef20d9cSFrederic Weisbecker fmt += read; 1645fef20d9cSFrederic Weisbecker 1646fef20d9cSFrederic Weisbecker switch (spec.type) { 1647fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1648fef20d9cSFrederic Weisbecker int copy = read; 1649fef20d9cSFrederic Weisbecker if (str < end) { 1650fef20d9cSFrederic Weisbecker if (copy > end - str) 1651fef20d9cSFrederic Weisbecker copy = end - str; 1652fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1653fef20d9cSFrederic Weisbecker } 1654fef20d9cSFrederic Weisbecker str += read; 1655fef20d9cSFrederic Weisbecker break; 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 1658ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1659fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1660fef20d9cSFrederic Weisbecker break; 16611da177e4SLinus Torvalds 1662fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1663fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1664fef20d9cSFrederic Weisbecker break; 16651da177e4SLinus Torvalds 1666d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 1667d4be151bSAndré Goddard Rosa char c; 1668d4be151bSAndré Goddard Rosa 1669fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1670fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1671f796937aSJeremy Fitzhardinge if (str < end) 16721da177e4SLinus Torvalds *str = ' '; 16731da177e4SLinus Torvalds ++str; 1674fef20d9cSFrederic Weisbecker 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1678f796937aSJeremy Fitzhardinge if (str < end) 16791da177e4SLinus Torvalds *str = c; 16801da177e4SLinus Torvalds ++str; 1681fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1682f796937aSJeremy Fitzhardinge if (str < end) 16831da177e4SLinus Torvalds *str = ' '; 16841da177e4SLinus Torvalds ++str; 16851da177e4SLinus Torvalds } 1686fef20d9cSFrederic Weisbecker break; 1687d4be151bSAndré Goddard Rosa } 16881da177e4SLinus Torvalds 1689fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1690fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1691fef20d9cSFrederic Weisbecker break; 16921da177e4SLinus Torvalds 1693fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1694fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, va_arg(args, void *), 1695fef20d9cSFrederic Weisbecker spec); 1696fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 16974d8a743cSLinus Torvalds fmt++; 1698fef20d9cSFrederic Weisbecker break; 16991da177e4SLinus Torvalds 1700fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1701f796937aSJeremy Fitzhardinge if (str < end) 17021da177e4SLinus Torvalds *str = '%'; 17031da177e4SLinus Torvalds ++str; 17041da177e4SLinus Torvalds break; 17051da177e4SLinus Torvalds 1706fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1707f796937aSJeremy Fitzhardinge if (str < end) 17081da177e4SLinus Torvalds *str = '%'; 17091da177e4SLinus Torvalds ++str; 1710fef20d9cSFrederic Weisbecker break; 1711fef20d9cSFrederic Weisbecker 1712fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 1713ef0658f3SJoe Perches u8 qualifier = spec.qualifier; 1714fef20d9cSFrederic Weisbecker 1715fef20d9cSFrederic Weisbecker if (qualifier == 'l') { 1716fef20d9cSFrederic Weisbecker long *ip = va_arg(args, long *); 1717fef20d9cSFrederic Weisbecker *ip = (str - buf); 171875fb8f26SAndy Shevchenko } else if (_tolower(qualifier) == 'z') { 1719fef20d9cSFrederic Weisbecker size_t *ip = va_arg(args, size_t *); 1720fef20d9cSFrederic Weisbecker *ip = (str - buf); 17211da177e4SLinus Torvalds } else { 1722fef20d9cSFrederic Weisbecker int *ip = va_arg(args, int *); 1723fef20d9cSFrederic Weisbecker *ip = (str - buf); 1724fef20d9cSFrederic Weisbecker } 1725fef20d9cSFrederic Weisbecker break; 1726fef20d9cSFrederic Weisbecker } 1727fef20d9cSFrederic Weisbecker 1728fef20d9cSFrederic Weisbecker default: 1729fef20d9cSFrederic Weisbecker switch (spec.type) { 1730fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1731fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1732fef20d9cSFrederic Weisbecker break; 1733fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1734fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1735fef20d9cSFrederic Weisbecker break; 1736fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1737fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1738fef20d9cSFrederic Weisbecker break; 1739fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1740ef124960SJason Gunthorpe if (spec.flags & SIGN) 1741ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 1742ef124960SJason Gunthorpe else 1743fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1744fef20d9cSFrederic Weisbecker break; 1745fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1746fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1747fef20d9cSFrederic Weisbecker break; 1748a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1749a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1750a4e94ef0SZhaolei break; 1751a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1752a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1753a4e94ef0SZhaolei break; 1754fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1755fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1756fef20d9cSFrederic Weisbecker break; 1757fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1758fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1759fef20d9cSFrederic Weisbecker break; 176039e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 176139e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1762fef20d9cSFrederic Weisbecker break; 1763fef20d9cSFrederic Weisbecker default: 1764fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 17651da177e4SLinus Torvalds } 1766fef20d9cSFrederic Weisbecker 1767fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 17681da177e4SLinus Torvalds } 1769fef20d9cSFrederic Weisbecker } 1770fef20d9cSFrederic Weisbecker 1771f796937aSJeremy Fitzhardinge if (size > 0) { 1772f796937aSJeremy Fitzhardinge if (str < end) 17731da177e4SLinus Torvalds *str = '\0'; 1774f796937aSJeremy Fitzhardinge else 17750a6047eeSLinus Torvalds end[-1] = '\0'; 1776f796937aSJeremy Fitzhardinge } 1777fef20d9cSFrederic Weisbecker 1778f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 17791da177e4SLinus Torvalds return str-buf; 1780fef20d9cSFrederic Weisbecker 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds /** 17851da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 17861da177e4SLinus Torvalds * @buf: The buffer to place the result into 17871da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 17881da177e4SLinus Torvalds * @fmt: The format string to use 17891da177e4SLinus Torvalds * @args: Arguments for the format string 17901da177e4SLinus Torvalds * 17911da177e4SLinus Torvalds * The return value is the number of characters which have been written into 1792b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 17931da177e4SLinus Torvalds * returns 0. 17941da177e4SLinus Torvalds * 1795ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 179620036fdcSAndi Kleen * 179720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 17981da177e4SLinus Torvalds */ 17991da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 18001da177e4SLinus Torvalds { 18011da177e4SLinus Torvalds int i; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 18047b9186f5SAndré Goddard Rosa 1805b921c69fSAnton Arapov if (likely(i < size)) 1806b921c69fSAnton Arapov return i; 1807b921c69fSAnton Arapov if (size != 0) 1808b921c69fSAnton Arapov return size - 1; 1809b921c69fSAnton Arapov return 0; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds /** 18141da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 18151da177e4SLinus Torvalds * @buf: The buffer to place the result into 18161da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18171da177e4SLinus Torvalds * @fmt: The format string to use 18181da177e4SLinus Torvalds * @...: Arguments for the format string 18191da177e4SLinus Torvalds * 18201da177e4SLinus Torvalds * The return value is the number of characters which would be 18211da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 18221da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 18231da177e4SLinus Torvalds * @size, the resulting string is truncated. 182420036fdcSAndi Kleen * 182520036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 18261da177e4SLinus Torvalds */ 18271da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 18281da177e4SLinus Torvalds { 18291da177e4SLinus Torvalds va_list args; 18301da177e4SLinus Torvalds int i; 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds va_start(args, fmt); 18331da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 18341da177e4SLinus Torvalds va_end(args); 18357b9186f5SAndré Goddard Rosa 18361da177e4SLinus Torvalds return i; 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds /** 18411da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 18421da177e4SLinus Torvalds * @buf: The buffer to place the result into 18431da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18441da177e4SLinus Torvalds * @fmt: The format string to use 18451da177e4SLinus Torvalds * @...: Arguments for the format string 18461da177e4SLinus Torvalds * 18471da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 1848b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 18491da177e4SLinus Torvalds */ 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 18521da177e4SLinus Torvalds { 18531da177e4SLinus Torvalds va_list args; 18541da177e4SLinus Torvalds int i; 18551da177e4SLinus Torvalds 18561da177e4SLinus Torvalds va_start(args, fmt); 1857b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 18581da177e4SLinus Torvalds va_end(args); 18597b9186f5SAndré Goddard Rosa 1860b903c0b8SChangli Gao return i; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds /** 18651da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 18661da177e4SLinus Torvalds * @buf: The buffer to place the result into 18671da177e4SLinus Torvalds * @fmt: The format string to use 18681da177e4SLinus Torvalds * @args: Arguments for the format string 18691da177e4SLinus Torvalds * 18701da177e4SLinus Torvalds * The function returns the number of characters written 187172fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 18721da177e4SLinus Torvalds * buffer overflows. 18731da177e4SLinus Torvalds * 1874ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 187520036fdcSAndi Kleen * 187620036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 18771da177e4SLinus Torvalds */ 18781da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 18791da177e4SLinus Torvalds { 18801da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 18811da177e4SLinus Torvalds } 18821da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds /** 18851da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 18861da177e4SLinus Torvalds * @buf: The buffer to place the result into 18871da177e4SLinus Torvalds * @fmt: The format string to use 18881da177e4SLinus Torvalds * @...: Arguments for the format string 18891da177e4SLinus Torvalds * 18901da177e4SLinus Torvalds * The function returns the number of characters written 189172fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 18921da177e4SLinus Torvalds * buffer overflows. 189320036fdcSAndi Kleen * 189420036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 18951da177e4SLinus Torvalds */ 18961da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 18971da177e4SLinus Torvalds { 18981da177e4SLinus Torvalds va_list args; 18991da177e4SLinus Torvalds int i; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds va_start(args, fmt); 19021da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 19031da177e4SLinus Torvalds va_end(args); 19047b9186f5SAndré Goddard Rosa 19051da177e4SLinus Torvalds return i; 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 19081da177e4SLinus Torvalds 19094370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 19104370aa4aSLai Jiangshan /* 19114370aa4aSLai Jiangshan * bprintf service: 19124370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 19134370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 19144370aa4aSLai Jiangshan */ 19154370aa4aSLai Jiangshan 19164370aa4aSLai Jiangshan /** 19174370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 19184370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 19194370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 19204370aa4aSLai Jiangshan * @fmt: The format string to use 19214370aa4aSLai Jiangshan * @args: Arguments for the format string 19224370aa4aSLai Jiangshan * 19234370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 19244370aa4aSLai Jiangshan * is skiped. 19254370aa4aSLai Jiangshan * 19264370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 19274370aa4aSLai Jiangshan * the given input. 19284370aa4aSLai Jiangshan * 19294370aa4aSLai Jiangshan * NOTE: 19304370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 19314370aa4aSLai Jiangshan * valid for bstr_printf(). 19324370aa4aSLai Jiangshan */ 19334370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 19344370aa4aSLai Jiangshan { 1935fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 19364370aa4aSLai Jiangshan char *str, *end; 19374370aa4aSLai Jiangshan 19384370aa4aSLai Jiangshan str = (char *)bin_buf; 19394370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 19404370aa4aSLai Jiangshan 19414370aa4aSLai Jiangshan #define save_arg(type) \ 19424370aa4aSLai Jiangshan do { \ 19434370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 19444370aa4aSLai Jiangshan unsigned long long value; \ 19454370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 19464370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 19474370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 19484370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 19494370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 19504370aa4aSLai Jiangshan } \ 19514370aa4aSLai Jiangshan } else { \ 19524370aa4aSLai Jiangshan unsigned long value; \ 19534370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 19544370aa4aSLai Jiangshan value = va_arg(args, int); \ 19554370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 19564370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 19574370aa4aSLai Jiangshan } \ 19584370aa4aSLai Jiangshan str += sizeof(type); \ 19594370aa4aSLai Jiangshan } while (0) 19604370aa4aSLai Jiangshan 1961fef20d9cSFrederic Weisbecker while (*fmt) { 1962d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 19634370aa4aSLai Jiangshan 1964fef20d9cSFrederic Weisbecker fmt += read; 1965fef20d9cSFrederic Weisbecker 1966fef20d9cSFrederic Weisbecker switch (spec.type) { 1967fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 1968d4be151bSAndré Goddard Rosa case FORMAT_TYPE_INVALID: 1969d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 1970fef20d9cSFrederic Weisbecker break; 1971fef20d9cSFrederic Weisbecker 1972ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1973fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 19744370aa4aSLai Jiangshan save_arg(int); 1975fef20d9cSFrederic Weisbecker break; 19764370aa4aSLai Jiangshan 1977fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 19784370aa4aSLai Jiangshan save_arg(char); 1979fef20d9cSFrederic Weisbecker break; 1980fef20d9cSFrederic Weisbecker 1981fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 19824370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 19834370aa4aSLai Jiangshan size_t len; 19846c356634SAndré Goddard Rosa 19854370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 19864370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 19870f4f81dcSAndré Goddard Rosa save_str = "(null)"; 19886c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 19896c356634SAndré Goddard Rosa if (str + len < end) 19906c356634SAndré Goddard Rosa memcpy(str, save_str, len); 19916c356634SAndré Goddard Rosa str += len; 1992fef20d9cSFrederic Weisbecker break; 19934370aa4aSLai Jiangshan } 1994fef20d9cSFrederic Weisbecker 1995fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 19964370aa4aSLai Jiangshan save_arg(void *); 19974370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 1998fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 19994370aa4aSLai Jiangshan fmt++; 2000fef20d9cSFrederic Weisbecker break; 2001fef20d9cSFrederic Weisbecker 2002fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 20034370aa4aSLai Jiangshan /* skip %n 's argument */ 2004ef0658f3SJoe Perches u8 qualifier = spec.qualifier; 20054370aa4aSLai Jiangshan void *skip_arg; 20064370aa4aSLai Jiangshan if (qualifier == 'l') 20074370aa4aSLai Jiangshan skip_arg = va_arg(args, long *); 200875fb8f26SAndy Shevchenko else if (_tolower(qualifier) == 'z') 20094370aa4aSLai Jiangshan skip_arg = va_arg(args, size_t *); 20104370aa4aSLai Jiangshan else 20114370aa4aSLai Jiangshan skip_arg = va_arg(args, int *); 2012fef20d9cSFrederic Weisbecker break; 20134370aa4aSLai Jiangshan } 20144370aa4aSLai Jiangshan 2015fef20d9cSFrederic Weisbecker default: 2016fef20d9cSFrederic Weisbecker switch (spec.type) { 2017fef20d9cSFrederic Weisbecker 2018fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2019fef20d9cSFrederic Weisbecker save_arg(long long); 2020fef20d9cSFrederic Weisbecker break; 2021fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2022fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2023fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2024fef20d9cSFrederic Weisbecker break; 2025fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2026fef20d9cSFrederic Weisbecker save_arg(size_t); 2027fef20d9cSFrederic Weisbecker break; 2028fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2029fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2030fef20d9cSFrederic Weisbecker break; 2031a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2032a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2033a4e94ef0SZhaolei save_arg(char); 2034a4e94ef0SZhaolei break; 2035fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2036fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2037fef20d9cSFrederic Weisbecker save_arg(short); 2038fef20d9cSFrederic Weisbecker break; 2039fef20d9cSFrederic Weisbecker default: 2040fef20d9cSFrederic Weisbecker save_arg(int); 2041fef20d9cSFrederic Weisbecker } 2042fef20d9cSFrederic Weisbecker } 2043fef20d9cSFrederic Weisbecker } 2044fef20d9cSFrederic Weisbecker 20457b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2046fef20d9cSFrederic Weisbecker #undef save_arg 20474370aa4aSLai Jiangshan } 20484370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 20494370aa4aSLai Jiangshan 20504370aa4aSLai Jiangshan /** 20514370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 20524370aa4aSLai Jiangshan * @buf: The buffer to place the result into 20534370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 20544370aa4aSLai Jiangshan * @fmt: The format string to use 20554370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 20564370aa4aSLai Jiangshan * 20574370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 20584370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 20594370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 20604370aa4aSLai Jiangshan * 20614370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 20620efb4d20SSteven Rostedt * see vsnprintf comment for details. 20634370aa4aSLai Jiangshan * 20644370aa4aSLai Jiangshan * The return value is the number of characters which would 20654370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 20664370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 20674370aa4aSLai Jiangshan * number of characters written into @buf as return value 20684370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 20694370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 20704370aa4aSLai Jiangshan * string is truncated. 20714370aa4aSLai Jiangshan */ 20724370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 20734370aa4aSLai Jiangshan { 2074fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2075d4be151bSAndré Goddard Rosa char *str, *end; 2076d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 20774370aa4aSLai Jiangshan 20782f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 20794370aa4aSLai Jiangshan return 0; 20804370aa4aSLai Jiangshan 20814370aa4aSLai Jiangshan str = buf; 20824370aa4aSLai Jiangshan end = buf + size; 20834370aa4aSLai Jiangshan 20844370aa4aSLai Jiangshan #define get_arg(type) \ 20854370aa4aSLai Jiangshan ({ \ 20864370aa4aSLai Jiangshan typeof(type) value; \ 20874370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 20884370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 20894370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 20904370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 20914370aa4aSLai Jiangshan } else { \ 20924370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 20934370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 20944370aa4aSLai Jiangshan } \ 20954370aa4aSLai Jiangshan args += sizeof(type); \ 20964370aa4aSLai Jiangshan value; \ 20974370aa4aSLai Jiangshan }) 20984370aa4aSLai Jiangshan 20994370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 21004370aa4aSLai Jiangshan if (end < buf) { 21014370aa4aSLai Jiangshan end = ((void *)-1); 21024370aa4aSLai Jiangshan size = end - buf; 21034370aa4aSLai Jiangshan } 21044370aa4aSLai Jiangshan 2105fef20d9cSFrederic Weisbecker while (*fmt) { 2106fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2107d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2108fef20d9cSFrederic Weisbecker 2109fef20d9cSFrederic Weisbecker fmt += read; 2110fef20d9cSFrederic Weisbecker 2111fef20d9cSFrederic Weisbecker switch (spec.type) { 2112fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2113fef20d9cSFrederic Weisbecker int copy = read; 2114fef20d9cSFrederic Weisbecker if (str < end) { 2115fef20d9cSFrederic Weisbecker if (copy > end - str) 2116fef20d9cSFrederic Weisbecker copy = end - str; 2117fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2118fef20d9cSFrederic Weisbecker } 2119fef20d9cSFrederic Weisbecker str += read; 2120fef20d9cSFrederic Weisbecker break; 21214370aa4aSLai Jiangshan } 21224370aa4aSLai Jiangshan 2123ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2124fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 2125fef20d9cSFrederic Weisbecker break; 21264370aa4aSLai Jiangshan 2127fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2128fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 2129fef20d9cSFrederic Weisbecker break; 21304370aa4aSLai Jiangshan 2131d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2132d4be151bSAndré Goddard Rosa char c; 2133d4be151bSAndré Goddard Rosa 2134fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2135fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 21364370aa4aSLai Jiangshan if (str < end) 21374370aa4aSLai Jiangshan *str = ' '; 21384370aa4aSLai Jiangshan ++str; 21394370aa4aSLai Jiangshan } 21404370aa4aSLai Jiangshan } 21414370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 21424370aa4aSLai Jiangshan if (str < end) 21434370aa4aSLai Jiangshan *str = c; 21444370aa4aSLai Jiangshan ++str; 2145fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 21464370aa4aSLai Jiangshan if (str < end) 21474370aa4aSLai Jiangshan *str = ' '; 21484370aa4aSLai Jiangshan ++str; 21494370aa4aSLai Jiangshan } 2150fef20d9cSFrederic Weisbecker break; 2151d4be151bSAndré Goddard Rosa } 21524370aa4aSLai Jiangshan 2153fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 21544370aa4aSLai Jiangshan const char *str_arg = args; 2155d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2156fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2157fef20d9cSFrederic Weisbecker break; 21584370aa4aSLai Jiangshan } 21594370aa4aSLai Jiangshan 2160fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2161fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, get_arg(void *), spec); 2162fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 21634370aa4aSLai Jiangshan fmt++; 2164fef20d9cSFrederic Weisbecker break; 21654370aa4aSLai Jiangshan 2166fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2167fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 21684370aa4aSLai Jiangshan if (str < end) 21694370aa4aSLai Jiangshan *str = '%'; 21704370aa4aSLai Jiangshan ++str; 2171fef20d9cSFrederic Weisbecker break; 2172fef20d9cSFrederic Weisbecker 2173fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: 2174fef20d9cSFrederic Weisbecker /* skip */ 2175fef20d9cSFrederic Weisbecker break; 2176fef20d9cSFrederic Weisbecker 2177d4be151bSAndré Goddard Rosa default: { 2178d4be151bSAndré Goddard Rosa unsigned long long num; 2179d4be151bSAndré Goddard Rosa 2180fef20d9cSFrederic Weisbecker switch (spec.type) { 2181fef20d9cSFrederic Weisbecker 2182fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 21834370aa4aSLai Jiangshan num = get_arg(long long); 2184fef20d9cSFrederic Weisbecker break; 2185fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2186fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2187fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2188fef20d9cSFrederic Weisbecker break; 2189fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 21904370aa4aSLai Jiangshan num = get_arg(size_t); 2191fef20d9cSFrederic Weisbecker break; 2192fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 21934370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2194fef20d9cSFrederic Weisbecker break; 2195a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2196a4e94ef0SZhaolei num = get_arg(unsigned char); 2197a4e94ef0SZhaolei break; 2198a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2199a4e94ef0SZhaolei num = get_arg(signed char); 2200a4e94ef0SZhaolei break; 2201fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2202fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2203fef20d9cSFrederic Weisbecker break; 2204fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2205fef20d9cSFrederic Weisbecker num = get_arg(short); 2206fef20d9cSFrederic Weisbecker break; 2207fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 22084370aa4aSLai Jiangshan num = get_arg(unsigned int); 2209fef20d9cSFrederic Weisbecker break; 2210fef20d9cSFrederic Weisbecker default: 2211fef20d9cSFrederic Weisbecker num = get_arg(int); 22124370aa4aSLai Jiangshan } 2213fef20d9cSFrederic Weisbecker 2214fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2215d4be151bSAndré Goddard Rosa } /* default: */ 2216d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2217d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2218fef20d9cSFrederic Weisbecker 22194370aa4aSLai Jiangshan if (size > 0) { 22204370aa4aSLai Jiangshan if (str < end) 22214370aa4aSLai Jiangshan *str = '\0'; 22224370aa4aSLai Jiangshan else 22234370aa4aSLai Jiangshan end[-1] = '\0'; 22244370aa4aSLai Jiangshan } 2225fef20d9cSFrederic Weisbecker 22264370aa4aSLai Jiangshan #undef get_arg 22274370aa4aSLai Jiangshan 22284370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 22294370aa4aSLai Jiangshan return str - buf; 22304370aa4aSLai Jiangshan } 22314370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 22324370aa4aSLai Jiangshan 22334370aa4aSLai Jiangshan /** 22344370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 22354370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 22364370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 22374370aa4aSLai Jiangshan * @fmt: The format string to use 22384370aa4aSLai Jiangshan * @...: Arguments for the format string 22394370aa4aSLai Jiangshan * 22404370aa4aSLai Jiangshan * The function returns the number of words(u32) written 22414370aa4aSLai Jiangshan * into @bin_buf. 22424370aa4aSLai Jiangshan */ 22434370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 22444370aa4aSLai Jiangshan { 22454370aa4aSLai Jiangshan va_list args; 22464370aa4aSLai Jiangshan int ret; 22474370aa4aSLai Jiangshan 22484370aa4aSLai Jiangshan va_start(args, fmt); 22494370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 22504370aa4aSLai Jiangshan va_end(args); 22517b9186f5SAndré Goddard Rosa 22524370aa4aSLai Jiangshan return ret; 22534370aa4aSLai Jiangshan } 22544370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 22554370aa4aSLai Jiangshan 22564370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 22574370aa4aSLai Jiangshan 22581da177e4SLinus Torvalds /** 22591da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 22601da177e4SLinus Torvalds * @buf: input buffer 22611da177e4SLinus Torvalds * @fmt: format of buffer 22621da177e4SLinus Torvalds * @args: arguments 22631da177e4SLinus Torvalds */ 22641da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 22651da177e4SLinus Torvalds { 22661da177e4SLinus Torvalds const char *str = buf; 22671da177e4SLinus Torvalds char *next; 22681da177e4SLinus Torvalds char digit; 22691da177e4SLinus Torvalds int num = 0; 2270ef0658f3SJoe Perches u8 qualifier; 227153809751SJan Beulich unsigned int base; 227253809751SJan Beulich union { 227353809751SJan Beulich long long s; 227453809751SJan Beulich unsigned long long u; 227553809751SJan Beulich } val; 2276ef0658f3SJoe Perches s16 field_width; 2277d4be151bSAndré Goddard Rosa bool is_sign; 22781da177e4SLinus Torvalds 2279da99075cSJan Beulich while (*fmt) { 22801da177e4SLinus Torvalds /* skip any white space in format */ 22811da177e4SLinus Torvalds /* white space in format matchs any amount of 22821da177e4SLinus Torvalds * white space, including none, in the input. 22831da177e4SLinus Torvalds */ 22841da177e4SLinus Torvalds if (isspace(*fmt)) { 2285e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 2286e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 22901da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 22911da177e4SLinus Torvalds if (*fmt++ != *str++) 22921da177e4SLinus Torvalds break; 22931da177e4SLinus Torvalds continue; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds if (!*fmt) 22971da177e4SLinus Torvalds break; 22981da177e4SLinus Torvalds ++fmt; 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds /* skip this conversion. 23011da177e4SLinus Torvalds * advance both strings to next white space 23021da177e4SLinus Torvalds */ 23031da177e4SLinus Torvalds if (*fmt == '*') { 2304da99075cSJan Beulich if (!*str) 2305da99075cSJan Beulich break; 23068fccae2cSAndy Spencer while (!isspace(*fmt) && *fmt != '%' && *fmt) 23071da177e4SLinus Torvalds fmt++; 23081da177e4SLinus Torvalds while (!isspace(*str) && *str) 23091da177e4SLinus Torvalds str++; 23101da177e4SLinus Torvalds continue; 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds /* get field width */ 23141da177e4SLinus Torvalds field_width = -1; 231553809751SJan Beulich if (isdigit(*fmt)) { 23161da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 231753809751SJan Beulich if (field_width <= 0) 231853809751SJan Beulich break; 231953809751SJan Beulich } 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds /* get conversion qualifier */ 23221da177e4SLinus Torvalds qualifier = -1; 232375fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 232475fb8f26SAndy Shevchenko _tolower(*fmt) == 'z') { 23251da177e4SLinus Torvalds qualifier = *fmt++; 23261da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 23271da177e4SLinus Torvalds if (qualifier == 'h') { 23281da177e4SLinus Torvalds qualifier = 'H'; 23291da177e4SLinus Torvalds fmt++; 23301da177e4SLinus Torvalds } else if (qualifier == 'l') { 23311da177e4SLinus Torvalds qualifier = 'L'; 23321da177e4SLinus Torvalds fmt++; 23331da177e4SLinus Torvalds } 23341da177e4SLinus Torvalds } 23351da177e4SLinus Torvalds } 23361da177e4SLinus Torvalds 2337da99075cSJan Beulich if (!*fmt) 2338da99075cSJan Beulich break; 2339da99075cSJan Beulich 2340da99075cSJan Beulich if (*fmt == 'n') { 2341da99075cSJan Beulich /* return number of characters read so far */ 2342da99075cSJan Beulich *va_arg(args, int *) = str - buf; 2343da99075cSJan Beulich ++fmt; 2344da99075cSJan Beulich continue; 2345da99075cSJan Beulich } 2346da99075cSJan Beulich 2347da99075cSJan Beulich if (!*str) 23481da177e4SLinus Torvalds break; 23491da177e4SLinus Torvalds 2350d4be151bSAndré Goddard Rosa base = 10; 2351d4be151bSAndré Goddard Rosa is_sign = 0; 2352d4be151bSAndré Goddard Rosa 23531da177e4SLinus Torvalds switch (*fmt++) { 23541da177e4SLinus Torvalds case 'c': 23551da177e4SLinus Torvalds { 23561da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 23571da177e4SLinus Torvalds if (field_width == -1) 23581da177e4SLinus Torvalds field_width = 1; 23591da177e4SLinus Torvalds do { 23601da177e4SLinus Torvalds *s++ = *str++; 23611da177e4SLinus Torvalds } while (--field_width > 0 && *str); 23621da177e4SLinus Torvalds num++; 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds continue; 23651da177e4SLinus Torvalds case 's': 23661da177e4SLinus Torvalds { 23671da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 23681da177e4SLinus Torvalds if (field_width == -1) 23694be929beSAlexey Dobriyan field_width = SHRT_MAX; 23701da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 2371e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds /* now copy until next white space */ 23747b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 23751da177e4SLinus Torvalds *s++ = *str++; 23761da177e4SLinus Torvalds *s = '\0'; 23771da177e4SLinus Torvalds num++; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds continue; 23801da177e4SLinus Torvalds case 'o': 23811da177e4SLinus Torvalds base = 8; 23821da177e4SLinus Torvalds break; 23831da177e4SLinus Torvalds case 'x': 23841da177e4SLinus Torvalds case 'X': 23851da177e4SLinus Torvalds base = 16; 23861da177e4SLinus Torvalds break; 23871da177e4SLinus Torvalds case 'i': 23881da177e4SLinus Torvalds base = 0; 23891da177e4SLinus Torvalds case 'd': 23901da177e4SLinus Torvalds is_sign = 1; 23911da177e4SLinus Torvalds case 'u': 23921da177e4SLinus Torvalds break; 23931da177e4SLinus Torvalds case '%': 23941da177e4SLinus Torvalds /* looking for '%' in str */ 23951da177e4SLinus Torvalds if (*str++ != '%') 23961da177e4SLinus Torvalds return num; 23971da177e4SLinus Torvalds continue; 23981da177e4SLinus Torvalds default: 23991da177e4SLinus Torvalds /* invalid format; stop here */ 24001da177e4SLinus Torvalds return num; 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds /* have some sort of integer conversion. 24041da177e4SLinus Torvalds * first, skip white space in buffer. 24051da177e4SLinus Torvalds */ 2406e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds digit = *str; 24091da177e4SLinus Torvalds if (is_sign && digit == '-') 24101da177e4SLinus Torvalds digit = *(str + 1); 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds if (!digit 24131da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 24141da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 24151da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 24161da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 24171da177e4SLinus Torvalds break; 24181da177e4SLinus Torvalds 241953809751SJan Beulich if (is_sign) 242053809751SJan Beulich val.s = qualifier != 'L' ? 242153809751SJan Beulich simple_strtol(str, &next, base) : 242253809751SJan Beulich simple_strtoll(str, &next, base); 242353809751SJan Beulich else 242453809751SJan Beulich val.u = qualifier != 'L' ? 242553809751SJan Beulich simple_strtoul(str, &next, base) : 242653809751SJan Beulich simple_strtoull(str, &next, base); 242753809751SJan Beulich 242853809751SJan Beulich if (field_width > 0 && next - str > field_width) { 242953809751SJan Beulich if (base == 0) 243053809751SJan Beulich _parse_integer_fixup_radix(str, &base); 243153809751SJan Beulich while (next - str > field_width) { 243253809751SJan Beulich if (is_sign) 243353809751SJan Beulich val.s = div_s64(val.s, base); 243453809751SJan Beulich else 243553809751SJan Beulich val.u = div_u64(val.u, base); 243653809751SJan Beulich --next; 243753809751SJan Beulich } 243853809751SJan Beulich } 243953809751SJan Beulich 24401da177e4SLinus Torvalds switch (qualifier) { 24411da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 244253809751SJan Beulich if (is_sign) 244353809751SJan Beulich *va_arg(args, signed char *) = val.s; 244453809751SJan Beulich else 244553809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 24461da177e4SLinus Torvalds break; 24471da177e4SLinus Torvalds case 'h': 244853809751SJan Beulich if (is_sign) 244953809751SJan Beulich *va_arg(args, short *) = val.s; 245053809751SJan Beulich else 245153809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 24521da177e4SLinus Torvalds break; 24531da177e4SLinus Torvalds case 'l': 245453809751SJan Beulich if (is_sign) 245553809751SJan Beulich *va_arg(args, long *) = val.s; 245653809751SJan Beulich else 245753809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 24581da177e4SLinus Torvalds break; 24591da177e4SLinus Torvalds case 'L': 246053809751SJan Beulich if (is_sign) 246153809751SJan Beulich *va_arg(args, long long *) = val.s; 246253809751SJan Beulich else 246353809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 24641da177e4SLinus Torvalds break; 24651da177e4SLinus Torvalds case 'Z': 24661da177e4SLinus Torvalds case 'z': 246753809751SJan Beulich *va_arg(args, size_t *) = val.u; 24681da177e4SLinus Torvalds break; 24691da177e4SLinus Torvalds default: 247053809751SJan Beulich if (is_sign) 247153809751SJan Beulich *va_arg(args, int *) = val.s; 247253809751SJan Beulich else 247353809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 24741da177e4SLinus Torvalds break; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds num++; 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds if (!next) 24791da177e4SLinus Torvalds break; 24801da177e4SLinus Torvalds str = next; 24811da177e4SLinus Torvalds } 2482c6b40d16SJohannes Berg 24831da177e4SLinus Torvalds return num; 24841da177e4SLinus Torvalds } 24851da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds /** 24881da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 24891da177e4SLinus Torvalds * @buf: input buffer 24901da177e4SLinus Torvalds * @fmt: formatting of buffer 24911da177e4SLinus Torvalds * @...: resulting arguments 24921da177e4SLinus Torvalds */ 24931da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 24941da177e4SLinus Torvalds { 24951da177e4SLinus Torvalds va_list args; 24961da177e4SLinus Torvalds int i; 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds va_start(args, fmt); 24991da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 25001da177e4SLinus Torvalds va_end(args); 25017b9186f5SAndré Goddard Rosa 25021da177e4SLinus Torvalds return i; 25031da177e4SLinus Torvalds } 25041da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2505