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> 20900cca29SGeert Uytterhoeven #include <linux/clk-provider.h> 218bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 221da177e4SLinus Torvalds #include <linux/types.h> 231da177e4SLinus Torvalds #include <linux/string.h> 241da177e4SLinus Torvalds #include <linux/ctype.h> 251da177e4SLinus Torvalds #include <linux/kernel.h> 260fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 2753809751SJan Beulich #include <linux/math64.h> 280fe1ef24SLinus Torvalds #include <linux/uaccess.h> 29332d2e78SLinus Torvalds #include <linux/ioport.h> 304b6ccca7SAl Viro #include <linux/dcache.h> 31312b4e22SRyan Mallon #include <linux/cred.h> 328a27f7c9SJoe Perches #include <net/addrconf.h> 331da177e4SLinus Torvalds 344e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 35deac93dfSJames Bottomley #include <asm/sections.h> /* for dereference_function_descriptor() */ 361da177e4SLinus Torvalds 3771dca95dSAndy Shevchenko #include <linux/string_helpers.h> 381dff46d6SAlexey Dobriyan #include "kstrtox.h" 39aa46a63eSHarvey Harrison 401da177e4SLinus Torvalds /** 411da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 421da177e4SLinus Torvalds * @cp: The start of the string 431da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 441da177e4SLinus Torvalds * @base: The number base to use 45462e4711SEldad Zack * 46462e4711SEldad Zack * This function is obsolete. Please use kstrtoull instead. 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 491da177e4SLinus Torvalds { 501dff46d6SAlexey Dobriyan unsigned long long result; 511dff46d6SAlexey Dobriyan unsigned int rv; 521da177e4SLinus Torvalds 531dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 541dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 551dff46d6SAlexey Dobriyan /* FIXME */ 561dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 57aa46a63eSHarvey Harrison 581da177e4SLinus Torvalds if (endp) 591da177e4SLinus Torvalds *endp = (char *)cp; 607b9186f5SAndré Goddard Rosa 611da177e4SLinus Torvalds return result; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /** 66922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 67922ac25cSAndré Goddard Rosa * @cp: The start of the string 68922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 69922ac25cSAndré Goddard Rosa * @base: The number base to use 70462e4711SEldad Zack * 71462e4711SEldad Zack * This function is obsolete. Please use kstrtoul instead. 72922ac25cSAndré Goddard Rosa */ 73922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 74922ac25cSAndré Goddard Rosa { 75922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 76922ac25cSAndré Goddard Rosa } 77922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 78922ac25cSAndré Goddard Rosa 79922ac25cSAndré Goddard Rosa /** 80922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 81922ac25cSAndré Goddard Rosa * @cp: The start of the string 82922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 83922ac25cSAndré Goddard Rosa * @base: The number base to use 84462e4711SEldad Zack * 85462e4711SEldad Zack * This function is obsolete. Please use kstrtol instead. 86922ac25cSAndré Goddard Rosa */ 87922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 88922ac25cSAndré Goddard Rosa { 89922ac25cSAndré Goddard Rosa if (*cp == '-') 90922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 91922ac25cSAndré Goddard Rosa 92922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 93922ac25cSAndré Goddard Rosa } 94922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 95922ac25cSAndré Goddard Rosa 96922ac25cSAndré Goddard Rosa /** 971da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 981da177e4SLinus Torvalds * @cp: The start of the string 991da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1001da177e4SLinus Torvalds * @base: The number base to use 101462e4711SEldad Zack * 102462e4711SEldad Zack * This function is obsolete. Please use kstrtoll instead. 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds if (*cp == '-') 1071da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1087b9186f5SAndré Goddard Rosa 1091da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1101da177e4SLinus Torvalds } 11198d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1121da177e4SLinus Torvalds 113cf3b429bSJoe Perches static noinline_for_stack 114cf3b429bSJoe Perches int skip_atoi(const char **s) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds int i = 0; 1171da177e4SLinus Torvalds 11843e5b666SRasmus Villemoes do { 1191da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 12043e5b666SRasmus Villemoes } while (isdigit(**s)); 1217b9186f5SAndré Goddard Rosa 1221da177e4SLinus Torvalds return i; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1254277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used 1264277eeddSDenis Vlasenko * for /proc and /sys data. This directly impacts e.g. top performance 1274277eeddSDenis Vlasenko * with many processes running. We optimize it for speed 128133fd9f5SDenys Vlasenko * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html> 129133fd9f5SDenys Vlasenko * (with permission from the author, Douglas W. Jones). 130133fd9f5SDenys Vlasenko */ 1314277eeddSDenis Vlasenko 132133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 133133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */ 134cf3b429bSJoe Perches static noinline_for_stack 135133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q) 1364277eeddSDenis Vlasenko { 137133fd9f5SDenys Vlasenko unsigned r; 1384277eeddSDenis Vlasenko 1397b9186f5SAndré Goddard Rosa /* 1407b9186f5SAndré Goddard Rosa * Possible ways to approx. divide by 10 141133fd9f5SDenys Vlasenko * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) 142133fd9f5SDenys Vlasenko * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) 143133fd9f5SDenys Vlasenko * (x * 0x6667) >> 18 x < 43699 144133fd9f5SDenys Vlasenko * (x * 0x3334) >> 17 x < 16389 145133fd9f5SDenys Vlasenko * (x * 0x199a) >> 16 x < 16389 146133fd9f5SDenys Vlasenko * (x * 0x0ccd) >> 15 x < 16389 147133fd9f5SDenys Vlasenko * (x * 0x0667) >> 14 x < 2739 148133fd9f5SDenys Vlasenko * (x * 0x0334) >> 13 x < 1029 149133fd9f5SDenys Vlasenko * (x * 0x019a) >> 12 x < 1029 150133fd9f5SDenys Vlasenko * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) 151133fd9f5SDenys Vlasenko * (x * 0x0067) >> 10 x < 179 152133fd9f5SDenys Vlasenko * (x * 0x0034) >> 9 x < 69 same 153133fd9f5SDenys Vlasenko * (x * 0x001a) >> 8 x < 69 same 154133fd9f5SDenys Vlasenko * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) 155133fd9f5SDenys Vlasenko * (x * 0x0007) >> 6 x < 19 156133fd9f5SDenys Vlasenko * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html> 1577b9186f5SAndré Goddard Rosa */ 158133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 159133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 1 */ 160133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 161133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 2 */ 162133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 163133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 3 */ 164133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 165133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 4 */ 166133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 167133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 5 */ 168133fd9f5SDenys Vlasenko /* Now value is under 10000, can avoid 64-bit multiply */ 169133fd9f5SDenys Vlasenko q = (r * 0x199a) >> 16; 170133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 6 */ 171133fd9f5SDenys Vlasenko r = (q * 0xcd) >> 11; 172133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 7 */ 173133fd9f5SDenys Vlasenko q = (r * 0xcd) >> 11; 174133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 8 */ 175133fd9f5SDenys Vlasenko *buf++ = q + '0'; /* 9 */ 176133fd9f5SDenys Vlasenko return buf; 177133fd9f5SDenys Vlasenko } 178133fd9f5SDenys Vlasenko #endif 1794277eeddSDenis Vlasenko 180133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros. 181133fd9f5SDenys Vlasenko * Code can be easily arranged to print 9 digits too, but our callers 182133fd9f5SDenys Vlasenko * always call put_dec_full9() instead when the number has 9 decimal digits. 183133fd9f5SDenys Vlasenko */ 184133fd9f5SDenys Vlasenko static noinline_for_stack 185133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 186133fd9f5SDenys Vlasenko { 187133fd9f5SDenys Vlasenko unsigned q; 1884277eeddSDenis Vlasenko 189133fd9f5SDenys Vlasenko /* Copy of previous function's body with added early returns */ 190cb239d0aSGeorge Spelvin while (r >= 10000) { 191cb239d0aSGeorge Spelvin q = r + '0'; 192cb239d0aSGeorge Spelvin r = (r * (uint64_t)0x1999999a) >> 32; 193cb239d0aSGeorge Spelvin *buf++ = q - 10*r; 194cb239d0aSGeorge Spelvin } 195cb239d0aSGeorge Spelvin 196f4000516SGeorge Spelvin q = (r * 0x199a) >> 16; /* r <= 9999 */ 197f4000516SGeorge Spelvin *buf++ = (r - 10 * q) + '0'; 198133fd9f5SDenys Vlasenko if (q == 0) 199133fd9f5SDenys Vlasenko return buf; 200f4000516SGeorge Spelvin r = (q * 0xcd) >> 11; /* q <= 999 */ 201f4000516SGeorge Spelvin *buf++ = (q - 10 * r) + '0'; 202133fd9f5SDenys Vlasenko if (r == 0) 203133fd9f5SDenys Vlasenko return buf; 204f4000516SGeorge Spelvin q = (r * 0xcd) >> 11; /* r <= 99 */ 205f4000516SGeorge Spelvin *buf++ = (r - 10 * q) + '0'; 206133fd9f5SDenys Vlasenko if (q == 0) 207133fd9f5SDenys Vlasenko return buf; 208f4000516SGeorge Spelvin *buf++ = q + '0'; /* q <= 9 */ 209133fd9f5SDenys Vlasenko return buf; 210133fd9f5SDenys Vlasenko } 211133fd9f5SDenys Vlasenko 212133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers. 213133fd9f5SDenys Vlasenko * One is generic: divide by 1000000000 and repeatedly print 214133fd9f5SDenys Vlasenko * groups of (up to) 9 digits. It's conceptually simple, 215133fd9f5SDenys Vlasenko * but requires a (unsigned long long) / 1000000000 division. 216133fd9f5SDenys Vlasenko * 217133fd9f5SDenys Vlasenko * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, 218133fd9f5SDenys Vlasenko * manipulates them cleverly and generates groups of 4 decimal digits. 219133fd9f5SDenys Vlasenko * It so happens that it does NOT require long long division. 220133fd9f5SDenys Vlasenko * 221133fd9f5SDenys Vlasenko * If long is > 32 bits, division of 64-bit values is relatively easy, 222133fd9f5SDenys Vlasenko * and we will use the first algorithm. 223133fd9f5SDenys Vlasenko * If long long is > 64 bits (strange architecture with VERY large long long), 224133fd9f5SDenys Vlasenko * second algorithm can't be used, and we again use the first one. 225133fd9f5SDenys Vlasenko * 226133fd9f5SDenys Vlasenko * Else (if long is 32 bits and long long is 64 bits) we use second one. 227133fd9f5SDenys Vlasenko */ 228133fd9f5SDenys Vlasenko 229133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 230133fd9f5SDenys Vlasenko 231133fd9f5SDenys Vlasenko /* First algorithm: generic */ 232133fd9f5SDenys Vlasenko 233133fd9f5SDenys Vlasenko static 234133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 235133fd9f5SDenys Vlasenko { 236133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) { 237133fd9f5SDenys Vlasenko while (n >= 1000*1000*1000) 238133fd9f5SDenys Vlasenko buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); 239133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 240133fd9f5SDenys Vlasenko return put_dec_full9(buf, n); 241133fd9f5SDenys Vlasenko } 242133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 243133fd9f5SDenys Vlasenko } 244133fd9f5SDenys Vlasenko 245133fd9f5SDenys Vlasenko #else 246133fd9f5SDenys Vlasenko 247133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */ 248133fd9f5SDenys Vlasenko 249e49317d4SGeorge Spelvin /* See comment in put_dec_full9 for choice of constants */ 250133fd9f5SDenys Vlasenko static noinline_for_stack 2512359172aSGeorge Spelvin void put_dec_full4(char *buf, unsigned q) 252133fd9f5SDenys Vlasenko { 253133fd9f5SDenys Vlasenko unsigned r; 254e49317d4SGeorge Spelvin r = (q * 0xccd) >> 15; 2552359172aSGeorge Spelvin buf[0] = (q - 10 * r) + '0'; 256e49317d4SGeorge Spelvin q = (r * 0xcd) >> 11; 2572359172aSGeorge Spelvin buf[1] = (r - 10 * q) + '0'; 258133fd9f5SDenys Vlasenko r = (q * 0xcd) >> 11; 2592359172aSGeorge Spelvin buf[2] = (q - 10 * r) + '0'; 2602359172aSGeorge Spelvin buf[3] = r + '0'; 2612359172aSGeorge Spelvin } 2622359172aSGeorge Spelvin 2632359172aSGeorge Spelvin /* 2642359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2652359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2662359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2672359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2682359172aSGeorge Spelvin * (d1 in the put_dec code, assuming n is all-ones). 2692359172aSGeorge Spelvin */ 2702359172aSGeorge Spelvin static 2712359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2722359172aSGeorge Spelvin { 2732359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2742359172aSGeorge Spelvin 2752359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2762359172aSGeorge Spelvin return q; 277133fd9f5SDenys Vlasenko } 278133fd9f5SDenys Vlasenko 279133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 280133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 281133fd9f5SDenys Vlasenko * (with permission from the author). 282133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 283133fd9f5SDenys Vlasenko */ 284133fd9f5SDenys Vlasenko static 285133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 286133fd9f5SDenys Vlasenko { 287133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 288133fd9f5SDenys Vlasenko 289133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 290133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 291133fd9f5SDenys Vlasenko 292133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 293133fd9f5SDenys Vlasenko h = (n >> 32); 294133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 295133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 296133fd9f5SDenys Vlasenko 297133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 2982359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 299133fd9f5SDenys Vlasenko 3002359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3012359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 302133fd9f5SDenys Vlasenko 3032359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3042359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 305133fd9f5SDenys Vlasenko 3062359172aSGeorge Spelvin q += 281 * d3; 3072359172aSGeorge Spelvin buf += 12; 3082359172aSGeorge Spelvin if (q) 3092359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3102359172aSGeorge Spelvin else while (buf[-1] == '0') 311133fd9f5SDenys Vlasenko --buf; 3127b9186f5SAndré Goddard Rosa 3134277eeddSDenis Vlasenko return buf; 3144277eeddSDenis Vlasenko } 315133fd9f5SDenys Vlasenko 316133fd9f5SDenys Vlasenko #endif 3174277eeddSDenis Vlasenko 3181ac101a5SKAMEZAWA Hiroyuki /* 3191ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3201ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3211ac101a5SKAMEZAWA Hiroyuki * 3221ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3231ac101a5SKAMEZAWA Hiroyuki */ 3241ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num) 3251ac101a5SKAMEZAWA Hiroyuki { 326133fd9f5SDenys Vlasenko char tmp[sizeof(num) * 3]; 3271ac101a5SKAMEZAWA Hiroyuki int idx, len; 3281ac101a5SKAMEZAWA Hiroyuki 329133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 330133fd9f5SDenys Vlasenko if (num <= 9) { 331133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 332133fd9f5SDenys Vlasenko len = 1; 333133fd9f5SDenys Vlasenko } else { 3341ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 335133fd9f5SDenys Vlasenko } 3361ac101a5SKAMEZAWA Hiroyuki 3371ac101a5SKAMEZAWA Hiroyuki if (len > size) 3381ac101a5SKAMEZAWA Hiroyuki return 0; 3391ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 3401ac101a5SKAMEZAWA Hiroyuki buf[idx] = tmp[len - idx - 1]; 3411ac101a5SKAMEZAWA Hiroyuki return len; 3421ac101a5SKAMEZAWA Hiroyuki } 3431ac101a5SKAMEZAWA Hiroyuki 34451be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 345d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3461da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3471da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 348d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 349b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 350b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3511da177e4SLinus Torvalds 352fef20d9cSFrederic Weisbecker enum format_type { 353fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 354ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 355fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 356fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 357fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 358fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 359fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 360fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 361fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 362fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 363fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 364a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 365a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 366fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 367fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 368fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 369fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 370fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 371fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 372fef20d9cSFrederic Weisbecker }; 373fef20d9cSFrederic Weisbecker 374fef20d9cSFrederic Weisbecker struct printf_spec { 3754e310fdaSJoe Perches u8 type; /* format_type enum */ 376ef0658f3SJoe Perches u8 flags; /* flags to number() */ 3774e310fdaSJoe Perches u8 base; /* number base, 8, 10 or 16 only */ 3784e310fdaSJoe Perches u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ 3794e310fdaSJoe Perches s16 field_width; /* width of output field */ 3804e310fdaSJoe Perches s16 precision; /* # of digits/chars */ 381fef20d9cSFrederic Weisbecker }; 382fef20d9cSFrederic Weisbecker 383cf3b429bSJoe Perches static noinline_for_stack 384cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 385fef20d9cSFrederic Weisbecker struct printf_spec spec) 3861da177e4SLinus Torvalds { 387e26c12c7SRasmus Villemoes char tmp[3 * sizeof(num)]; 3889b706aeeSDenys Vlasenko char sign; 3899b706aeeSDenys Vlasenko char locase; 390fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 3911da177e4SLinus Torvalds int i; 3927c203422SPierre Carrier bool is_zero = num == 0LL; 3931da177e4SLinus Torvalds 3949b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 3959b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 396fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 397fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 398fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 3991da177e4SLinus Torvalds sign = 0; 400fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4011da177e4SLinus Torvalds if ((signed long long)num < 0) { 4021da177e4SLinus Torvalds sign = '-'; 4031da177e4SLinus Torvalds num = -(signed long long)num; 404fef20d9cSFrederic Weisbecker spec.field_width--; 405fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4061da177e4SLinus Torvalds sign = '+'; 407fef20d9cSFrederic Weisbecker spec.field_width--; 408fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4091da177e4SLinus Torvalds sign = ' '; 410fef20d9cSFrederic Weisbecker spec.field_width--; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds } 413b39a7340SDenis Vlasenko if (need_pfx) { 414fef20d9cSFrederic Weisbecker if (spec.base == 16) 4157c203422SPierre Carrier spec.field_width -= 2; 4167c203422SPierre Carrier else if (!is_zero) 417fef20d9cSFrederic Weisbecker spec.field_width--; 4181da177e4SLinus Torvalds } 419b39a7340SDenis Vlasenko 420b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4211da177e4SLinus Torvalds i = 0; 422133fd9f5SDenys Vlasenko if (num < spec.base) 4233ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 424fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 425fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 426b39a7340SDenis Vlasenko int shift = 3; 4277b9186f5SAndré Goddard Rosa 4287b9186f5SAndré Goddard Rosa if (spec.base == 16) 4297b9186f5SAndré Goddard Rosa shift = 4; 430b39a7340SDenis Vlasenko do { 4313ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 432b39a7340SDenis Vlasenko num >>= shift; 433b39a7340SDenis Vlasenko } while (num); 4344277eeddSDenis Vlasenko } else { /* base 10 */ 4354277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4364277eeddSDenis Vlasenko } 437b39a7340SDenis Vlasenko 438b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 439fef20d9cSFrederic Weisbecker if (i > spec.precision) 440fef20d9cSFrederic Weisbecker spec.precision = i; 441b39a7340SDenis Vlasenko /* leading space padding */ 442fef20d9cSFrederic Weisbecker spec.field_width -= spec.precision; 44351be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 444fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 445f796937aSJeremy Fitzhardinge if (buf < end) 4461da177e4SLinus Torvalds *buf = ' '; 4471da177e4SLinus Torvalds ++buf; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds } 450b39a7340SDenis Vlasenko /* sign */ 4511da177e4SLinus Torvalds if (sign) { 452f796937aSJeremy Fitzhardinge if (buf < end) 4531da177e4SLinus Torvalds *buf = sign; 4541da177e4SLinus Torvalds ++buf; 4551da177e4SLinus Torvalds } 456b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 457b39a7340SDenis Vlasenko if (need_pfx) { 4587c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 459f796937aSJeremy Fitzhardinge if (buf < end) 4601da177e4SLinus Torvalds *buf = '0'; 4611da177e4SLinus Torvalds ++buf; 4627c203422SPierre Carrier } 463fef20d9cSFrederic Weisbecker if (spec.base == 16) { 464f796937aSJeremy Fitzhardinge if (buf < end) 4659b706aeeSDenys Vlasenko *buf = ('X' | locase); 4661da177e4SLinus Torvalds ++buf; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds } 469b39a7340SDenis Vlasenko /* zero or space padding */ 470fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 471d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 472d1c1b121SRasmus Villemoes BUILD_BUG_ON(' ' + ZEROPAD != '0'); 473fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 474f796937aSJeremy Fitzhardinge if (buf < end) 4751da177e4SLinus Torvalds *buf = c; 4761da177e4SLinus Torvalds ++buf; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds } 479b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 480fef20d9cSFrederic Weisbecker while (i <= --spec.precision) { 481f796937aSJeremy Fitzhardinge if (buf < end) 4821da177e4SLinus Torvalds *buf = '0'; 4831da177e4SLinus Torvalds ++buf; 4841da177e4SLinus Torvalds } 485b39a7340SDenis Vlasenko /* actual digits of result */ 486b39a7340SDenis Vlasenko while (--i >= 0) { 487f796937aSJeremy Fitzhardinge if (buf < end) 4881da177e4SLinus Torvalds *buf = tmp[i]; 4891da177e4SLinus Torvalds ++buf; 4901da177e4SLinus Torvalds } 491b39a7340SDenis Vlasenko /* trailing space padding */ 492fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 493f796937aSJeremy Fitzhardinge if (buf < end) 4941da177e4SLinus Torvalds *buf = ' '; 4951da177e4SLinus Torvalds ++buf; 4961da177e4SLinus Torvalds } 4977b9186f5SAndré Goddard Rosa 4981da177e4SLinus Torvalds return buf; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 501cf3b429bSJoe Perches static noinline_for_stack 502cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec) 5030f9bfa56SLinus Torvalds { 5040f9bfa56SLinus Torvalds int len, i; 5050f9bfa56SLinus Torvalds 5060f9bfa56SLinus Torvalds if ((unsigned long)s < PAGE_SIZE) 5070f4f81dcSAndré Goddard Rosa s = "(null)"; 5080f9bfa56SLinus Torvalds 509fef20d9cSFrederic Weisbecker len = strnlen(s, spec.precision); 5100f9bfa56SLinus Torvalds 511fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 512fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5130f9bfa56SLinus Torvalds if (buf < end) 5140f9bfa56SLinus Torvalds *buf = ' '; 5150f9bfa56SLinus Torvalds ++buf; 5160f9bfa56SLinus Torvalds } 5170f9bfa56SLinus Torvalds } 5180f9bfa56SLinus Torvalds for (i = 0; i < len; ++i) { 5190f9bfa56SLinus Torvalds if (buf < end) 5200f9bfa56SLinus Torvalds *buf = *s; 5210f9bfa56SLinus Torvalds ++buf; ++s; 5220f9bfa56SLinus Torvalds } 523fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5240f9bfa56SLinus Torvalds if (buf < end) 5250f9bfa56SLinus Torvalds *buf = ' '; 5260f9bfa56SLinus Torvalds ++buf; 5270f9bfa56SLinus Torvalds } 5287b9186f5SAndré Goddard Rosa 5290f9bfa56SLinus Torvalds return buf; 5300f9bfa56SLinus Torvalds } 5310f9bfa56SLinus Torvalds 5324b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces) 5334b6ccca7SAl Viro { 5344b6ccca7SAl Viro size_t size; 5354b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5364b6ccca7SAl Viro return; 5374b6ccca7SAl Viro size = end - buf; 5384b6ccca7SAl Viro if (size <= spaces) { 5394b6ccca7SAl Viro memset(buf, ' ', size); 5404b6ccca7SAl Viro return; 5414b6ccca7SAl Viro } 5424b6ccca7SAl Viro if (len) { 5434b6ccca7SAl Viro if (len > size - spaces) 5444b6ccca7SAl Viro len = size - spaces; 5454b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5464b6ccca7SAl Viro } 5474b6ccca7SAl Viro memset(buf, ' ', spaces); 5484b6ccca7SAl Viro } 5494b6ccca7SAl Viro 5504b6ccca7SAl Viro static noinline_for_stack 5514b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 5524b6ccca7SAl Viro const char *fmt) 5534b6ccca7SAl Viro { 5544b6ccca7SAl Viro const char *array[4], *s; 5554b6ccca7SAl Viro const struct dentry *p; 5564b6ccca7SAl Viro int depth; 5574b6ccca7SAl Viro int i, n; 5584b6ccca7SAl Viro 5594b6ccca7SAl Viro switch (fmt[1]) { 5604b6ccca7SAl Viro case '2': case '3': case '4': 5614b6ccca7SAl Viro depth = fmt[1] - '0'; 5624b6ccca7SAl Viro break; 5634b6ccca7SAl Viro default: 5644b6ccca7SAl Viro depth = 1; 5654b6ccca7SAl Viro } 5664b6ccca7SAl Viro 5674b6ccca7SAl Viro rcu_read_lock(); 5684b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 5694b6ccca7SAl Viro p = ACCESS_ONCE(d->d_parent); 5704b6ccca7SAl Viro array[i] = ACCESS_ONCE(d->d_name.name); 5714b6ccca7SAl Viro if (p == d) { 5724b6ccca7SAl Viro if (i) 5734b6ccca7SAl Viro array[i] = ""; 5744b6ccca7SAl Viro i++; 5754b6ccca7SAl Viro break; 5764b6ccca7SAl Viro } 5774b6ccca7SAl Viro } 5784b6ccca7SAl Viro s = array[--i]; 5794b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 5804b6ccca7SAl Viro char c = *s++; 5814b6ccca7SAl Viro if (!c) { 5824b6ccca7SAl Viro if (!i) 5834b6ccca7SAl Viro break; 5844b6ccca7SAl Viro c = '/'; 5854b6ccca7SAl Viro s = array[--i]; 5864b6ccca7SAl Viro } 5874b6ccca7SAl Viro if (buf < end) 5884b6ccca7SAl Viro *buf = c; 5894b6ccca7SAl Viro } 5904b6ccca7SAl Viro rcu_read_unlock(); 5914b6ccca7SAl Viro if (n < spec.field_width) { 5924b6ccca7SAl Viro /* we want to pad the sucker */ 5934b6ccca7SAl Viro unsigned spaces = spec.field_width - n; 5944b6ccca7SAl Viro if (!(spec.flags & LEFT)) { 5954b6ccca7SAl Viro widen(buf - n, end, n, spaces); 5964b6ccca7SAl Viro return buf + spaces; 5974b6ccca7SAl Viro } 5984b6ccca7SAl Viro while (spaces--) { 5994b6ccca7SAl Viro if (buf < end) 6004b6ccca7SAl Viro *buf = ' '; 6014b6ccca7SAl Viro ++buf; 6024b6ccca7SAl Viro } 6034b6ccca7SAl Viro } 6044b6ccca7SAl Viro return buf; 6054b6ccca7SAl Viro } 6064b6ccca7SAl Viro 607cf3b429bSJoe Perches static noinline_for_stack 608cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 609b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 6100fe1ef24SLinus Torvalds { 611b0d33c2bSJoe Perches unsigned long value; 6120fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 6130fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 614b0d33c2bSJoe Perches #endif 615b0d33c2bSJoe Perches 616b0d33c2bSJoe Perches if (fmt[1] == 'R') 617b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 618b0d33c2bSJoe Perches value = (unsigned long)ptr; 619b0d33c2bSJoe Perches 620b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 621b0d33c2bSJoe Perches if (*fmt == 'B') 6220f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 623b0d33c2bSJoe Perches else if (*fmt != 'f' && *fmt != 's') 6240fe1ef24SLinus Torvalds sprint_symbol(sym, value); 6250c8b946eSFrederic Weisbecker else 6264796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 6277b9186f5SAndré Goddard Rosa 628fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 6290fe1ef24SLinus Torvalds #else 630fef20d9cSFrederic Weisbecker spec.field_width = 2 * sizeof(void *); 631fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL | SMALL | ZEROPAD; 632fef20d9cSFrederic Weisbecker spec.base = 16; 6337b9186f5SAndré Goddard Rosa 634fef20d9cSFrederic Weisbecker return number(buf, end, value, spec); 6350fe1ef24SLinus Torvalds #endif 6360fe1ef24SLinus Torvalds } 6370fe1ef24SLinus Torvalds 638cf3b429bSJoe Perches static noinline_for_stack 639cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 640fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 641332d2e78SLinus Torvalds { 642332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 64328405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 644332d2e78SLinus Torvalds #endif 645332d2e78SLinus Torvalds 646332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 64728405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 648332d2e78SLinus Torvalds #endif 6494da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 650fef20d9cSFrederic Weisbecker .base = 16, 6514da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 652fef20d9cSFrederic Weisbecker .precision = -1, 653fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 654fef20d9cSFrederic Weisbecker }; 6554da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 6564da0b66cSBjorn Helgaas .base = 16, 6574da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 6584da0b66cSBjorn Helgaas .precision = -1, 6594da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 6604da0b66cSBjorn Helgaas }; 6610f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 6620f4050c7SBjorn Helgaas .base = 16, 6630f4050c7SBjorn Helgaas .field_width = 2, 6640f4050c7SBjorn Helgaas .precision = -1, 6650f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 6660f4050c7SBjorn Helgaas }; 6674da0b66cSBjorn Helgaas static const struct printf_spec dec_spec = { 668c91d3376SBjorn Helgaas .base = 10, 669c91d3376SBjorn Helgaas .precision = -1, 670c91d3376SBjorn Helgaas .flags = 0, 671c91d3376SBjorn Helgaas }; 6724da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 673fd95541eSBjorn Helgaas .field_width = -1, 674fd95541eSBjorn Helgaas .precision = 10, 675fd95541eSBjorn Helgaas .flags = LEFT, 676fd95541eSBjorn Helgaas }; 6774da0b66cSBjorn Helgaas static const struct printf_spec flag_spec = { 678fd95541eSBjorn Helgaas .base = 16, 679fd95541eSBjorn Helgaas .precision = -1, 680fd95541eSBjorn Helgaas .flags = SPECIAL | SMALL, 681fd95541eSBjorn Helgaas }; 682c7dabef8SBjorn Helgaas 683c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 684c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 685c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 686c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 6879d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 688c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 689c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 690c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 691c7dabef8SBjorn Helgaas 692332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 693c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 6944da0b66cSBjorn Helgaas const struct printf_spec *specp; 695332d2e78SLinus Torvalds 696332d2e78SLinus Torvalds *p++ = '['; 6974da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 698fd95541eSBjorn Helgaas p = string(p, pend, "io ", str_spec); 6994da0b66cSBjorn Helgaas specp = &io_spec; 7004da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 701fd95541eSBjorn Helgaas p = string(p, pend, "mem ", str_spec); 7024da0b66cSBjorn Helgaas specp = &mem_spec; 7034da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 704fd95541eSBjorn Helgaas p = string(p, pend, "irq ", str_spec); 7054da0b66cSBjorn Helgaas specp = &dec_spec; 7064da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 707fd95541eSBjorn Helgaas p = string(p, pend, "dma ", str_spec); 7084da0b66cSBjorn Helgaas specp = &dec_spec; 7090f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 7100f4050c7SBjorn Helgaas p = string(p, pend, "bus ", str_spec); 7110f4050c7SBjorn Helgaas specp = &bus_spec; 7124da0b66cSBjorn Helgaas } else { 713c7dabef8SBjorn Helgaas p = string(p, pend, "??? ", str_spec); 7144da0b66cSBjorn Helgaas specp = &mem_spec; 715c7dabef8SBjorn Helgaas decode = 0; 716fd95541eSBjorn Helgaas } 717d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 718d19cb803SBjorn Helgaas p = string(p, pend, "size ", str_spec); 719d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 720d19cb803SBjorn Helgaas } else { 7214da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 722c91d3376SBjorn Helgaas if (res->start != res->end) { 723332d2e78SLinus Torvalds *p++ = '-'; 7244da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 725c91d3376SBjorn Helgaas } 726d19cb803SBjorn Helgaas } 727c7dabef8SBjorn Helgaas if (decode) { 728fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 729fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 730fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 731fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 7329d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 7339d7cca04SBjorn Helgaas p = string(p, pend, " window", str_spec); 734fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 735fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 736c7dabef8SBjorn Helgaas } else { 737fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 738c7dabef8SBjorn Helgaas p = number(p, pend, res->flags, flag_spec); 739fd95541eSBjorn Helgaas } 740332d2e78SLinus Torvalds *p++ = ']'; 741c7dabef8SBjorn Helgaas *p = '\0'; 742332d2e78SLinus Torvalds 743fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 744332d2e78SLinus Torvalds } 745332d2e78SLinus Torvalds 746cf3b429bSJoe Perches static noinline_for_stack 74731550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 74831550a16SAndy Shevchenko const char *fmt) 74931550a16SAndy Shevchenko { 750360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 75131550a16SAndy Shevchenko negative value, fallback to the default */ 75231550a16SAndy Shevchenko char separator; 75331550a16SAndy Shevchenko 75431550a16SAndy Shevchenko if (spec.field_width == 0) 75531550a16SAndy Shevchenko /* nothing to print */ 75631550a16SAndy Shevchenko return buf; 75731550a16SAndy Shevchenko 75831550a16SAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 75931550a16SAndy Shevchenko /* NULL pointer */ 76031550a16SAndy Shevchenko return string(buf, end, NULL, spec); 76131550a16SAndy Shevchenko 76231550a16SAndy Shevchenko switch (fmt[1]) { 76331550a16SAndy Shevchenko case 'C': 76431550a16SAndy Shevchenko separator = ':'; 76531550a16SAndy Shevchenko break; 76631550a16SAndy Shevchenko case 'D': 76731550a16SAndy Shevchenko separator = '-'; 76831550a16SAndy Shevchenko break; 76931550a16SAndy Shevchenko case 'N': 77031550a16SAndy Shevchenko separator = 0; 77131550a16SAndy Shevchenko break; 77231550a16SAndy Shevchenko default: 77331550a16SAndy Shevchenko separator = ' '; 77431550a16SAndy Shevchenko break; 77531550a16SAndy Shevchenko } 77631550a16SAndy Shevchenko 77731550a16SAndy Shevchenko if (spec.field_width > 0) 77831550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 77931550a16SAndy Shevchenko 7809c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 7819c98f235SRasmus Villemoes if (buf < end) 7829c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 7839c98f235SRasmus Villemoes ++buf; 7849c98f235SRasmus Villemoes if (buf < end) 7859c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 7869c98f235SRasmus Villemoes ++buf; 78731550a16SAndy Shevchenko 7889c98f235SRasmus Villemoes if (separator && i != len - 1) { 7899c98f235SRasmus Villemoes if (buf < end) 7909c98f235SRasmus Villemoes *buf = separator; 7919c98f235SRasmus Villemoes ++buf; 7929c98f235SRasmus Villemoes } 79331550a16SAndy Shevchenko } 79431550a16SAndy Shevchenko 79531550a16SAndy Shevchenko return buf; 79631550a16SAndy Shevchenko } 79731550a16SAndy Shevchenko 79831550a16SAndy Shevchenko static noinline_for_stack 799dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 800dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 801dbc760bcSTejun Heo { 802dbc760bcSTejun Heo const int CHUNKSZ = 32; 803dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 804dbc760bcSTejun Heo int i, chunksz; 805dbc760bcSTejun Heo bool first = true; 806dbc760bcSTejun Heo 807dbc760bcSTejun Heo /* reused to print numbers */ 808dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 809dbc760bcSTejun Heo 810dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 811dbc760bcSTejun Heo if (chunksz == 0) 812dbc760bcSTejun Heo chunksz = CHUNKSZ; 813dbc760bcSTejun Heo 814dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 815dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 816dbc760bcSTejun Heo u32 chunkmask, val; 817dbc760bcSTejun Heo int word, bit; 818dbc760bcSTejun Heo 819dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 820dbc760bcSTejun Heo word = i / BITS_PER_LONG; 821dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 822dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 823dbc760bcSTejun Heo 824dbc760bcSTejun Heo if (!first) { 825dbc760bcSTejun Heo if (buf < end) 826dbc760bcSTejun Heo *buf = ','; 827dbc760bcSTejun Heo buf++; 828dbc760bcSTejun Heo } 829dbc760bcSTejun Heo first = false; 830dbc760bcSTejun Heo 831dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 832dbc760bcSTejun Heo buf = number(buf, end, val, spec); 833dbc760bcSTejun Heo 834dbc760bcSTejun Heo chunksz = CHUNKSZ; 835dbc760bcSTejun Heo } 836dbc760bcSTejun Heo return buf; 837dbc760bcSTejun Heo } 838dbc760bcSTejun Heo 839dbc760bcSTejun Heo static noinline_for_stack 840dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 841dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 842dbc760bcSTejun Heo { 843dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 844dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 845dbc760bcSTejun Heo int cur, rbot, rtop; 846dbc760bcSTejun Heo bool first = true; 847dbc760bcSTejun Heo 848dbc760bcSTejun Heo /* reused to print numbers */ 849dbc760bcSTejun Heo spec = (struct printf_spec){ .base = 10 }; 850dbc760bcSTejun Heo 851dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 852dbc760bcSTejun Heo while (cur < nr_bits) { 853dbc760bcSTejun Heo rtop = cur; 854dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 855dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 856dbc760bcSTejun Heo continue; 857dbc760bcSTejun Heo 858dbc760bcSTejun Heo if (!first) { 859dbc760bcSTejun Heo if (buf < end) 860dbc760bcSTejun Heo *buf = ','; 861dbc760bcSTejun Heo buf++; 862dbc760bcSTejun Heo } 863dbc760bcSTejun Heo first = false; 864dbc760bcSTejun Heo 865dbc760bcSTejun Heo buf = number(buf, end, rbot, spec); 866dbc760bcSTejun Heo if (rbot < rtop) { 867dbc760bcSTejun Heo if (buf < end) 868dbc760bcSTejun Heo *buf = '-'; 869dbc760bcSTejun Heo buf++; 870dbc760bcSTejun Heo 871dbc760bcSTejun Heo buf = number(buf, end, rtop, spec); 872dbc760bcSTejun Heo } 873dbc760bcSTejun Heo 874dbc760bcSTejun Heo rbot = cur; 875dbc760bcSTejun Heo } 876dbc760bcSTejun Heo return buf; 877dbc760bcSTejun Heo } 878dbc760bcSTejun Heo 879dbc760bcSTejun Heo static noinline_for_stack 880cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 8818a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 882dd45c9cfSHarvey Harrison { 8838a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 884dd45c9cfSHarvey Harrison char *p = mac_addr; 885dd45c9cfSHarvey Harrison int i; 886bc7259a2SJoe Perches char separator; 88776597ff9SAndrei Emeltchenko bool reversed = false; 888bc7259a2SJoe Perches 88976597ff9SAndrei Emeltchenko switch (fmt[1]) { 89076597ff9SAndrei Emeltchenko case 'F': 891bc7259a2SJoe Perches separator = '-'; 89276597ff9SAndrei Emeltchenko break; 89376597ff9SAndrei Emeltchenko 89476597ff9SAndrei Emeltchenko case 'R': 89576597ff9SAndrei Emeltchenko reversed = true; 89676597ff9SAndrei Emeltchenko /* fall through */ 89776597ff9SAndrei Emeltchenko 89876597ff9SAndrei Emeltchenko default: 899bc7259a2SJoe Perches separator = ':'; 90076597ff9SAndrei Emeltchenko break; 901bc7259a2SJoe Perches } 902dd45c9cfSHarvey Harrison 903dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 90476597ff9SAndrei Emeltchenko if (reversed) 90576597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 90676597ff9SAndrei Emeltchenko else 90755036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 90876597ff9SAndrei Emeltchenko 9098a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 910bc7259a2SJoe Perches *p++ = separator; 911dd45c9cfSHarvey Harrison } 912dd45c9cfSHarvey Harrison *p = '\0'; 913dd45c9cfSHarvey Harrison 914fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 915dd45c9cfSHarvey Harrison } 916dd45c9cfSHarvey Harrison 917cf3b429bSJoe Perches static noinline_for_stack 918cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 919689afa7dSHarvey Harrison { 920689afa7dSHarvey Harrison int i; 9210159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 9220159f24eSJoe Perches int index; 9230159f24eSJoe Perches int step; 924689afa7dSHarvey Harrison 9250159f24eSJoe Perches switch (fmt[2]) { 9260159f24eSJoe Perches case 'h': 9270159f24eSJoe Perches #ifdef __BIG_ENDIAN 9280159f24eSJoe Perches index = 0; 9290159f24eSJoe Perches step = 1; 9300159f24eSJoe Perches #else 9310159f24eSJoe Perches index = 3; 9320159f24eSJoe Perches step = -1; 9330159f24eSJoe Perches #endif 9340159f24eSJoe Perches break; 9350159f24eSJoe Perches case 'l': 9360159f24eSJoe Perches index = 3; 9370159f24eSJoe Perches step = -1; 9380159f24eSJoe Perches break; 9390159f24eSJoe Perches case 'n': 9400159f24eSJoe Perches case 'b': 9410159f24eSJoe Perches default: 9420159f24eSJoe Perches index = 0; 9430159f24eSJoe Perches step = 1; 9440159f24eSJoe Perches break; 9450159f24eSJoe Perches } 9468a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 9478a27f7c9SJoe Perches char temp[3]; /* hold each IP quad in reverse order */ 948133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 9498a27f7c9SJoe Perches if (leading_zeros) { 9508a27f7c9SJoe Perches if (digits < 3) 9518a27f7c9SJoe Perches *p++ = '0'; 9528a27f7c9SJoe Perches if (digits < 2) 9538a27f7c9SJoe Perches *p++ = '0'; 9548a27f7c9SJoe Perches } 9558a27f7c9SJoe Perches /* reverse the digits in the quad */ 9568a27f7c9SJoe Perches while (digits--) 9578a27f7c9SJoe Perches *p++ = temp[digits]; 9588a27f7c9SJoe Perches if (i < 3) 9598a27f7c9SJoe Perches *p++ = '.'; 9600159f24eSJoe Perches index += step; 9618a27f7c9SJoe Perches } 9628a27f7c9SJoe Perches *p = '\0'; 9637b9186f5SAndré Goddard Rosa 9648a27f7c9SJoe Perches return p; 9658a27f7c9SJoe Perches } 9668a27f7c9SJoe Perches 967cf3b429bSJoe Perches static noinline_for_stack 968cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 9698a27f7c9SJoe Perches { 9707b9186f5SAndré Goddard Rosa int i, j, range; 9718a27f7c9SJoe Perches unsigned char zerolength[8]; 9728a27f7c9SJoe Perches int longest = 1; 9738a27f7c9SJoe Perches int colonpos = -1; 9748a27f7c9SJoe Perches u16 word; 9757b9186f5SAndré Goddard Rosa u8 hi, lo; 9768a27f7c9SJoe Perches bool needcolon = false; 977eb78cd26SJoe Perches bool useIPv4; 978eb78cd26SJoe Perches struct in6_addr in6; 979eb78cd26SJoe Perches 980eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 981eb78cd26SJoe Perches 982eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 9838a27f7c9SJoe Perches 9848a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 9858a27f7c9SJoe Perches 9868a27f7c9SJoe Perches if (useIPv4) 9878a27f7c9SJoe Perches range = 6; 9888a27f7c9SJoe Perches else 9898a27f7c9SJoe Perches range = 8; 9908a27f7c9SJoe Perches 9918a27f7c9SJoe Perches /* find position of longest 0 run */ 9928a27f7c9SJoe Perches for (i = 0; i < range; i++) { 9938a27f7c9SJoe Perches for (j = i; j < range; j++) { 994eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 9958a27f7c9SJoe Perches break; 9968a27f7c9SJoe Perches zerolength[i]++; 9978a27f7c9SJoe Perches } 9988a27f7c9SJoe Perches } 9998a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10008a27f7c9SJoe Perches if (zerolength[i] > longest) { 10018a27f7c9SJoe Perches longest = zerolength[i]; 10028a27f7c9SJoe Perches colonpos = i; 10038a27f7c9SJoe Perches } 10048a27f7c9SJoe Perches } 100529cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 100629cf519eSJoe Perches colonpos = -1; 10078a27f7c9SJoe Perches 10088a27f7c9SJoe Perches /* emit address */ 10098a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10108a27f7c9SJoe Perches if (i == colonpos) { 10118a27f7c9SJoe Perches if (needcolon || i == 0) 10128a27f7c9SJoe Perches *p++ = ':'; 10138a27f7c9SJoe Perches *p++ = ':'; 10148a27f7c9SJoe Perches needcolon = false; 10158a27f7c9SJoe Perches i += longest - 1; 10168a27f7c9SJoe Perches continue; 10178a27f7c9SJoe Perches } 10188a27f7c9SJoe Perches if (needcolon) { 10198a27f7c9SJoe Perches *p++ = ':'; 10208a27f7c9SJoe Perches needcolon = false; 10218a27f7c9SJoe Perches } 10228a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1023eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 10248a27f7c9SJoe Perches hi = word >> 8; 10258a27f7c9SJoe Perches lo = word & 0xff; 10268a27f7c9SJoe Perches if (hi) { 10278a27f7c9SJoe Perches if (hi > 0x0f) 102855036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 10298a27f7c9SJoe Perches else 10308a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 103155036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 10328a27f7c9SJoe Perches } 1033b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 103455036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 10358a27f7c9SJoe Perches else 10368a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 10378a27f7c9SJoe Perches needcolon = true; 10388a27f7c9SJoe Perches } 10398a27f7c9SJoe Perches 10408a27f7c9SJoe Perches if (useIPv4) { 10418a27f7c9SJoe Perches if (needcolon) 10428a27f7c9SJoe Perches *p++ = ':'; 10430159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 10448a27f7c9SJoe Perches } 10458a27f7c9SJoe Perches *p = '\0'; 10467b9186f5SAndré Goddard Rosa 10478a27f7c9SJoe Perches return p; 10488a27f7c9SJoe Perches } 10498a27f7c9SJoe Perches 1050cf3b429bSJoe Perches static noinline_for_stack 1051cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 10528a27f7c9SJoe Perches { 10538a27f7c9SJoe Perches int i; 10547b9186f5SAndré Goddard Rosa 1055689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 105655036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 105755036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 10588a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1059689afa7dSHarvey Harrison *p++ = ':'; 1060689afa7dSHarvey Harrison } 1061689afa7dSHarvey Harrison *p = '\0'; 10627b9186f5SAndré Goddard Rosa 10638a27f7c9SJoe Perches return p; 10648a27f7c9SJoe Perches } 10658a27f7c9SJoe Perches 1066cf3b429bSJoe Perches static noinline_for_stack 1067cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 10688a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 10698a27f7c9SJoe Perches { 10708a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 10718a27f7c9SJoe Perches 10728a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1073eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 10748a27f7c9SJoe Perches else 1075eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1076689afa7dSHarvey Harrison 1077fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 1078689afa7dSHarvey Harrison } 1079689afa7dSHarvey Harrison 1080cf3b429bSJoe Perches static noinline_for_stack 1081cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 10828a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 10834aa99606SHarvey Harrison { 10848a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 10854aa99606SHarvey Harrison 10860159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 10874aa99606SHarvey Harrison 1088fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 10894aa99606SHarvey Harrison } 10904aa99606SHarvey Harrison 1091cf3b429bSJoe Perches static noinline_for_stack 109210679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 109310679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 109410679643SDaniel Borkmann { 109510679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 109610679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 109710679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 109810679643SDaniel Borkmann sizeof("%1234567890")]; 109910679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 110010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 110110679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 110210679643SDaniel Borkmann u8 off = 0; 110310679643SDaniel Borkmann 110410679643SDaniel Borkmann fmt++; 110510679643SDaniel Borkmann while (isalpha(*++fmt)) { 110610679643SDaniel Borkmann switch (*fmt) { 110710679643SDaniel Borkmann case 'p': 110810679643SDaniel Borkmann have_p = true; 110910679643SDaniel Borkmann break; 111010679643SDaniel Borkmann case 'f': 111110679643SDaniel Borkmann have_f = true; 111210679643SDaniel Borkmann break; 111310679643SDaniel Borkmann case 's': 111410679643SDaniel Borkmann have_s = true; 111510679643SDaniel Borkmann break; 111610679643SDaniel Borkmann case 'c': 111710679643SDaniel Borkmann have_c = true; 111810679643SDaniel Borkmann break; 111910679643SDaniel Borkmann } 112010679643SDaniel Borkmann } 112110679643SDaniel Borkmann 112210679643SDaniel Borkmann if (have_p || have_s || have_f) { 112310679643SDaniel Borkmann *p = '['; 112410679643SDaniel Borkmann off = 1; 112510679643SDaniel Borkmann } 112610679643SDaniel Borkmann 112710679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 112810679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 112910679643SDaniel Borkmann else 113010679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 113110679643SDaniel Borkmann 113210679643SDaniel Borkmann if (have_p || have_s || have_f) 113310679643SDaniel Borkmann *p++ = ']'; 113410679643SDaniel Borkmann 113510679643SDaniel Borkmann if (have_p) { 113610679643SDaniel Borkmann *p++ = ':'; 113710679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 113810679643SDaniel Borkmann } 113910679643SDaniel Borkmann if (have_f) { 114010679643SDaniel Borkmann *p++ = '/'; 114110679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 114210679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 114310679643SDaniel Borkmann } 114410679643SDaniel Borkmann if (have_s) { 114510679643SDaniel Borkmann *p++ = '%'; 114610679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 114710679643SDaniel Borkmann } 114810679643SDaniel Borkmann *p = '\0'; 114910679643SDaniel Borkmann 115010679643SDaniel Borkmann return string(buf, end, ip6_addr, spec); 115110679643SDaniel Borkmann } 115210679643SDaniel Borkmann 115310679643SDaniel Borkmann static noinline_for_stack 115410679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 115510679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 115610679643SDaniel Borkmann { 115710679643SDaniel Borkmann bool have_p = false; 115810679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 115910679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 116010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 116110679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 116210679643SDaniel Borkmann 116310679643SDaniel Borkmann fmt++; 116410679643SDaniel Borkmann while (isalpha(*++fmt)) { 116510679643SDaniel Borkmann switch (*fmt) { 116610679643SDaniel Borkmann case 'p': 116710679643SDaniel Borkmann have_p = true; 116810679643SDaniel Borkmann break; 116910679643SDaniel Borkmann case 'h': 117010679643SDaniel Borkmann case 'l': 117110679643SDaniel Borkmann case 'n': 117210679643SDaniel Borkmann case 'b': 117310679643SDaniel Borkmann fmt4[2] = *fmt; 117410679643SDaniel Borkmann break; 117510679643SDaniel Borkmann } 117610679643SDaniel Borkmann } 117710679643SDaniel Borkmann 117810679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 117910679643SDaniel Borkmann if (have_p) { 118010679643SDaniel Borkmann *p++ = ':'; 118110679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 118210679643SDaniel Borkmann } 118310679643SDaniel Borkmann *p = '\0'; 118410679643SDaniel Borkmann 118510679643SDaniel Borkmann return string(buf, end, ip4_addr, spec); 118610679643SDaniel Borkmann } 118710679643SDaniel Borkmann 118810679643SDaniel Borkmann static noinline_for_stack 118971dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 119071dca95dSAndy Shevchenko const char *fmt) 119171dca95dSAndy Shevchenko { 119271dca95dSAndy Shevchenko bool found = true; 119371dca95dSAndy Shevchenko int count = 1; 119471dca95dSAndy Shevchenko unsigned int flags = 0; 119571dca95dSAndy Shevchenko int len; 119671dca95dSAndy Shevchenko 119771dca95dSAndy Shevchenko if (spec.field_width == 0) 119871dca95dSAndy Shevchenko return buf; /* nothing to print */ 119971dca95dSAndy Shevchenko 120071dca95dSAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 120171dca95dSAndy Shevchenko return string(buf, end, NULL, spec); /* NULL pointer */ 120271dca95dSAndy Shevchenko 120371dca95dSAndy Shevchenko 120471dca95dSAndy Shevchenko do { 120571dca95dSAndy Shevchenko switch (fmt[count++]) { 120671dca95dSAndy Shevchenko case 'a': 120771dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 120871dca95dSAndy Shevchenko break; 120971dca95dSAndy Shevchenko case 'c': 121071dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 121171dca95dSAndy Shevchenko break; 121271dca95dSAndy Shevchenko case 'h': 121371dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 121471dca95dSAndy Shevchenko break; 121571dca95dSAndy Shevchenko case 'n': 121671dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 121771dca95dSAndy Shevchenko break; 121871dca95dSAndy Shevchenko case 'o': 121971dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 122071dca95dSAndy Shevchenko break; 122171dca95dSAndy Shevchenko case 'p': 122271dca95dSAndy Shevchenko flags |= ESCAPE_NP; 122371dca95dSAndy Shevchenko break; 122471dca95dSAndy Shevchenko case 's': 122571dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 122671dca95dSAndy Shevchenko break; 122771dca95dSAndy Shevchenko default: 122871dca95dSAndy Shevchenko found = false; 122971dca95dSAndy Shevchenko break; 123071dca95dSAndy Shevchenko } 123171dca95dSAndy Shevchenko } while (found); 123271dca95dSAndy Shevchenko 123371dca95dSAndy Shevchenko if (!flags) 123471dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 123571dca95dSAndy Shevchenko 123671dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 123771dca95dSAndy Shevchenko 1238*41416f23SRasmus Villemoes /* 1239*41416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 1240*41416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 1241*41416f23SRasmus Villemoes * had the buffer been big enough. 1242*41416f23SRasmus Villemoes */ 1243*41416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 124471dca95dSAndy Shevchenko 124571dca95dSAndy Shevchenko return buf; 124671dca95dSAndy Shevchenko } 124771dca95dSAndy Shevchenko 124871dca95dSAndy Shevchenko static noinline_for_stack 1249cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 12509ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 12519ac6e44eSJoe Perches { 12529ac6e44eSJoe Perches char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; 12539ac6e44eSJoe Perches char *p = uuid; 12549ac6e44eSJoe Perches int i; 12559ac6e44eSJoe Perches static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 12569ac6e44eSJoe Perches static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; 12579ac6e44eSJoe Perches const u8 *index = be; 12589ac6e44eSJoe Perches bool uc = false; 12599ac6e44eSJoe Perches 12609ac6e44eSJoe Perches switch (*(++fmt)) { 12619ac6e44eSJoe Perches case 'L': 12629ac6e44eSJoe Perches uc = true; /* fall-through */ 12639ac6e44eSJoe Perches case 'l': 12649ac6e44eSJoe Perches index = le; 12659ac6e44eSJoe Perches break; 12669ac6e44eSJoe Perches case 'B': 12679ac6e44eSJoe Perches uc = true; 12689ac6e44eSJoe Perches break; 12699ac6e44eSJoe Perches } 12709ac6e44eSJoe Perches 12719ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 127255036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 12739ac6e44eSJoe Perches switch (i) { 12749ac6e44eSJoe Perches case 3: 12759ac6e44eSJoe Perches case 5: 12769ac6e44eSJoe Perches case 7: 12779ac6e44eSJoe Perches case 9: 12789ac6e44eSJoe Perches *p++ = '-'; 12799ac6e44eSJoe Perches break; 12809ac6e44eSJoe Perches } 12819ac6e44eSJoe Perches } 12829ac6e44eSJoe Perches 12839ac6e44eSJoe Perches *p = 0; 12849ac6e44eSJoe Perches 12859ac6e44eSJoe Perches if (uc) { 12869ac6e44eSJoe Perches p = uuid; 12879ac6e44eSJoe Perches do { 12889ac6e44eSJoe Perches *p = toupper(*p); 12899ac6e44eSJoe Perches } while (*(++p)); 12909ac6e44eSJoe Perches } 12919ac6e44eSJoe Perches 12929ac6e44eSJoe Perches return string(buf, end, uuid, spec); 12939ac6e44eSJoe Perches } 12949ac6e44eSJoe Perches 1295c8f44affSMichał Mirosław static 1296c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr, 1297c8f44affSMichał Mirosław struct printf_spec spec) 1298c8f44affSMichał Mirosław { 1299c8f44affSMichał Mirosław spec.flags |= SPECIAL | SMALL | ZEROPAD; 1300c8f44affSMichał Mirosław if (spec.field_width == -1) 1301c8f44affSMichał Mirosław spec.field_width = 2 + 2 * sizeof(netdev_features_t); 1302c8f44affSMichał Mirosław spec.base = 16; 1303c8f44affSMichał Mirosław 1304c8f44affSMichał Mirosław return number(buf, end, *(const netdev_features_t *)addr, spec); 1305c8f44affSMichał Mirosław } 1306c8f44affSMichał Mirosław 1307aaf07621SJoe Perches static noinline_for_stack 1308aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr, 1309aaf07621SJoe Perches struct printf_spec spec, const char *fmt) 1310aaf07621SJoe Perches { 1311aaf07621SJoe Perches unsigned long long num; 1312aaf07621SJoe Perches 1313aaf07621SJoe Perches spec.flags |= SPECIAL | SMALL | ZEROPAD; 1314aaf07621SJoe Perches spec.base = 16; 1315aaf07621SJoe Perches 1316aaf07621SJoe Perches switch (fmt[1]) { 1317aaf07621SJoe Perches case 'd': 1318aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 1319aaf07621SJoe Perches spec.field_width = sizeof(dma_addr_t) * 2 + 2; 1320aaf07621SJoe Perches break; 1321aaf07621SJoe Perches case 'p': 1322aaf07621SJoe Perches default: 1323aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 1324aaf07621SJoe Perches spec.field_width = sizeof(phys_addr_t) * 2 + 2; 1325aaf07621SJoe Perches break; 1326aaf07621SJoe Perches } 1327aaf07621SJoe Perches 1328aaf07621SJoe Perches return number(buf, end, num, spec); 1329aaf07621SJoe Perches } 1330aaf07621SJoe Perches 1331900cca29SGeert Uytterhoeven static noinline_for_stack 1332900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1333900cca29SGeert Uytterhoeven const char *fmt) 1334900cca29SGeert Uytterhoeven { 1335900cca29SGeert Uytterhoeven if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) 1336900cca29SGeert Uytterhoeven return string(buf, end, NULL, spec); 1337900cca29SGeert Uytterhoeven 1338900cca29SGeert Uytterhoeven switch (fmt[1]) { 1339900cca29SGeert Uytterhoeven case 'r': 1340900cca29SGeert Uytterhoeven return number(buf, end, clk_get_rate(clk), spec); 1341900cca29SGeert Uytterhoeven 1342900cca29SGeert Uytterhoeven case 'n': 1343900cca29SGeert Uytterhoeven default: 1344900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1345900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1346900cca29SGeert Uytterhoeven #else 1347900cca29SGeert Uytterhoeven spec.base = 16; 1348900cca29SGeert Uytterhoeven spec.field_width = sizeof(unsigned long) * 2 + 2; 1349900cca29SGeert Uytterhoeven spec.flags |= SPECIAL | SMALL | ZEROPAD; 1350900cca29SGeert Uytterhoeven return number(buf, end, (unsigned long)clk, spec); 1351900cca29SGeert Uytterhoeven #endif 1352900cca29SGeert Uytterhoeven } 1353900cca29SGeert Uytterhoeven } 1354900cca29SGeert Uytterhoeven 1355411f05f1SIngo Molnar int kptr_restrict __read_mostly; 1356455cd5abSDan Rosenberg 13574d8a743cSLinus Torvalds /* 13584d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 13594d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 13604d8a743cSLinus Torvalds * specifiers. 13614d8a743cSLinus Torvalds * 1362332d2e78SLinus Torvalds * Right now we handle: 13630fe1ef24SLinus Torvalds * 13640c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 13650c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 13660efb4d20SSteven Rostedt * - 'S' For symbolic direct pointers with offset 13670efb4d20SSteven Rostedt * - 's' For symbolic direct pointers without offset 1368b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 13690f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 1370c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1371c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1372dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 1373dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 1374dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 1375dbc760bcSTejun Heo * range-list format instead of hex format 1376dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 1377dd45c9cfSHarvey Harrison * usual colon-separated hex notation 13788a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1379bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 1380c8e00060SJoe Perches * with a dash-separated hex notation 13817c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 13828a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 13838a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 13848a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 138510679643SDaniel Borkmann * [S][pfs] 138610679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 138710679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 13888a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 13898a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 13908a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 139110679643SDaniel Borkmann * [S][pfs] 139210679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 139310679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 139410679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 139510679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 139629cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 139771dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 139871dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 139971dca95dSAndy Shevchenko * details): 140071dca95dSAndy Shevchenko * a - ESCAPE_ANY 140171dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 140271dca95dSAndy Shevchenko * h - ESCAPE_HEX 140371dca95dSAndy Shevchenko * n - ESCAPE_NULL 140471dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 140571dca95dSAndy Shevchenko * p - ESCAPE_NP 140671dca95dSAndy Shevchenko * s - ESCAPE_SPACE 140771dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 14089ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 14099ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 14109ac6e44eSJoe Perches * Options for %pU are: 14119ac6e44eSJoe Perches * b big endian lower case hex (default) 14129ac6e44eSJoe Perches * B big endian UPPER case hex 14139ac6e44eSJoe Perches * l little endian lower case hex 14149ac6e44eSJoe Perches * L little endian UPPER case hex 14159ac6e44eSJoe Perches * big endian output byte order is: 14169ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 14179ac6e44eSJoe Perches * little endian output byte order is: 14189ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 14197db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 14207db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 14217db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 14227db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 14237db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 1424455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 1425c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 142631550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 142731550a16SAndy Shevchenko * a certain separator (' ' by default): 142831550a16SAndy Shevchenko * C colon 142931550a16SAndy Shevchenko * D dash 143031550a16SAndy Shevchenko * N no separator 143131550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 143231550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 1433aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 1434aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 1435c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 1436c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 1437900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 1438900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1439900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 1440900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1441900cca29SGeert Uytterhoeven * - 'Cr' For a clock, it prints the current rate of the clock 14429ac6e44eSJoe Perches * 1443332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1444332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 1445332d2e78SLinus Torvalds * pointer to the real address. 14464d8a743cSLinus Torvalds */ 1447cf3b429bSJoe Perches static noinline_for_stack 1448cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1449fef20d9cSFrederic Weisbecker struct printf_spec spec) 145078a8bf69SLinus Torvalds { 1451725fe002SGrant Likely int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); 1452725fe002SGrant Likely 14539f36e2c4SKees Cook if (!ptr && *fmt != 'K') { 14545e057981SJoe Perches /* 14555e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 14565e057981SJoe Perches * tabular output look nice. 14575e057981SJoe Perches */ 14585e057981SJoe Perches if (spec.field_width == -1) 1459725fe002SGrant Likely spec.field_width = default_width; 1460fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 14615e057981SJoe Perches } 1462d97106abSLinus Torvalds 14630fe1ef24SLinus Torvalds switch (*fmt) { 14640fe1ef24SLinus Torvalds case 'F': 14650c8b946eSFrederic Weisbecker case 'f': 14660fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 14670fe1ef24SLinus Torvalds /* Fallthrough */ 14680fe1ef24SLinus Torvalds case 'S': 14699ac6e44eSJoe Perches case 's': 14700f77a8d3SNamhyung Kim case 'B': 1471b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1472332d2e78SLinus Torvalds case 'R': 1473c7dabef8SBjorn Helgaas case 'r': 1474fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 147531550a16SAndy Shevchenko case 'h': 147631550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 1477dbc760bcSTejun Heo case 'b': 1478dbc760bcSTejun Heo switch (fmt[1]) { 1479dbc760bcSTejun Heo case 'l': 1480dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 1481dbc760bcSTejun Heo default: 1482dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 1483dbc760bcSTejun Heo } 14848a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 14858a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 148676597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 148776597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 14888a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 14898a27f7c9SJoe Perches case 'I': /* Formatted IP supported 14908a27f7c9SJoe Perches * 4: 1.2.3.4 14918a27f7c9SJoe Perches * 6: 0001:0203:...:0708 14928a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 14938a27f7c9SJoe Perches */ 14948a27f7c9SJoe Perches case 'i': /* Contiguous: 14958a27f7c9SJoe Perches * 4: 001.002.003.004 14968a27f7c9SJoe Perches * 6: 000102...0f 14978a27f7c9SJoe Perches */ 14988a27f7c9SJoe Perches switch (fmt[1]) { 14998a27f7c9SJoe Perches case '6': 15008a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 15018a27f7c9SJoe Perches case '4': 15028a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 150310679643SDaniel Borkmann case 'S': { 150410679643SDaniel Borkmann const union { 150510679643SDaniel Borkmann struct sockaddr raw; 150610679643SDaniel Borkmann struct sockaddr_in v4; 150710679643SDaniel Borkmann struct sockaddr_in6 v6; 150810679643SDaniel Borkmann } *sa = ptr; 150910679643SDaniel Borkmann 151010679643SDaniel Borkmann switch (sa->raw.sa_family) { 151110679643SDaniel Borkmann case AF_INET: 151210679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 151310679643SDaniel Borkmann case AF_INET6: 151410679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 151510679643SDaniel Borkmann default: 151610679643SDaniel Borkmann return string(buf, end, "(invalid address)", spec); 151710679643SDaniel Borkmann }} 15188a27f7c9SJoe Perches } 15194aa99606SHarvey Harrison break; 152071dca95dSAndy Shevchenko case 'E': 152171dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 15229ac6e44eSJoe Perches case 'U': 15239ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 15247db6f5fbSJoe Perches case 'V': 15255756b76eSJan Beulich { 15265756b76eSJan Beulich va_list va; 15275756b76eSJan Beulich 15285756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 15295756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 15305756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 15315756b76eSJan Beulich va_end(va); 15325756b76eSJan Beulich return buf; 15335756b76eSJan Beulich } 1534455cd5abSDan Rosenberg case 'K': 1535455cd5abSDan Rosenberg /* 1536455cd5abSDan Rosenberg * %pK cannot be used in IRQ context because its test 1537455cd5abSDan Rosenberg * for CAP_SYSLOG would be meaningless. 1538455cd5abSDan Rosenberg */ 15393715c530SDan Rosenberg if (kptr_restrict && (in_irq() || in_serving_softirq() || 15403715c530SDan Rosenberg in_nmi())) { 1541455cd5abSDan Rosenberg if (spec.field_width == -1) 1542725fe002SGrant Likely spec.field_width = default_width; 1543455cd5abSDan Rosenberg return string(buf, end, "pK-error", spec); 1544455cd5abSDan Rosenberg } 1545312b4e22SRyan Mallon 1546312b4e22SRyan Mallon switch (kptr_restrict) { 1547312b4e22SRyan Mallon case 0: 1548312b4e22SRyan Mallon /* Always print %pK values */ 1549312b4e22SRyan Mallon break; 1550312b4e22SRyan Mallon case 1: { 1551312b4e22SRyan Mallon /* 1552312b4e22SRyan Mallon * Only print the real pointer value if the current 1553312b4e22SRyan Mallon * process has CAP_SYSLOG and is running with the 1554312b4e22SRyan Mallon * same credentials it started with. This is because 1555312b4e22SRyan Mallon * access to files is checked at open() time, but %pK 1556312b4e22SRyan Mallon * checks permission at read() time. We don't want to 1557312b4e22SRyan Mallon * leak pointer values if a binary opens a file using 1558312b4e22SRyan Mallon * %pK and then elevates privileges before reading it. 1559312b4e22SRyan Mallon */ 1560312b4e22SRyan Mallon const struct cred *cred = current_cred(); 1561312b4e22SRyan Mallon 1562312b4e22SRyan Mallon if (!has_capability_noaudit(current, CAP_SYSLOG) || 1563312b4e22SRyan Mallon !uid_eq(cred->euid, cred->uid) || 1564312b4e22SRyan Mallon !gid_eq(cred->egid, cred->gid)) 156526297607SJoe Perches ptr = NULL; 156626297607SJoe Perches break; 1567312b4e22SRyan Mallon } 1568312b4e22SRyan Mallon case 2: 1569312b4e22SRyan Mallon default: 1570312b4e22SRyan Mallon /* Always print 0's for %pK */ 1571312b4e22SRyan Mallon ptr = NULL; 1572312b4e22SRyan Mallon break; 1573312b4e22SRyan Mallon } 1574312b4e22SRyan Mallon break; 1575312b4e22SRyan Mallon 1576c8f44affSMichał Mirosław case 'N': 1577c8f44affSMichał Mirosław switch (fmt[1]) { 1578c8f44affSMichał Mirosław case 'F': 1579c8f44affSMichał Mirosław return netdev_feature_string(buf, end, ptr, spec); 1580c8f44affSMichał Mirosław } 1581c8f44affSMichał Mirosław break; 15827d799210SStepan Moskovchenko case 'a': 1583aaf07621SJoe Perches return address_val(buf, end, ptr, spec, fmt); 15844b6ccca7SAl Viro case 'd': 15854b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 1586900cca29SGeert Uytterhoeven case 'C': 1587900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 15884b6ccca7SAl Viro case 'D': 15894b6ccca7SAl Viro return dentry_name(buf, end, 15904b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 15914b6ccca7SAl Viro spec, fmt); 15920fe1ef24SLinus Torvalds } 1593fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 1594fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 1595725fe002SGrant Likely spec.field_width = default_width; 1596fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 159778a8bf69SLinus Torvalds } 1598fef20d9cSFrederic Weisbecker spec.base = 16; 1599fef20d9cSFrederic Weisbecker 1600fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 1601fef20d9cSFrederic Weisbecker } 1602fef20d9cSFrederic Weisbecker 1603fef20d9cSFrederic Weisbecker /* 1604fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1605fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1606fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1607fef20d9cSFrederic Weisbecker * to go on the next call). 1608fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1609fef20d9cSFrederic Weisbecker * 1610fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1611fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1612fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 1613fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1614fef20d9cSFrederic Weisbecker * 1615fef20d9cSFrederic Weisbecker * @fmt: the format string 1616fef20d9cSFrederic Weisbecker * @type of the token returned 1617fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1618fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1619fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1620fef20d9cSFrederic Weisbecker * @precision: precision of a number 1621fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1622fef20d9cSFrederic Weisbecker */ 1623cf3b429bSJoe Perches static noinline_for_stack 1624cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 1625fef20d9cSFrederic Weisbecker { 1626fef20d9cSFrederic Weisbecker const char *start = fmt; 1627fef20d9cSFrederic Weisbecker 1628fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 1629ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 1630fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 1631fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 1632fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 1633fef20d9cSFrederic Weisbecker } 1634fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1635fef20d9cSFrederic Weisbecker goto precision; 1636fef20d9cSFrederic Weisbecker } 1637fef20d9cSFrederic Weisbecker 1638fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 1639fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 1640fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1641fef20d9cSFrederic Weisbecker spec->precision = 0; 1642fef20d9cSFrederic Weisbecker 1643fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1644fef20d9cSFrederic Weisbecker goto qualifier; 1645fef20d9cSFrederic Weisbecker } 1646fef20d9cSFrederic Weisbecker 1647fef20d9cSFrederic Weisbecker /* By default */ 1648fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1649fef20d9cSFrederic Weisbecker 1650fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 1651fef20d9cSFrederic Weisbecker if (*fmt == '%') 1652fef20d9cSFrederic Weisbecker break; 1653fef20d9cSFrederic Weisbecker } 1654fef20d9cSFrederic Weisbecker 1655fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 1656fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 1657fef20d9cSFrederic Weisbecker return fmt - start; 1658fef20d9cSFrederic Weisbecker 1659fef20d9cSFrederic Weisbecker /* Process flags */ 1660fef20d9cSFrederic Weisbecker spec->flags = 0; 1661fef20d9cSFrederic Weisbecker 1662fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 1663fef20d9cSFrederic Weisbecker bool found = true; 1664fef20d9cSFrederic Weisbecker 1665fef20d9cSFrederic Weisbecker ++fmt; 1666fef20d9cSFrederic Weisbecker 1667fef20d9cSFrederic Weisbecker switch (*fmt) { 1668fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 1669fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 1670fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 1671fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 1672fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 1673fef20d9cSFrederic Weisbecker default: found = false; 1674fef20d9cSFrederic Weisbecker } 1675fef20d9cSFrederic Weisbecker 1676fef20d9cSFrederic Weisbecker if (!found) 1677fef20d9cSFrederic Weisbecker break; 1678fef20d9cSFrederic Weisbecker } 1679fef20d9cSFrederic Weisbecker 1680fef20d9cSFrederic Weisbecker /* get field width */ 1681fef20d9cSFrederic Weisbecker spec->field_width = -1; 1682fef20d9cSFrederic Weisbecker 1683fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1684fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1685fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1686fef20d9cSFrederic Weisbecker /* it's the next argument */ 1687ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1688fef20d9cSFrederic Weisbecker return ++fmt - start; 1689fef20d9cSFrederic Weisbecker } 1690fef20d9cSFrederic Weisbecker 1691fef20d9cSFrederic Weisbecker precision: 1692fef20d9cSFrederic Weisbecker /* get the precision */ 1693fef20d9cSFrederic Weisbecker spec->precision = -1; 1694fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1695fef20d9cSFrederic Weisbecker ++fmt; 1696fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1697fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1698fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1699fef20d9cSFrederic Weisbecker spec->precision = 0; 1700fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1701fef20d9cSFrederic Weisbecker /* it's the next argument */ 1702adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1703fef20d9cSFrederic Weisbecker return ++fmt - start; 1704fef20d9cSFrederic Weisbecker } 1705fef20d9cSFrederic Weisbecker } 1706fef20d9cSFrederic Weisbecker 1707fef20d9cSFrederic Weisbecker qualifier: 1708fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1709fef20d9cSFrederic Weisbecker spec->qualifier = -1; 171075fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 171175fb8f26SAndy Shevchenko _tolower(*fmt) == 'z' || *fmt == 't') { 1712a4e94ef0SZhaolei spec->qualifier = *fmt++; 1713a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 1714a4e94ef0SZhaolei if (spec->qualifier == 'l') { 1715fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 1716fef20d9cSFrederic Weisbecker ++fmt; 1717a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 1718a4e94ef0SZhaolei spec->qualifier = 'H'; 1719a4e94ef0SZhaolei ++fmt; 1720a4e94ef0SZhaolei } 1721fef20d9cSFrederic Weisbecker } 1722fef20d9cSFrederic Weisbecker } 1723fef20d9cSFrederic Weisbecker 1724fef20d9cSFrederic Weisbecker /* default base */ 1725fef20d9cSFrederic Weisbecker spec->base = 10; 1726fef20d9cSFrederic Weisbecker switch (*fmt) { 1727fef20d9cSFrederic Weisbecker case 'c': 1728fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1729fef20d9cSFrederic Weisbecker return ++fmt - start; 1730fef20d9cSFrederic Weisbecker 1731fef20d9cSFrederic Weisbecker case 's': 1732fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1733fef20d9cSFrederic Weisbecker return ++fmt - start; 1734fef20d9cSFrederic Weisbecker 1735fef20d9cSFrederic Weisbecker case 'p': 1736fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1737ffbfed03SRasmus Villemoes return ++fmt - start; 1738fef20d9cSFrederic Weisbecker 1739fef20d9cSFrederic Weisbecker case '%': 1740fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1741fef20d9cSFrederic Weisbecker return ++fmt - start; 1742fef20d9cSFrederic Weisbecker 1743fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1744fef20d9cSFrederic Weisbecker case 'o': 1745fef20d9cSFrederic Weisbecker spec->base = 8; 1746fef20d9cSFrederic Weisbecker break; 1747fef20d9cSFrederic Weisbecker 1748fef20d9cSFrederic Weisbecker case 'x': 1749fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1750fef20d9cSFrederic Weisbecker 1751fef20d9cSFrederic Weisbecker case 'X': 1752fef20d9cSFrederic Weisbecker spec->base = 16; 1753fef20d9cSFrederic Weisbecker break; 1754fef20d9cSFrederic Weisbecker 1755fef20d9cSFrederic Weisbecker case 'd': 1756fef20d9cSFrederic Weisbecker case 'i': 175739e874f8SFrederic Weisbecker spec->flags |= SIGN; 1758fef20d9cSFrederic Weisbecker case 'u': 1759fef20d9cSFrederic Weisbecker break; 1760fef20d9cSFrederic Weisbecker 1761708d96fdSRyan Mallon case 'n': 1762708d96fdSRyan Mallon /* 1763708d96fdSRyan Mallon * Since %n poses a greater security risk than utility, treat 1764708d96fdSRyan Mallon * it as an invalid format specifier. Warn about its use so 1765708d96fdSRyan Mallon * that new instances don't get added. 1766708d96fdSRyan Mallon */ 1767708d96fdSRyan Mallon WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt); 1768708d96fdSRyan Mallon /* Fall-through */ 1769708d96fdSRyan Mallon 1770fef20d9cSFrederic Weisbecker default: 1771fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1772fef20d9cSFrederic Weisbecker return fmt - start; 1773fef20d9cSFrederic Weisbecker } 1774fef20d9cSFrederic Weisbecker 1775fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 1776fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1777fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 177851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 177951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 178075fb8f26SAndy Shevchenko } else if (_tolower(spec->qualifier) == 'z') { 1781fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1782fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 1783fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1784a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 178551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 178651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 1787fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 178851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 178951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 1790fef20d9cSFrederic Weisbecker } else { 179151be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 179251be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 1793fef20d9cSFrederic Weisbecker } 1794fef20d9cSFrederic Weisbecker 1795fef20d9cSFrederic Weisbecker return ++fmt - start; 179678a8bf69SLinus Torvalds } 179778a8bf69SLinus Torvalds 17981da177e4SLinus Torvalds /** 17991da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 18001da177e4SLinus Torvalds * @buf: The buffer to place the result into 18011da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18021da177e4SLinus Torvalds * @fmt: The format string to use 18031da177e4SLinus Torvalds * @args: Arguments for the format string 18041da177e4SLinus Torvalds * 180520036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 180691adcd2cSSteven Rostedt * %pS output the name of a text symbol with offset 180791adcd2cSSteven Rostedt * %ps output the name of a text symbol without offset 18080c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 18090c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 18100f77a8d3SNamhyung Kim * %pB output the name of a backtrace symbol with its offset 18118a79503aSUwe Kleine-König * %pR output the address range in a struct resource with decoded flags 18128a79503aSUwe Kleine-König * %pr output the address range in a struct resource with raw flags 1813dbc760bcSTejun Heo * %pb output the bitmap with field width as the number of bits 1814dbc760bcSTejun Heo * %pbl output the bitmap as range list with field width as the number of bits 18158a79503aSUwe Kleine-König * %pM output a 6-byte MAC address with colons 18167c59154eSAndy Shevchenko * %pMR output a 6-byte MAC address with colons in reversed order 18177c59154eSAndy Shevchenko * %pMF output a 6-byte MAC address with dashes 18188a79503aSUwe Kleine-König * %pm output a 6-byte MAC address without colons 18197c59154eSAndy Shevchenko * %pmR output a 6-byte MAC address without colons in reversed order 18208a79503aSUwe Kleine-König * %pI4 print an IPv4 address without leading zeros 18218a79503aSUwe Kleine-König * %pi4 print an IPv4 address with leading zeros 18228a79503aSUwe Kleine-König * %pI6 print an IPv6 address with colons 18238a79503aSUwe Kleine-König * %pi6 print an IPv6 address without colons 1824f996f208SJan Engelhardt * %pI6c print an IPv6 address as specified by RFC 5952 182510679643SDaniel Borkmann * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 182610679643SDaniel Borkmann * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 18278a79503aSUwe Kleine-König * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper 18288a79503aSUwe Kleine-König * case. 182971dca95dSAndy Shevchenko * %*pE[achnops] print an escaped buffer 183031550a16SAndy Shevchenko * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 183131550a16SAndy Shevchenko * bytes of the input) 1832900cca29SGeert Uytterhoeven * %pC output the name (Common Clock Framework) or address (legacy clock 1833900cca29SGeert Uytterhoeven * framework) of a clock 1834900cca29SGeert Uytterhoeven * %pCn output the name (Common Clock Framework) or address (legacy clock 1835900cca29SGeert Uytterhoeven * framework) of a clock 1836900cca29SGeert Uytterhoeven * %pCr output the current rate of a clock 18370efb4d20SSteven Rostedt * %n is ignored 183820036fdcSAndi Kleen * 183980f548e0SAndrew Morton * ** Please update Documentation/printk-formats.txt when making changes ** 184080f548e0SAndrew Morton * 18411da177e4SLinus Torvalds * The return value is the number of characters which would 18421da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 18431da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 18441da177e4SLinus Torvalds * number of characters written into @buf as return value 184572fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 18461da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 18471da177e4SLinus Torvalds * string is truncated. 18481da177e4SLinus Torvalds * 1849ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 18501da177e4SLinus Torvalds */ 18511da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 18521da177e4SLinus Torvalds { 18531da177e4SLinus Torvalds unsigned long long num; 1854d4be151bSAndré Goddard Rosa char *str, *end; 1855fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 18561da177e4SLinus Torvalds 1857f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1858f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 18592aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 18601da177e4SLinus Torvalds return 0; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds str = buf; 1863f796937aSJeremy Fitzhardinge end = buf + size; 18641da177e4SLinus Torvalds 1865f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1866f796937aSJeremy Fitzhardinge if (end < buf) { 18671da177e4SLinus Torvalds end = ((void *)-1); 1868f796937aSJeremy Fitzhardinge size = end - buf; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 1871fef20d9cSFrederic Weisbecker while (*fmt) { 1872fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1873d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 1874fef20d9cSFrederic Weisbecker 1875fef20d9cSFrederic Weisbecker fmt += read; 1876fef20d9cSFrederic Weisbecker 1877fef20d9cSFrederic Weisbecker switch (spec.type) { 1878fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1879fef20d9cSFrederic Weisbecker int copy = read; 1880fef20d9cSFrederic Weisbecker if (str < end) { 1881fef20d9cSFrederic Weisbecker if (copy > end - str) 1882fef20d9cSFrederic Weisbecker copy = end - str; 1883fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1884fef20d9cSFrederic Weisbecker } 1885fef20d9cSFrederic Weisbecker str += read; 1886fef20d9cSFrederic Weisbecker break; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 1889ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1890fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1891fef20d9cSFrederic Weisbecker break; 18921da177e4SLinus Torvalds 1893fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1894fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1895fef20d9cSFrederic Weisbecker break; 18961da177e4SLinus Torvalds 1897d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 1898d4be151bSAndré Goddard Rosa char c; 1899d4be151bSAndré Goddard Rosa 1900fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1901fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1902f796937aSJeremy Fitzhardinge if (str < end) 19031da177e4SLinus Torvalds *str = ' '; 19041da177e4SLinus Torvalds ++str; 1905fef20d9cSFrederic Weisbecker 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1909f796937aSJeremy Fitzhardinge if (str < end) 19101da177e4SLinus Torvalds *str = c; 19111da177e4SLinus Torvalds ++str; 1912fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1913f796937aSJeremy Fitzhardinge if (str < end) 19141da177e4SLinus Torvalds *str = ' '; 19151da177e4SLinus Torvalds ++str; 19161da177e4SLinus Torvalds } 1917fef20d9cSFrederic Weisbecker break; 1918d4be151bSAndré Goddard Rosa } 19191da177e4SLinus Torvalds 1920fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1921fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1922fef20d9cSFrederic Weisbecker break; 19231da177e4SLinus Torvalds 1924fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1925ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 1926fef20d9cSFrederic Weisbecker spec); 1927fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 19284d8a743cSLinus Torvalds fmt++; 1929fef20d9cSFrederic Weisbecker break; 19301da177e4SLinus Torvalds 1931fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1932f796937aSJeremy Fitzhardinge if (str < end) 19331da177e4SLinus Torvalds *str = '%'; 19341da177e4SLinus Torvalds ++str; 19351da177e4SLinus Torvalds break; 19361da177e4SLinus Torvalds 1937fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1938f796937aSJeremy Fitzhardinge if (str < end) 19391da177e4SLinus Torvalds *str = '%'; 19401da177e4SLinus Torvalds ++str; 1941fef20d9cSFrederic Weisbecker break; 1942fef20d9cSFrederic Weisbecker 1943fef20d9cSFrederic Weisbecker default: 1944fef20d9cSFrederic Weisbecker switch (spec.type) { 1945fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1946fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1947fef20d9cSFrederic Weisbecker break; 1948fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1949fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1950fef20d9cSFrederic Weisbecker break; 1951fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1952fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1953fef20d9cSFrederic Weisbecker break; 1954fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1955ef124960SJason Gunthorpe if (spec.flags & SIGN) 1956ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 1957ef124960SJason Gunthorpe else 1958fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1959fef20d9cSFrederic Weisbecker break; 1960fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1961fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1962fef20d9cSFrederic Weisbecker break; 1963a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1964a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1965a4e94ef0SZhaolei break; 1966a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1967a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1968a4e94ef0SZhaolei break; 1969fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1970fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1971fef20d9cSFrederic Weisbecker break; 1972fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1973fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1974fef20d9cSFrederic Weisbecker break; 197539e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 197639e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1977fef20d9cSFrederic Weisbecker break; 1978fef20d9cSFrederic Weisbecker default: 1979fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 19801da177e4SLinus Torvalds } 1981fef20d9cSFrederic Weisbecker 1982fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 19831da177e4SLinus Torvalds } 1984fef20d9cSFrederic Weisbecker } 1985fef20d9cSFrederic Weisbecker 1986f796937aSJeremy Fitzhardinge if (size > 0) { 1987f796937aSJeremy Fitzhardinge if (str < end) 19881da177e4SLinus Torvalds *str = '\0'; 1989f796937aSJeremy Fitzhardinge else 19900a6047eeSLinus Torvalds end[-1] = '\0'; 1991f796937aSJeremy Fitzhardinge } 1992fef20d9cSFrederic Weisbecker 1993f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 19941da177e4SLinus Torvalds return str-buf; 1995fef20d9cSFrederic Weisbecker 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds /** 20001da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 20011da177e4SLinus Torvalds * @buf: The buffer to place the result into 20021da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20031da177e4SLinus Torvalds * @fmt: The format string to use 20041da177e4SLinus Torvalds * @args: Arguments for the format string 20051da177e4SLinus Torvalds * 20061da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2007b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 20081da177e4SLinus Torvalds * returns 0. 20091da177e4SLinus Torvalds * 2010ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 201120036fdcSAndi Kleen * 201220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20131da177e4SLinus Torvalds */ 20141da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds int i; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 20197b9186f5SAndré Goddard Rosa 2020b921c69fSAnton Arapov if (likely(i < size)) 2021b921c69fSAnton Arapov return i; 2022b921c69fSAnton Arapov if (size != 0) 2023b921c69fSAnton Arapov return size - 1; 2024b921c69fSAnton Arapov return 0; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 20271da177e4SLinus Torvalds 20281da177e4SLinus Torvalds /** 20291da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 20301da177e4SLinus Torvalds * @buf: The buffer to place the result into 20311da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20321da177e4SLinus Torvalds * @fmt: The format string to use 20331da177e4SLinus Torvalds * @...: Arguments for the format string 20341da177e4SLinus Torvalds * 20351da177e4SLinus Torvalds * The return value is the number of characters which would be 20361da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 20371da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 20381da177e4SLinus Torvalds * @size, the resulting string is truncated. 203920036fdcSAndi Kleen * 204020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20411da177e4SLinus Torvalds */ 20421da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 20431da177e4SLinus Torvalds { 20441da177e4SLinus Torvalds va_list args; 20451da177e4SLinus Torvalds int i; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds va_start(args, fmt); 20481da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 20491da177e4SLinus Torvalds va_end(args); 20507b9186f5SAndré Goddard Rosa 20511da177e4SLinus Torvalds return i; 20521da177e4SLinus Torvalds } 20531da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds /** 20561da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 20571da177e4SLinus Torvalds * @buf: The buffer to place the result into 20581da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20591da177e4SLinus Torvalds * @fmt: The format string to use 20601da177e4SLinus Torvalds * @...: Arguments for the format string 20611da177e4SLinus Torvalds * 20621da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2063b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 20641da177e4SLinus Torvalds */ 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 20671da177e4SLinus Torvalds { 20681da177e4SLinus Torvalds va_list args; 20691da177e4SLinus Torvalds int i; 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds va_start(args, fmt); 2072b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 20731da177e4SLinus Torvalds va_end(args); 20747b9186f5SAndré Goddard Rosa 2075b903c0b8SChangli Gao return i; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds /** 20801da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 20811da177e4SLinus Torvalds * @buf: The buffer to place the result into 20821da177e4SLinus Torvalds * @fmt: The format string to use 20831da177e4SLinus Torvalds * @args: Arguments for the format string 20841da177e4SLinus Torvalds * 20851da177e4SLinus Torvalds * The function returns the number of characters written 208672fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 20871da177e4SLinus Torvalds * buffer overflows. 20881da177e4SLinus Torvalds * 2089ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 209020036fdcSAndi Kleen * 209120036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20921da177e4SLinus Torvalds */ 20931da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 20941da177e4SLinus Torvalds { 20951da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds /** 21001da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 21011da177e4SLinus Torvalds * @buf: The buffer to place the result into 21021da177e4SLinus Torvalds * @fmt: The format string to use 21031da177e4SLinus Torvalds * @...: Arguments for the format string 21041da177e4SLinus Torvalds * 21051da177e4SLinus Torvalds * The function returns the number of characters written 210672fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 21071da177e4SLinus Torvalds * buffer overflows. 210820036fdcSAndi Kleen * 210920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 21101da177e4SLinus Torvalds */ 21111da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 21121da177e4SLinus Torvalds { 21131da177e4SLinus Torvalds va_list args; 21141da177e4SLinus Torvalds int i; 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds va_start(args, fmt); 21171da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 21181da177e4SLinus Torvalds va_end(args); 21197b9186f5SAndré Goddard Rosa 21201da177e4SLinus Torvalds return i; 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 21231da177e4SLinus Torvalds 21244370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 21254370aa4aSLai Jiangshan /* 21264370aa4aSLai Jiangshan * bprintf service: 21274370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 21284370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 21294370aa4aSLai Jiangshan */ 21304370aa4aSLai Jiangshan 21314370aa4aSLai Jiangshan /** 21324370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 21334370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 21344370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 21354370aa4aSLai Jiangshan * @fmt: The format string to use 21364370aa4aSLai Jiangshan * @args: Arguments for the format string 21374370aa4aSLai Jiangshan * 21384370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2139da3dae54SMasanari Iida * is skipped. 21404370aa4aSLai Jiangshan * 21414370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 21424370aa4aSLai Jiangshan * the given input. 21434370aa4aSLai Jiangshan * 21444370aa4aSLai Jiangshan * NOTE: 21454370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 21464370aa4aSLai Jiangshan * valid for bstr_printf(). 21474370aa4aSLai Jiangshan */ 21484370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 21494370aa4aSLai Jiangshan { 2150fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 21514370aa4aSLai Jiangshan char *str, *end; 21524370aa4aSLai Jiangshan 21534370aa4aSLai Jiangshan str = (char *)bin_buf; 21544370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 21554370aa4aSLai Jiangshan 21564370aa4aSLai Jiangshan #define save_arg(type) \ 21574370aa4aSLai Jiangshan do { \ 21584370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 21594370aa4aSLai Jiangshan unsigned long long value; \ 21604370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 21614370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 21624370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 21634370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 21644370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 21654370aa4aSLai Jiangshan } \ 21664370aa4aSLai Jiangshan } else { \ 21674370aa4aSLai Jiangshan unsigned long value; \ 21684370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 21694370aa4aSLai Jiangshan value = va_arg(args, int); \ 21704370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 21714370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 21724370aa4aSLai Jiangshan } \ 21734370aa4aSLai Jiangshan str += sizeof(type); \ 21744370aa4aSLai Jiangshan } while (0) 21754370aa4aSLai Jiangshan 2176fef20d9cSFrederic Weisbecker while (*fmt) { 2177d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 21784370aa4aSLai Jiangshan 2179fef20d9cSFrederic Weisbecker fmt += read; 2180fef20d9cSFrederic Weisbecker 2181fef20d9cSFrederic Weisbecker switch (spec.type) { 2182fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2183d4be151bSAndré Goddard Rosa case FORMAT_TYPE_INVALID: 2184d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2185fef20d9cSFrederic Weisbecker break; 2186fef20d9cSFrederic Weisbecker 2187ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2188fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 21894370aa4aSLai Jiangshan save_arg(int); 2190fef20d9cSFrederic Weisbecker break; 21914370aa4aSLai Jiangshan 2192fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 21934370aa4aSLai Jiangshan save_arg(char); 2194fef20d9cSFrederic Weisbecker break; 2195fef20d9cSFrederic Weisbecker 2196fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 21974370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 21984370aa4aSLai Jiangshan size_t len; 21996c356634SAndré Goddard Rosa 22004370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 22014370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 22020f4f81dcSAndré Goddard Rosa save_str = "(null)"; 22036c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 22046c356634SAndré Goddard Rosa if (str + len < end) 22056c356634SAndré Goddard Rosa memcpy(str, save_str, len); 22066c356634SAndré Goddard Rosa str += len; 2207fef20d9cSFrederic Weisbecker break; 22084370aa4aSLai Jiangshan } 2209fef20d9cSFrederic Weisbecker 2210fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 22114370aa4aSLai Jiangshan save_arg(void *); 22124370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2213fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 22144370aa4aSLai Jiangshan fmt++; 2215fef20d9cSFrederic Weisbecker break; 2216fef20d9cSFrederic Weisbecker 2217fef20d9cSFrederic Weisbecker default: 2218fef20d9cSFrederic Weisbecker switch (spec.type) { 2219fef20d9cSFrederic Weisbecker 2220fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2221fef20d9cSFrederic Weisbecker save_arg(long long); 2222fef20d9cSFrederic Weisbecker break; 2223fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2224fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2225fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2226fef20d9cSFrederic Weisbecker break; 2227fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2228fef20d9cSFrederic Weisbecker save_arg(size_t); 2229fef20d9cSFrederic Weisbecker break; 2230fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2231fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2232fef20d9cSFrederic Weisbecker break; 2233a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2234a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2235a4e94ef0SZhaolei save_arg(char); 2236a4e94ef0SZhaolei break; 2237fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2238fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2239fef20d9cSFrederic Weisbecker save_arg(short); 2240fef20d9cSFrederic Weisbecker break; 2241fef20d9cSFrederic Weisbecker default: 2242fef20d9cSFrederic Weisbecker save_arg(int); 2243fef20d9cSFrederic Weisbecker } 2244fef20d9cSFrederic Weisbecker } 2245fef20d9cSFrederic Weisbecker } 2246fef20d9cSFrederic Weisbecker 22477b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2248fef20d9cSFrederic Weisbecker #undef save_arg 22494370aa4aSLai Jiangshan } 22504370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 22514370aa4aSLai Jiangshan 22524370aa4aSLai Jiangshan /** 22534370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 22544370aa4aSLai Jiangshan * @buf: The buffer to place the result into 22554370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 22564370aa4aSLai Jiangshan * @fmt: The format string to use 22574370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 22584370aa4aSLai Jiangshan * 22594370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 22604370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 22614370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 22624370aa4aSLai Jiangshan * 22634370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 22640efb4d20SSteven Rostedt * see vsnprintf comment for details. 22654370aa4aSLai Jiangshan * 22664370aa4aSLai Jiangshan * The return value is the number of characters which would 22674370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 22684370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 22694370aa4aSLai Jiangshan * number of characters written into @buf as return value 22704370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 22714370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 22724370aa4aSLai Jiangshan * string is truncated. 22734370aa4aSLai Jiangshan */ 22744370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 22754370aa4aSLai Jiangshan { 2276fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2277d4be151bSAndré Goddard Rosa char *str, *end; 2278d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 22794370aa4aSLai Jiangshan 22802f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 22814370aa4aSLai Jiangshan return 0; 22824370aa4aSLai Jiangshan 22834370aa4aSLai Jiangshan str = buf; 22844370aa4aSLai Jiangshan end = buf + size; 22854370aa4aSLai Jiangshan 22864370aa4aSLai Jiangshan #define get_arg(type) \ 22874370aa4aSLai Jiangshan ({ \ 22884370aa4aSLai Jiangshan typeof(type) value; \ 22894370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 22904370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 22914370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 22924370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 22934370aa4aSLai Jiangshan } else { \ 22944370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 22954370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 22964370aa4aSLai Jiangshan } \ 22974370aa4aSLai Jiangshan args += sizeof(type); \ 22984370aa4aSLai Jiangshan value; \ 22994370aa4aSLai Jiangshan }) 23004370aa4aSLai Jiangshan 23014370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 23024370aa4aSLai Jiangshan if (end < buf) { 23034370aa4aSLai Jiangshan end = ((void *)-1); 23044370aa4aSLai Jiangshan size = end - buf; 23054370aa4aSLai Jiangshan } 23064370aa4aSLai Jiangshan 2307fef20d9cSFrederic Weisbecker while (*fmt) { 2308fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2309d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2310fef20d9cSFrederic Weisbecker 2311fef20d9cSFrederic Weisbecker fmt += read; 2312fef20d9cSFrederic Weisbecker 2313fef20d9cSFrederic Weisbecker switch (spec.type) { 2314fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2315fef20d9cSFrederic Weisbecker int copy = read; 2316fef20d9cSFrederic Weisbecker if (str < end) { 2317fef20d9cSFrederic Weisbecker if (copy > end - str) 2318fef20d9cSFrederic Weisbecker copy = end - str; 2319fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2320fef20d9cSFrederic Weisbecker } 2321fef20d9cSFrederic Weisbecker str += read; 2322fef20d9cSFrederic Weisbecker break; 23234370aa4aSLai Jiangshan } 23244370aa4aSLai Jiangshan 2325ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2326fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 2327fef20d9cSFrederic Weisbecker break; 23284370aa4aSLai Jiangshan 2329fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2330fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 2331fef20d9cSFrederic Weisbecker break; 23324370aa4aSLai Jiangshan 2333d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2334d4be151bSAndré Goddard Rosa char c; 2335d4be151bSAndré Goddard Rosa 2336fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2337fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 23384370aa4aSLai Jiangshan if (str < end) 23394370aa4aSLai Jiangshan *str = ' '; 23404370aa4aSLai Jiangshan ++str; 23414370aa4aSLai Jiangshan } 23424370aa4aSLai Jiangshan } 23434370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 23444370aa4aSLai Jiangshan if (str < end) 23454370aa4aSLai Jiangshan *str = c; 23464370aa4aSLai Jiangshan ++str; 2347fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 23484370aa4aSLai Jiangshan if (str < end) 23494370aa4aSLai Jiangshan *str = ' '; 23504370aa4aSLai Jiangshan ++str; 23514370aa4aSLai Jiangshan } 2352fef20d9cSFrederic Weisbecker break; 2353d4be151bSAndré Goddard Rosa } 23544370aa4aSLai Jiangshan 2355fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 23564370aa4aSLai Jiangshan const char *str_arg = args; 2357d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2358fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2359fef20d9cSFrederic Weisbecker break; 23604370aa4aSLai Jiangshan } 23614370aa4aSLai Jiangshan 2362fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2363ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 2364fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 23654370aa4aSLai Jiangshan fmt++; 2366fef20d9cSFrederic Weisbecker break; 23674370aa4aSLai Jiangshan 2368fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2369fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 23704370aa4aSLai Jiangshan if (str < end) 23714370aa4aSLai Jiangshan *str = '%'; 23724370aa4aSLai Jiangshan ++str; 2373fef20d9cSFrederic Weisbecker break; 2374fef20d9cSFrederic Weisbecker 2375d4be151bSAndré Goddard Rosa default: { 2376d4be151bSAndré Goddard Rosa unsigned long long num; 2377d4be151bSAndré Goddard Rosa 2378fef20d9cSFrederic Weisbecker switch (spec.type) { 2379fef20d9cSFrederic Weisbecker 2380fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 23814370aa4aSLai Jiangshan num = get_arg(long long); 2382fef20d9cSFrederic Weisbecker break; 2383fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2384fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2385fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2386fef20d9cSFrederic Weisbecker break; 2387fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 23884370aa4aSLai Jiangshan num = get_arg(size_t); 2389fef20d9cSFrederic Weisbecker break; 2390fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 23914370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2392fef20d9cSFrederic Weisbecker break; 2393a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2394a4e94ef0SZhaolei num = get_arg(unsigned char); 2395a4e94ef0SZhaolei break; 2396a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2397a4e94ef0SZhaolei num = get_arg(signed char); 2398a4e94ef0SZhaolei break; 2399fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2400fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2401fef20d9cSFrederic Weisbecker break; 2402fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2403fef20d9cSFrederic Weisbecker num = get_arg(short); 2404fef20d9cSFrederic Weisbecker break; 2405fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 24064370aa4aSLai Jiangshan num = get_arg(unsigned int); 2407fef20d9cSFrederic Weisbecker break; 2408fef20d9cSFrederic Weisbecker default: 2409fef20d9cSFrederic Weisbecker num = get_arg(int); 24104370aa4aSLai Jiangshan } 2411fef20d9cSFrederic Weisbecker 2412fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2413d4be151bSAndré Goddard Rosa } /* default: */ 2414d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2415d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2416fef20d9cSFrederic Weisbecker 24174370aa4aSLai Jiangshan if (size > 0) { 24184370aa4aSLai Jiangshan if (str < end) 24194370aa4aSLai Jiangshan *str = '\0'; 24204370aa4aSLai Jiangshan else 24214370aa4aSLai Jiangshan end[-1] = '\0'; 24224370aa4aSLai Jiangshan } 2423fef20d9cSFrederic Weisbecker 24244370aa4aSLai Jiangshan #undef get_arg 24254370aa4aSLai Jiangshan 24264370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 24274370aa4aSLai Jiangshan return str - buf; 24284370aa4aSLai Jiangshan } 24294370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 24304370aa4aSLai Jiangshan 24314370aa4aSLai Jiangshan /** 24324370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 24334370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 24344370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 24354370aa4aSLai Jiangshan * @fmt: The format string to use 24364370aa4aSLai Jiangshan * @...: Arguments for the format string 24374370aa4aSLai Jiangshan * 24384370aa4aSLai Jiangshan * The function returns the number of words(u32) written 24394370aa4aSLai Jiangshan * into @bin_buf. 24404370aa4aSLai Jiangshan */ 24414370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 24424370aa4aSLai Jiangshan { 24434370aa4aSLai Jiangshan va_list args; 24444370aa4aSLai Jiangshan int ret; 24454370aa4aSLai Jiangshan 24464370aa4aSLai Jiangshan va_start(args, fmt); 24474370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 24484370aa4aSLai Jiangshan va_end(args); 24497b9186f5SAndré Goddard Rosa 24504370aa4aSLai Jiangshan return ret; 24514370aa4aSLai Jiangshan } 24524370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 24534370aa4aSLai Jiangshan 24544370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 24554370aa4aSLai Jiangshan 24561da177e4SLinus Torvalds /** 24571da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 24581da177e4SLinus Torvalds * @buf: input buffer 24591da177e4SLinus Torvalds * @fmt: format of buffer 24601da177e4SLinus Torvalds * @args: arguments 24611da177e4SLinus Torvalds */ 24621da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 24631da177e4SLinus Torvalds { 24641da177e4SLinus Torvalds const char *str = buf; 24651da177e4SLinus Torvalds char *next; 24661da177e4SLinus Torvalds char digit; 24671da177e4SLinus Torvalds int num = 0; 2468ef0658f3SJoe Perches u8 qualifier; 246953809751SJan Beulich unsigned int base; 247053809751SJan Beulich union { 247153809751SJan Beulich long long s; 247253809751SJan Beulich unsigned long long u; 247353809751SJan Beulich } val; 2474ef0658f3SJoe Perches s16 field_width; 2475d4be151bSAndré Goddard Rosa bool is_sign; 24761da177e4SLinus Torvalds 2477da99075cSJan Beulich while (*fmt) { 24781da177e4SLinus Torvalds /* skip any white space in format */ 24791da177e4SLinus Torvalds /* white space in format matchs any amount of 24801da177e4SLinus Torvalds * white space, including none, in the input. 24811da177e4SLinus Torvalds */ 24821da177e4SLinus Torvalds if (isspace(*fmt)) { 2483e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 2484e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 24881da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 24891da177e4SLinus Torvalds if (*fmt++ != *str++) 24901da177e4SLinus Torvalds break; 24911da177e4SLinus Torvalds continue; 24921da177e4SLinus Torvalds } 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds if (!*fmt) 24951da177e4SLinus Torvalds break; 24961da177e4SLinus Torvalds ++fmt; 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds /* skip this conversion. 24991da177e4SLinus Torvalds * advance both strings to next white space 25001da177e4SLinus Torvalds */ 25011da177e4SLinus Torvalds if (*fmt == '*') { 2502da99075cSJan Beulich if (!*str) 2503da99075cSJan Beulich break; 25048fccae2cSAndy Spencer while (!isspace(*fmt) && *fmt != '%' && *fmt) 25051da177e4SLinus Torvalds fmt++; 25061da177e4SLinus Torvalds while (!isspace(*str) && *str) 25071da177e4SLinus Torvalds str++; 25081da177e4SLinus Torvalds continue; 25091da177e4SLinus Torvalds } 25101da177e4SLinus Torvalds 25111da177e4SLinus Torvalds /* get field width */ 25121da177e4SLinus Torvalds field_width = -1; 251353809751SJan Beulich if (isdigit(*fmt)) { 25141da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 251553809751SJan Beulich if (field_width <= 0) 251653809751SJan Beulich break; 251753809751SJan Beulich } 25181da177e4SLinus Torvalds 25191da177e4SLinus Torvalds /* get conversion qualifier */ 25201da177e4SLinus Torvalds qualifier = -1; 252175fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 252275fb8f26SAndy Shevchenko _tolower(*fmt) == 'z') { 25231da177e4SLinus Torvalds qualifier = *fmt++; 25241da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 25251da177e4SLinus Torvalds if (qualifier == 'h') { 25261da177e4SLinus Torvalds qualifier = 'H'; 25271da177e4SLinus Torvalds fmt++; 25281da177e4SLinus Torvalds } else if (qualifier == 'l') { 25291da177e4SLinus Torvalds qualifier = 'L'; 25301da177e4SLinus Torvalds fmt++; 25311da177e4SLinus Torvalds } 25321da177e4SLinus Torvalds } 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds 2535da99075cSJan Beulich if (!*fmt) 2536da99075cSJan Beulich break; 2537da99075cSJan Beulich 2538da99075cSJan Beulich if (*fmt == 'n') { 2539da99075cSJan Beulich /* return number of characters read so far */ 2540da99075cSJan Beulich *va_arg(args, int *) = str - buf; 2541da99075cSJan Beulich ++fmt; 2542da99075cSJan Beulich continue; 2543da99075cSJan Beulich } 2544da99075cSJan Beulich 2545da99075cSJan Beulich if (!*str) 25461da177e4SLinus Torvalds break; 25471da177e4SLinus Torvalds 2548d4be151bSAndré Goddard Rosa base = 10; 25493f623ebaSFabian Frederick is_sign = false; 2550d4be151bSAndré Goddard Rosa 25511da177e4SLinus Torvalds switch (*fmt++) { 25521da177e4SLinus Torvalds case 'c': 25531da177e4SLinus Torvalds { 25541da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 25551da177e4SLinus Torvalds if (field_width == -1) 25561da177e4SLinus Torvalds field_width = 1; 25571da177e4SLinus Torvalds do { 25581da177e4SLinus Torvalds *s++ = *str++; 25591da177e4SLinus Torvalds } while (--field_width > 0 && *str); 25601da177e4SLinus Torvalds num++; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds continue; 25631da177e4SLinus Torvalds case 's': 25641da177e4SLinus Torvalds { 25651da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 25661da177e4SLinus Torvalds if (field_width == -1) 25674be929beSAlexey Dobriyan field_width = SHRT_MAX; 25681da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 2569e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds /* now copy until next white space */ 25727b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 25731da177e4SLinus Torvalds *s++ = *str++; 25741da177e4SLinus Torvalds *s = '\0'; 25751da177e4SLinus Torvalds num++; 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds continue; 25781da177e4SLinus Torvalds case 'o': 25791da177e4SLinus Torvalds base = 8; 25801da177e4SLinus Torvalds break; 25811da177e4SLinus Torvalds case 'x': 25821da177e4SLinus Torvalds case 'X': 25831da177e4SLinus Torvalds base = 16; 25841da177e4SLinus Torvalds break; 25851da177e4SLinus Torvalds case 'i': 25861da177e4SLinus Torvalds base = 0; 25871da177e4SLinus Torvalds case 'd': 25883f623ebaSFabian Frederick is_sign = true; 25891da177e4SLinus Torvalds case 'u': 25901da177e4SLinus Torvalds break; 25911da177e4SLinus Torvalds case '%': 25921da177e4SLinus Torvalds /* looking for '%' in str */ 25931da177e4SLinus Torvalds if (*str++ != '%') 25941da177e4SLinus Torvalds return num; 25951da177e4SLinus Torvalds continue; 25961da177e4SLinus Torvalds default: 25971da177e4SLinus Torvalds /* invalid format; stop here */ 25981da177e4SLinus Torvalds return num; 25991da177e4SLinus Torvalds } 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds /* have some sort of integer conversion. 26021da177e4SLinus Torvalds * first, skip white space in buffer. 26031da177e4SLinus Torvalds */ 2604e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds digit = *str; 26071da177e4SLinus Torvalds if (is_sign && digit == '-') 26081da177e4SLinus Torvalds digit = *(str + 1); 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds if (!digit 26111da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 26121da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 26131da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 26141da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 26151da177e4SLinus Torvalds break; 26161da177e4SLinus Torvalds 261753809751SJan Beulich if (is_sign) 261853809751SJan Beulich val.s = qualifier != 'L' ? 261953809751SJan Beulich simple_strtol(str, &next, base) : 262053809751SJan Beulich simple_strtoll(str, &next, base); 262153809751SJan Beulich else 262253809751SJan Beulich val.u = qualifier != 'L' ? 262353809751SJan Beulich simple_strtoul(str, &next, base) : 262453809751SJan Beulich simple_strtoull(str, &next, base); 262553809751SJan Beulich 262653809751SJan Beulich if (field_width > 0 && next - str > field_width) { 262753809751SJan Beulich if (base == 0) 262853809751SJan Beulich _parse_integer_fixup_radix(str, &base); 262953809751SJan Beulich while (next - str > field_width) { 263053809751SJan Beulich if (is_sign) 263153809751SJan Beulich val.s = div_s64(val.s, base); 263253809751SJan Beulich else 263353809751SJan Beulich val.u = div_u64(val.u, base); 263453809751SJan Beulich --next; 263553809751SJan Beulich } 263653809751SJan Beulich } 263753809751SJan Beulich 26381da177e4SLinus Torvalds switch (qualifier) { 26391da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 264053809751SJan Beulich if (is_sign) 264153809751SJan Beulich *va_arg(args, signed char *) = val.s; 264253809751SJan Beulich else 264353809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 26441da177e4SLinus Torvalds break; 26451da177e4SLinus Torvalds case 'h': 264653809751SJan Beulich if (is_sign) 264753809751SJan Beulich *va_arg(args, short *) = val.s; 264853809751SJan Beulich else 264953809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 26501da177e4SLinus Torvalds break; 26511da177e4SLinus Torvalds case 'l': 265253809751SJan Beulich if (is_sign) 265353809751SJan Beulich *va_arg(args, long *) = val.s; 265453809751SJan Beulich else 265553809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 26561da177e4SLinus Torvalds break; 26571da177e4SLinus Torvalds case 'L': 265853809751SJan Beulich if (is_sign) 265953809751SJan Beulich *va_arg(args, long long *) = val.s; 266053809751SJan Beulich else 266153809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 26621da177e4SLinus Torvalds break; 26631da177e4SLinus Torvalds case 'Z': 26641da177e4SLinus Torvalds case 'z': 266553809751SJan Beulich *va_arg(args, size_t *) = val.u; 26661da177e4SLinus Torvalds break; 26671da177e4SLinus Torvalds default: 266853809751SJan Beulich if (is_sign) 266953809751SJan Beulich *va_arg(args, int *) = val.s; 267053809751SJan Beulich else 267153809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 26721da177e4SLinus Torvalds break; 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds num++; 26751da177e4SLinus Torvalds 26761da177e4SLinus Torvalds if (!next) 26771da177e4SLinus Torvalds break; 26781da177e4SLinus Torvalds str = next; 26791da177e4SLinus Torvalds } 2680c6b40d16SJohannes Berg 26811da177e4SLinus Torvalds return num; 26821da177e4SLinus Torvalds } 26831da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds /** 26861da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 26871da177e4SLinus Torvalds * @buf: input buffer 26881da177e4SLinus Torvalds * @fmt: formatting of buffer 26891da177e4SLinus Torvalds * @...: resulting arguments 26901da177e4SLinus Torvalds */ 26911da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 26921da177e4SLinus Torvalds { 26931da177e4SLinus Torvalds va_list args; 26941da177e4SLinus Torvalds int i; 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds va_start(args, fmt); 26971da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 26981da177e4SLinus Torvalds va_end(args); 26997b9186f5SAndré Goddard Rosa 27001da177e4SLinus Torvalds return i; 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2703