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> 30312b4e22SRyan 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 } 722*d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 723*d19cb803SBjorn Helgaas p = string(p, pend, "size ", str_spec); 724*d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 725*d19cb803SBjorn Helgaas } else { 7264da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 727c91d3376SBjorn Helgaas if (res->start != res->end) { 728332d2e78SLinus Torvalds *p++ = '-'; 7294da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 730c91d3376SBjorn Helgaas } 731*d19cb803SBjorn Helgaas } 732c7dabef8SBjorn Helgaas if (decode) { 733fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 734fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 735fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 736fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 7379d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 7389d7cca04SBjorn Helgaas p = string(p, pend, " window", str_spec); 739fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 740fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 741c7dabef8SBjorn Helgaas } else { 742fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 743c7dabef8SBjorn Helgaas p = number(p, pend, res->flags, flag_spec); 744fd95541eSBjorn Helgaas } 745332d2e78SLinus Torvalds *p++ = ']'; 746c7dabef8SBjorn Helgaas *p = '\0'; 747332d2e78SLinus Torvalds 748fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 749332d2e78SLinus Torvalds } 750332d2e78SLinus Torvalds 751cf3b429bSJoe Perches static noinline_for_stack 75231550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 75331550a16SAndy Shevchenko const char *fmt) 75431550a16SAndy Shevchenko { 755360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 75631550a16SAndy Shevchenko negative value, fallback to the default */ 75731550a16SAndy Shevchenko char separator; 75831550a16SAndy Shevchenko 75931550a16SAndy Shevchenko if (spec.field_width == 0) 76031550a16SAndy Shevchenko /* nothing to print */ 76131550a16SAndy Shevchenko return buf; 76231550a16SAndy Shevchenko 76331550a16SAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 76431550a16SAndy Shevchenko /* NULL pointer */ 76531550a16SAndy Shevchenko return string(buf, end, NULL, spec); 76631550a16SAndy Shevchenko 76731550a16SAndy Shevchenko switch (fmt[1]) { 76831550a16SAndy Shevchenko case 'C': 76931550a16SAndy Shevchenko separator = ':'; 77031550a16SAndy Shevchenko break; 77131550a16SAndy Shevchenko case 'D': 77231550a16SAndy Shevchenko separator = '-'; 77331550a16SAndy Shevchenko break; 77431550a16SAndy Shevchenko case 'N': 77531550a16SAndy Shevchenko separator = 0; 77631550a16SAndy Shevchenko break; 77731550a16SAndy Shevchenko default: 77831550a16SAndy Shevchenko separator = ' '; 77931550a16SAndy Shevchenko break; 78031550a16SAndy Shevchenko } 78131550a16SAndy Shevchenko 78231550a16SAndy Shevchenko if (spec.field_width > 0) 78331550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 78431550a16SAndy Shevchenko 78531550a16SAndy Shevchenko for (i = 0; i < len && buf < end - 1; i++) { 78631550a16SAndy Shevchenko buf = hex_byte_pack(buf, addr[i]); 78731550a16SAndy Shevchenko 78831550a16SAndy Shevchenko if (buf < end && separator && i != len - 1) 78931550a16SAndy Shevchenko *buf++ = separator; 79031550a16SAndy Shevchenko } 79131550a16SAndy Shevchenko 79231550a16SAndy Shevchenko return buf; 79331550a16SAndy Shevchenko } 79431550a16SAndy Shevchenko 79531550a16SAndy Shevchenko static noinline_for_stack 796cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 7978a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 798dd45c9cfSHarvey Harrison { 7998a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 800dd45c9cfSHarvey Harrison char *p = mac_addr; 801dd45c9cfSHarvey Harrison int i; 802bc7259a2SJoe Perches char separator; 80376597ff9SAndrei Emeltchenko bool reversed = false; 804bc7259a2SJoe Perches 80576597ff9SAndrei Emeltchenko switch (fmt[1]) { 80676597ff9SAndrei Emeltchenko case 'F': 807bc7259a2SJoe Perches separator = '-'; 80876597ff9SAndrei Emeltchenko break; 80976597ff9SAndrei Emeltchenko 81076597ff9SAndrei Emeltchenko case 'R': 81176597ff9SAndrei Emeltchenko reversed = true; 81276597ff9SAndrei Emeltchenko /* fall through */ 81376597ff9SAndrei Emeltchenko 81476597ff9SAndrei Emeltchenko default: 815bc7259a2SJoe Perches separator = ':'; 81676597ff9SAndrei Emeltchenko break; 817bc7259a2SJoe Perches } 818dd45c9cfSHarvey Harrison 819dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 82076597ff9SAndrei Emeltchenko if (reversed) 82176597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 82276597ff9SAndrei Emeltchenko else 82355036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 82476597ff9SAndrei Emeltchenko 8258a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 826bc7259a2SJoe Perches *p++ = separator; 827dd45c9cfSHarvey Harrison } 828dd45c9cfSHarvey Harrison *p = '\0'; 829dd45c9cfSHarvey Harrison 830fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 831dd45c9cfSHarvey Harrison } 832dd45c9cfSHarvey Harrison 833cf3b429bSJoe Perches static noinline_for_stack 834cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 835689afa7dSHarvey Harrison { 836689afa7dSHarvey Harrison int i; 8370159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 8380159f24eSJoe Perches int index; 8390159f24eSJoe Perches int step; 840689afa7dSHarvey Harrison 8410159f24eSJoe Perches switch (fmt[2]) { 8420159f24eSJoe Perches case 'h': 8430159f24eSJoe Perches #ifdef __BIG_ENDIAN 8440159f24eSJoe Perches index = 0; 8450159f24eSJoe Perches step = 1; 8460159f24eSJoe Perches #else 8470159f24eSJoe Perches index = 3; 8480159f24eSJoe Perches step = -1; 8490159f24eSJoe Perches #endif 8500159f24eSJoe Perches break; 8510159f24eSJoe Perches case 'l': 8520159f24eSJoe Perches index = 3; 8530159f24eSJoe Perches step = -1; 8540159f24eSJoe Perches break; 8550159f24eSJoe Perches case 'n': 8560159f24eSJoe Perches case 'b': 8570159f24eSJoe Perches default: 8580159f24eSJoe Perches index = 0; 8590159f24eSJoe Perches step = 1; 8600159f24eSJoe Perches break; 8610159f24eSJoe Perches } 8628a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 8638a27f7c9SJoe Perches char temp[3]; /* hold each IP quad in reverse order */ 864133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 8658a27f7c9SJoe Perches if (leading_zeros) { 8668a27f7c9SJoe Perches if (digits < 3) 8678a27f7c9SJoe Perches *p++ = '0'; 8688a27f7c9SJoe Perches if (digits < 2) 8698a27f7c9SJoe Perches *p++ = '0'; 8708a27f7c9SJoe Perches } 8718a27f7c9SJoe Perches /* reverse the digits in the quad */ 8728a27f7c9SJoe Perches while (digits--) 8738a27f7c9SJoe Perches *p++ = temp[digits]; 8748a27f7c9SJoe Perches if (i < 3) 8758a27f7c9SJoe Perches *p++ = '.'; 8760159f24eSJoe Perches index += step; 8778a27f7c9SJoe Perches } 8788a27f7c9SJoe Perches *p = '\0'; 8797b9186f5SAndré Goddard Rosa 8808a27f7c9SJoe Perches return p; 8818a27f7c9SJoe Perches } 8828a27f7c9SJoe Perches 883cf3b429bSJoe Perches static noinline_for_stack 884cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 8858a27f7c9SJoe Perches { 8867b9186f5SAndré Goddard Rosa int i, j, range; 8878a27f7c9SJoe Perches unsigned char zerolength[8]; 8888a27f7c9SJoe Perches int longest = 1; 8898a27f7c9SJoe Perches int colonpos = -1; 8908a27f7c9SJoe Perches u16 word; 8917b9186f5SAndré Goddard Rosa u8 hi, lo; 8928a27f7c9SJoe Perches bool needcolon = false; 893eb78cd26SJoe Perches bool useIPv4; 894eb78cd26SJoe Perches struct in6_addr in6; 895eb78cd26SJoe Perches 896eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 897eb78cd26SJoe Perches 898eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 8998a27f7c9SJoe Perches 9008a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 9018a27f7c9SJoe Perches 9028a27f7c9SJoe Perches if (useIPv4) 9038a27f7c9SJoe Perches range = 6; 9048a27f7c9SJoe Perches else 9058a27f7c9SJoe Perches range = 8; 9068a27f7c9SJoe Perches 9078a27f7c9SJoe Perches /* find position of longest 0 run */ 9088a27f7c9SJoe Perches for (i = 0; i < range; i++) { 9098a27f7c9SJoe Perches for (j = i; j < range; j++) { 910eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 9118a27f7c9SJoe Perches break; 9128a27f7c9SJoe Perches zerolength[i]++; 9138a27f7c9SJoe Perches } 9148a27f7c9SJoe Perches } 9158a27f7c9SJoe Perches for (i = 0; i < range; i++) { 9168a27f7c9SJoe Perches if (zerolength[i] > longest) { 9178a27f7c9SJoe Perches longest = zerolength[i]; 9188a27f7c9SJoe Perches colonpos = i; 9198a27f7c9SJoe Perches } 9208a27f7c9SJoe Perches } 92129cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 92229cf519eSJoe Perches colonpos = -1; 9238a27f7c9SJoe Perches 9248a27f7c9SJoe Perches /* emit address */ 9258a27f7c9SJoe Perches for (i = 0; i < range; i++) { 9268a27f7c9SJoe Perches if (i == colonpos) { 9278a27f7c9SJoe Perches if (needcolon || i == 0) 9288a27f7c9SJoe Perches *p++ = ':'; 9298a27f7c9SJoe Perches *p++ = ':'; 9308a27f7c9SJoe Perches needcolon = false; 9318a27f7c9SJoe Perches i += longest - 1; 9328a27f7c9SJoe Perches continue; 9338a27f7c9SJoe Perches } 9348a27f7c9SJoe Perches if (needcolon) { 9358a27f7c9SJoe Perches *p++ = ':'; 9368a27f7c9SJoe Perches needcolon = false; 9378a27f7c9SJoe Perches } 9388a27f7c9SJoe Perches /* hex u16 without leading 0s */ 939eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 9408a27f7c9SJoe Perches hi = word >> 8; 9418a27f7c9SJoe Perches lo = word & 0xff; 9428a27f7c9SJoe Perches if (hi) { 9438a27f7c9SJoe Perches if (hi > 0x0f) 94455036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 9458a27f7c9SJoe Perches else 9468a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 94755036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 9488a27f7c9SJoe Perches } 949b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 95055036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 9518a27f7c9SJoe Perches else 9528a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 9538a27f7c9SJoe Perches needcolon = true; 9548a27f7c9SJoe Perches } 9558a27f7c9SJoe Perches 9568a27f7c9SJoe Perches if (useIPv4) { 9578a27f7c9SJoe Perches if (needcolon) 9588a27f7c9SJoe Perches *p++ = ':'; 9590159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 9608a27f7c9SJoe Perches } 9618a27f7c9SJoe Perches *p = '\0'; 9627b9186f5SAndré Goddard Rosa 9638a27f7c9SJoe Perches return p; 9648a27f7c9SJoe Perches } 9658a27f7c9SJoe Perches 966cf3b429bSJoe Perches static noinline_for_stack 967cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 9688a27f7c9SJoe Perches { 9698a27f7c9SJoe Perches int i; 9707b9186f5SAndré Goddard Rosa 971689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 97255036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 97355036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 9748a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 975689afa7dSHarvey Harrison *p++ = ':'; 976689afa7dSHarvey Harrison } 977689afa7dSHarvey Harrison *p = '\0'; 9787b9186f5SAndré Goddard Rosa 9798a27f7c9SJoe Perches return p; 9808a27f7c9SJoe Perches } 9818a27f7c9SJoe Perches 982cf3b429bSJoe Perches static noinline_for_stack 983cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 9848a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 9858a27f7c9SJoe Perches { 9868a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 9878a27f7c9SJoe Perches 9888a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 989eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 9908a27f7c9SJoe Perches else 991eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 992689afa7dSHarvey Harrison 993fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 994689afa7dSHarvey Harrison } 995689afa7dSHarvey Harrison 996cf3b429bSJoe Perches static noinline_for_stack 997cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 9988a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 9994aa99606SHarvey Harrison { 10008a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 10014aa99606SHarvey Harrison 10020159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 10034aa99606SHarvey Harrison 1004fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 10054aa99606SHarvey Harrison } 10064aa99606SHarvey Harrison 1007cf3b429bSJoe Perches static noinline_for_stack 100810679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 100910679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 101010679643SDaniel Borkmann { 101110679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 101210679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 101310679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 101410679643SDaniel Borkmann sizeof("%1234567890")]; 101510679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 101610679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 101710679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 101810679643SDaniel Borkmann u8 off = 0; 101910679643SDaniel Borkmann 102010679643SDaniel Borkmann fmt++; 102110679643SDaniel Borkmann while (isalpha(*++fmt)) { 102210679643SDaniel Borkmann switch (*fmt) { 102310679643SDaniel Borkmann case 'p': 102410679643SDaniel Borkmann have_p = true; 102510679643SDaniel Borkmann break; 102610679643SDaniel Borkmann case 'f': 102710679643SDaniel Borkmann have_f = true; 102810679643SDaniel Borkmann break; 102910679643SDaniel Borkmann case 's': 103010679643SDaniel Borkmann have_s = true; 103110679643SDaniel Borkmann break; 103210679643SDaniel Borkmann case 'c': 103310679643SDaniel Borkmann have_c = true; 103410679643SDaniel Borkmann break; 103510679643SDaniel Borkmann } 103610679643SDaniel Borkmann } 103710679643SDaniel Borkmann 103810679643SDaniel Borkmann if (have_p || have_s || have_f) { 103910679643SDaniel Borkmann *p = '['; 104010679643SDaniel Borkmann off = 1; 104110679643SDaniel Borkmann } 104210679643SDaniel Borkmann 104310679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 104410679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 104510679643SDaniel Borkmann else 104610679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 104710679643SDaniel Borkmann 104810679643SDaniel Borkmann if (have_p || have_s || have_f) 104910679643SDaniel Borkmann *p++ = ']'; 105010679643SDaniel Borkmann 105110679643SDaniel Borkmann if (have_p) { 105210679643SDaniel Borkmann *p++ = ':'; 105310679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 105410679643SDaniel Borkmann } 105510679643SDaniel Borkmann if (have_f) { 105610679643SDaniel Borkmann *p++ = '/'; 105710679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 105810679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 105910679643SDaniel Borkmann } 106010679643SDaniel Borkmann if (have_s) { 106110679643SDaniel Borkmann *p++ = '%'; 106210679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 106310679643SDaniel Borkmann } 106410679643SDaniel Borkmann *p = '\0'; 106510679643SDaniel Borkmann 106610679643SDaniel Borkmann return string(buf, end, ip6_addr, spec); 106710679643SDaniel Borkmann } 106810679643SDaniel Borkmann 106910679643SDaniel Borkmann static noinline_for_stack 107010679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 107110679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 107210679643SDaniel Borkmann { 107310679643SDaniel Borkmann bool have_p = false; 107410679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 107510679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 107610679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 107710679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 107810679643SDaniel Borkmann 107910679643SDaniel Borkmann fmt++; 108010679643SDaniel Borkmann while (isalpha(*++fmt)) { 108110679643SDaniel Borkmann switch (*fmt) { 108210679643SDaniel Borkmann case 'p': 108310679643SDaniel Borkmann have_p = true; 108410679643SDaniel Borkmann break; 108510679643SDaniel Borkmann case 'h': 108610679643SDaniel Borkmann case 'l': 108710679643SDaniel Borkmann case 'n': 108810679643SDaniel Borkmann case 'b': 108910679643SDaniel Borkmann fmt4[2] = *fmt; 109010679643SDaniel Borkmann break; 109110679643SDaniel Borkmann } 109210679643SDaniel Borkmann } 109310679643SDaniel Borkmann 109410679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 109510679643SDaniel Borkmann if (have_p) { 109610679643SDaniel Borkmann *p++ = ':'; 109710679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 109810679643SDaniel Borkmann } 109910679643SDaniel Borkmann *p = '\0'; 110010679643SDaniel Borkmann 110110679643SDaniel Borkmann return string(buf, end, ip4_addr, spec); 110210679643SDaniel Borkmann } 110310679643SDaniel Borkmann 110410679643SDaniel Borkmann static noinline_for_stack 1105cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 11069ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 11079ac6e44eSJoe Perches { 11089ac6e44eSJoe Perches char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; 11099ac6e44eSJoe Perches char *p = uuid; 11109ac6e44eSJoe Perches int i; 11119ac6e44eSJoe Perches static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 11129ac6e44eSJoe Perches static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; 11139ac6e44eSJoe Perches const u8 *index = be; 11149ac6e44eSJoe Perches bool uc = false; 11159ac6e44eSJoe Perches 11169ac6e44eSJoe Perches switch (*(++fmt)) { 11179ac6e44eSJoe Perches case 'L': 11189ac6e44eSJoe Perches uc = true; /* fall-through */ 11199ac6e44eSJoe Perches case 'l': 11209ac6e44eSJoe Perches index = le; 11219ac6e44eSJoe Perches break; 11229ac6e44eSJoe Perches case 'B': 11239ac6e44eSJoe Perches uc = true; 11249ac6e44eSJoe Perches break; 11259ac6e44eSJoe Perches } 11269ac6e44eSJoe Perches 11279ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 112855036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 11299ac6e44eSJoe Perches switch (i) { 11309ac6e44eSJoe Perches case 3: 11319ac6e44eSJoe Perches case 5: 11329ac6e44eSJoe Perches case 7: 11339ac6e44eSJoe Perches case 9: 11349ac6e44eSJoe Perches *p++ = '-'; 11359ac6e44eSJoe Perches break; 11369ac6e44eSJoe Perches } 11379ac6e44eSJoe Perches } 11389ac6e44eSJoe Perches 11399ac6e44eSJoe Perches *p = 0; 11409ac6e44eSJoe Perches 11419ac6e44eSJoe Perches if (uc) { 11429ac6e44eSJoe Perches p = uuid; 11439ac6e44eSJoe Perches do { 11449ac6e44eSJoe Perches *p = toupper(*p); 11459ac6e44eSJoe Perches } while (*(++p)); 11469ac6e44eSJoe Perches } 11479ac6e44eSJoe Perches 11489ac6e44eSJoe Perches return string(buf, end, uuid, spec); 11499ac6e44eSJoe Perches } 11509ac6e44eSJoe Perches 1151c8f44affSMichał Mirosław static 1152c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr, 1153c8f44affSMichał Mirosław struct printf_spec spec) 1154c8f44affSMichał Mirosław { 1155c8f44affSMichał Mirosław spec.flags |= SPECIAL | SMALL | ZEROPAD; 1156c8f44affSMichał Mirosław if (spec.field_width == -1) 1157c8f44affSMichał Mirosław spec.field_width = 2 + 2 * sizeof(netdev_features_t); 1158c8f44affSMichał Mirosław spec.base = 16; 1159c8f44affSMichał Mirosław 1160c8f44affSMichał Mirosław return number(buf, end, *(const netdev_features_t *)addr, spec); 1161c8f44affSMichał Mirosław } 1162c8f44affSMichał Mirosław 1163aaf07621SJoe Perches static noinline_for_stack 1164aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr, 1165aaf07621SJoe Perches struct printf_spec spec, const char *fmt) 1166aaf07621SJoe Perches { 1167aaf07621SJoe Perches unsigned long long num; 1168aaf07621SJoe Perches 1169aaf07621SJoe Perches spec.flags |= SPECIAL | SMALL | ZEROPAD; 1170aaf07621SJoe Perches spec.base = 16; 1171aaf07621SJoe Perches 1172aaf07621SJoe Perches switch (fmt[1]) { 1173aaf07621SJoe Perches case 'd': 1174aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 1175aaf07621SJoe Perches spec.field_width = sizeof(dma_addr_t) * 2 + 2; 1176aaf07621SJoe Perches break; 1177aaf07621SJoe Perches case 'p': 1178aaf07621SJoe Perches default: 1179aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 1180aaf07621SJoe Perches spec.field_width = sizeof(phys_addr_t) * 2 + 2; 1181aaf07621SJoe Perches break; 1182aaf07621SJoe Perches } 1183aaf07621SJoe Perches 1184aaf07621SJoe Perches return number(buf, end, num, spec); 1185aaf07621SJoe Perches } 1186aaf07621SJoe Perches 1187411f05f1SIngo Molnar int kptr_restrict __read_mostly; 1188455cd5abSDan Rosenberg 11894d8a743cSLinus Torvalds /* 11904d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 11914d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 11924d8a743cSLinus Torvalds * specifiers. 11934d8a743cSLinus Torvalds * 1194332d2e78SLinus Torvalds * Right now we handle: 11950fe1ef24SLinus Torvalds * 11960c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 11970c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 11980efb4d20SSteven Rostedt * - 'S' For symbolic direct pointers with offset 11990efb4d20SSteven Rostedt * - 's' For symbolic direct pointers without offset 1200b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 12010f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 1202c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1203c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1204dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 1205dd45c9cfSHarvey Harrison * usual colon-separated hex notation 12068a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1207bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 1208c8e00060SJoe Perches * with a dash-separated hex notation 12097c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 12108a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 12118a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 12128a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 121310679643SDaniel Borkmann * [S][pfs] 121410679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 121510679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 12168a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 12178a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 12188a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 121910679643SDaniel Borkmann * [S][pfs] 122010679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 122110679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 122210679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 122310679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 122429cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 12259ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 12269ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 12279ac6e44eSJoe Perches * Options for %pU are: 12289ac6e44eSJoe Perches * b big endian lower case hex (default) 12299ac6e44eSJoe Perches * B big endian UPPER case hex 12309ac6e44eSJoe Perches * l little endian lower case hex 12319ac6e44eSJoe Perches * L little endian UPPER case hex 12329ac6e44eSJoe Perches * big endian output byte order is: 12339ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 12349ac6e44eSJoe Perches * little endian output byte order is: 12359ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 12367db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 12377db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 12387db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 12397db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 12407db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 1241455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 1242c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 124331550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 124431550a16SAndy Shevchenko * a certain separator (' ' by default): 124531550a16SAndy Shevchenko * C colon 124631550a16SAndy Shevchenko * D dash 124731550a16SAndy Shevchenko * N no separator 124831550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 124931550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 1250aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 1251aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 1252c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 1253c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 12549ac6e44eSJoe Perches * 1255332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1256332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 1257332d2e78SLinus Torvalds * pointer to the real address. 12584d8a743cSLinus Torvalds */ 1259cf3b429bSJoe Perches static noinline_for_stack 1260cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1261fef20d9cSFrederic Weisbecker struct printf_spec spec) 126278a8bf69SLinus Torvalds { 1263725fe002SGrant Likely int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); 1264725fe002SGrant Likely 12659f36e2c4SKees Cook if (!ptr && *fmt != 'K') { 12665e057981SJoe Perches /* 12675e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 12685e057981SJoe Perches * tabular output look nice. 12695e057981SJoe Perches */ 12705e057981SJoe Perches if (spec.field_width == -1) 1271725fe002SGrant Likely spec.field_width = default_width; 1272fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 12735e057981SJoe Perches } 1274d97106abSLinus Torvalds 12750fe1ef24SLinus Torvalds switch (*fmt) { 12760fe1ef24SLinus Torvalds case 'F': 12770c8b946eSFrederic Weisbecker case 'f': 12780fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 12790fe1ef24SLinus Torvalds /* Fallthrough */ 12800fe1ef24SLinus Torvalds case 'S': 12819ac6e44eSJoe Perches case 's': 12820f77a8d3SNamhyung Kim case 'B': 1283b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1284332d2e78SLinus Torvalds case 'R': 1285c7dabef8SBjorn Helgaas case 'r': 1286fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 128731550a16SAndy Shevchenko case 'h': 128831550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 12898a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 12908a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 129176597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 129276597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 12938a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 12948a27f7c9SJoe Perches case 'I': /* Formatted IP supported 12958a27f7c9SJoe Perches * 4: 1.2.3.4 12968a27f7c9SJoe Perches * 6: 0001:0203:...:0708 12978a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 12988a27f7c9SJoe Perches */ 12998a27f7c9SJoe Perches case 'i': /* Contiguous: 13008a27f7c9SJoe Perches * 4: 001.002.003.004 13018a27f7c9SJoe Perches * 6: 000102...0f 13028a27f7c9SJoe Perches */ 13038a27f7c9SJoe Perches switch (fmt[1]) { 13048a27f7c9SJoe Perches case '6': 13058a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 13068a27f7c9SJoe Perches case '4': 13078a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 130810679643SDaniel Borkmann case 'S': { 130910679643SDaniel Borkmann const union { 131010679643SDaniel Borkmann struct sockaddr raw; 131110679643SDaniel Borkmann struct sockaddr_in v4; 131210679643SDaniel Borkmann struct sockaddr_in6 v6; 131310679643SDaniel Borkmann } *sa = ptr; 131410679643SDaniel Borkmann 131510679643SDaniel Borkmann switch (sa->raw.sa_family) { 131610679643SDaniel Borkmann case AF_INET: 131710679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 131810679643SDaniel Borkmann case AF_INET6: 131910679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 132010679643SDaniel Borkmann default: 132110679643SDaniel Borkmann return string(buf, end, "(invalid address)", spec); 132210679643SDaniel Borkmann }} 13238a27f7c9SJoe Perches } 13244aa99606SHarvey Harrison break; 13259ac6e44eSJoe Perches case 'U': 13269ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 13277db6f5fbSJoe Perches case 'V': 13285756b76eSJan Beulich { 13295756b76eSJan Beulich va_list va; 13305756b76eSJan Beulich 13315756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 13325756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 13335756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 13345756b76eSJan Beulich va_end(va); 13355756b76eSJan Beulich return buf; 13365756b76eSJan Beulich } 1337455cd5abSDan Rosenberg case 'K': 1338455cd5abSDan Rosenberg /* 1339455cd5abSDan Rosenberg * %pK cannot be used in IRQ context because its test 1340455cd5abSDan Rosenberg * for CAP_SYSLOG would be meaningless. 1341455cd5abSDan Rosenberg */ 13423715c530SDan Rosenberg if (kptr_restrict && (in_irq() || in_serving_softirq() || 13433715c530SDan Rosenberg in_nmi())) { 1344455cd5abSDan Rosenberg if (spec.field_width == -1) 1345725fe002SGrant Likely spec.field_width = default_width; 1346455cd5abSDan Rosenberg return string(buf, end, "pK-error", spec); 1347455cd5abSDan Rosenberg } 1348312b4e22SRyan Mallon 1349312b4e22SRyan Mallon switch (kptr_restrict) { 1350312b4e22SRyan Mallon case 0: 1351312b4e22SRyan Mallon /* Always print %pK values */ 1352312b4e22SRyan Mallon break; 1353312b4e22SRyan Mallon case 1: { 1354312b4e22SRyan Mallon /* 1355312b4e22SRyan Mallon * Only print the real pointer value if the current 1356312b4e22SRyan Mallon * process has CAP_SYSLOG and is running with the 1357312b4e22SRyan Mallon * same credentials it started with. This is because 1358312b4e22SRyan Mallon * access to files is checked at open() time, but %pK 1359312b4e22SRyan Mallon * checks permission at read() time. We don't want to 1360312b4e22SRyan Mallon * leak pointer values if a binary opens a file using 1361312b4e22SRyan Mallon * %pK and then elevates privileges before reading it. 1362312b4e22SRyan Mallon */ 1363312b4e22SRyan Mallon const struct cred *cred = current_cred(); 1364312b4e22SRyan Mallon 1365312b4e22SRyan Mallon if (!has_capability_noaudit(current, CAP_SYSLOG) || 1366312b4e22SRyan Mallon !uid_eq(cred->euid, cred->uid) || 1367312b4e22SRyan Mallon !gid_eq(cred->egid, cred->gid)) 136826297607SJoe Perches ptr = NULL; 136926297607SJoe Perches break; 1370312b4e22SRyan Mallon } 1371312b4e22SRyan Mallon case 2: 1372312b4e22SRyan Mallon default: 1373312b4e22SRyan Mallon /* Always print 0's for %pK */ 1374312b4e22SRyan Mallon ptr = NULL; 1375312b4e22SRyan Mallon break; 1376312b4e22SRyan Mallon } 1377312b4e22SRyan Mallon break; 1378312b4e22SRyan Mallon 1379c8f44affSMichał Mirosław case 'N': 1380c8f44affSMichał Mirosław switch (fmt[1]) { 1381c8f44affSMichał Mirosław case 'F': 1382c8f44affSMichał Mirosław return netdev_feature_string(buf, end, ptr, spec); 1383c8f44affSMichał Mirosław } 1384c8f44affSMichał Mirosław break; 13857d799210SStepan Moskovchenko case 'a': 1386aaf07621SJoe Perches return address_val(buf, end, ptr, spec, fmt); 13874b6ccca7SAl Viro case 'd': 13884b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 13894b6ccca7SAl Viro case 'D': 13904b6ccca7SAl Viro return dentry_name(buf, end, 13914b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 13924b6ccca7SAl Viro spec, fmt); 13930fe1ef24SLinus Torvalds } 1394fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 1395fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 1396725fe002SGrant Likely spec.field_width = default_width; 1397fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 139878a8bf69SLinus Torvalds } 1399fef20d9cSFrederic Weisbecker spec.base = 16; 1400fef20d9cSFrederic Weisbecker 1401fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 1402fef20d9cSFrederic Weisbecker } 1403fef20d9cSFrederic Weisbecker 1404fef20d9cSFrederic Weisbecker /* 1405fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1406fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1407fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1408fef20d9cSFrederic Weisbecker * to go on the next call). 1409fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1410fef20d9cSFrederic Weisbecker * 1411fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1412fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1413fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 1414fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1415fef20d9cSFrederic Weisbecker * 1416fef20d9cSFrederic Weisbecker * @fmt: the format string 1417fef20d9cSFrederic Weisbecker * @type of the token returned 1418fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1419fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1420fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1421fef20d9cSFrederic Weisbecker * @precision: precision of a number 1422fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1423fef20d9cSFrederic Weisbecker */ 1424cf3b429bSJoe Perches static noinline_for_stack 1425cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 1426fef20d9cSFrederic Weisbecker { 1427fef20d9cSFrederic Weisbecker const char *start = fmt; 1428fef20d9cSFrederic Weisbecker 1429fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 1430ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 1431fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 1432fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 1433fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 1434fef20d9cSFrederic Weisbecker } 1435fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1436fef20d9cSFrederic Weisbecker goto precision; 1437fef20d9cSFrederic Weisbecker } 1438fef20d9cSFrederic Weisbecker 1439fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 1440fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 1441fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1442fef20d9cSFrederic Weisbecker spec->precision = 0; 1443fef20d9cSFrederic Weisbecker 1444fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1445fef20d9cSFrederic Weisbecker goto qualifier; 1446fef20d9cSFrederic Weisbecker } 1447fef20d9cSFrederic Weisbecker 1448fef20d9cSFrederic Weisbecker /* By default */ 1449fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1450fef20d9cSFrederic Weisbecker 1451fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 1452fef20d9cSFrederic Weisbecker if (*fmt == '%') 1453fef20d9cSFrederic Weisbecker break; 1454fef20d9cSFrederic Weisbecker } 1455fef20d9cSFrederic Weisbecker 1456fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 1457fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 1458fef20d9cSFrederic Weisbecker return fmt - start; 1459fef20d9cSFrederic Weisbecker 1460fef20d9cSFrederic Weisbecker /* Process flags */ 1461fef20d9cSFrederic Weisbecker spec->flags = 0; 1462fef20d9cSFrederic Weisbecker 1463fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 1464fef20d9cSFrederic Weisbecker bool found = true; 1465fef20d9cSFrederic Weisbecker 1466fef20d9cSFrederic Weisbecker ++fmt; 1467fef20d9cSFrederic Weisbecker 1468fef20d9cSFrederic Weisbecker switch (*fmt) { 1469fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 1470fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 1471fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 1472fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 1473fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 1474fef20d9cSFrederic Weisbecker default: found = false; 1475fef20d9cSFrederic Weisbecker } 1476fef20d9cSFrederic Weisbecker 1477fef20d9cSFrederic Weisbecker if (!found) 1478fef20d9cSFrederic Weisbecker break; 1479fef20d9cSFrederic Weisbecker } 1480fef20d9cSFrederic Weisbecker 1481fef20d9cSFrederic Weisbecker /* get field width */ 1482fef20d9cSFrederic Weisbecker spec->field_width = -1; 1483fef20d9cSFrederic Weisbecker 1484fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1485fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1486fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1487fef20d9cSFrederic Weisbecker /* it's the next argument */ 1488ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1489fef20d9cSFrederic Weisbecker return ++fmt - start; 1490fef20d9cSFrederic Weisbecker } 1491fef20d9cSFrederic Weisbecker 1492fef20d9cSFrederic Weisbecker precision: 1493fef20d9cSFrederic Weisbecker /* get the precision */ 1494fef20d9cSFrederic Weisbecker spec->precision = -1; 1495fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1496fef20d9cSFrederic Weisbecker ++fmt; 1497fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1498fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1499fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1500fef20d9cSFrederic Weisbecker spec->precision = 0; 1501fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1502fef20d9cSFrederic Weisbecker /* it's the next argument */ 1503adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1504fef20d9cSFrederic Weisbecker return ++fmt - start; 1505fef20d9cSFrederic Weisbecker } 1506fef20d9cSFrederic Weisbecker } 1507fef20d9cSFrederic Weisbecker 1508fef20d9cSFrederic Weisbecker qualifier: 1509fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1510fef20d9cSFrederic Weisbecker spec->qualifier = -1; 151175fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 151275fb8f26SAndy Shevchenko _tolower(*fmt) == 'z' || *fmt == 't') { 1513a4e94ef0SZhaolei spec->qualifier = *fmt++; 1514a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 1515a4e94ef0SZhaolei if (spec->qualifier == 'l') { 1516fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 1517fef20d9cSFrederic Weisbecker ++fmt; 1518a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 1519a4e94ef0SZhaolei spec->qualifier = 'H'; 1520a4e94ef0SZhaolei ++fmt; 1521a4e94ef0SZhaolei } 1522fef20d9cSFrederic Weisbecker } 1523fef20d9cSFrederic Weisbecker } 1524fef20d9cSFrederic Weisbecker 1525fef20d9cSFrederic Weisbecker /* default base */ 1526fef20d9cSFrederic Weisbecker spec->base = 10; 1527fef20d9cSFrederic Weisbecker switch (*fmt) { 1528fef20d9cSFrederic Weisbecker case 'c': 1529fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1530fef20d9cSFrederic Weisbecker return ++fmt - start; 1531fef20d9cSFrederic Weisbecker 1532fef20d9cSFrederic Weisbecker case 's': 1533fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1534fef20d9cSFrederic Weisbecker return ++fmt - start; 1535fef20d9cSFrederic Weisbecker 1536fef20d9cSFrederic Weisbecker case 'p': 1537fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1538fef20d9cSFrederic Weisbecker return fmt - start; 1539fef20d9cSFrederic Weisbecker /* skip alnum */ 1540fef20d9cSFrederic Weisbecker 1541fef20d9cSFrederic Weisbecker case 'n': 1542fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NRCHARS; 1543fef20d9cSFrederic Weisbecker return ++fmt - start; 1544fef20d9cSFrederic Weisbecker 1545fef20d9cSFrederic Weisbecker case '%': 1546fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1547fef20d9cSFrederic Weisbecker return ++fmt - start; 1548fef20d9cSFrederic Weisbecker 1549fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1550fef20d9cSFrederic Weisbecker case 'o': 1551fef20d9cSFrederic Weisbecker spec->base = 8; 1552fef20d9cSFrederic Weisbecker break; 1553fef20d9cSFrederic Weisbecker 1554fef20d9cSFrederic Weisbecker case 'x': 1555fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1556fef20d9cSFrederic Weisbecker 1557fef20d9cSFrederic Weisbecker case 'X': 1558fef20d9cSFrederic Weisbecker spec->base = 16; 1559fef20d9cSFrederic Weisbecker break; 1560fef20d9cSFrederic Weisbecker 1561fef20d9cSFrederic Weisbecker case 'd': 1562fef20d9cSFrederic Weisbecker case 'i': 156339e874f8SFrederic Weisbecker spec->flags |= SIGN; 1564fef20d9cSFrederic Weisbecker case 'u': 1565fef20d9cSFrederic Weisbecker break; 1566fef20d9cSFrederic Weisbecker 1567fef20d9cSFrederic Weisbecker default: 1568fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1569fef20d9cSFrederic Weisbecker return fmt - start; 1570fef20d9cSFrederic Weisbecker } 1571fef20d9cSFrederic Weisbecker 1572fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 1573fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1574fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 157539e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1576fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG; 1577fef20d9cSFrederic Weisbecker else 1578fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_ULONG; 157975fb8f26SAndy Shevchenko } else if (_tolower(spec->qualifier) == 'z') { 1580fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1581fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 1582fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1583a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 1584a4e94ef0SZhaolei if (spec->flags & SIGN) 1585a4e94ef0SZhaolei spec->type = FORMAT_TYPE_BYTE; 1586a4e94ef0SZhaolei else 1587a4e94ef0SZhaolei spec->type = FORMAT_TYPE_UBYTE; 1588fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 158939e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1590fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SHORT; 1591fef20d9cSFrederic Weisbecker else 1592fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_USHORT; 1593fef20d9cSFrederic Weisbecker } else { 159439e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1595fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INT; 1596fef20d9cSFrederic Weisbecker else 1597fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_UINT; 1598fef20d9cSFrederic Weisbecker } 1599fef20d9cSFrederic Weisbecker 1600fef20d9cSFrederic Weisbecker return ++fmt - start; 160178a8bf69SLinus Torvalds } 160278a8bf69SLinus Torvalds 16031da177e4SLinus Torvalds /** 16041da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 16051da177e4SLinus Torvalds * @buf: The buffer to place the result into 16061da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 16071da177e4SLinus Torvalds * @fmt: The format string to use 16081da177e4SLinus Torvalds * @args: Arguments for the format string 16091da177e4SLinus Torvalds * 161020036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 161191adcd2cSSteven Rostedt * %pS output the name of a text symbol with offset 161291adcd2cSSteven Rostedt * %ps output the name of a text symbol without offset 16130c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 16140c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 16150f77a8d3SNamhyung Kim * %pB output the name of a backtrace symbol with its offset 16168a79503aSUwe Kleine-König * %pR output the address range in a struct resource with decoded flags 16178a79503aSUwe Kleine-König * %pr output the address range in a struct resource with raw flags 16188a79503aSUwe Kleine-König * %pM output a 6-byte MAC address with colons 16197c59154eSAndy Shevchenko * %pMR output a 6-byte MAC address with colons in reversed order 16207c59154eSAndy Shevchenko * %pMF output a 6-byte MAC address with dashes 16218a79503aSUwe Kleine-König * %pm output a 6-byte MAC address without colons 16227c59154eSAndy Shevchenko * %pmR output a 6-byte MAC address without colons in reversed order 16238a79503aSUwe Kleine-König * %pI4 print an IPv4 address without leading zeros 16248a79503aSUwe Kleine-König * %pi4 print an IPv4 address with leading zeros 16258a79503aSUwe Kleine-König * %pI6 print an IPv6 address with colons 16268a79503aSUwe Kleine-König * %pi6 print an IPv6 address without colons 1627f996f208SJan Engelhardt * %pI6c print an IPv6 address as specified by RFC 5952 162810679643SDaniel Borkmann * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 162910679643SDaniel Borkmann * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 16308a79503aSUwe Kleine-König * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper 16318a79503aSUwe Kleine-König * case. 163231550a16SAndy Shevchenko * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 163331550a16SAndy Shevchenko * bytes of the input) 16340efb4d20SSteven Rostedt * %n is ignored 163520036fdcSAndi Kleen * 163680f548e0SAndrew Morton * ** Please update Documentation/printk-formats.txt when making changes ** 163780f548e0SAndrew Morton * 16381da177e4SLinus Torvalds * The return value is the number of characters which would 16391da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 16401da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 16411da177e4SLinus Torvalds * number of characters written into @buf as return value 164272fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 16431da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 16441da177e4SLinus Torvalds * string is truncated. 16451da177e4SLinus Torvalds * 1646ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 16471da177e4SLinus Torvalds */ 16481da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 16491da177e4SLinus Torvalds { 16501da177e4SLinus Torvalds unsigned long long num; 1651d4be151bSAndré Goddard Rosa char *str, *end; 1652fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 16531da177e4SLinus Torvalds 1654f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1655f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 16562f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 16571da177e4SLinus Torvalds return 0; 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds str = buf; 1660f796937aSJeremy Fitzhardinge end = buf + size; 16611da177e4SLinus Torvalds 1662f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1663f796937aSJeremy Fitzhardinge if (end < buf) { 16641da177e4SLinus Torvalds end = ((void *)-1); 1665f796937aSJeremy Fitzhardinge size = end - buf; 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 1668fef20d9cSFrederic Weisbecker while (*fmt) { 1669fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1670d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 1671fef20d9cSFrederic Weisbecker 1672fef20d9cSFrederic Weisbecker fmt += read; 1673fef20d9cSFrederic Weisbecker 1674fef20d9cSFrederic Weisbecker switch (spec.type) { 1675fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1676fef20d9cSFrederic Weisbecker int copy = read; 1677fef20d9cSFrederic Weisbecker if (str < end) { 1678fef20d9cSFrederic Weisbecker if (copy > end - str) 1679fef20d9cSFrederic Weisbecker copy = end - str; 1680fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1681fef20d9cSFrederic Weisbecker } 1682fef20d9cSFrederic Weisbecker str += read; 1683fef20d9cSFrederic Weisbecker break; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 1686ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1687fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1688fef20d9cSFrederic Weisbecker break; 16891da177e4SLinus Torvalds 1690fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1691fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1692fef20d9cSFrederic Weisbecker break; 16931da177e4SLinus Torvalds 1694d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 1695d4be151bSAndré Goddard Rosa char c; 1696d4be151bSAndré Goddard Rosa 1697fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1698fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1699f796937aSJeremy Fitzhardinge if (str < end) 17001da177e4SLinus Torvalds *str = ' '; 17011da177e4SLinus Torvalds ++str; 1702fef20d9cSFrederic Weisbecker 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1706f796937aSJeremy Fitzhardinge if (str < end) 17071da177e4SLinus Torvalds *str = c; 17081da177e4SLinus Torvalds ++str; 1709fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1710f796937aSJeremy Fitzhardinge if (str < end) 17111da177e4SLinus Torvalds *str = ' '; 17121da177e4SLinus Torvalds ++str; 17131da177e4SLinus Torvalds } 1714fef20d9cSFrederic Weisbecker break; 1715d4be151bSAndré Goddard Rosa } 17161da177e4SLinus Torvalds 1717fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1718fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1719fef20d9cSFrederic Weisbecker break; 17201da177e4SLinus Torvalds 1721fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1722fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, va_arg(args, void *), 1723fef20d9cSFrederic Weisbecker spec); 1724fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 17254d8a743cSLinus Torvalds fmt++; 1726fef20d9cSFrederic Weisbecker break; 17271da177e4SLinus Torvalds 1728fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1729f796937aSJeremy Fitzhardinge if (str < end) 17301da177e4SLinus Torvalds *str = '%'; 17311da177e4SLinus Torvalds ++str; 17321da177e4SLinus Torvalds break; 17331da177e4SLinus Torvalds 1734fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1735f796937aSJeremy Fitzhardinge if (str < end) 17361da177e4SLinus Torvalds *str = '%'; 17371da177e4SLinus Torvalds ++str; 1738fef20d9cSFrederic Weisbecker break; 1739fef20d9cSFrederic Weisbecker 1740fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 17419196436aSKees Cook /* 17429196436aSKees Cook * Since %n poses a greater security risk than 17439196436aSKees Cook * utility, ignore %n and skip its argument. 17449196436aSKees Cook */ 17459196436aSKees Cook void *skip_arg; 1746fef20d9cSFrederic Weisbecker 17479196436aSKees Cook WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", 17489196436aSKees Cook old_fmt); 17499196436aSKees Cook 17509196436aSKees Cook skip_arg = va_arg(args, void *); 1751fef20d9cSFrederic Weisbecker break; 1752fef20d9cSFrederic Weisbecker } 1753fef20d9cSFrederic Weisbecker 1754fef20d9cSFrederic Weisbecker default: 1755fef20d9cSFrederic Weisbecker switch (spec.type) { 1756fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1757fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1758fef20d9cSFrederic Weisbecker break; 1759fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1760fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1761fef20d9cSFrederic Weisbecker break; 1762fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1763fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1764fef20d9cSFrederic Weisbecker break; 1765fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1766ef124960SJason Gunthorpe if (spec.flags & SIGN) 1767ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 1768ef124960SJason Gunthorpe else 1769fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1770fef20d9cSFrederic Weisbecker break; 1771fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1772fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1773fef20d9cSFrederic Weisbecker break; 1774a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1775a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1776a4e94ef0SZhaolei break; 1777a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1778a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1779a4e94ef0SZhaolei break; 1780fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1781fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1782fef20d9cSFrederic Weisbecker break; 1783fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1784fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1785fef20d9cSFrederic Weisbecker break; 178639e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 178739e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1788fef20d9cSFrederic Weisbecker break; 1789fef20d9cSFrederic Weisbecker default: 1790fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 17911da177e4SLinus Torvalds } 1792fef20d9cSFrederic Weisbecker 1793fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 17941da177e4SLinus Torvalds } 1795fef20d9cSFrederic Weisbecker } 1796fef20d9cSFrederic Weisbecker 1797f796937aSJeremy Fitzhardinge if (size > 0) { 1798f796937aSJeremy Fitzhardinge if (str < end) 17991da177e4SLinus Torvalds *str = '\0'; 1800f796937aSJeremy Fitzhardinge else 18010a6047eeSLinus Torvalds end[-1] = '\0'; 1802f796937aSJeremy Fitzhardinge } 1803fef20d9cSFrederic Weisbecker 1804f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 18051da177e4SLinus Torvalds return str-buf; 1806fef20d9cSFrederic Weisbecker 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds /** 18111da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 18121da177e4SLinus Torvalds * @buf: The buffer to place the result into 18131da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18141da177e4SLinus Torvalds * @fmt: The format string to use 18151da177e4SLinus Torvalds * @args: Arguments for the format string 18161da177e4SLinus Torvalds * 18171da177e4SLinus Torvalds * The return value is the number of characters which have been written into 1818b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 18191da177e4SLinus Torvalds * returns 0. 18201da177e4SLinus Torvalds * 1821ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 182220036fdcSAndi Kleen * 182320036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 18241da177e4SLinus Torvalds */ 18251da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 18261da177e4SLinus Torvalds { 18271da177e4SLinus Torvalds int i; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 18307b9186f5SAndré Goddard Rosa 1831b921c69fSAnton Arapov if (likely(i < size)) 1832b921c69fSAnton Arapov return i; 1833b921c69fSAnton Arapov if (size != 0) 1834b921c69fSAnton Arapov return size - 1; 1835b921c69fSAnton Arapov return 0; 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds /** 18401da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 18411da177e4SLinus Torvalds * @buf: The buffer to place the result into 18421da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18431da177e4SLinus Torvalds * @fmt: The format string to use 18441da177e4SLinus Torvalds * @...: Arguments for the format string 18451da177e4SLinus Torvalds * 18461da177e4SLinus Torvalds * The return value is the number of characters which would be 18471da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 18481da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 18491da177e4SLinus Torvalds * @size, the resulting string is truncated. 185020036fdcSAndi Kleen * 185120036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 18521da177e4SLinus Torvalds */ 18531da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 18541da177e4SLinus Torvalds { 18551da177e4SLinus Torvalds va_list args; 18561da177e4SLinus Torvalds int i; 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds va_start(args, fmt); 18591da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 18601da177e4SLinus Torvalds va_end(args); 18617b9186f5SAndré Goddard Rosa 18621da177e4SLinus Torvalds return i; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 18651da177e4SLinus Torvalds 18661da177e4SLinus Torvalds /** 18671da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 18681da177e4SLinus Torvalds * @buf: The buffer to place the result into 18691da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18701da177e4SLinus Torvalds * @fmt: The format string to use 18711da177e4SLinus Torvalds * @...: Arguments for the format string 18721da177e4SLinus Torvalds * 18731da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 1874b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 18751da177e4SLinus Torvalds */ 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 18781da177e4SLinus Torvalds { 18791da177e4SLinus Torvalds va_list args; 18801da177e4SLinus Torvalds int i; 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds va_start(args, fmt); 1883b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 18841da177e4SLinus Torvalds va_end(args); 18857b9186f5SAndré Goddard Rosa 1886b903c0b8SChangli Gao return i; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds /** 18911da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 18921da177e4SLinus Torvalds * @buf: The buffer to place the result into 18931da177e4SLinus Torvalds * @fmt: The format string to use 18941da177e4SLinus Torvalds * @args: Arguments for the format string 18951da177e4SLinus Torvalds * 18961da177e4SLinus Torvalds * The function returns the number of characters written 189772fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 18981da177e4SLinus Torvalds * buffer overflows. 18991da177e4SLinus Torvalds * 1900ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 190120036fdcSAndi Kleen * 190220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 19031da177e4SLinus Torvalds */ 19041da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 19051da177e4SLinus Torvalds { 19061da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /** 19111da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 19121da177e4SLinus Torvalds * @buf: The buffer to place the result into 19131da177e4SLinus Torvalds * @fmt: The format string to use 19141da177e4SLinus Torvalds * @...: Arguments for the format string 19151da177e4SLinus Torvalds * 19161da177e4SLinus Torvalds * The function returns the number of characters written 191772fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 19181da177e4SLinus Torvalds * buffer overflows. 191920036fdcSAndi Kleen * 192020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 19211da177e4SLinus Torvalds */ 19221da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 19231da177e4SLinus Torvalds { 19241da177e4SLinus Torvalds va_list args; 19251da177e4SLinus Torvalds int i; 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds va_start(args, fmt); 19281da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 19291da177e4SLinus Torvalds va_end(args); 19307b9186f5SAndré Goddard Rosa 19311da177e4SLinus Torvalds return i; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 19341da177e4SLinus Torvalds 19354370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 19364370aa4aSLai Jiangshan /* 19374370aa4aSLai Jiangshan * bprintf service: 19384370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 19394370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 19404370aa4aSLai Jiangshan */ 19414370aa4aSLai Jiangshan 19424370aa4aSLai Jiangshan /** 19434370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 19444370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 19454370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 19464370aa4aSLai Jiangshan * @fmt: The format string to use 19474370aa4aSLai Jiangshan * @args: Arguments for the format string 19484370aa4aSLai Jiangshan * 19494370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 19504370aa4aSLai Jiangshan * is skiped. 19514370aa4aSLai Jiangshan * 19524370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 19534370aa4aSLai Jiangshan * the given input. 19544370aa4aSLai Jiangshan * 19554370aa4aSLai Jiangshan * NOTE: 19564370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 19574370aa4aSLai Jiangshan * valid for bstr_printf(). 19584370aa4aSLai Jiangshan */ 19594370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 19604370aa4aSLai Jiangshan { 1961fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 19624370aa4aSLai Jiangshan char *str, *end; 19634370aa4aSLai Jiangshan 19644370aa4aSLai Jiangshan str = (char *)bin_buf; 19654370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 19664370aa4aSLai Jiangshan 19674370aa4aSLai Jiangshan #define save_arg(type) \ 19684370aa4aSLai Jiangshan do { \ 19694370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 19704370aa4aSLai Jiangshan unsigned long long value; \ 19714370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 19724370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 19734370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 19744370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 19754370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 19764370aa4aSLai Jiangshan } \ 19774370aa4aSLai Jiangshan } else { \ 19784370aa4aSLai Jiangshan unsigned long value; \ 19794370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 19804370aa4aSLai Jiangshan value = va_arg(args, int); \ 19814370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 19824370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 19834370aa4aSLai Jiangshan } \ 19844370aa4aSLai Jiangshan str += sizeof(type); \ 19854370aa4aSLai Jiangshan } while (0) 19864370aa4aSLai Jiangshan 1987fef20d9cSFrederic Weisbecker while (*fmt) { 1988d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 19894370aa4aSLai Jiangshan 1990fef20d9cSFrederic Weisbecker fmt += read; 1991fef20d9cSFrederic Weisbecker 1992fef20d9cSFrederic Weisbecker switch (spec.type) { 1993fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 1994d4be151bSAndré Goddard Rosa case FORMAT_TYPE_INVALID: 1995d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 1996fef20d9cSFrederic Weisbecker break; 1997fef20d9cSFrederic Weisbecker 1998ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1999fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 20004370aa4aSLai Jiangshan save_arg(int); 2001fef20d9cSFrederic Weisbecker break; 20024370aa4aSLai Jiangshan 2003fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 20044370aa4aSLai Jiangshan save_arg(char); 2005fef20d9cSFrederic Weisbecker break; 2006fef20d9cSFrederic Weisbecker 2007fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 20084370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 20094370aa4aSLai Jiangshan size_t len; 20106c356634SAndré Goddard Rosa 20114370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 20124370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 20130f4f81dcSAndré Goddard Rosa save_str = "(null)"; 20146c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 20156c356634SAndré Goddard Rosa if (str + len < end) 20166c356634SAndré Goddard Rosa memcpy(str, save_str, len); 20176c356634SAndré Goddard Rosa str += len; 2018fef20d9cSFrederic Weisbecker break; 20194370aa4aSLai Jiangshan } 2020fef20d9cSFrederic Weisbecker 2021fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 20224370aa4aSLai Jiangshan save_arg(void *); 20234370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2024fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 20254370aa4aSLai Jiangshan fmt++; 2026fef20d9cSFrederic Weisbecker break; 2027fef20d9cSFrederic Weisbecker 2028fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 20294370aa4aSLai Jiangshan /* skip %n 's argument */ 2030ef0658f3SJoe Perches u8 qualifier = spec.qualifier; 20314370aa4aSLai Jiangshan void *skip_arg; 20324370aa4aSLai Jiangshan if (qualifier == 'l') 20334370aa4aSLai Jiangshan skip_arg = va_arg(args, long *); 203475fb8f26SAndy Shevchenko else if (_tolower(qualifier) == 'z') 20354370aa4aSLai Jiangshan skip_arg = va_arg(args, size_t *); 20364370aa4aSLai Jiangshan else 20374370aa4aSLai Jiangshan skip_arg = va_arg(args, int *); 2038fef20d9cSFrederic Weisbecker break; 20394370aa4aSLai Jiangshan } 20404370aa4aSLai Jiangshan 2041fef20d9cSFrederic Weisbecker default: 2042fef20d9cSFrederic Weisbecker switch (spec.type) { 2043fef20d9cSFrederic Weisbecker 2044fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2045fef20d9cSFrederic Weisbecker save_arg(long long); 2046fef20d9cSFrederic Weisbecker break; 2047fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2048fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2049fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2050fef20d9cSFrederic Weisbecker break; 2051fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2052fef20d9cSFrederic Weisbecker save_arg(size_t); 2053fef20d9cSFrederic Weisbecker break; 2054fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2055fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2056fef20d9cSFrederic Weisbecker break; 2057a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2058a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2059a4e94ef0SZhaolei save_arg(char); 2060a4e94ef0SZhaolei break; 2061fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2062fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2063fef20d9cSFrederic Weisbecker save_arg(short); 2064fef20d9cSFrederic Weisbecker break; 2065fef20d9cSFrederic Weisbecker default: 2066fef20d9cSFrederic Weisbecker save_arg(int); 2067fef20d9cSFrederic Weisbecker } 2068fef20d9cSFrederic Weisbecker } 2069fef20d9cSFrederic Weisbecker } 2070fef20d9cSFrederic Weisbecker 20717b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2072fef20d9cSFrederic Weisbecker #undef save_arg 20734370aa4aSLai Jiangshan } 20744370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 20754370aa4aSLai Jiangshan 20764370aa4aSLai Jiangshan /** 20774370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 20784370aa4aSLai Jiangshan * @buf: The buffer to place the result into 20794370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 20804370aa4aSLai Jiangshan * @fmt: The format string to use 20814370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 20824370aa4aSLai Jiangshan * 20834370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 20844370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 20854370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 20864370aa4aSLai Jiangshan * 20874370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 20880efb4d20SSteven Rostedt * see vsnprintf comment for details. 20894370aa4aSLai Jiangshan * 20904370aa4aSLai Jiangshan * The return value is the number of characters which would 20914370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 20924370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 20934370aa4aSLai Jiangshan * number of characters written into @buf as return value 20944370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 20954370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 20964370aa4aSLai Jiangshan * string is truncated. 20974370aa4aSLai Jiangshan */ 20984370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 20994370aa4aSLai Jiangshan { 2100fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2101d4be151bSAndré Goddard Rosa char *str, *end; 2102d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 21034370aa4aSLai Jiangshan 21042f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 21054370aa4aSLai Jiangshan return 0; 21064370aa4aSLai Jiangshan 21074370aa4aSLai Jiangshan str = buf; 21084370aa4aSLai Jiangshan end = buf + size; 21094370aa4aSLai Jiangshan 21104370aa4aSLai Jiangshan #define get_arg(type) \ 21114370aa4aSLai Jiangshan ({ \ 21124370aa4aSLai Jiangshan typeof(type) value; \ 21134370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 21144370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 21154370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 21164370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 21174370aa4aSLai Jiangshan } else { \ 21184370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 21194370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 21204370aa4aSLai Jiangshan } \ 21214370aa4aSLai Jiangshan args += sizeof(type); \ 21224370aa4aSLai Jiangshan value; \ 21234370aa4aSLai Jiangshan }) 21244370aa4aSLai Jiangshan 21254370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 21264370aa4aSLai Jiangshan if (end < buf) { 21274370aa4aSLai Jiangshan end = ((void *)-1); 21284370aa4aSLai Jiangshan size = end - buf; 21294370aa4aSLai Jiangshan } 21304370aa4aSLai Jiangshan 2131fef20d9cSFrederic Weisbecker while (*fmt) { 2132fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2133d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2134fef20d9cSFrederic Weisbecker 2135fef20d9cSFrederic Weisbecker fmt += read; 2136fef20d9cSFrederic Weisbecker 2137fef20d9cSFrederic Weisbecker switch (spec.type) { 2138fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2139fef20d9cSFrederic Weisbecker int copy = read; 2140fef20d9cSFrederic Weisbecker if (str < end) { 2141fef20d9cSFrederic Weisbecker if (copy > end - str) 2142fef20d9cSFrederic Weisbecker copy = end - str; 2143fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2144fef20d9cSFrederic Weisbecker } 2145fef20d9cSFrederic Weisbecker str += read; 2146fef20d9cSFrederic Weisbecker break; 21474370aa4aSLai Jiangshan } 21484370aa4aSLai Jiangshan 2149ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2150fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 2151fef20d9cSFrederic Weisbecker break; 21524370aa4aSLai Jiangshan 2153fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2154fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 2155fef20d9cSFrederic Weisbecker break; 21564370aa4aSLai Jiangshan 2157d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2158d4be151bSAndré Goddard Rosa char c; 2159d4be151bSAndré Goddard Rosa 2160fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2161fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 21624370aa4aSLai Jiangshan if (str < end) 21634370aa4aSLai Jiangshan *str = ' '; 21644370aa4aSLai Jiangshan ++str; 21654370aa4aSLai Jiangshan } 21664370aa4aSLai Jiangshan } 21674370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 21684370aa4aSLai Jiangshan if (str < end) 21694370aa4aSLai Jiangshan *str = c; 21704370aa4aSLai Jiangshan ++str; 2171fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 21724370aa4aSLai Jiangshan if (str < end) 21734370aa4aSLai Jiangshan *str = ' '; 21744370aa4aSLai Jiangshan ++str; 21754370aa4aSLai Jiangshan } 2176fef20d9cSFrederic Weisbecker break; 2177d4be151bSAndré Goddard Rosa } 21784370aa4aSLai Jiangshan 2179fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 21804370aa4aSLai Jiangshan const char *str_arg = args; 2181d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2182fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2183fef20d9cSFrederic Weisbecker break; 21844370aa4aSLai Jiangshan } 21854370aa4aSLai Jiangshan 2186fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2187fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, get_arg(void *), spec); 2188fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 21894370aa4aSLai Jiangshan fmt++; 2190fef20d9cSFrederic Weisbecker break; 21914370aa4aSLai Jiangshan 2192fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2193fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 21944370aa4aSLai Jiangshan if (str < end) 21954370aa4aSLai Jiangshan *str = '%'; 21964370aa4aSLai Jiangshan ++str; 2197fef20d9cSFrederic Weisbecker break; 2198fef20d9cSFrederic Weisbecker 2199fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: 2200fef20d9cSFrederic Weisbecker /* skip */ 2201fef20d9cSFrederic Weisbecker break; 2202fef20d9cSFrederic Weisbecker 2203d4be151bSAndré Goddard Rosa default: { 2204d4be151bSAndré Goddard Rosa unsigned long long num; 2205d4be151bSAndré Goddard Rosa 2206fef20d9cSFrederic Weisbecker switch (spec.type) { 2207fef20d9cSFrederic Weisbecker 2208fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 22094370aa4aSLai Jiangshan num = get_arg(long long); 2210fef20d9cSFrederic Weisbecker break; 2211fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2212fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2213fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2214fef20d9cSFrederic Weisbecker break; 2215fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 22164370aa4aSLai Jiangshan num = get_arg(size_t); 2217fef20d9cSFrederic Weisbecker break; 2218fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 22194370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2220fef20d9cSFrederic Weisbecker break; 2221a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2222a4e94ef0SZhaolei num = get_arg(unsigned char); 2223a4e94ef0SZhaolei break; 2224a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2225a4e94ef0SZhaolei num = get_arg(signed char); 2226a4e94ef0SZhaolei break; 2227fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2228fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2229fef20d9cSFrederic Weisbecker break; 2230fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2231fef20d9cSFrederic Weisbecker num = get_arg(short); 2232fef20d9cSFrederic Weisbecker break; 2233fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 22344370aa4aSLai Jiangshan num = get_arg(unsigned int); 2235fef20d9cSFrederic Weisbecker break; 2236fef20d9cSFrederic Weisbecker default: 2237fef20d9cSFrederic Weisbecker num = get_arg(int); 22384370aa4aSLai Jiangshan } 2239fef20d9cSFrederic Weisbecker 2240fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2241d4be151bSAndré Goddard Rosa } /* default: */ 2242d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2243d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2244fef20d9cSFrederic Weisbecker 22454370aa4aSLai Jiangshan if (size > 0) { 22464370aa4aSLai Jiangshan if (str < end) 22474370aa4aSLai Jiangshan *str = '\0'; 22484370aa4aSLai Jiangshan else 22494370aa4aSLai Jiangshan end[-1] = '\0'; 22504370aa4aSLai Jiangshan } 2251fef20d9cSFrederic Weisbecker 22524370aa4aSLai Jiangshan #undef get_arg 22534370aa4aSLai Jiangshan 22544370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 22554370aa4aSLai Jiangshan return str - buf; 22564370aa4aSLai Jiangshan } 22574370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 22584370aa4aSLai Jiangshan 22594370aa4aSLai Jiangshan /** 22604370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 22614370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 22624370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 22634370aa4aSLai Jiangshan * @fmt: The format string to use 22644370aa4aSLai Jiangshan * @...: Arguments for the format string 22654370aa4aSLai Jiangshan * 22664370aa4aSLai Jiangshan * The function returns the number of words(u32) written 22674370aa4aSLai Jiangshan * into @bin_buf. 22684370aa4aSLai Jiangshan */ 22694370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 22704370aa4aSLai Jiangshan { 22714370aa4aSLai Jiangshan va_list args; 22724370aa4aSLai Jiangshan int ret; 22734370aa4aSLai Jiangshan 22744370aa4aSLai Jiangshan va_start(args, fmt); 22754370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 22764370aa4aSLai Jiangshan va_end(args); 22777b9186f5SAndré Goddard Rosa 22784370aa4aSLai Jiangshan return ret; 22794370aa4aSLai Jiangshan } 22804370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 22814370aa4aSLai Jiangshan 22824370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 22834370aa4aSLai Jiangshan 22841da177e4SLinus Torvalds /** 22851da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 22861da177e4SLinus Torvalds * @buf: input buffer 22871da177e4SLinus Torvalds * @fmt: format of buffer 22881da177e4SLinus Torvalds * @args: arguments 22891da177e4SLinus Torvalds */ 22901da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 22911da177e4SLinus Torvalds { 22921da177e4SLinus Torvalds const char *str = buf; 22931da177e4SLinus Torvalds char *next; 22941da177e4SLinus Torvalds char digit; 22951da177e4SLinus Torvalds int num = 0; 2296ef0658f3SJoe Perches u8 qualifier; 229753809751SJan Beulich unsigned int base; 229853809751SJan Beulich union { 229953809751SJan Beulich long long s; 230053809751SJan Beulich unsigned long long u; 230153809751SJan Beulich } val; 2302ef0658f3SJoe Perches s16 field_width; 2303d4be151bSAndré Goddard Rosa bool is_sign; 23041da177e4SLinus Torvalds 2305da99075cSJan Beulich while (*fmt) { 23061da177e4SLinus Torvalds /* skip any white space in format */ 23071da177e4SLinus Torvalds /* white space in format matchs any amount of 23081da177e4SLinus Torvalds * white space, including none, in the input. 23091da177e4SLinus Torvalds */ 23101da177e4SLinus Torvalds if (isspace(*fmt)) { 2311e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 2312e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 23131da177e4SLinus Torvalds } 23141da177e4SLinus Torvalds 23151da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 23161da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 23171da177e4SLinus Torvalds if (*fmt++ != *str++) 23181da177e4SLinus Torvalds break; 23191da177e4SLinus Torvalds continue; 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds if (!*fmt) 23231da177e4SLinus Torvalds break; 23241da177e4SLinus Torvalds ++fmt; 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds /* skip this conversion. 23271da177e4SLinus Torvalds * advance both strings to next white space 23281da177e4SLinus Torvalds */ 23291da177e4SLinus Torvalds if (*fmt == '*') { 2330da99075cSJan Beulich if (!*str) 2331da99075cSJan Beulich break; 23328fccae2cSAndy Spencer while (!isspace(*fmt) && *fmt != '%' && *fmt) 23331da177e4SLinus Torvalds fmt++; 23341da177e4SLinus Torvalds while (!isspace(*str) && *str) 23351da177e4SLinus Torvalds str++; 23361da177e4SLinus Torvalds continue; 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds /* get field width */ 23401da177e4SLinus Torvalds field_width = -1; 234153809751SJan Beulich if (isdigit(*fmt)) { 23421da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 234353809751SJan Beulich if (field_width <= 0) 234453809751SJan Beulich break; 234553809751SJan Beulich } 23461da177e4SLinus Torvalds 23471da177e4SLinus Torvalds /* get conversion qualifier */ 23481da177e4SLinus Torvalds qualifier = -1; 234975fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 235075fb8f26SAndy Shevchenko _tolower(*fmt) == 'z') { 23511da177e4SLinus Torvalds qualifier = *fmt++; 23521da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 23531da177e4SLinus Torvalds if (qualifier == 'h') { 23541da177e4SLinus Torvalds qualifier = 'H'; 23551da177e4SLinus Torvalds fmt++; 23561da177e4SLinus Torvalds } else if (qualifier == 'l') { 23571da177e4SLinus Torvalds qualifier = 'L'; 23581da177e4SLinus Torvalds fmt++; 23591da177e4SLinus Torvalds } 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 2363da99075cSJan Beulich if (!*fmt) 2364da99075cSJan Beulich break; 2365da99075cSJan Beulich 2366da99075cSJan Beulich if (*fmt == 'n') { 2367da99075cSJan Beulich /* return number of characters read so far */ 2368da99075cSJan Beulich *va_arg(args, int *) = str - buf; 2369da99075cSJan Beulich ++fmt; 2370da99075cSJan Beulich continue; 2371da99075cSJan Beulich } 2372da99075cSJan Beulich 2373da99075cSJan Beulich if (!*str) 23741da177e4SLinus Torvalds break; 23751da177e4SLinus Torvalds 2376d4be151bSAndré Goddard Rosa base = 10; 2377d4be151bSAndré Goddard Rosa is_sign = 0; 2378d4be151bSAndré Goddard Rosa 23791da177e4SLinus Torvalds switch (*fmt++) { 23801da177e4SLinus Torvalds case 'c': 23811da177e4SLinus Torvalds { 23821da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 23831da177e4SLinus Torvalds if (field_width == -1) 23841da177e4SLinus Torvalds field_width = 1; 23851da177e4SLinus Torvalds do { 23861da177e4SLinus Torvalds *s++ = *str++; 23871da177e4SLinus Torvalds } while (--field_width > 0 && *str); 23881da177e4SLinus Torvalds num++; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds continue; 23911da177e4SLinus Torvalds case 's': 23921da177e4SLinus Torvalds { 23931da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 23941da177e4SLinus Torvalds if (field_width == -1) 23954be929beSAlexey Dobriyan field_width = SHRT_MAX; 23961da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 2397e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds /* now copy until next white space */ 24007b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 24011da177e4SLinus Torvalds *s++ = *str++; 24021da177e4SLinus Torvalds *s = '\0'; 24031da177e4SLinus Torvalds num++; 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds continue; 24061da177e4SLinus Torvalds case 'o': 24071da177e4SLinus Torvalds base = 8; 24081da177e4SLinus Torvalds break; 24091da177e4SLinus Torvalds case 'x': 24101da177e4SLinus Torvalds case 'X': 24111da177e4SLinus Torvalds base = 16; 24121da177e4SLinus Torvalds break; 24131da177e4SLinus Torvalds case 'i': 24141da177e4SLinus Torvalds base = 0; 24151da177e4SLinus Torvalds case 'd': 24161da177e4SLinus Torvalds is_sign = 1; 24171da177e4SLinus Torvalds case 'u': 24181da177e4SLinus Torvalds break; 24191da177e4SLinus Torvalds case '%': 24201da177e4SLinus Torvalds /* looking for '%' in str */ 24211da177e4SLinus Torvalds if (*str++ != '%') 24221da177e4SLinus Torvalds return num; 24231da177e4SLinus Torvalds continue; 24241da177e4SLinus Torvalds default: 24251da177e4SLinus Torvalds /* invalid format; stop here */ 24261da177e4SLinus Torvalds return num; 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds /* have some sort of integer conversion. 24301da177e4SLinus Torvalds * first, skip white space in buffer. 24311da177e4SLinus Torvalds */ 2432e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds digit = *str; 24351da177e4SLinus Torvalds if (is_sign && digit == '-') 24361da177e4SLinus Torvalds digit = *(str + 1); 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds if (!digit 24391da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 24401da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 24411da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 24421da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 24431da177e4SLinus Torvalds break; 24441da177e4SLinus Torvalds 244553809751SJan Beulich if (is_sign) 244653809751SJan Beulich val.s = qualifier != 'L' ? 244753809751SJan Beulich simple_strtol(str, &next, base) : 244853809751SJan Beulich simple_strtoll(str, &next, base); 244953809751SJan Beulich else 245053809751SJan Beulich val.u = qualifier != 'L' ? 245153809751SJan Beulich simple_strtoul(str, &next, base) : 245253809751SJan Beulich simple_strtoull(str, &next, base); 245353809751SJan Beulich 245453809751SJan Beulich if (field_width > 0 && next - str > field_width) { 245553809751SJan Beulich if (base == 0) 245653809751SJan Beulich _parse_integer_fixup_radix(str, &base); 245753809751SJan Beulich while (next - str > field_width) { 245853809751SJan Beulich if (is_sign) 245953809751SJan Beulich val.s = div_s64(val.s, base); 246053809751SJan Beulich else 246153809751SJan Beulich val.u = div_u64(val.u, base); 246253809751SJan Beulich --next; 246353809751SJan Beulich } 246453809751SJan Beulich } 246553809751SJan Beulich 24661da177e4SLinus Torvalds switch (qualifier) { 24671da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 246853809751SJan Beulich if (is_sign) 246953809751SJan Beulich *va_arg(args, signed char *) = val.s; 247053809751SJan Beulich else 247153809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 24721da177e4SLinus Torvalds break; 24731da177e4SLinus Torvalds case 'h': 247453809751SJan Beulich if (is_sign) 247553809751SJan Beulich *va_arg(args, short *) = val.s; 247653809751SJan Beulich else 247753809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 24781da177e4SLinus Torvalds break; 24791da177e4SLinus Torvalds case 'l': 248053809751SJan Beulich if (is_sign) 248153809751SJan Beulich *va_arg(args, long *) = val.s; 248253809751SJan Beulich else 248353809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 24841da177e4SLinus Torvalds break; 24851da177e4SLinus Torvalds case 'L': 248653809751SJan Beulich if (is_sign) 248753809751SJan Beulich *va_arg(args, long long *) = val.s; 248853809751SJan Beulich else 248953809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 24901da177e4SLinus Torvalds break; 24911da177e4SLinus Torvalds case 'Z': 24921da177e4SLinus Torvalds case 'z': 249353809751SJan Beulich *va_arg(args, size_t *) = val.u; 24941da177e4SLinus Torvalds break; 24951da177e4SLinus Torvalds default: 249653809751SJan Beulich if (is_sign) 249753809751SJan Beulich *va_arg(args, int *) = val.s; 249853809751SJan Beulich else 249953809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 25001da177e4SLinus Torvalds break; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds num++; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds if (!next) 25051da177e4SLinus Torvalds break; 25061da177e4SLinus Torvalds str = next; 25071da177e4SLinus Torvalds } 2508c6b40d16SJohannes Berg 25091da177e4SLinus Torvalds return num; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds /** 25141da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 25151da177e4SLinus Torvalds * @buf: input buffer 25161da177e4SLinus Torvalds * @fmt: formatting of buffer 25171da177e4SLinus Torvalds * @...: resulting arguments 25181da177e4SLinus Torvalds */ 25191da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 25201da177e4SLinus Torvalds { 25211da177e4SLinus Torvalds va_list args; 25221da177e4SLinus Torvalds int i; 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds va_start(args, fmt); 25251da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 25261da177e4SLinus Torvalds va_end(args); 25277b9186f5SAndré Goddard Rosa 25281da177e4SLinus Torvalds return i; 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2531