18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/lib/vsprintf.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1991, 1992 Linus Torvalds 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 98c2ecf20Sopenharmony_ci/* 108c2ecf20Sopenharmony_ci * Wirzenius wrote this portably, Torvalds fucked it up :-) 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci/* 148c2ecf20Sopenharmony_ci * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 158c2ecf20Sopenharmony_ci * - changed to provide snprintf and vsnprintf functions 168c2ecf20Sopenharmony_ci * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 178c2ecf20Sopenharmony_ci * - scnprintf and vscnprintf 188c2ecf20Sopenharmony_ci */ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <stdarg.h> 218c2ecf20Sopenharmony_ci#include <linux/build_bug.h> 228c2ecf20Sopenharmony_ci#include <linux/clk.h> 238c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 248c2ecf20Sopenharmony_ci#include <linux/errname.h> 258c2ecf20Sopenharmony_ci#include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 268c2ecf20Sopenharmony_ci#include <linux/types.h> 278c2ecf20Sopenharmony_ci#include <linux/string.h> 288c2ecf20Sopenharmony_ci#include <linux/ctype.h> 298c2ecf20Sopenharmony_ci#include <linux/kernel.h> 308c2ecf20Sopenharmony_ci#include <linux/kallsyms.h> 318c2ecf20Sopenharmony_ci#include <linux/math64.h> 328c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 338c2ecf20Sopenharmony_ci#include <linux/ioport.h> 348c2ecf20Sopenharmony_ci#include <linux/dcache.h> 358c2ecf20Sopenharmony_ci#include <linux/cred.h> 368c2ecf20Sopenharmony_ci#include <linux/rtc.h> 378c2ecf20Sopenharmony_ci#include <linux/time.h> 388c2ecf20Sopenharmony_ci#include <linux/uuid.h> 398c2ecf20Sopenharmony_ci#include <linux/of.h> 408c2ecf20Sopenharmony_ci#include <net/addrconf.h> 418c2ecf20Sopenharmony_ci#include <linux/siphash.h> 428c2ecf20Sopenharmony_ci#include <linux/compiler.h> 438c2ecf20Sopenharmony_ci#include <linux/property.h> 448c2ecf20Sopenharmony_ci#ifdef CONFIG_BLOCK 458c2ecf20Sopenharmony_ci#include <linux/blkdev.h> 468c2ecf20Sopenharmony_ci#endif 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci#include "../mm/internal.h" /* For the trace_print_flags arrays */ 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#include <asm/page.h> /* for PAGE_SIZE */ 518c2ecf20Sopenharmony_ci#include <asm/byteorder.h> /* cpu_to_le16 */ 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#include <linux/string_helpers.h> 548c2ecf20Sopenharmony_ci#include "kstrtox.h" 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic unsigned long long simple_strntoull(const char *startp, size_t max_chars, 578c2ecf20Sopenharmony_ci char **endp, unsigned int base) 588c2ecf20Sopenharmony_ci{ 598c2ecf20Sopenharmony_ci const char *cp; 608c2ecf20Sopenharmony_ci unsigned long long result = 0ULL; 618c2ecf20Sopenharmony_ci size_t prefix_chars; 628c2ecf20Sopenharmony_ci unsigned int rv; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci cp = _parse_integer_fixup_radix(startp, &base); 658c2ecf20Sopenharmony_ci prefix_chars = cp - startp; 668c2ecf20Sopenharmony_ci if (prefix_chars < max_chars) { 678c2ecf20Sopenharmony_ci rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars); 688c2ecf20Sopenharmony_ci /* FIXME */ 698c2ecf20Sopenharmony_ci cp += (rv & ~KSTRTOX_OVERFLOW); 708c2ecf20Sopenharmony_ci } else { 718c2ecf20Sopenharmony_ci /* Field too short for prefix + digit, skip over without converting */ 728c2ecf20Sopenharmony_ci cp = startp + max_chars; 738c2ecf20Sopenharmony_ci } 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci if (endp) 768c2ecf20Sopenharmony_ci *endp = (char *)cp; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci return result; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/** 828c2ecf20Sopenharmony_ci * simple_strtoull - convert a string to an unsigned long long 838c2ecf20Sopenharmony_ci * @cp: The start of the string 848c2ecf20Sopenharmony_ci * @endp: A pointer to the end of the parsed string will be placed here 858c2ecf20Sopenharmony_ci * @base: The number base to use 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * This function has caveats. Please use kstrtoull instead. 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ciunsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci return simple_strntoull(cp, INT_MAX, endp, base); 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_strtoull); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci/** 968c2ecf20Sopenharmony_ci * simple_strtoul - convert a string to an unsigned long 978c2ecf20Sopenharmony_ci * @cp: The start of the string 988c2ecf20Sopenharmony_ci * @endp: A pointer to the end of the parsed string will be placed here 998c2ecf20Sopenharmony_ci * @base: The number base to use 1008c2ecf20Sopenharmony_ci * 1018c2ecf20Sopenharmony_ci * This function has caveats. Please use kstrtoul instead. 1028c2ecf20Sopenharmony_ci */ 1038c2ecf20Sopenharmony_ciunsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci return simple_strtoull(cp, endp, base); 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_strtoul); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/** 1108c2ecf20Sopenharmony_ci * simple_strtol - convert a string to a signed long 1118c2ecf20Sopenharmony_ci * @cp: The start of the string 1128c2ecf20Sopenharmony_ci * @endp: A pointer to the end of the parsed string will be placed here 1138c2ecf20Sopenharmony_ci * @base: The number base to use 1148c2ecf20Sopenharmony_ci * 1158c2ecf20Sopenharmony_ci * This function has caveats. Please use kstrtol instead. 1168c2ecf20Sopenharmony_ci */ 1178c2ecf20Sopenharmony_cilong simple_strtol(const char *cp, char **endp, unsigned int base) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci if (*cp == '-') 1208c2ecf20Sopenharmony_ci return -simple_strtoul(cp + 1, endp, base); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci return simple_strtoul(cp, endp, base); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_strtol); 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cistatic long long simple_strntoll(const char *cp, size_t max_chars, char **endp, 1278c2ecf20Sopenharmony_ci unsigned int base) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci /* 1308c2ecf20Sopenharmony_ci * simple_strntoull() safely handles receiving max_chars==0 in the 1318c2ecf20Sopenharmony_ci * case cp[0] == '-' && max_chars == 1. 1328c2ecf20Sopenharmony_ci * If max_chars == 0 we can drop through and pass it to simple_strntoull() 1338c2ecf20Sopenharmony_ci * and the content of *cp is irrelevant. 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_ci if (*cp == '-' && max_chars > 0) 1368c2ecf20Sopenharmony_ci return -simple_strntoull(cp + 1, max_chars - 1, endp, base); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci return simple_strntoull(cp, max_chars, endp, base); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci/** 1428c2ecf20Sopenharmony_ci * simple_strtoll - convert a string to a signed long long 1438c2ecf20Sopenharmony_ci * @cp: The start of the string 1448c2ecf20Sopenharmony_ci * @endp: A pointer to the end of the parsed string will be placed here 1458c2ecf20Sopenharmony_ci * @base: The number base to use 1468c2ecf20Sopenharmony_ci * 1478c2ecf20Sopenharmony_ci * This function has caveats. Please use kstrtoll instead. 1488c2ecf20Sopenharmony_ci */ 1498c2ecf20Sopenharmony_cilong long simple_strtoll(const char *cp, char **endp, unsigned int base) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci return simple_strntoll(cp, INT_MAX, endp, base); 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_strtoll); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic noinline_for_stack 1568c2ecf20Sopenharmony_ciint skip_atoi(const char **s) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci int i = 0; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci do { 1618c2ecf20Sopenharmony_ci i = i*10 + *((*s)++) - '0'; 1628c2ecf20Sopenharmony_ci } while (isdigit(**s)); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci return i; 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci/* 1688c2ecf20Sopenharmony_ci * Decimal conversion is by far the most typical, and is used for 1698c2ecf20Sopenharmony_ci * /proc and /sys data. This directly impacts e.g. top performance 1708c2ecf20Sopenharmony_ci * with many processes running. We optimize it for speed by emitting 1718c2ecf20Sopenharmony_ci * two characters at a time, using a 200 byte lookup table. This 1728c2ecf20Sopenharmony_ci * roughly halves the number of multiplications compared to computing 1738c2ecf20Sopenharmony_ci * the digits one at a time. Implementation strongly inspired by the 1748c2ecf20Sopenharmony_ci * previous version, which in turn used ideas described at 1758c2ecf20Sopenharmony_ci * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1768c2ecf20Sopenharmony_ci * from the author, Douglas W. Jones). 1778c2ecf20Sopenharmony_ci * 1788c2ecf20Sopenharmony_ci * It turns out there is precisely one 26 bit fixed-point 1798c2ecf20Sopenharmony_ci * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1808c2ecf20Sopenharmony_ci * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1818c2ecf20Sopenharmony_ci * range happens to be somewhat larger (x <= 1073741898), but that's 1828c2ecf20Sopenharmony_ci * irrelevant for our purpose. 1838c2ecf20Sopenharmony_ci * 1848c2ecf20Sopenharmony_ci * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1858c2ecf20Sopenharmony_ci * need a 32x32->64 bit multiply, so we simply use the same constant. 1868c2ecf20Sopenharmony_ci * 1878c2ecf20Sopenharmony_ci * For dividing a number in the range [100, 10^4-1] by 100, there are 1888c2ecf20Sopenharmony_ci * several options. The simplest is (x * 0x147b) >> 19, which is valid 1898c2ecf20Sopenharmony_ci * for all x <= 43698. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic const u16 decpair[100] = { 1938c2ecf20Sopenharmony_ci#define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1948c2ecf20Sopenharmony_ci _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1958c2ecf20Sopenharmony_ci _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1968c2ecf20Sopenharmony_ci _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1978c2ecf20Sopenharmony_ci _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1988c2ecf20Sopenharmony_ci _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1998c2ecf20Sopenharmony_ci _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 2008c2ecf20Sopenharmony_ci _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 2018c2ecf20Sopenharmony_ci _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 2028c2ecf20Sopenharmony_ci _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 2038c2ecf20Sopenharmony_ci _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 2048c2ecf20Sopenharmony_ci#undef _ 2058c2ecf20Sopenharmony_ci}; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci/* 2088c2ecf20Sopenharmony_ci * This will print a single '0' even if r == 0, since we would 2098c2ecf20Sopenharmony_ci * immediately jump to out_r where two 0s would be written but only 2108c2ecf20Sopenharmony_ci * one of them accounted for in buf. This is needed by ip4_string 2118c2ecf20Sopenharmony_ci * below. All other callers pass a non-zero value of r. 2128c2ecf20Sopenharmony_ci*/ 2138c2ecf20Sopenharmony_cistatic noinline_for_stack 2148c2ecf20Sopenharmony_cichar *put_dec_trunc8(char *buf, unsigned r) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci unsigned q; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci /* 1 <= r < 10^8 */ 2198c2ecf20Sopenharmony_ci if (r < 100) 2208c2ecf20Sopenharmony_ci goto out_r; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci /* 100 <= r < 10^8 */ 2238c2ecf20Sopenharmony_ci q = (r * (u64)0x28f5c29) >> 32; 2248c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[r - 100*q]; 2258c2ecf20Sopenharmony_ci buf += 2; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci /* 1 <= q < 10^6 */ 2288c2ecf20Sopenharmony_ci if (q < 100) 2298c2ecf20Sopenharmony_ci goto out_q; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci /* 100 <= q < 10^6 */ 2328c2ecf20Sopenharmony_ci r = (q * (u64)0x28f5c29) >> 32; 2338c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[q - 100*r]; 2348c2ecf20Sopenharmony_ci buf += 2; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci /* 1 <= r < 10^4 */ 2378c2ecf20Sopenharmony_ci if (r < 100) 2388c2ecf20Sopenharmony_ci goto out_r; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci /* 100 <= r < 10^4 */ 2418c2ecf20Sopenharmony_ci q = (r * 0x147b) >> 19; 2428c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[r - 100*q]; 2438c2ecf20Sopenharmony_ci buf += 2; 2448c2ecf20Sopenharmony_ciout_q: 2458c2ecf20Sopenharmony_ci /* 1 <= q < 100 */ 2468c2ecf20Sopenharmony_ci r = q; 2478c2ecf20Sopenharmony_ciout_r: 2488c2ecf20Sopenharmony_ci /* 1 <= r < 100 */ 2498c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[r]; 2508c2ecf20Sopenharmony_ci buf += r < 10 ? 1 : 2; 2518c2ecf20Sopenharmony_ci return buf; 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2558c2ecf20Sopenharmony_cistatic noinline_for_stack 2568c2ecf20Sopenharmony_cichar *put_dec_full8(char *buf, unsigned r) 2578c2ecf20Sopenharmony_ci{ 2588c2ecf20Sopenharmony_ci unsigned q; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci /* 0 <= r < 10^8 */ 2618c2ecf20Sopenharmony_ci q = (r * (u64)0x28f5c29) >> 32; 2628c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[r - 100*q]; 2638c2ecf20Sopenharmony_ci buf += 2; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci /* 0 <= q < 10^6 */ 2668c2ecf20Sopenharmony_ci r = (q * (u64)0x28f5c29) >> 32; 2678c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[q - 100*r]; 2688c2ecf20Sopenharmony_ci buf += 2; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci /* 0 <= r < 10^4 */ 2718c2ecf20Sopenharmony_ci q = (r * 0x147b) >> 19; 2728c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[r - 100*q]; 2738c2ecf20Sopenharmony_ci buf += 2; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci /* 0 <= q < 100 */ 2768c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[q]; 2778c2ecf20Sopenharmony_ci buf += 2; 2788c2ecf20Sopenharmony_ci return buf; 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_cistatic noinline_for_stack 2828c2ecf20Sopenharmony_cichar *put_dec(char *buf, unsigned long long n) 2838c2ecf20Sopenharmony_ci{ 2848c2ecf20Sopenharmony_ci if (n >= 100*1000*1000) 2858c2ecf20Sopenharmony_ci buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2868c2ecf20Sopenharmony_ci /* 1 <= n <= 1.6e11 */ 2878c2ecf20Sopenharmony_ci if (n >= 100*1000*1000) 2888c2ecf20Sopenharmony_ci buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2898c2ecf20Sopenharmony_ci /* 1 <= n < 1e8 */ 2908c2ecf20Sopenharmony_ci return put_dec_trunc8(buf, n); 2918c2ecf20Sopenharmony_ci} 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci#elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_cistatic void 2968c2ecf20Sopenharmony_ciput_dec_full4(char *buf, unsigned r) 2978c2ecf20Sopenharmony_ci{ 2988c2ecf20Sopenharmony_ci unsigned q; 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci /* 0 <= r < 10^4 */ 3018c2ecf20Sopenharmony_ci q = (r * 0x147b) >> 19; 3028c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[r - 100*q]; 3038c2ecf20Sopenharmony_ci buf += 2; 3048c2ecf20Sopenharmony_ci /* 0 <= q < 100 */ 3058c2ecf20Sopenharmony_ci *((u16 *)buf) = decpair[q]; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci/* 3098c2ecf20Sopenharmony_ci * Call put_dec_full4 on x % 10000, return x / 10000. 3108c2ecf20Sopenharmony_ci * The approximation x/10000 == (x * 0x346DC5D7) >> 43 3118c2ecf20Sopenharmony_ci * holds for all x < 1,128,869,999. The largest value this 3128c2ecf20Sopenharmony_ci * helper will ever be asked to convert is 1,125,520,955. 3138c2ecf20Sopenharmony_ci * (second call in the put_dec code, assuming n is all-ones). 3148c2ecf20Sopenharmony_ci */ 3158c2ecf20Sopenharmony_cistatic noinline_for_stack 3168c2ecf20Sopenharmony_ciunsigned put_dec_helper4(char *buf, unsigned x) 3178c2ecf20Sopenharmony_ci{ 3188c2ecf20Sopenharmony_ci uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci put_dec_full4(buf, x - q * 10000); 3218c2ecf20Sopenharmony_ci return q; 3228c2ecf20Sopenharmony_ci} 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci/* Based on code by Douglas W. Jones found at 3258c2ecf20Sopenharmony_ci * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 3268c2ecf20Sopenharmony_ci * (with permission from the author). 3278c2ecf20Sopenharmony_ci * Performs no 64-bit division and hence should be fast on 32-bit machines. 3288c2ecf20Sopenharmony_ci */ 3298c2ecf20Sopenharmony_cistatic 3308c2ecf20Sopenharmony_cichar *put_dec(char *buf, unsigned long long n) 3318c2ecf20Sopenharmony_ci{ 3328c2ecf20Sopenharmony_ci uint32_t d3, d2, d1, q, h; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci if (n < 100*1000*1000) 3358c2ecf20Sopenharmony_ci return put_dec_trunc8(buf, n); 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 3388c2ecf20Sopenharmony_ci h = (n >> 32); 3398c2ecf20Sopenharmony_ci d2 = (h ) & 0xffff; 3408c2ecf20Sopenharmony_ci d3 = (h >> 16); /* implicit "& 0xffff" */ 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3438c2ecf20Sopenharmony_ci = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 3448c2ecf20Sopenharmony_ci q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3458c2ecf20Sopenharmony_ci q = put_dec_helper4(buf, q); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci q += 7671 * d3 + 9496 * d2 + 6 * d1; 3488c2ecf20Sopenharmony_ci q = put_dec_helper4(buf+4, q); 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci q += 4749 * d3 + 42 * d2; 3518c2ecf20Sopenharmony_ci q = put_dec_helper4(buf+8, q); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci q += 281 * d3; 3548c2ecf20Sopenharmony_ci buf += 12; 3558c2ecf20Sopenharmony_ci if (q) 3568c2ecf20Sopenharmony_ci buf = put_dec_trunc8(buf, q); 3578c2ecf20Sopenharmony_ci else while (buf[-1] == '0') 3588c2ecf20Sopenharmony_ci --buf; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci return buf; 3618c2ecf20Sopenharmony_ci} 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci#endif 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci/* 3668c2ecf20Sopenharmony_ci * Convert passed number to decimal string. 3678c2ecf20Sopenharmony_ci * Returns the length of string. On buffer overflow, returns 0. 3688c2ecf20Sopenharmony_ci * 3698c2ecf20Sopenharmony_ci * If speed is not important, use snprintf(). It's easy to read the code. 3708c2ecf20Sopenharmony_ci */ 3718c2ecf20Sopenharmony_ciint num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3728c2ecf20Sopenharmony_ci{ 3738c2ecf20Sopenharmony_ci /* put_dec requires 2-byte alignment of the buffer. */ 3748c2ecf20Sopenharmony_ci char tmp[sizeof(num) * 3] __aligned(2); 3758c2ecf20Sopenharmony_ci int idx, len; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 3788c2ecf20Sopenharmony_ci if (num <= 9) { 3798c2ecf20Sopenharmony_ci tmp[0] = '0' + num; 3808c2ecf20Sopenharmony_ci len = 1; 3818c2ecf20Sopenharmony_ci } else { 3828c2ecf20Sopenharmony_ci len = put_dec(tmp, num) - tmp; 3838c2ecf20Sopenharmony_ci } 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci if (len > size || width > size) 3868c2ecf20Sopenharmony_ci return 0; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci if (width > len) { 3898c2ecf20Sopenharmony_ci width = width - len; 3908c2ecf20Sopenharmony_ci for (idx = 0; idx < width; idx++) 3918c2ecf20Sopenharmony_ci buf[idx] = ' '; 3928c2ecf20Sopenharmony_ci } else { 3938c2ecf20Sopenharmony_ci width = 0; 3948c2ecf20Sopenharmony_ci } 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci for (idx = 0; idx < len; ++idx) 3978c2ecf20Sopenharmony_ci buf[idx + width] = tmp[len - idx - 1]; 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci return len + width; 4008c2ecf20Sopenharmony_ci} 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci#define SIGN 1 /* unsigned/signed, must be 1 */ 4038c2ecf20Sopenharmony_ci#define LEFT 2 /* left justified */ 4048c2ecf20Sopenharmony_ci#define PLUS 4 /* show plus */ 4058c2ecf20Sopenharmony_ci#define SPACE 8 /* space if plus */ 4068c2ecf20Sopenharmony_ci#define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 4078c2ecf20Sopenharmony_ci#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 4088c2ecf20Sopenharmony_ci#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic_assert(ZEROPAD == ('0' - ' ')); 4118c2ecf20Sopenharmony_cistatic_assert(SMALL == ' '); 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_cienum format_type { 4148c2ecf20Sopenharmony_ci FORMAT_TYPE_NONE, /* Just a string part */ 4158c2ecf20Sopenharmony_ci FORMAT_TYPE_WIDTH, 4168c2ecf20Sopenharmony_ci FORMAT_TYPE_PRECISION, 4178c2ecf20Sopenharmony_ci FORMAT_TYPE_CHAR, 4188c2ecf20Sopenharmony_ci FORMAT_TYPE_STR, 4198c2ecf20Sopenharmony_ci FORMAT_TYPE_PTR, 4208c2ecf20Sopenharmony_ci FORMAT_TYPE_PERCENT_CHAR, 4218c2ecf20Sopenharmony_ci FORMAT_TYPE_INVALID, 4228c2ecf20Sopenharmony_ci FORMAT_TYPE_LONG_LONG, 4238c2ecf20Sopenharmony_ci FORMAT_TYPE_ULONG, 4248c2ecf20Sopenharmony_ci FORMAT_TYPE_LONG, 4258c2ecf20Sopenharmony_ci FORMAT_TYPE_UBYTE, 4268c2ecf20Sopenharmony_ci FORMAT_TYPE_BYTE, 4278c2ecf20Sopenharmony_ci FORMAT_TYPE_USHORT, 4288c2ecf20Sopenharmony_ci FORMAT_TYPE_SHORT, 4298c2ecf20Sopenharmony_ci FORMAT_TYPE_UINT, 4308c2ecf20Sopenharmony_ci FORMAT_TYPE_INT, 4318c2ecf20Sopenharmony_ci FORMAT_TYPE_SIZE_T, 4328c2ecf20Sopenharmony_ci FORMAT_TYPE_PTRDIFF 4338c2ecf20Sopenharmony_ci}; 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cistruct printf_spec { 4368c2ecf20Sopenharmony_ci unsigned int type:8; /* format_type enum */ 4378c2ecf20Sopenharmony_ci signed int field_width:24; /* width of output field */ 4388c2ecf20Sopenharmony_ci unsigned int flags:8; /* flags to number() */ 4398c2ecf20Sopenharmony_ci unsigned int base:8; /* number base, 8, 10 or 16 only */ 4408c2ecf20Sopenharmony_ci signed int precision:16; /* # of digits/chars */ 4418c2ecf20Sopenharmony_ci} __packed; 4428c2ecf20Sopenharmony_cistatic_assert(sizeof(struct printf_spec) == 8); 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci#define FIELD_WIDTH_MAX ((1 << 23) - 1) 4458c2ecf20Sopenharmony_ci#define PRECISION_MAX ((1 << 15) - 1) 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic noinline_for_stack 4488c2ecf20Sopenharmony_cichar *number(char *buf, char *end, unsigned long long num, 4498c2ecf20Sopenharmony_ci struct printf_spec spec) 4508c2ecf20Sopenharmony_ci{ 4518c2ecf20Sopenharmony_ci /* put_dec requires 2-byte alignment of the buffer. */ 4528c2ecf20Sopenharmony_ci char tmp[3 * sizeof(num)] __aligned(2); 4538c2ecf20Sopenharmony_ci char sign; 4548c2ecf20Sopenharmony_ci char locase; 4558c2ecf20Sopenharmony_ci int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4568c2ecf20Sopenharmony_ci int i; 4578c2ecf20Sopenharmony_ci bool is_zero = num == 0LL; 4588c2ecf20Sopenharmony_ci int field_width = spec.field_width; 4598c2ecf20Sopenharmony_ci int precision = spec.precision; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4628c2ecf20Sopenharmony_ci * produces same digits or (maybe lowercased) letters */ 4638c2ecf20Sopenharmony_ci locase = (spec.flags & SMALL); 4648c2ecf20Sopenharmony_ci if (spec.flags & LEFT) 4658c2ecf20Sopenharmony_ci spec.flags &= ~ZEROPAD; 4668c2ecf20Sopenharmony_ci sign = 0; 4678c2ecf20Sopenharmony_ci if (spec.flags & SIGN) { 4688c2ecf20Sopenharmony_ci if ((signed long long)num < 0) { 4698c2ecf20Sopenharmony_ci sign = '-'; 4708c2ecf20Sopenharmony_ci num = -(signed long long)num; 4718c2ecf20Sopenharmony_ci field_width--; 4728c2ecf20Sopenharmony_ci } else if (spec.flags & PLUS) { 4738c2ecf20Sopenharmony_ci sign = '+'; 4748c2ecf20Sopenharmony_ci field_width--; 4758c2ecf20Sopenharmony_ci } else if (spec.flags & SPACE) { 4768c2ecf20Sopenharmony_ci sign = ' '; 4778c2ecf20Sopenharmony_ci field_width--; 4788c2ecf20Sopenharmony_ci } 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci if (need_pfx) { 4818c2ecf20Sopenharmony_ci if (spec.base == 16) 4828c2ecf20Sopenharmony_ci field_width -= 2; 4838c2ecf20Sopenharmony_ci else if (!is_zero) 4848c2ecf20Sopenharmony_ci field_width--; 4858c2ecf20Sopenharmony_ci } 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci /* generate full string in tmp[], in reverse order */ 4888c2ecf20Sopenharmony_ci i = 0; 4898c2ecf20Sopenharmony_ci if (num < spec.base) 4908c2ecf20Sopenharmony_ci tmp[i++] = hex_asc_upper[num] | locase; 4918c2ecf20Sopenharmony_ci else if (spec.base != 10) { /* 8 or 16 */ 4928c2ecf20Sopenharmony_ci int mask = spec.base - 1; 4938c2ecf20Sopenharmony_ci int shift = 3; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci if (spec.base == 16) 4968c2ecf20Sopenharmony_ci shift = 4; 4978c2ecf20Sopenharmony_ci do { 4988c2ecf20Sopenharmony_ci tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 4998c2ecf20Sopenharmony_ci num >>= shift; 5008c2ecf20Sopenharmony_ci } while (num); 5018c2ecf20Sopenharmony_ci } else { /* base 10 */ 5028c2ecf20Sopenharmony_ci i = put_dec(tmp, num) - tmp; 5038c2ecf20Sopenharmony_ci } 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci /* printing 100 using %2d gives "100", not "00" */ 5068c2ecf20Sopenharmony_ci if (i > precision) 5078c2ecf20Sopenharmony_ci precision = i; 5088c2ecf20Sopenharmony_ci /* leading space padding */ 5098c2ecf20Sopenharmony_ci field_width -= precision; 5108c2ecf20Sopenharmony_ci if (!(spec.flags & (ZEROPAD | LEFT))) { 5118c2ecf20Sopenharmony_ci while (--field_width >= 0) { 5128c2ecf20Sopenharmony_ci if (buf < end) 5138c2ecf20Sopenharmony_ci *buf = ' '; 5148c2ecf20Sopenharmony_ci ++buf; 5158c2ecf20Sopenharmony_ci } 5168c2ecf20Sopenharmony_ci } 5178c2ecf20Sopenharmony_ci /* sign */ 5188c2ecf20Sopenharmony_ci if (sign) { 5198c2ecf20Sopenharmony_ci if (buf < end) 5208c2ecf20Sopenharmony_ci *buf = sign; 5218c2ecf20Sopenharmony_ci ++buf; 5228c2ecf20Sopenharmony_ci } 5238c2ecf20Sopenharmony_ci /* "0x" / "0" prefix */ 5248c2ecf20Sopenharmony_ci if (need_pfx) { 5258c2ecf20Sopenharmony_ci if (spec.base == 16 || !is_zero) { 5268c2ecf20Sopenharmony_ci if (buf < end) 5278c2ecf20Sopenharmony_ci *buf = '0'; 5288c2ecf20Sopenharmony_ci ++buf; 5298c2ecf20Sopenharmony_ci } 5308c2ecf20Sopenharmony_ci if (spec.base == 16) { 5318c2ecf20Sopenharmony_ci if (buf < end) 5328c2ecf20Sopenharmony_ci *buf = ('X' | locase); 5338c2ecf20Sopenharmony_ci ++buf; 5348c2ecf20Sopenharmony_ci } 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci /* zero or space padding */ 5378c2ecf20Sopenharmony_ci if (!(spec.flags & LEFT)) { 5388c2ecf20Sopenharmony_ci char c = ' ' + (spec.flags & ZEROPAD); 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci while (--field_width >= 0) { 5418c2ecf20Sopenharmony_ci if (buf < end) 5428c2ecf20Sopenharmony_ci *buf = c; 5438c2ecf20Sopenharmony_ci ++buf; 5448c2ecf20Sopenharmony_ci } 5458c2ecf20Sopenharmony_ci } 5468c2ecf20Sopenharmony_ci /* hmm even more zero padding? */ 5478c2ecf20Sopenharmony_ci while (i <= --precision) { 5488c2ecf20Sopenharmony_ci if (buf < end) 5498c2ecf20Sopenharmony_ci *buf = '0'; 5508c2ecf20Sopenharmony_ci ++buf; 5518c2ecf20Sopenharmony_ci } 5528c2ecf20Sopenharmony_ci /* actual digits of result */ 5538c2ecf20Sopenharmony_ci while (--i >= 0) { 5548c2ecf20Sopenharmony_ci if (buf < end) 5558c2ecf20Sopenharmony_ci *buf = tmp[i]; 5568c2ecf20Sopenharmony_ci ++buf; 5578c2ecf20Sopenharmony_ci } 5588c2ecf20Sopenharmony_ci /* trailing space padding */ 5598c2ecf20Sopenharmony_ci while (--field_width >= 0) { 5608c2ecf20Sopenharmony_ci if (buf < end) 5618c2ecf20Sopenharmony_ci *buf = ' '; 5628c2ecf20Sopenharmony_ci ++buf; 5638c2ecf20Sopenharmony_ci } 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci return buf; 5668c2ecf20Sopenharmony_ci} 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_cistatic noinline_for_stack 5698c2ecf20Sopenharmony_cichar *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5708c2ecf20Sopenharmony_ci{ 5718c2ecf20Sopenharmony_ci struct printf_spec spec; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci spec.type = FORMAT_TYPE_PTR; 5748c2ecf20Sopenharmony_ci spec.field_width = 2 + 2 * size; /* 0x + hex */ 5758c2ecf20Sopenharmony_ci spec.flags = SPECIAL | SMALL | ZEROPAD; 5768c2ecf20Sopenharmony_ci spec.base = 16; 5778c2ecf20Sopenharmony_ci spec.precision = -1; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci return number(buf, end, num, spec); 5808c2ecf20Sopenharmony_ci} 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_cistatic void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5838c2ecf20Sopenharmony_ci{ 5848c2ecf20Sopenharmony_ci size_t size; 5858c2ecf20Sopenharmony_ci if (buf >= end) /* nowhere to put anything */ 5868c2ecf20Sopenharmony_ci return; 5878c2ecf20Sopenharmony_ci size = end - buf; 5888c2ecf20Sopenharmony_ci if (size <= spaces) { 5898c2ecf20Sopenharmony_ci memset(buf, ' ', size); 5908c2ecf20Sopenharmony_ci return; 5918c2ecf20Sopenharmony_ci } 5928c2ecf20Sopenharmony_ci if (len) { 5938c2ecf20Sopenharmony_ci if (len > size - spaces) 5948c2ecf20Sopenharmony_ci len = size - spaces; 5958c2ecf20Sopenharmony_ci memmove(buf + spaces, buf, len); 5968c2ecf20Sopenharmony_ci } 5978c2ecf20Sopenharmony_ci memset(buf, ' ', spaces); 5988c2ecf20Sopenharmony_ci} 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_ci/* 6018c2ecf20Sopenharmony_ci * Handle field width padding for a string. 6028c2ecf20Sopenharmony_ci * @buf: current buffer position 6038c2ecf20Sopenharmony_ci * @n: length of string 6048c2ecf20Sopenharmony_ci * @end: end of output buffer 6058c2ecf20Sopenharmony_ci * @spec: for field width and flags 6068c2ecf20Sopenharmony_ci * Returns: new buffer position after padding. 6078c2ecf20Sopenharmony_ci */ 6088c2ecf20Sopenharmony_cistatic noinline_for_stack 6098c2ecf20Sopenharmony_cichar *widen_string(char *buf, int n, char *end, struct printf_spec spec) 6108c2ecf20Sopenharmony_ci{ 6118c2ecf20Sopenharmony_ci unsigned spaces; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci if (likely(n >= spec.field_width)) 6148c2ecf20Sopenharmony_ci return buf; 6158c2ecf20Sopenharmony_ci /* we want to pad the sucker */ 6168c2ecf20Sopenharmony_ci spaces = spec.field_width - n; 6178c2ecf20Sopenharmony_ci if (!(spec.flags & LEFT)) { 6188c2ecf20Sopenharmony_ci move_right(buf - n, end, n, spaces); 6198c2ecf20Sopenharmony_ci return buf + spaces; 6208c2ecf20Sopenharmony_ci } 6218c2ecf20Sopenharmony_ci while (spaces--) { 6228c2ecf20Sopenharmony_ci if (buf < end) 6238c2ecf20Sopenharmony_ci *buf = ' '; 6248c2ecf20Sopenharmony_ci ++buf; 6258c2ecf20Sopenharmony_ci } 6268c2ecf20Sopenharmony_ci return buf; 6278c2ecf20Sopenharmony_ci} 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci/* Handle string from a well known address. */ 6308c2ecf20Sopenharmony_cistatic char *string_nocheck(char *buf, char *end, const char *s, 6318c2ecf20Sopenharmony_ci struct printf_spec spec) 6328c2ecf20Sopenharmony_ci{ 6338c2ecf20Sopenharmony_ci int len = 0; 6348c2ecf20Sopenharmony_ci int lim = spec.precision; 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci while (lim--) { 6378c2ecf20Sopenharmony_ci char c = *s++; 6388c2ecf20Sopenharmony_ci if (!c) 6398c2ecf20Sopenharmony_ci break; 6408c2ecf20Sopenharmony_ci if (buf < end) 6418c2ecf20Sopenharmony_ci *buf = c; 6428c2ecf20Sopenharmony_ci ++buf; 6438c2ecf20Sopenharmony_ci ++len; 6448c2ecf20Sopenharmony_ci } 6458c2ecf20Sopenharmony_ci return widen_string(buf, len, end, spec); 6468c2ecf20Sopenharmony_ci} 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_cistatic char *err_ptr(char *buf, char *end, void *ptr, 6498c2ecf20Sopenharmony_ci struct printf_spec spec) 6508c2ecf20Sopenharmony_ci{ 6518c2ecf20Sopenharmony_ci int err = PTR_ERR(ptr); 6528c2ecf20Sopenharmony_ci const char *sym = errname(err); 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci if (sym) 6558c2ecf20Sopenharmony_ci return string_nocheck(buf, end, sym, spec); 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci /* 6588c2ecf20Sopenharmony_ci * Somebody passed ERR_PTR(-1234) or some other non-existing 6598c2ecf20Sopenharmony_ci * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to 6608c2ecf20Sopenharmony_ci * printing it as its decimal representation. 6618c2ecf20Sopenharmony_ci */ 6628c2ecf20Sopenharmony_ci spec.flags |= SIGN; 6638c2ecf20Sopenharmony_ci spec.base = 10; 6648c2ecf20Sopenharmony_ci return number(buf, end, err, spec); 6658c2ecf20Sopenharmony_ci} 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci/* Be careful: error messages must fit into the given buffer. */ 6688c2ecf20Sopenharmony_cistatic char *error_string(char *buf, char *end, const char *s, 6698c2ecf20Sopenharmony_ci struct printf_spec spec) 6708c2ecf20Sopenharmony_ci{ 6718c2ecf20Sopenharmony_ci /* 6728c2ecf20Sopenharmony_ci * Hard limit to avoid a completely insane messages. It actually 6738c2ecf20Sopenharmony_ci * works pretty well because most error messages are in 6748c2ecf20Sopenharmony_ci * the many pointer format modifiers. 6758c2ecf20Sopenharmony_ci */ 6768c2ecf20Sopenharmony_ci if (spec.precision == -1) 6778c2ecf20Sopenharmony_ci spec.precision = 2 * sizeof(void *); 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci return string_nocheck(buf, end, s, spec); 6808c2ecf20Sopenharmony_ci} 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci/* 6838c2ecf20Sopenharmony_ci * Do not call any complex external code here. Nested printk()/vsprintf() 6848c2ecf20Sopenharmony_ci * might cause infinite loops. Failures might break printk() and would 6858c2ecf20Sopenharmony_ci * be hard to debug. 6868c2ecf20Sopenharmony_ci */ 6878c2ecf20Sopenharmony_cistatic const char *check_pointer_msg(const void *ptr) 6888c2ecf20Sopenharmony_ci{ 6898c2ecf20Sopenharmony_ci if (!ptr) 6908c2ecf20Sopenharmony_ci return "(null)"; 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 6938c2ecf20Sopenharmony_ci return "(efault)"; 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci return NULL; 6968c2ecf20Sopenharmony_ci} 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_cistatic int check_pointer(char **buf, char *end, const void *ptr, 6998c2ecf20Sopenharmony_ci struct printf_spec spec) 7008c2ecf20Sopenharmony_ci{ 7018c2ecf20Sopenharmony_ci const char *err_msg; 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci err_msg = check_pointer_msg(ptr); 7048c2ecf20Sopenharmony_ci if (err_msg) { 7058c2ecf20Sopenharmony_ci *buf = error_string(*buf, end, err_msg, spec); 7068c2ecf20Sopenharmony_ci return -EFAULT; 7078c2ecf20Sopenharmony_ci } 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_ci return 0; 7108c2ecf20Sopenharmony_ci} 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_cistatic noinline_for_stack 7138c2ecf20Sopenharmony_cichar *string(char *buf, char *end, const char *s, 7148c2ecf20Sopenharmony_ci struct printf_spec spec) 7158c2ecf20Sopenharmony_ci{ 7168c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, s, spec)) 7178c2ecf20Sopenharmony_ci return buf; 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci return string_nocheck(buf, end, s, spec); 7208c2ecf20Sopenharmony_ci} 7218c2ecf20Sopenharmony_ci 7228c2ecf20Sopenharmony_cistatic char *pointer_string(char *buf, char *end, 7238c2ecf20Sopenharmony_ci const void *ptr, 7248c2ecf20Sopenharmony_ci struct printf_spec spec) 7258c2ecf20Sopenharmony_ci{ 7268c2ecf20Sopenharmony_ci spec.base = 16; 7278c2ecf20Sopenharmony_ci spec.flags |= SMALL; 7288c2ecf20Sopenharmony_ci if (spec.field_width == -1) { 7298c2ecf20Sopenharmony_ci spec.field_width = 2 * sizeof(ptr); 7308c2ecf20Sopenharmony_ci spec.flags |= ZEROPAD; 7318c2ecf20Sopenharmony_ci } 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ci return number(buf, end, (unsigned long int)ptr, spec); 7348c2ecf20Sopenharmony_ci} 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci/* Make pointers available for printing early in the boot sequence. */ 7378c2ecf20Sopenharmony_cistatic int debug_boot_weak_hash __ro_after_init; 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_cistatic int __init debug_boot_weak_hash_enable(char *str) 7408c2ecf20Sopenharmony_ci{ 7418c2ecf20Sopenharmony_ci debug_boot_weak_hash = 1; 7428c2ecf20Sopenharmony_ci pr_info("debug_boot_weak_hash enabled\n"); 7438c2ecf20Sopenharmony_ci return 0; 7448c2ecf20Sopenharmony_ci} 7458c2ecf20Sopenharmony_ciearly_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_cistatic DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 7488c2ecf20Sopenharmony_cistatic siphash_key_t ptr_key __read_mostly; 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_cistatic void enable_ptr_key_workfn(struct work_struct *work) 7518c2ecf20Sopenharmony_ci{ 7528c2ecf20Sopenharmony_ci get_random_bytes(&ptr_key, sizeof(ptr_key)); 7538c2ecf20Sopenharmony_ci /* Needs to run from preemptible context */ 7548c2ecf20Sopenharmony_ci static_branch_disable(¬_filled_random_ptr_key); 7558c2ecf20Sopenharmony_ci} 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_cistatic DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_cistatic int fill_random_ptr_key(struct notifier_block *nb, 7608c2ecf20Sopenharmony_ci unsigned long action, void *data) 7618c2ecf20Sopenharmony_ci{ 7628c2ecf20Sopenharmony_ci /* This may be in an interrupt handler. */ 7638c2ecf20Sopenharmony_ci queue_work(system_unbound_wq, &enable_ptr_key_work); 7648c2ecf20Sopenharmony_ci return 0; 7658c2ecf20Sopenharmony_ci} 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_cistatic struct notifier_block random_ready = { 7688c2ecf20Sopenharmony_ci .notifier_call = fill_random_ptr_key 7698c2ecf20Sopenharmony_ci}; 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_cistatic int __init initialize_ptr_random(void) 7728c2ecf20Sopenharmony_ci{ 7738c2ecf20Sopenharmony_ci int key_size = sizeof(ptr_key); 7748c2ecf20Sopenharmony_ci int ret; 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci /* Use hw RNG if available. */ 7778c2ecf20Sopenharmony_ci if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { 7788c2ecf20Sopenharmony_ci static_branch_disable(¬_filled_random_ptr_key); 7798c2ecf20Sopenharmony_ci return 0; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci ret = register_random_ready_notifier(&random_ready); 7838c2ecf20Sopenharmony_ci if (!ret) { 7848c2ecf20Sopenharmony_ci return 0; 7858c2ecf20Sopenharmony_ci } else if (ret == -EALREADY) { 7868c2ecf20Sopenharmony_ci /* This is in preemptible context */ 7878c2ecf20Sopenharmony_ci enable_ptr_key_workfn(&enable_ptr_key_work); 7888c2ecf20Sopenharmony_ci return 0; 7898c2ecf20Sopenharmony_ci } 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci return ret; 7928c2ecf20Sopenharmony_ci} 7938c2ecf20Sopenharmony_ciearly_initcall(initialize_ptr_random); 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci/* Maps a pointer to a 32 bit unique identifier. */ 7968c2ecf20Sopenharmony_cistatic inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 7978c2ecf20Sopenharmony_ci{ 7988c2ecf20Sopenharmony_ci unsigned long hashval; 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci if (static_branch_unlikely(¬_filled_random_ptr_key)) 8018c2ecf20Sopenharmony_ci return -EAGAIN; 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT 8048c2ecf20Sopenharmony_ci hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 8058c2ecf20Sopenharmony_ci /* 8068c2ecf20Sopenharmony_ci * Mask off the first 32 bits, this makes explicit that we have 8078c2ecf20Sopenharmony_ci * modified the address (and 32 bits is plenty for a unique ID). 8088c2ecf20Sopenharmony_ci */ 8098c2ecf20Sopenharmony_ci hashval = hashval & 0xffffffff; 8108c2ecf20Sopenharmony_ci#else 8118c2ecf20Sopenharmony_ci hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 8128c2ecf20Sopenharmony_ci#endif 8138c2ecf20Sopenharmony_ci *hashval_out = hashval; 8148c2ecf20Sopenharmony_ci return 0; 8158c2ecf20Sopenharmony_ci} 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ciint ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 8188c2ecf20Sopenharmony_ci{ 8198c2ecf20Sopenharmony_ci return __ptr_to_hashval(ptr, hashval_out); 8208c2ecf20Sopenharmony_ci} 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_cistatic char *ptr_to_id(char *buf, char *end, const void *ptr, 8238c2ecf20Sopenharmony_ci struct printf_spec spec) 8248c2ecf20Sopenharmony_ci{ 8258c2ecf20Sopenharmony_ci const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 8268c2ecf20Sopenharmony_ci unsigned long hashval; 8278c2ecf20Sopenharmony_ci int ret; 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ci /* 8308c2ecf20Sopenharmony_ci * Print the real pointer value for NULL and error pointers, 8318c2ecf20Sopenharmony_ci * as they are not actual addresses. 8328c2ecf20Sopenharmony_ci */ 8338c2ecf20Sopenharmony_ci if (IS_ERR_OR_NULL(ptr)) 8348c2ecf20Sopenharmony_ci return pointer_string(buf, end, ptr, spec); 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci /* When debugging early boot use non-cryptographically secure hash. */ 8378c2ecf20Sopenharmony_ci if (unlikely(debug_boot_weak_hash)) { 8388c2ecf20Sopenharmony_ci hashval = hash_long((unsigned long)ptr, 32); 8398c2ecf20Sopenharmony_ci return pointer_string(buf, end, (const void *)hashval, spec); 8408c2ecf20Sopenharmony_ci } 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci ret = __ptr_to_hashval(ptr, &hashval); 8438c2ecf20Sopenharmony_ci if (ret) { 8448c2ecf20Sopenharmony_ci spec.field_width = 2 * sizeof(ptr); 8458c2ecf20Sopenharmony_ci /* string length must be less than default_width */ 8468c2ecf20Sopenharmony_ci return error_string(buf, end, str, spec); 8478c2ecf20Sopenharmony_ci } 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci return pointer_string(buf, end, (const void *)hashval, spec); 8508c2ecf20Sopenharmony_ci} 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ciint kptr_restrict __read_mostly; 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_cistatic noinline_for_stack 8558c2ecf20Sopenharmony_cichar *restricted_pointer(char *buf, char *end, const void *ptr, 8568c2ecf20Sopenharmony_ci struct printf_spec spec) 8578c2ecf20Sopenharmony_ci{ 8588c2ecf20Sopenharmony_ci switch (kptr_restrict) { 8598c2ecf20Sopenharmony_ci case 0: 8608c2ecf20Sopenharmony_ci /* Handle as %p, hash and do _not_ leak addresses. */ 8618c2ecf20Sopenharmony_ci return ptr_to_id(buf, end, ptr, spec); 8628c2ecf20Sopenharmony_ci case 1: { 8638c2ecf20Sopenharmony_ci const struct cred *cred; 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ci /* 8668c2ecf20Sopenharmony_ci * kptr_restrict==1 cannot be used in IRQ context 8678c2ecf20Sopenharmony_ci * because its test for CAP_SYSLOG would be meaningless. 8688c2ecf20Sopenharmony_ci */ 8698c2ecf20Sopenharmony_ci if (in_irq() || in_serving_softirq() || in_nmi()) { 8708c2ecf20Sopenharmony_ci if (spec.field_width == -1) 8718c2ecf20Sopenharmony_ci spec.field_width = 2 * sizeof(ptr); 8728c2ecf20Sopenharmony_ci return error_string(buf, end, "pK-error", spec); 8738c2ecf20Sopenharmony_ci } 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci /* 8768c2ecf20Sopenharmony_ci * Only print the real pointer value if the current 8778c2ecf20Sopenharmony_ci * process has CAP_SYSLOG and is running with the 8788c2ecf20Sopenharmony_ci * same credentials it started with. This is because 8798c2ecf20Sopenharmony_ci * access to files is checked at open() time, but %pK 8808c2ecf20Sopenharmony_ci * checks permission at read() time. We don't want to 8818c2ecf20Sopenharmony_ci * leak pointer values if a binary opens a file using 8828c2ecf20Sopenharmony_ci * %pK and then elevates privileges before reading it. 8838c2ecf20Sopenharmony_ci */ 8848c2ecf20Sopenharmony_ci cred = current_cred(); 8858c2ecf20Sopenharmony_ci if (!has_capability_noaudit(current, CAP_SYSLOG) || 8868c2ecf20Sopenharmony_ci !uid_eq(cred->euid, cred->uid) || 8878c2ecf20Sopenharmony_ci !gid_eq(cred->egid, cred->gid)) 8888c2ecf20Sopenharmony_ci ptr = NULL; 8898c2ecf20Sopenharmony_ci break; 8908c2ecf20Sopenharmony_ci } 8918c2ecf20Sopenharmony_ci case 2: 8928c2ecf20Sopenharmony_ci default: 8938c2ecf20Sopenharmony_ci /* Always print 0's for %pK */ 8948c2ecf20Sopenharmony_ci ptr = NULL; 8958c2ecf20Sopenharmony_ci break; 8968c2ecf20Sopenharmony_ci } 8978c2ecf20Sopenharmony_ci 8988c2ecf20Sopenharmony_ci return pointer_string(buf, end, ptr, spec); 8998c2ecf20Sopenharmony_ci} 9008c2ecf20Sopenharmony_ci 9018c2ecf20Sopenharmony_cistatic noinline_for_stack 9028c2ecf20Sopenharmony_cichar *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 9038c2ecf20Sopenharmony_ci const char *fmt) 9048c2ecf20Sopenharmony_ci{ 9058c2ecf20Sopenharmony_ci const char *array[4], *s; 9068c2ecf20Sopenharmony_ci const struct dentry *p; 9078c2ecf20Sopenharmony_ci int depth; 9088c2ecf20Sopenharmony_ci int i, n; 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ci switch (fmt[1]) { 9118c2ecf20Sopenharmony_ci case '2': case '3': case '4': 9128c2ecf20Sopenharmony_ci depth = fmt[1] - '0'; 9138c2ecf20Sopenharmony_ci break; 9148c2ecf20Sopenharmony_ci default: 9158c2ecf20Sopenharmony_ci depth = 1; 9168c2ecf20Sopenharmony_ci } 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci rcu_read_lock(); 9198c2ecf20Sopenharmony_ci for (i = 0; i < depth; i++, d = p) { 9208c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, d, spec)) { 9218c2ecf20Sopenharmony_ci rcu_read_unlock(); 9228c2ecf20Sopenharmony_ci return buf; 9238c2ecf20Sopenharmony_ci } 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci p = READ_ONCE(d->d_parent); 9268c2ecf20Sopenharmony_ci array[i] = READ_ONCE(d->d_name.name); 9278c2ecf20Sopenharmony_ci if (p == d) { 9288c2ecf20Sopenharmony_ci if (i) 9298c2ecf20Sopenharmony_ci array[i] = ""; 9308c2ecf20Sopenharmony_ci i++; 9318c2ecf20Sopenharmony_ci break; 9328c2ecf20Sopenharmony_ci } 9338c2ecf20Sopenharmony_ci } 9348c2ecf20Sopenharmony_ci s = array[--i]; 9358c2ecf20Sopenharmony_ci for (n = 0; n != spec.precision; n++, buf++) { 9368c2ecf20Sopenharmony_ci char c = *s++; 9378c2ecf20Sopenharmony_ci if (!c) { 9388c2ecf20Sopenharmony_ci if (!i) 9398c2ecf20Sopenharmony_ci break; 9408c2ecf20Sopenharmony_ci c = '/'; 9418c2ecf20Sopenharmony_ci s = array[--i]; 9428c2ecf20Sopenharmony_ci } 9438c2ecf20Sopenharmony_ci if (buf < end) 9448c2ecf20Sopenharmony_ci *buf = c; 9458c2ecf20Sopenharmony_ci } 9468c2ecf20Sopenharmony_ci rcu_read_unlock(); 9478c2ecf20Sopenharmony_ci return widen_string(buf, n, end, spec); 9488c2ecf20Sopenharmony_ci} 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_cistatic noinline_for_stack 9518c2ecf20Sopenharmony_cichar *file_dentry_name(char *buf, char *end, const struct file *f, 9528c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 9538c2ecf20Sopenharmony_ci{ 9548c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, f, spec)) 9558c2ecf20Sopenharmony_ci return buf; 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_ci return dentry_name(buf, end, f->f_path.dentry, spec, fmt); 9588c2ecf20Sopenharmony_ci} 9598c2ecf20Sopenharmony_ci#ifdef CONFIG_BLOCK 9608c2ecf20Sopenharmony_cistatic noinline_for_stack 9618c2ecf20Sopenharmony_cichar *bdev_name(char *buf, char *end, struct block_device *bdev, 9628c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 9638c2ecf20Sopenharmony_ci{ 9648c2ecf20Sopenharmony_ci struct gendisk *hd; 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, bdev, spec)) 9678c2ecf20Sopenharmony_ci return buf; 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci hd = bdev->bd_disk; 9708c2ecf20Sopenharmony_ci buf = string(buf, end, hd->disk_name, spec); 9718c2ecf20Sopenharmony_ci if (bdev->bd_partno) { 9728c2ecf20Sopenharmony_ci if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 9738c2ecf20Sopenharmony_ci if (buf < end) 9748c2ecf20Sopenharmony_ci *buf = 'p'; 9758c2ecf20Sopenharmony_ci buf++; 9768c2ecf20Sopenharmony_ci } 9778c2ecf20Sopenharmony_ci buf = number(buf, end, bdev->bd_partno, spec); 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci return buf; 9808c2ecf20Sopenharmony_ci} 9818c2ecf20Sopenharmony_ci#endif 9828c2ecf20Sopenharmony_ci 9838c2ecf20Sopenharmony_cistatic noinline_for_stack 9848c2ecf20Sopenharmony_cichar *symbol_string(char *buf, char *end, void *ptr, 9858c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 9868c2ecf20Sopenharmony_ci{ 9878c2ecf20Sopenharmony_ci unsigned long value; 9888c2ecf20Sopenharmony_ci#ifdef CONFIG_KALLSYMS 9898c2ecf20Sopenharmony_ci char sym[KSYM_SYMBOL_LEN]; 9908c2ecf20Sopenharmony_ci#endif 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_ci if (fmt[1] == 'R') 9938c2ecf20Sopenharmony_ci ptr = __builtin_extract_return_addr(ptr); 9948c2ecf20Sopenharmony_ci value = (unsigned long)ptr; 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci#ifdef CONFIG_KALLSYMS 9978c2ecf20Sopenharmony_ci if (*fmt == 'B') 9988c2ecf20Sopenharmony_ci sprint_backtrace(sym, value); 9998c2ecf20Sopenharmony_ci else if (*fmt != 's') 10008c2ecf20Sopenharmony_ci sprint_symbol(sym, value); 10018c2ecf20Sopenharmony_ci else 10028c2ecf20Sopenharmony_ci sprint_symbol_no_offset(sym, value); 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci return string_nocheck(buf, end, sym, spec); 10058c2ecf20Sopenharmony_ci#else 10068c2ecf20Sopenharmony_ci return special_hex_number(buf, end, value, sizeof(void *)); 10078c2ecf20Sopenharmony_ci#endif 10088c2ecf20Sopenharmony_ci} 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_cistatic const struct printf_spec default_str_spec = { 10118c2ecf20Sopenharmony_ci .field_width = -1, 10128c2ecf20Sopenharmony_ci .precision = -1, 10138c2ecf20Sopenharmony_ci}; 10148c2ecf20Sopenharmony_ci 10158c2ecf20Sopenharmony_cistatic const struct printf_spec default_flag_spec = { 10168c2ecf20Sopenharmony_ci .base = 16, 10178c2ecf20Sopenharmony_ci .precision = -1, 10188c2ecf20Sopenharmony_ci .flags = SPECIAL | SMALL, 10198c2ecf20Sopenharmony_ci}; 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_cistatic const struct printf_spec default_dec_spec = { 10228c2ecf20Sopenharmony_ci .base = 10, 10238c2ecf20Sopenharmony_ci .precision = -1, 10248c2ecf20Sopenharmony_ci}; 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_cistatic const struct printf_spec default_dec02_spec = { 10278c2ecf20Sopenharmony_ci .base = 10, 10288c2ecf20Sopenharmony_ci .field_width = 2, 10298c2ecf20Sopenharmony_ci .precision = -1, 10308c2ecf20Sopenharmony_ci .flags = ZEROPAD, 10318c2ecf20Sopenharmony_ci}; 10328c2ecf20Sopenharmony_ci 10338c2ecf20Sopenharmony_cistatic const struct printf_spec default_dec04_spec = { 10348c2ecf20Sopenharmony_ci .base = 10, 10358c2ecf20Sopenharmony_ci .field_width = 4, 10368c2ecf20Sopenharmony_ci .precision = -1, 10378c2ecf20Sopenharmony_ci .flags = ZEROPAD, 10388c2ecf20Sopenharmony_ci}; 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_cistatic noinline_for_stack 10418c2ecf20Sopenharmony_cichar *resource_string(char *buf, char *end, struct resource *res, 10428c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 10438c2ecf20Sopenharmony_ci{ 10448c2ecf20Sopenharmony_ci#ifndef IO_RSRC_PRINTK_SIZE 10458c2ecf20Sopenharmony_ci#define IO_RSRC_PRINTK_SIZE 6 10468c2ecf20Sopenharmony_ci#endif 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci#ifndef MEM_RSRC_PRINTK_SIZE 10498c2ecf20Sopenharmony_ci#define MEM_RSRC_PRINTK_SIZE 10 10508c2ecf20Sopenharmony_ci#endif 10518c2ecf20Sopenharmony_ci static const struct printf_spec io_spec = { 10528c2ecf20Sopenharmony_ci .base = 16, 10538c2ecf20Sopenharmony_ci .field_width = IO_RSRC_PRINTK_SIZE, 10548c2ecf20Sopenharmony_ci .precision = -1, 10558c2ecf20Sopenharmony_ci .flags = SPECIAL | SMALL | ZEROPAD, 10568c2ecf20Sopenharmony_ci }; 10578c2ecf20Sopenharmony_ci static const struct printf_spec mem_spec = { 10588c2ecf20Sopenharmony_ci .base = 16, 10598c2ecf20Sopenharmony_ci .field_width = MEM_RSRC_PRINTK_SIZE, 10608c2ecf20Sopenharmony_ci .precision = -1, 10618c2ecf20Sopenharmony_ci .flags = SPECIAL | SMALL | ZEROPAD, 10628c2ecf20Sopenharmony_ci }; 10638c2ecf20Sopenharmony_ci static const struct printf_spec bus_spec = { 10648c2ecf20Sopenharmony_ci .base = 16, 10658c2ecf20Sopenharmony_ci .field_width = 2, 10668c2ecf20Sopenharmony_ci .precision = -1, 10678c2ecf20Sopenharmony_ci .flags = SMALL | ZEROPAD, 10688c2ecf20Sopenharmony_ci }; 10698c2ecf20Sopenharmony_ci static const struct printf_spec str_spec = { 10708c2ecf20Sopenharmony_ci .field_width = -1, 10718c2ecf20Sopenharmony_ci .precision = 10, 10728c2ecf20Sopenharmony_ci .flags = LEFT, 10738c2ecf20Sopenharmony_ci }; 10748c2ecf20Sopenharmony_ci 10758c2ecf20Sopenharmony_ci /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 10768c2ecf20Sopenharmony_ci * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 10778c2ecf20Sopenharmony_ci#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 10788c2ecf20Sopenharmony_ci#define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 10798c2ecf20Sopenharmony_ci#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 10808c2ecf20Sopenharmony_ci#define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 10818c2ecf20Sopenharmony_ci char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 10828c2ecf20Sopenharmony_ci 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_ci char *p = sym, *pend = sym + sizeof(sym); 10858c2ecf20Sopenharmony_ci int decode = (fmt[0] == 'R') ? 1 : 0; 10868c2ecf20Sopenharmony_ci const struct printf_spec *specp; 10878c2ecf20Sopenharmony_ci 10888c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, res, spec)) 10898c2ecf20Sopenharmony_ci return buf; 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci *p++ = '['; 10928c2ecf20Sopenharmony_ci if (res->flags & IORESOURCE_IO) { 10938c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, "io ", str_spec); 10948c2ecf20Sopenharmony_ci specp = &io_spec; 10958c2ecf20Sopenharmony_ci } else if (res->flags & IORESOURCE_MEM) { 10968c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, "mem ", str_spec); 10978c2ecf20Sopenharmony_ci specp = &mem_spec; 10988c2ecf20Sopenharmony_ci } else if (res->flags & IORESOURCE_IRQ) { 10998c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, "irq ", str_spec); 11008c2ecf20Sopenharmony_ci specp = &default_dec_spec; 11018c2ecf20Sopenharmony_ci } else if (res->flags & IORESOURCE_DMA) { 11028c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, "dma ", str_spec); 11038c2ecf20Sopenharmony_ci specp = &default_dec_spec; 11048c2ecf20Sopenharmony_ci } else if (res->flags & IORESOURCE_BUS) { 11058c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, "bus ", str_spec); 11068c2ecf20Sopenharmony_ci specp = &bus_spec; 11078c2ecf20Sopenharmony_ci } else { 11088c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, "??? ", str_spec); 11098c2ecf20Sopenharmony_ci specp = &mem_spec; 11108c2ecf20Sopenharmony_ci decode = 0; 11118c2ecf20Sopenharmony_ci } 11128c2ecf20Sopenharmony_ci if (decode && res->flags & IORESOURCE_UNSET) { 11138c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, "size ", str_spec); 11148c2ecf20Sopenharmony_ci p = number(p, pend, resource_size(res), *specp); 11158c2ecf20Sopenharmony_ci } else { 11168c2ecf20Sopenharmony_ci p = number(p, pend, res->start, *specp); 11178c2ecf20Sopenharmony_ci if (res->start != res->end) { 11188c2ecf20Sopenharmony_ci *p++ = '-'; 11198c2ecf20Sopenharmony_ci p = number(p, pend, res->end, *specp); 11208c2ecf20Sopenharmony_ci } 11218c2ecf20Sopenharmony_ci } 11228c2ecf20Sopenharmony_ci if (decode) { 11238c2ecf20Sopenharmony_ci if (res->flags & IORESOURCE_MEM_64) 11248c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, " 64bit", str_spec); 11258c2ecf20Sopenharmony_ci if (res->flags & IORESOURCE_PREFETCH) 11268c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, " pref", str_spec); 11278c2ecf20Sopenharmony_ci if (res->flags & IORESOURCE_WINDOW) 11288c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, " window", str_spec); 11298c2ecf20Sopenharmony_ci if (res->flags & IORESOURCE_DISABLED) 11308c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, " disabled", str_spec); 11318c2ecf20Sopenharmony_ci } else { 11328c2ecf20Sopenharmony_ci p = string_nocheck(p, pend, " flags ", str_spec); 11338c2ecf20Sopenharmony_ci p = number(p, pend, res->flags, default_flag_spec); 11348c2ecf20Sopenharmony_ci } 11358c2ecf20Sopenharmony_ci *p++ = ']'; 11368c2ecf20Sopenharmony_ci *p = '\0'; 11378c2ecf20Sopenharmony_ci 11388c2ecf20Sopenharmony_ci return string_nocheck(buf, end, sym, spec); 11398c2ecf20Sopenharmony_ci} 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_cistatic noinline_for_stack 11428c2ecf20Sopenharmony_cichar *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 11438c2ecf20Sopenharmony_ci const char *fmt) 11448c2ecf20Sopenharmony_ci{ 11458c2ecf20Sopenharmony_ci int i, len = 1; /* if we pass '%ph[CDN]', field width remains 11468c2ecf20Sopenharmony_ci negative value, fallback to the default */ 11478c2ecf20Sopenharmony_ci char separator; 11488c2ecf20Sopenharmony_ci 11498c2ecf20Sopenharmony_ci if (spec.field_width == 0) 11508c2ecf20Sopenharmony_ci /* nothing to print */ 11518c2ecf20Sopenharmony_ci return buf; 11528c2ecf20Sopenharmony_ci 11538c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, addr, spec)) 11548c2ecf20Sopenharmony_ci return buf; 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci switch (fmt[1]) { 11578c2ecf20Sopenharmony_ci case 'C': 11588c2ecf20Sopenharmony_ci separator = ':'; 11598c2ecf20Sopenharmony_ci break; 11608c2ecf20Sopenharmony_ci case 'D': 11618c2ecf20Sopenharmony_ci separator = '-'; 11628c2ecf20Sopenharmony_ci break; 11638c2ecf20Sopenharmony_ci case 'N': 11648c2ecf20Sopenharmony_ci separator = 0; 11658c2ecf20Sopenharmony_ci break; 11668c2ecf20Sopenharmony_ci default: 11678c2ecf20Sopenharmony_ci separator = ' '; 11688c2ecf20Sopenharmony_ci break; 11698c2ecf20Sopenharmony_ci } 11708c2ecf20Sopenharmony_ci 11718c2ecf20Sopenharmony_ci if (spec.field_width > 0) 11728c2ecf20Sopenharmony_ci len = min_t(int, spec.field_width, 64); 11738c2ecf20Sopenharmony_ci 11748c2ecf20Sopenharmony_ci for (i = 0; i < len; ++i) { 11758c2ecf20Sopenharmony_ci if (buf < end) 11768c2ecf20Sopenharmony_ci *buf = hex_asc_hi(addr[i]); 11778c2ecf20Sopenharmony_ci ++buf; 11788c2ecf20Sopenharmony_ci if (buf < end) 11798c2ecf20Sopenharmony_ci *buf = hex_asc_lo(addr[i]); 11808c2ecf20Sopenharmony_ci ++buf; 11818c2ecf20Sopenharmony_ci 11828c2ecf20Sopenharmony_ci if (separator && i != len - 1) { 11838c2ecf20Sopenharmony_ci if (buf < end) 11848c2ecf20Sopenharmony_ci *buf = separator; 11858c2ecf20Sopenharmony_ci ++buf; 11868c2ecf20Sopenharmony_ci } 11878c2ecf20Sopenharmony_ci } 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci return buf; 11908c2ecf20Sopenharmony_ci} 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_cistatic noinline_for_stack 11938c2ecf20Sopenharmony_cichar *bitmap_string(char *buf, char *end, unsigned long *bitmap, 11948c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 11958c2ecf20Sopenharmony_ci{ 11968c2ecf20Sopenharmony_ci const int CHUNKSZ = 32; 11978c2ecf20Sopenharmony_ci int nr_bits = max_t(int, spec.field_width, 0); 11988c2ecf20Sopenharmony_ci int i, chunksz; 11998c2ecf20Sopenharmony_ci bool first = true; 12008c2ecf20Sopenharmony_ci 12018c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, bitmap, spec)) 12028c2ecf20Sopenharmony_ci return buf; 12038c2ecf20Sopenharmony_ci 12048c2ecf20Sopenharmony_ci /* reused to print numbers */ 12058c2ecf20Sopenharmony_ci spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 12068c2ecf20Sopenharmony_ci 12078c2ecf20Sopenharmony_ci chunksz = nr_bits & (CHUNKSZ - 1); 12088c2ecf20Sopenharmony_ci if (chunksz == 0) 12098c2ecf20Sopenharmony_ci chunksz = CHUNKSZ; 12108c2ecf20Sopenharmony_ci 12118c2ecf20Sopenharmony_ci i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 12128c2ecf20Sopenharmony_ci for (; i >= 0; i -= CHUNKSZ) { 12138c2ecf20Sopenharmony_ci u32 chunkmask, val; 12148c2ecf20Sopenharmony_ci int word, bit; 12158c2ecf20Sopenharmony_ci 12168c2ecf20Sopenharmony_ci chunkmask = ((1ULL << chunksz) - 1); 12178c2ecf20Sopenharmony_ci word = i / BITS_PER_LONG; 12188c2ecf20Sopenharmony_ci bit = i % BITS_PER_LONG; 12198c2ecf20Sopenharmony_ci val = (bitmap[word] >> bit) & chunkmask; 12208c2ecf20Sopenharmony_ci 12218c2ecf20Sopenharmony_ci if (!first) { 12228c2ecf20Sopenharmony_ci if (buf < end) 12238c2ecf20Sopenharmony_ci *buf = ','; 12248c2ecf20Sopenharmony_ci buf++; 12258c2ecf20Sopenharmony_ci } 12268c2ecf20Sopenharmony_ci first = false; 12278c2ecf20Sopenharmony_ci 12288c2ecf20Sopenharmony_ci spec.field_width = DIV_ROUND_UP(chunksz, 4); 12298c2ecf20Sopenharmony_ci buf = number(buf, end, val, spec); 12308c2ecf20Sopenharmony_ci 12318c2ecf20Sopenharmony_ci chunksz = CHUNKSZ; 12328c2ecf20Sopenharmony_ci } 12338c2ecf20Sopenharmony_ci return buf; 12348c2ecf20Sopenharmony_ci} 12358c2ecf20Sopenharmony_ci 12368c2ecf20Sopenharmony_cistatic noinline_for_stack 12378c2ecf20Sopenharmony_cichar *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 12388c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 12398c2ecf20Sopenharmony_ci{ 12408c2ecf20Sopenharmony_ci int nr_bits = max_t(int, spec.field_width, 0); 12418c2ecf20Sopenharmony_ci /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 12428c2ecf20Sopenharmony_ci int cur, rbot, rtop; 12438c2ecf20Sopenharmony_ci bool first = true; 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, bitmap, spec)) 12468c2ecf20Sopenharmony_ci return buf; 12478c2ecf20Sopenharmony_ci 12488c2ecf20Sopenharmony_ci rbot = cur = find_first_bit(bitmap, nr_bits); 12498c2ecf20Sopenharmony_ci while (cur < nr_bits) { 12508c2ecf20Sopenharmony_ci rtop = cur; 12518c2ecf20Sopenharmony_ci cur = find_next_bit(bitmap, nr_bits, cur + 1); 12528c2ecf20Sopenharmony_ci if (cur < nr_bits && cur <= rtop + 1) 12538c2ecf20Sopenharmony_ci continue; 12548c2ecf20Sopenharmony_ci 12558c2ecf20Sopenharmony_ci if (!first) { 12568c2ecf20Sopenharmony_ci if (buf < end) 12578c2ecf20Sopenharmony_ci *buf = ','; 12588c2ecf20Sopenharmony_ci buf++; 12598c2ecf20Sopenharmony_ci } 12608c2ecf20Sopenharmony_ci first = false; 12618c2ecf20Sopenharmony_ci 12628c2ecf20Sopenharmony_ci buf = number(buf, end, rbot, default_dec_spec); 12638c2ecf20Sopenharmony_ci if (rbot < rtop) { 12648c2ecf20Sopenharmony_ci if (buf < end) 12658c2ecf20Sopenharmony_ci *buf = '-'; 12668c2ecf20Sopenharmony_ci buf++; 12678c2ecf20Sopenharmony_ci 12688c2ecf20Sopenharmony_ci buf = number(buf, end, rtop, default_dec_spec); 12698c2ecf20Sopenharmony_ci } 12708c2ecf20Sopenharmony_ci 12718c2ecf20Sopenharmony_ci rbot = cur; 12728c2ecf20Sopenharmony_ci } 12738c2ecf20Sopenharmony_ci return buf; 12748c2ecf20Sopenharmony_ci} 12758c2ecf20Sopenharmony_ci 12768c2ecf20Sopenharmony_cistatic noinline_for_stack 12778c2ecf20Sopenharmony_cichar *mac_address_string(char *buf, char *end, u8 *addr, 12788c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 12798c2ecf20Sopenharmony_ci{ 12808c2ecf20Sopenharmony_ci char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 12818c2ecf20Sopenharmony_ci char *p = mac_addr; 12828c2ecf20Sopenharmony_ci int i; 12838c2ecf20Sopenharmony_ci char separator; 12848c2ecf20Sopenharmony_ci bool reversed = false; 12858c2ecf20Sopenharmony_ci 12868c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, addr, spec)) 12878c2ecf20Sopenharmony_ci return buf; 12888c2ecf20Sopenharmony_ci 12898c2ecf20Sopenharmony_ci switch (fmt[1]) { 12908c2ecf20Sopenharmony_ci case 'F': 12918c2ecf20Sopenharmony_ci separator = '-'; 12928c2ecf20Sopenharmony_ci break; 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci case 'R': 12958c2ecf20Sopenharmony_ci reversed = true; 12968c2ecf20Sopenharmony_ci /* fall through */ 12978c2ecf20Sopenharmony_ci 12988c2ecf20Sopenharmony_ci default: 12998c2ecf20Sopenharmony_ci separator = ':'; 13008c2ecf20Sopenharmony_ci break; 13018c2ecf20Sopenharmony_ci } 13028c2ecf20Sopenharmony_ci 13038c2ecf20Sopenharmony_ci for (i = 0; i < 6; i++) { 13048c2ecf20Sopenharmony_ci if (reversed) 13058c2ecf20Sopenharmony_ci p = hex_byte_pack(p, addr[5 - i]); 13068c2ecf20Sopenharmony_ci else 13078c2ecf20Sopenharmony_ci p = hex_byte_pack(p, addr[i]); 13088c2ecf20Sopenharmony_ci 13098c2ecf20Sopenharmony_ci if (fmt[0] == 'M' && i != 5) 13108c2ecf20Sopenharmony_ci *p++ = separator; 13118c2ecf20Sopenharmony_ci } 13128c2ecf20Sopenharmony_ci *p = '\0'; 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_ci return string_nocheck(buf, end, mac_addr, spec); 13158c2ecf20Sopenharmony_ci} 13168c2ecf20Sopenharmony_ci 13178c2ecf20Sopenharmony_cistatic noinline_for_stack 13188c2ecf20Sopenharmony_cichar *ip4_string(char *p, const u8 *addr, const char *fmt) 13198c2ecf20Sopenharmony_ci{ 13208c2ecf20Sopenharmony_ci int i; 13218c2ecf20Sopenharmony_ci bool leading_zeros = (fmt[0] == 'i'); 13228c2ecf20Sopenharmony_ci int index; 13238c2ecf20Sopenharmony_ci int step; 13248c2ecf20Sopenharmony_ci 13258c2ecf20Sopenharmony_ci switch (fmt[2]) { 13268c2ecf20Sopenharmony_ci case 'h': 13278c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN 13288c2ecf20Sopenharmony_ci index = 0; 13298c2ecf20Sopenharmony_ci step = 1; 13308c2ecf20Sopenharmony_ci#else 13318c2ecf20Sopenharmony_ci index = 3; 13328c2ecf20Sopenharmony_ci step = -1; 13338c2ecf20Sopenharmony_ci#endif 13348c2ecf20Sopenharmony_ci break; 13358c2ecf20Sopenharmony_ci case 'l': 13368c2ecf20Sopenharmony_ci index = 3; 13378c2ecf20Sopenharmony_ci step = -1; 13388c2ecf20Sopenharmony_ci break; 13398c2ecf20Sopenharmony_ci case 'n': 13408c2ecf20Sopenharmony_ci case 'b': 13418c2ecf20Sopenharmony_ci default: 13428c2ecf20Sopenharmony_ci index = 0; 13438c2ecf20Sopenharmony_ci step = 1; 13448c2ecf20Sopenharmony_ci break; 13458c2ecf20Sopenharmony_ci } 13468c2ecf20Sopenharmony_ci for (i = 0; i < 4; i++) { 13478c2ecf20Sopenharmony_ci char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 13488c2ecf20Sopenharmony_ci int digits = put_dec_trunc8(temp, addr[index]) - temp; 13498c2ecf20Sopenharmony_ci if (leading_zeros) { 13508c2ecf20Sopenharmony_ci if (digits < 3) 13518c2ecf20Sopenharmony_ci *p++ = '0'; 13528c2ecf20Sopenharmony_ci if (digits < 2) 13538c2ecf20Sopenharmony_ci *p++ = '0'; 13548c2ecf20Sopenharmony_ci } 13558c2ecf20Sopenharmony_ci /* reverse the digits in the quad */ 13568c2ecf20Sopenharmony_ci while (digits--) 13578c2ecf20Sopenharmony_ci *p++ = temp[digits]; 13588c2ecf20Sopenharmony_ci if (i < 3) 13598c2ecf20Sopenharmony_ci *p++ = '.'; 13608c2ecf20Sopenharmony_ci index += step; 13618c2ecf20Sopenharmony_ci } 13628c2ecf20Sopenharmony_ci *p = '\0'; 13638c2ecf20Sopenharmony_ci 13648c2ecf20Sopenharmony_ci return p; 13658c2ecf20Sopenharmony_ci} 13668c2ecf20Sopenharmony_ci 13678c2ecf20Sopenharmony_cistatic noinline_for_stack 13688c2ecf20Sopenharmony_cichar *ip6_compressed_string(char *p, const char *addr) 13698c2ecf20Sopenharmony_ci{ 13708c2ecf20Sopenharmony_ci int i, j, range; 13718c2ecf20Sopenharmony_ci unsigned char zerolength[8]; 13728c2ecf20Sopenharmony_ci int longest = 1; 13738c2ecf20Sopenharmony_ci int colonpos = -1; 13748c2ecf20Sopenharmony_ci u16 word; 13758c2ecf20Sopenharmony_ci u8 hi, lo; 13768c2ecf20Sopenharmony_ci bool needcolon = false; 13778c2ecf20Sopenharmony_ci bool useIPv4; 13788c2ecf20Sopenharmony_ci struct in6_addr in6; 13798c2ecf20Sopenharmony_ci 13808c2ecf20Sopenharmony_ci memcpy(&in6, addr, sizeof(struct in6_addr)); 13818c2ecf20Sopenharmony_ci 13828c2ecf20Sopenharmony_ci useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 13838c2ecf20Sopenharmony_ci 13848c2ecf20Sopenharmony_ci memset(zerolength, 0, sizeof(zerolength)); 13858c2ecf20Sopenharmony_ci 13868c2ecf20Sopenharmony_ci if (useIPv4) 13878c2ecf20Sopenharmony_ci range = 6; 13888c2ecf20Sopenharmony_ci else 13898c2ecf20Sopenharmony_ci range = 8; 13908c2ecf20Sopenharmony_ci 13918c2ecf20Sopenharmony_ci /* find position of longest 0 run */ 13928c2ecf20Sopenharmony_ci for (i = 0; i < range; i++) { 13938c2ecf20Sopenharmony_ci for (j = i; j < range; j++) { 13948c2ecf20Sopenharmony_ci if (in6.s6_addr16[j] != 0) 13958c2ecf20Sopenharmony_ci break; 13968c2ecf20Sopenharmony_ci zerolength[i]++; 13978c2ecf20Sopenharmony_ci } 13988c2ecf20Sopenharmony_ci } 13998c2ecf20Sopenharmony_ci for (i = 0; i < range; i++) { 14008c2ecf20Sopenharmony_ci if (zerolength[i] > longest) { 14018c2ecf20Sopenharmony_ci longest = zerolength[i]; 14028c2ecf20Sopenharmony_ci colonpos = i; 14038c2ecf20Sopenharmony_ci } 14048c2ecf20Sopenharmony_ci } 14058c2ecf20Sopenharmony_ci if (longest == 1) /* don't compress a single 0 */ 14068c2ecf20Sopenharmony_ci colonpos = -1; 14078c2ecf20Sopenharmony_ci 14088c2ecf20Sopenharmony_ci /* emit address */ 14098c2ecf20Sopenharmony_ci for (i = 0; i < range; i++) { 14108c2ecf20Sopenharmony_ci if (i == colonpos) { 14118c2ecf20Sopenharmony_ci if (needcolon || i == 0) 14128c2ecf20Sopenharmony_ci *p++ = ':'; 14138c2ecf20Sopenharmony_ci *p++ = ':'; 14148c2ecf20Sopenharmony_ci needcolon = false; 14158c2ecf20Sopenharmony_ci i += longest - 1; 14168c2ecf20Sopenharmony_ci continue; 14178c2ecf20Sopenharmony_ci } 14188c2ecf20Sopenharmony_ci if (needcolon) { 14198c2ecf20Sopenharmony_ci *p++ = ':'; 14208c2ecf20Sopenharmony_ci needcolon = false; 14218c2ecf20Sopenharmony_ci } 14228c2ecf20Sopenharmony_ci /* hex u16 without leading 0s */ 14238c2ecf20Sopenharmony_ci word = ntohs(in6.s6_addr16[i]); 14248c2ecf20Sopenharmony_ci hi = word >> 8; 14258c2ecf20Sopenharmony_ci lo = word & 0xff; 14268c2ecf20Sopenharmony_ci if (hi) { 14278c2ecf20Sopenharmony_ci if (hi > 0x0f) 14288c2ecf20Sopenharmony_ci p = hex_byte_pack(p, hi); 14298c2ecf20Sopenharmony_ci else 14308c2ecf20Sopenharmony_ci *p++ = hex_asc_lo(hi); 14318c2ecf20Sopenharmony_ci p = hex_byte_pack(p, lo); 14328c2ecf20Sopenharmony_ci } 14338c2ecf20Sopenharmony_ci else if (lo > 0x0f) 14348c2ecf20Sopenharmony_ci p = hex_byte_pack(p, lo); 14358c2ecf20Sopenharmony_ci else 14368c2ecf20Sopenharmony_ci *p++ = hex_asc_lo(lo); 14378c2ecf20Sopenharmony_ci needcolon = true; 14388c2ecf20Sopenharmony_ci } 14398c2ecf20Sopenharmony_ci 14408c2ecf20Sopenharmony_ci if (useIPv4) { 14418c2ecf20Sopenharmony_ci if (needcolon) 14428c2ecf20Sopenharmony_ci *p++ = ':'; 14438c2ecf20Sopenharmony_ci p = ip4_string(p, &in6.s6_addr[12], "I4"); 14448c2ecf20Sopenharmony_ci } 14458c2ecf20Sopenharmony_ci *p = '\0'; 14468c2ecf20Sopenharmony_ci 14478c2ecf20Sopenharmony_ci return p; 14488c2ecf20Sopenharmony_ci} 14498c2ecf20Sopenharmony_ci 14508c2ecf20Sopenharmony_cistatic noinline_for_stack 14518c2ecf20Sopenharmony_cichar *ip6_string(char *p, const char *addr, const char *fmt) 14528c2ecf20Sopenharmony_ci{ 14538c2ecf20Sopenharmony_ci int i; 14548c2ecf20Sopenharmony_ci 14558c2ecf20Sopenharmony_ci for (i = 0; i < 8; i++) { 14568c2ecf20Sopenharmony_ci p = hex_byte_pack(p, *addr++); 14578c2ecf20Sopenharmony_ci p = hex_byte_pack(p, *addr++); 14588c2ecf20Sopenharmony_ci if (fmt[0] == 'I' && i != 7) 14598c2ecf20Sopenharmony_ci *p++ = ':'; 14608c2ecf20Sopenharmony_ci } 14618c2ecf20Sopenharmony_ci *p = '\0'; 14628c2ecf20Sopenharmony_ci 14638c2ecf20Sopenharmony_ci return p; 14648c2ecf20Sopenharmony_ci} 14658c2ecf20Sopenharmony_ci 14668c2ecf20Sopenharmony_cistatic noinline_for_stack 14678c2ecf20Sopenharmony_cichar *ip6_addr_string(char *buf, char *end, const u8 *addr, 14688c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 14698c2ecf20Sopenharmony_ci{ 14708c2ecf20Sopenharmony_ci char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 14718c2ecf20Sopenharmony_ci 14728c2ecf20Sopenharmony_ci if (fmt[0] == 'I' && fmt[2] == 'c') 14738c2ecf20Sopenharmony_ci ip6_compressed_string(ip6_addr, addr); 14748c2ecf20Sopenharmony_ci else 14758c2ecf20Sopenharmony_ci ip6_string(ip6_addr, addr, fmt); 14768c2ecf20Sopenharmony_ci 14778c2ecf20Sopenharmony_ci return string_nocheck(buf, end, ip6_addr, spec); 14788c2ecf20Sopenharmony_ci} 14798c2ecf20Sopenharmony_ci 14808c2ecf20Sopenharmony_cistatic noinline_for_stack 14818c2ecf20Sopenharmony_cichar *ip4_addr_string(char *buf, char *end, const u8 *addr, 14828c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 14838c2ecf20Sopenharmony_ci{ 14848c2ecf20Sopenharmony_ci char ip4_addr[sizeof("255.255.255.255")]; 14858c2ecf20Sopenharmony_ci 14868c2ecf20Sopenharmony_ci ip4_string(ip4_addr, addr, fmt); 14878c2ecf20Sopenharmony_ci 14888c2ecf20Sopenharmony_ci return string_nocheck(buf, end, ip4_addr, spec); 14898c2ecf20Sopenharmony_ci} 14908c2ecf20Sopenharmony_ci 14918c2ecf20Sopenharmony_cistatic noinline_for_stack 14928c2ecf20Sopenharmony_cichar *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 14938c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 14948c2ecf20Sopenharmony_ci{ 14958c2ecf20Sopenharmony_ci bool have_p = false, have_s = false, have_f = false, have_c = false; 14968c2ecf20Sopenharmony_ci char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 14978c2ecf20Sopenharmony_ci sizeof(":12345") + sizeof("/123456789") + 14988c2ecf20Sopenharmony_ci sizeof("%1234567890")]; 14998c2ecf20Sopenharmony_ci char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 15008c2ecf20Sopenharmony_ci const u8 *addr = (const u8 *) &sa->sin6_addr; 15018c2ecf20Sopenharmony_ci char fmt6[2] = { fmt[0], '6' }; 15028c2ecf20Sopenharmony_ci u8 off = 0; 15038c2ecf20Sopenharmony_ci 15048c2ecf20Sopenharmony_ci fmt++; 15058c2ecf20Sopenharmony_ci while (isalpha(*++fmt)) { 15068c2ecf20Sopenharmony_ci switch (*fmt) { 15078c2ecf20Sopenharmony_ci case 'p': 15088c2ecf20Sopenharmony_ci have_p = true; 15098c2ecf20Sopenharmony_ci break; 15108c2ecf20Sopenharmony_ci case 'f': 15118c2ecf20Sopenharmony_ci have_f = true; 15128c2ecf20Sopenharmony_ci break; 15138c2ecf20Sopenharmony_ci case 's': 15148c2ecf20Sopenharmony_ci have_s = true; 15158c2ecf20Sopenharmony_ci break; 15168c2ecf20Sopenharmony_ci case 'c': 15178c2ecf20Sopenharmony_ci have_c = true; 15188c2ecf20Sopenharmony_ci break; 15198c2ecf20Sopenharmony_ci } 15208c2ecf20Sopenharmony_ci } 15218c2ecf20Sopenharmony_ci 15228c2ecf20Sopenharmony_ci if (have_p || have_s || have_f) { 15238c2ecf20Sopenharmony_ci *p = '['; 15248c2ecf20Sopenharmony_ci off = 1; 15258c2ecf20Sopenharmony_ci } 15268c2ecf20Sopenharmony_ci 15278c2ecf20Sopenharmony_ci if (fmt6[0] == 'I' && have_c) 15288c2ecf20Sopenharmony_ci p = ip6_compressed_string(ip6_addr + off, addr); 15298c2ecf20Sopenharmony_ci else 15308c2ecf20Sopenharmony_ci p = ip6_string(ip6_addr + off, addr, fmt6); 15318c2ecf20Sopenharmony_ci 15328c2ecf20Sopenharmony_ci if (have_p || have_s || have_f) 15338c2ecf20Sopenharmony_ci *p++ = ']'; 15348c2ecf20Sopenharmony_ci 15358c2ecf20Sopenharmony_ci if (have_p) { 15368c2ecf20Sopenharmony_ci *p++ = ':'; 15378c2ecf20Sopenharmony_ci p = number(p, pend, ntohs(sa->sin6_port), spec); 15388c2ecf20Sopenharmony_ci } 15398c2ecf20Sopenharmony_ci if (have_f) { 15408c2ecf20Sopenharmony_ci *p++ = '/'; 15418c2ecf20Sopenharmony_ci p = number(p, pend, ntohl(sa->sin6_flowinfo & 15428c2ecf20Sopenharmony_ci IPV6_FLOWINFO_MASK), spec); 15438c2ecf20Sopenharmony_ci } 15448c2ecf20Sopenharmony_ci if (have_s) { 15458c2ecf20Sopenharmony_ci *p++ = '%'; 15468c2ecf20Sopenharmony_ci p = number(p, pend, sa->sin6_scope_id, spec); 15478c2ecf20Sopenharmony_ci } 15488c2ecf20Sopenharmony_ci *p = '\0'; 15498c2ecf20Sopenharmony_ci 15508c2ecf20Sopenharmony_ci return string_nocheck(buf, end, ip6_addr, spec); 15518c2ecf20Sopenharmony_ci} 15528c2ecf20Sopenharmony_ci 15538c2ecf20Sopenharmony_cistatic noinline_for_stack 15548c2ecf20Sopenharmony_cichar *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 15558c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 15568c2ecf20Sopenharmony_ci{ 15578c2ecf20Sopenharmony_ci bool have_p = false; 15588c2ecf20Sopenharmony_ci char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 15598c2ecf20Sopenharmony_ci char *pend = ip4_addr + sizeof(ip4_addr); 15608c2ecf20Sopenharmony_ci const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 15618c2ecf20Sopenharmony_ci char fmt4[3] = { fmt[0], '4', 0 }; 15628c2ecf20Sopenharmony_ci 15638c2ecf20Sopenharmony_ci fmt++; 15648c2ecf20Sopenharmony_ci while (isalpha(*++fmt)) { 15658c2ecf20Sopenharmony_ci switch (*fmt) { 15668c2ecf20Sopenharmony_ci case 'p': 15678c2ecf20Sopenharmony_ci have_p = true; 15688c2ecf20Sopenharmony_ci break; 15698c2ecf20Sopenharmony_ci case 'h': 15708c2ecf20Sopenharmony_ci case 'l': 15718c2ecf20Sopenharmony_ci case 'n': 15728c2ecf20Sopenharmony_ci case 'b': 15738c2ecf20Sopenharmony_ci fmt4[2] = *fmt; 15748c2ecf20Sopenharmony_ci break; 15758c2ecf20Sopenharmony_ci } 15768c2ecf20Sopenharmony_ci } 15778c2ecf20Sopenharmony_ci 15788c2ecf20Sopenharmony_ci p = ip4_string(ip4_addr, addr, fmt4); 15798c2ecf20Sopenharmony_ci if (have_p) { 15808c2ecf20Sopenharmony_ci *p++ = ':'; 15818c2ecf20Sopenharmony_ci p = number(p, pend, ntohs(sa->sin_port), spec); 15828c2ecf20Sopenharmony_ci } 15838c2ecf20Sopenharmony_ci *p = '\0'; 15848c2ecf20Sopenharmony_ci 15858c2ecf20Sopenharmony_ci return string_nocheck(buf, end, ip4_addr, spec); 15868c2ecf20Sopenharmony_ci} 15878c2ecf20Sopenharmony_ci 15888c2ecf20Sopenharmony_cistatic noinline_for_stack 15898c2ecf20Sopenharmony_cichar *ip_addr_string(char *buf, char *end, const void *ptr, 15908c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 15918c2ecf20Sopenharmony_ci{ 15928c2ecf20Sopenharmony_ci char *err_fmt_msg; 15938c2ecf20Sopenharmony_ci 15948c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, ptr, spec)) 15958c2ecf20Sopenharmony_ci return buf; 15968c2ecf20Sopenharmony_ci 15978c2ecf20Sopenharmony_ci switch (fmt[1]) { 15988c2ecf20Sopenharmony_ci case '6': 15998c2ecf20Sopenharmony_ci return ip6_addr_string(buf, end, ptr, spec, fmt); 16008c2ecf20Sopenharmony_ci case '4': 16018c2ecf20Sopenharmony_ci return ip4_addr_string(buf, end, ptr, spec, fmt); 16028c2ecf20Sopenharmony_ci case 'S': { 16038c2ecf20Sopenharmony_ci const union { 16048c2ecf20Sopenharmony_ci struct sockaddr raw; 16058c2ecf20Sopenharmony_ci struct sockaddr_in v4; 16068c2ecf20Sopenharmony_ci struct sockaddr_in6 v6; 16078c2ecf20Sopenharmony_ci } *sa = ptr; 16088c2ecf20Sopenharmony_ci 16098c2ecf20Sopenharmony_ci switch (sa->raw.sa_family) { 16108c2ecf20Sopenharmony_ci case AF_INET: 16118c2ecf20Sopenharmony_ci return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 16128c2ecf20Sopenharmony_ci case AF_INET6: 16138c2ecf20Sopenharmony_ci return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 16148c2ecf20Sopenharmony_ci default: 16158c2ecf20Sopenharmony_ci return error_string(buf, end, "(einval)", spec); 16168c2ecf20Sopenharmony_ci }} 16178c2ecf20Sopenharmony_ci } 16188c2ecf20Sopenharmony_ci 16198c2ecf20Sopenharmony_ci err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; 16208c2ecf20Sopenharmony_ci return error_string(buf, end, err_fmt_msg, spec); 16218c2ecf20Sopenharmony_ci} 16228c2ecf20Sopenharmony_ci 16238c2ecf20Sopenharmony_cistatic noinline_for_stack 16248c2ecf20Sopenharmony_cichar *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 16258c2ecf20Sopenharmony_ci const char *fmt) 16268c2ecf20Sopenharmony_ci{ 16278c2ecf20Sopenharmony_ci bool found = true; 16288c2ecf20Sopenharmony_ci int count = 1; 16298c2ecf20Sopenharmony_ci unsigned int flags = 0; 16308c2ecf20Sopenharmony_ci int len; 16318c2ecf20Sopenharmony_ci 16328c2ecf20Sopenharmony_ci if (spec.field_width == 0) 16338c2ecf20Sopenharmony_ci return buf; /* nothing to print */ 16348c2ecf20Sopenharmony_ci 16358c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, addr, spec)) 16368c2ecf20Sopenharmony_ci return buf; 16378c2ecf20Sopenharmony_ci 16388c2ecf20Sopenharmony_ci do { 16398c2ecf20Sopenharmony_ci switch (fmt[count++]) { 16408c2ecf20Sopenharmony_ci case 'a': 16418c2ecf20Sopenharmony_ci flags |= ESCAPE_ANY; 16428c2ecf20Sopenharmony_ci break; 16438c2ecf20Sopenharmony_ci case 'c': 16448c2ecf20Sopenharmony_ci flags |= ESCAPE_SPECIAL; 16458c2ecf20Sopenharmony_ci break; 16468c2ecf20Sopenharmony_ci case 'h': 16478c2ecf20Sopenharmony_ci flags |= ESCAPE_HEX; 16488c2ecf20Sopenharmony_ci break; 16498c2ecf20Sopenharmony_ci case 'n': 16508c2ecf20Sopenharmony_ci flags |= ESCAPE_NULL; 16518c2ecf20Sopenharmony_ci break; 16528c2ecf20Sopenharmony_ci case 'o': 16538c2ecf20Sopenharmony_ci flags |= ESCAPE_OCTAL; 16548c2ecf20Sopenharmony_ci break; 16558c2ecf20Sopenharmony_ci case 'p': 16568c2ecf20Sopenharmony_ci flags |= ESCAPE_NP; 16578c2ecf20Sopenharmony_ci break; 16588c2ecf20Sopenharmony_ci case 's': 16598c2ecf20Sopenharmony_ci flags |= ESCAPE_SPACE; 16608c2ecf20Sopenharmony_ci break; 16618c2ecf20Sopenharmony_ci default: 16628c2ecf20Sopenharmony_ci found = false; 16638c2ecf20Sopenharmony_ci break; 16648c2ecf20Sopenharmony_ci } 16658c2ecf20Sopenharmony_ci } while (found); 16668c2ecf20Sopenharmony_ci 16678c2ecf20Sopenharmony_ci if (!flags) 16688c2ecf20Sopenharmony_ci flags = ESCAPE_ANY_NP; 16698c2ecf20Sopenharmony_ci 16708c2ecf20Sopenharmony_ci len = spec.field_width < 0 ? 1 : spec.field_width; 16718c2ecf20Sopenharmony_ci 16728c2ecf20Sopenharmony_ci /* 16738c2ecf20Sopenharmony_ci * string_escape_mem() writes as many characters as it can to 16748c2ecf20Sopenharmony_ci * the given buffer, and returns the total size of the output 16758c2ecf20Sopenharmony_ci * had the buffer been big enough. 16768c2ecf20Sopenharmony_ci */ 16778c2ecf20Sopenharmony_ci buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 16788c2ecf20Sopenharmony_ci 16798c2ecf20Sopenharmony_ci return buf; 16808c2ecf20Sopenharmony_ci} 16818c2ecf20Sopenharmony_ci 16828c2ecf20Sopenharmony_cistatic char *va_format(char *buf, char *end, struct va_format *va_fmt, 16838c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 16848c2ecf20Sopenharmony_ci{ 16858c2ecf20Sopenharmony_ci va_list va; 16868c2ecf20Sopenharmony_ci 16878c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, va_fmt, spec)) 16888c2ecf20Sopenharmony_ci return buf; 16898c2ecf20Sopenharmony_ci 16908c2ecf20Sopenharmony_ci va_copy(va, *va_fmt->va); 16918c2ecf20Sopenharmony_ci buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); 16928c2ecf20Sopenharmony_ci va_end(va); 16938c2ecf20Sopenharmony_ci 16948c2ecf20Sopenharmony_ci return buf; 16958c2ecf20Sopenharmony_ci} 16968c2ecf20Sopenharmony_ci 16978c2ecf20Sopenharmony_cistatic noinline_for_stack 16988c2ecf20Sopenharmony_cichar *uuid_string(char *buf, char *end, const u8 *addr, 16998c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 17008c2ecf20Sopenharmony_ci{ 17018c2ecf20Sopenharmony_ci char uuid[UUID_STRING_LEN + 1]; 17028c2ecf20Sopenharmony_ci char *p = uuid; 17038c2ecf20Sopenharmony_ci int i; 17048c2ecf20Sopenharmony_ci const u8 *index = uuid_index; 17058c2ecf20Sopenharmony_ci bool uc = false; 17068c2ecf20Sopenharmony_ci 17078c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, addr, spec)) 17088c2ecf20Sopenharmony_ci return buf; 17098c2ecf20Sopenharmony_ci 17108c2ecf20Sopenharmony_ci switch (*(++fmt)) { 17118c2ecf20Sopenharmony_ci case 'L': 17128c2ecf20Sopenharmony_ci uc = true; 17138c2ecf20Sopenharmony_ci /* fall through */ 17148c2ecf20Sopenharmony_ci case 'l': 17158c2ecf20Sopenharmony_ci index = guid_index; 17168c2ecf20Sopenharmony_ci break; 17178c2ecf20Sopenharmony_ci case 'B': 17188c2ecf20Sopenharmony_ci uc = true; 17198c2ecf20Sopenharmony_ci break; 17208c2ecf20Sopenharmony_ci } 17218c2ecf20Sopenharmony_ci 17228c2ecf20Sopenharmony_ci for (i = 0; i < 16; i++) { 17238c2ecf20Sopenharmony_ci if (uc) 17248c2ecf20Sopenharmony_ci p = hex_byte_pack_upper(p, addr[index[i]]); 17258c2ecf20Sopenharmony_ci else 17268c2ecf20Sopenharmony_ci p = hex_byte_pack(p, addr[index[i]]); 17278c2ecf20Sopenharmony_ci switch (i) { 17288c2ecf20Sopenharmony_ci case 3: 17298c2ecf20Sopenharmony_ci case 5: 17308c2ecf20Sopenharmony_ci case 7: 17318c2ecf20Sopenharmony_ci case 9: 17328c2ecf20Sopenharmony_ci *p++ = '-'; 17338c2ecf20Sopenharmony_ci break; 17348c2ecf20Sopenharmony_ci } 17358c2ecf20Sopenharmony_ci } 17368c2ecf20Sopenharmony_ci 17378c2ecf20Sopenharmony_ci *p = 0; 17388c2ecf20Sopenharmony_ci 17398c2ecf20Sopenharmony_ci return string_nocheck(buf, end, uuid, spec); 17408c2ecf20Sopenharmony_ci} 17418c2ecf20Sopenharmony_ci 17428c2ecf20Sopenharmony_cistatic noinline_for_stack 17438c2ecf20Sopenharmony_cichar *netdev_bits(char *buf, char *end, const void *addr, 17448c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 17458c2ecf20Sopenharmony_ci{ 17468c2ecf20Sopenharmony_ci unsigned long long num; 17478c2ecf20Sopenharmony_ci int size; 17488c2ecf20Sopenharmony_ci 17498c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, addr, spec)) 17508c2ecf20Sopenharmony_ci return buf; 17518c2ecf20Sopenharmony_ci 17528c2ecf20Sopenharmony_ci switch (fmt[1]) { 17538c2ecf20Sopenharmony_ci case 'F': 17548c2ecf20Sopenharmony_ci num = *(const netdev_features_t *)addr; 17558c2ecf20Sopenharmony_ci size = sizeof(netdev_features_t); 17568c2ecf20Sopenharmony_ci break; 17578c2ecf20Sopenharmony_ci default: 17588c2ecf20Sopenharmony_ci return error_string(buf, end, "(%pN?)", spec); 17598c2ecf20Sopenharmony_ci } 17608c2ecf20Sopenharmony_ci 17618c2ecf20Sopenharmony_ci return special_hex_number(buf, end, num, size); 17628c2ecf20Sopenharmony_ci} 17638c2ecf20Sopenharmony_ci 17648c2ecf20Sopenharmony_cistatic noinline_for_stack 17658c2ecf20Sopenharmony_cichar *address_val(char *buf, char *end, const void *addr, 17668c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 17678c2ecf20Sopenharmony_ci{ 17688c2ecf20Sopenharmony_ci unsigned long long num; 17698c2ecf20Sopenharmony_ci int size; 17708c2ecf20Sopenharmony_ci 17718c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, addr, spec)) 17728c2ecf20Sopenharmony_ci return buf; 17738c2ecf20Sopenharmony_ci 17748c2ecf20Sopenharmony_ci switch (fmt[1]) { 17758c2ecf20Sopenharmony_ci case 'd': 17768c2ecf20Sopenharmony_ci num = *(const dma_addr_t *)addr; 17778c2ecf20Sopenharmony_ci size = sizeof(dma_addr_t); 17788c2ecf20Sopenharmony_ci break; 17798c2ecf20Sopenharmony_ci case 'p': 17808c2ecf20Sopenharmony_ci default: 17818c2ecf20Sopenharmony_ci num = *(const phys_addr_t *)addr; 17828c2ecf20Sopenharmony_ci size = sizeof(phys_addr_t); 17838c2ecf20Sopenharmony_ci break; 17848c2ecf20Sopenharmony_ci } 17858c2ecf20Sopenharmony_ci 17868c2ecf20Sopenharmony_ci return special_hex_number(buf, end, num, size); 17878c2ecf20Sopenharmony_ci} 17888c2ecf20Sopenharmony_ci 17898c2ecf20Sopenharmony_cistatic noinline_for_stack 17908c2ecf20Sopenharmony_cichar *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17918c2ecf20Sopenharmony_ci{ 17928c2ecf20Sopenharmony_ci int year = tm->tm_year + (r ? 0 : 1900); 17938c2ecf20Sopenharmony_ci int mon = tm->tm_mon + (r ? 0 : 1); 17948c2ecf20Sopenharmony_ci 17958c2ecf20Sopenharmony_ci buf = number(buf, end, year, default_dec04_spec); 17968c2ecf20Sopenharmony_ci if (buf < end) 17978c2ecf20Sopenharmony_ci *buf = '-'; 17988c2ecf20Sopenharmony_ci buf++; 17998c2ecf20Sopenharmony_ci 18008c2ecf20Sopenharmony_ci buf = number(buf, end, mon, default_dec02_spec); 18018c2ecf20Sopenharmony_ci if (buf < end) 18028c2ecf20Sopenharmony_ci *buf = '-'; 18038c2ecf20Sopenharmony_ci buf++; 18048c2ecf20Sopenharmony_ci 18058c2ecf20Sopenharmony_ci return number(buf, end, tm->tm_mday, default_dec02_spec); 18068c2ecf20Sopenharmony_ci} 18078c2ecf20Sopenharmony_ci 18088c2ecf20Sopenharmony_cistatic noinline_for_stack 18098c2ecf20Sopenharmony_cichar *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 18108c2ecf20Sopenharmony_ci{ 18118c2ecf20Sopenharmony_ci buf = number(buf, end, tm->tm_hour, default_dec02_spec); 18128c2ecf20Sopenharmony_ci if (buf < end) 18138c2ecf20Sopenharmony_ci *buf = ':'; 18148c2ecf20Sopenharmony_ci buf++; 18158c2ecf20Sopenharmony_ci 18168c2ecf20Sopenharmony_ci buf = number(buf, end, tm->tm_min, default_dec02_spec); 18178c2ecf20Sopenharmony_ci if (buf < end) 18188c2ecf20Sopenharmony_ci *buf = ':'; 18198c2ecf20Sopenharmony_ci buf++; 18208c2ecf20Sopenharmony_ci 18218c2ecf20Sopenharmony_ci return number(buf, end, tm->tm_sec, default_dec02_spec); 18228c2ecf20Sopenharmony_ci} 18238c2ecf20Sopenharmony_ci 18248c2ecf20Sopenharmony_cistatic noinline_for_stack 18258c2ecf20Sopenharmony_cichar *rtc_str(char *buf, char *end, const struct rtc_time *tm, 18268c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 18278c2ecf20Sopenharmony_ci{ 18288c2ecf20Sopenharmony_ci bool have_t = true, have_d = true; 18298c2ecf20Sopenharmony_ci bool raw = false; 18308c2ecf20Sopenharmony_ci int count = 2; 18318c2ecf20Sopenharmony_ci 18328c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, tm, spec)) 18338c2ecf20Sopenharmony_ci return buf; 18348c2ecf20Sopenharmony_ci 18358c2ecf20Sopenharmony_ci switch (fmt[count]) { 18368c2ecf20Sopenharmony_ci case 'd': 18378c2ecf20Sopenharmony_ci have_t = false; 18388c2ecf20Sopenharmony_ci count++; 18398c2ecf20Sopenharmony_ci break; 18408c2ecf20Sopenharmony_ci case 't': 18418c2ecf20Sopenharmony_ci have_d = false; 18428c2ecf20Sopenharmony_ci count++; 18438c2ecf20Sopenharmony_ci break; 18448c2ecf20Sopenharmony_ci } 18458c2ecf20Sopenharmony_ci 18468c2ecf20Sopenharmony_ci raw = fmt[count] == 'r'; 18478c2ecf20Sopenharmony_ci 18488c2ecf20Sopenharmony_ci if (have_d) 18498c2ecf20Sopenharmony_ci buf = date_str(buf, end, tm, raw); 18508c2ecf20Sopenharmony_ci if (have_d && have_t) { 18518c2ecf20Sopenharmony_ci /* Respect ISO 8601 */ 18528c2ecf20Sopenharmony_ci if (buf < end) 18538c2ecf20Sopenharmony_ci *buf = 'T'; 18548c2ecf20Sopenharmony_ci buf++; 18558c2ecf20Sopenharmony_ci } 18568c2ecf20Sopenharmony_ci if (have_t) 18578c2ecf20Sopenharmony_ci buf = time_str(buf, end, tm, raw); 18588c2ecf20Sopenharmony_ci 18598c2ecf20Sopenharmony_ci return buf; 18608c2ecf20Sopenharmony_ci} 18618c2ecf20Sopenharmony_ci 18628c2ecf20Sopenharmony_cistatic noinline_for_stack 18638c2ecf20Sopenharmony_cichar *time64_str(char *buf, char *end, const time64_t time, 18648c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 18658c2ecf20Sopenharmony_ci{ 18668c2ecf20Sopenharmony_ci struct rtc_time rtc_time; 18678c2ecf20Sopenharmony_ci struct tm tm; 18688c2ecf20Sopenharmony_ci 18698c2ecf20Sopenharmony_ci time64_to_tm(time, 0, &tm); 18708c2ecf20Sopenharmony_ci 18718c2ecf20Sopenharmony_ci rtc_time.tm_sec = tm.tm_sec; 18728c2ecf20Sopenharmony_ci rtc_time.tm_min = tm.tm_min; 18738c2ecf20Sopenharmony_ci rtc_time.tm_hour = tm.tm_hour; 18748c2ecf20Sopenharmony_ci rtc_time.tm_mday = tm.tm_mday; 18758c2ecf20Sopenharmony_ci rtc_time.tm_mon = tm.tm_mon; 18768c2ecf20Sopenharmony_ci rtc_time.tm_year = tm.tm_year; 18778c2ecf20Sopenharmony_ci rtc_time.tm_wday = tm.tm_wday; 18788c2ecf20Sopenharmony_ci rtc_time.tm_yday = tm.tm_yday; 18798c2ecf20Sopenharmony_ci 18808c2ecf20Sopenharmony_ci rtc_time.tm_isdst = 0; 18818c2ecf20Sopenharmony_ci 18828c2ecf20Sopenharmony_ci return rtc_str(buf, end, &rtc_time, spec, fmt); 18838c2ecf20Sopenharmony_ci} 18848c2ecf20Sopenharmony_ci 18858c2ecf20Sopenharmony_cistatic noinline_for_stack 18868c2ecf20Sopenharmony_cichar *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 18878c2ecf20Sopenharmony_ci const char *fmt) 18888c2ecf20Sopenharmony_ci{ 18898c2ecf20Sopenharmony_ci switch (fmt[1]) { 18908c2ecf20Sopenharmony_ci case 'R': 18918c2ecf20Sopenharmony_ci return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 18928c2ecf20Sopenharmony_ci case 'T': 18938c2ecf20Sopenharmony_ci return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); 18948c2ecf20Sopenharmony_ci default: 18958c2ecf20Sopenharmony_ci return error_string(buf, end, "(%pt?)", spec); 18968c2ecf20Sopenharmony_ci } 18978c2ecf20Sopenharmony_ci} 18988c2ecf20Sopenharmony_ci 18998c2ecf20Sopenharmony_cistatic noinline_for_stack 19008c2ecf20Sopenharmony_cichar *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 19018c2ecf20Sopenharmony_ci const char *fmt) 19028c2ecf20Sopenharmony_ci{ 19038c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_HAVE_CLK)) 19048c2ecf20Sopenharmony_ci return error_string(buf, end, "(%pC?)", spec); 19058c2ecf20Sopenharmony_ci 19068c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, clk, spec)) 19078c2ecf20Sopenharmony_ci return buf; 19088c2ecf20Sopenharmony_ci 19098c2ecf20Sopenharmony_ci switch (fmt[1]) { 19108c2ecf20Sopenharmony_ci case 'n': 19118c2ecf20Sopenharmony_ci default: 19128c2ecf20Sopenharmony_ci#ifdef CONFIG_COMMON_CLK 19138c2ecf20Sopenharmony_ci return string(buf, end, __clk_get_name(clk), spec); 19148c2ecf20Sopenharmony_ci#else 19158c2ecf20Sopenharmony_ci return ptr_to_id(buf, end, clk, spec); 19168c2ecf20Sopenharmony_ci#endif 19178c2ecf20Sopenharmony_ci } 19188c2ecf20Sopenharmony_ci} 19198c2ecf20Sopenharmony_ci 19208c2ecf20Sopenharmony_cistatic 19218c2ecf20Sopenharmony_cichar *format_flags(char *buf, char *end, unsigned long flags, 19228c2ecf20Sopenharmony_ci const struct trace_print_flags *names) 19238c2ecf20Sopenharmony_ci{ 19248c2ecf20Sopenharmony_ci unsigned long mask; 19258c2ecf20Sopenharmony_ci 19268c2ecf20Sopenharmony_ci for ( ; flags && names->name; names++) { 19278c2ecf20Sopenharmony_ci mask = names->mask; 19288c2ecf20Sopenharmony_ci if ((flags & mask) != mask) 19298c2ecf20Sopenharmony_ci continue; 19308c2ecf20Sopenharmony_ci 19318c2ecf20Sopenharmony_ci buf = string(buf, end, names->name, default_str_spec); 19328c2ecf20Sopenharmony_ci 19338c2ecf20Sopenharmony_ci flags &= ~mask; 19348c2ecf20Sopenharmony_ci if (flags) { 19358c2ecf20Sopenharmony_ci if (buf < end) 19368c2ecf20Sopenharmony_ci *buf = '|'; 19378c2ecf20Sopenharmony_ci buf++; 19388c2ecf20Sopenharmony_ci } 19398c2ecf20Sopenharmony_ci } 19408c2ecf20Sopenharmony_ci 19418c2ecf20Sopenharmony_ci if (flags) 19428c2ecf20Sopenharmony_ci buf = number(buf, end, flags, default_flag_spec); 19438c2ecf20Sopenharmony_ci 19448c2ecf20Sopenharmony_ci return buf; 19458c2ecf20Sopenharmony_ci} 19468c2ecf20Sopenharmony_ci 19478c2ecf20Sopenharmony_cistatic noinline_for_stack 19488c2ecf20Sopenharmony_cichar *flags_string(char *buf, char *end, void *flags_ptr, 19498c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 19508c2ecf20Sopenharmony_ci{ 19518c2ecf20Sopenharmony_ci unsigned long flags; 19528c2ecf20Sopenharmony_ci const struct trace_print_flags *names; 19538c2ecf20Sopenharmony_ci 19548c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, flags_ptr, spec)) 19558c2ecf20Sopenharmony_ci return buf; 19568c2ecf20Sopenharmony_ci 19578c2ecf20Sopenharmony_ci switch (fmt[1]) { 19588c2ecf20Sopenharmony_ci case 'p': 19598c2ecf20Sopenharmony_ci flags = *(unsigned long *)flags_ptr; 19608c2ecf20Sopenharmony_ci /* Remove zone id */ 19618c2ecf20Sopenharmony_ci flags &= (1UL << NR_PAGEFLAGS) - 1; 19628c2ecf20Sopenharmony_ci names = pageflag_names; 19638c2ecf20Sopenharmony_ci break; 19648c2ecf20Sopenharmony_ci case 'v': 19658c2ecf20Sopenharmony_ci flags = *(unsigned long *)flags_ptr; 19668c2ecf20Sopenharmony_ci names = vmaflag_names; 19678c2ecf20Sopenharmony_ci break; 19688c2ecf20Sopenharmony_ci case 'g': 19698c2ecf20Sopenharmony_ci flags = (__force unsigned long)(*(gfp_t *)flags_ptr); 19708c2ecf20Sopenharmony_ci names = gfpflag_names; 19718c2ecf20Sopenharmony_ci break; 19728c2ecf20Sopenharmony_ci default: 19738c2ecf20Sopenharmony_ci return error_string(buf, end, "(%pG?)", spec); 19748c2ecf20Sopenharmony_ci } 19758c2ecf20Sopenharmony_ci 19768c2ecf20Sopenharmony_ci return format_flags(buf, end, flags, names); 19778c2ecf20Sopenharmony_ci} 19788c2ecf20Sopenharmony_ci 19798c2ecf20Sopenharmony_cistatic noinline_for_stack 19808c2ecf20Sopenharmony_cichar *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 19818c2ecf20Sopenharmony_ci char *end) 19828c2ecf20Sopenharmony_ci{ 19838c2ecf20Sopenharmony_ci int depth; 19848c2ecf20Sopenharmony_ci 19858c2ecf20Sopenharmony_ci /* Loop starting from the root node to the current node. */ 19868c2ecf20Sopenharmony_ci for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 19878c2ecf20Sopenharmony_ci /* 19888c2ecf20Sopenharmony_ci * Only get a reference for other nodes (i.e. parent nodes). 19898c2ecf20Sopenharmony_ci * fwnode refcount may be 0 here. 19908c2ecf20Sopenharmony_ci */ 19918c2ecf20Sopenharmony_ci struct fwnode_handle *__fwnode = depth ? 19928c2ecf20Sopenharmony_ci fwnode_get_nth_parent(fwnode, depth) : fwnode; 19938c2ecf20Sopenharmony_ci 19948c2ecf20Sopenharmony_ci buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 19958c2ecf20Sopenharmony_ci default_str_spec); 19968c2ecf20Sopenharmony_ci buf = string(buf, end, fwnode_get_name(__fwnode), 19978c2ecf20Sopenharmony_ci default_str_spec); 19988c2ecf20Sopenharmony_ci 19998c2ecf20Sopenharmony_ci if (depth) 20008c2ecf20Sopenharmony_ci fwnode_handle_put(__fwnode); 20018c2ecf20Sopenharmony_ci } 20028c2ecf20Sopenharmony_ci 20038c2ecf20Sopenharmony_ci return buf; 20048c2ecf20Sopenharmony_ci} 20058c2ecf20Sopenharmony_ci 20068c2ecf20Sopenharmony_cistatic noinline_for_stack 20078c2ecf20Sopenharmony_cichar *device_node_string(char *buf, char *end, struct device_node *dn, 20088c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 20098c2ecf20Sopenharmony_ci{ 20108c2ecf20Sopenharmony_ci char tbuf[sizeof("xxxx") + 1]; 20118c2ecf20Sopenharmony_ci const char *p; 20128c2ecf20Sopenharmony_ci int ret; 20138c2ecf20Sopenharmony_ci char *buf_start = buf; 20148c2ecf20Sopenharmony_ci struct property *prop; 20158c2ecf20Sopenharmony_ci bool has_mult, pass; 20168c2ecf20Sopenharmony_ci 20178c2ecf20Sopenharmony_ci struct printf_spec str_spec = spec; 20188c2ecf20Sopenharmony_ci str_spec.field_width = -1; 20198c2ecf20Sopenharmony_ci 20208c2ecf20Sopenharmony_ci if (fmt[0] != 'F') 20218c2ecf20Sopenharmony_ci return error_string(buf, end, "(%pO?)", spec); 20228c2ecf20Sopenharmony_ci 20238c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_OF)) 20248c2ecf20Sopenharmony_ci return error_string(buf, end, "(%pOF?)", spec); 20258c2ecf20Sopenharmony_ci 20268c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, dn, spec)) 20278c2ecf20Sopenharmony_ci return buf; 20288c2ecf20Sopenharmony_ci 20298c2ecf20Sopenharmony_ci /* simple case without anything any more format specifiers */ 20308c2ecf20Sopenharmony_ci fmt++; 20318c2ecf20Sopenharmony_ci if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 20328c2ecf20Sopenharmony_ci fmt = "f"; 20338c2ecf20Sopenharmony_ci 20348c2ecf20Sopenharmony_ci for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 20358c2ecf20Sopenharmony_ci int precision; 20368c2ecf20Sopenharmony_ci if (pass) { 20378c2ecf20Sopenharmony_ci if (buf < end) 20388c2ecf20Sopenharmony_ci *buf = ':'; 20398c2ecf20Sopenharmony_ci buf++; 20408c2ecf20Sopenharmony_ci } 20418c2ecf20Sopenharmony_ci 20428c2ecf20Sopenharmony_ci switch (*fmt) { 20438c2ecf20Sopenharmony_ci case 'f': /* full_name */ 20448c2ecf20Sopenharmony_ci buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 20458c2ecf20Sopenharmony_ci end); 20468c2ecf20Sopenharmony_ci break; 20478c2ecf20Sopenharmony_ci case 'n': /* name */ 20488c2ecf20Sopenharmony_ci p = fwnode_get_name(of_fwnode_handle(dn)); 20498c2ecf20Sopenharmony_ci precision = str_spec.precision; 20508c2ecf20Sopenharmony_ci str_spec.precision = strchrnul(p, '@') - p; 20518c2ecf20Sopenharmony_ci buf = string(buf, end, p, str_spec); 20528c2ecf20Sopenharmony_ci str_spec.precision = precision; 20538c2ecf20Sopenharmony_ci break; 20548c2ecf20Sopenharmony_ci case 'p': /* phandle */ 20558c2ecf20Sopenharmony_ci buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); 20568c2ecf20Sopenharmony_ci break; 20578c2ecf20Sopenharmony_ci case 'P': /* path-spec */ 20588c2ecf20Sopenharmony_ci p = fwnode_get_name(of_fwnode_handle(dn)); 20598c2ecf20Sopenharmony_ci if (!p[1]) 20608c2ecf20Sopenharmony_ci p = "/"; 20618c2ecf20Sopenharmony_ci buf = string(buf, end, p, str_spec); 20628c2ecf20Sopenharmony_ci break; 20638c2ecf20Sopenharmony_ci case 'F': /* flags */ 20648c2ecf20Sopenharmony_ci tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 20658c2ecf20Sopenharmony_ci tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 20668c2ecf20Sopenharmony_ci tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 20678c2ecf20Sopenharmony_ci tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 20688c2ecf20Sopenharmony_ci tbuf[4] = 0; 20698c2ecf20Sopenharmony_ci buf = string_nocheck(buf, end, tbuf, str_spec); 20708c2ecf20Sopenharmony_ci break; 20718c2ecf20Sopenharmony_ci case 'c': /* major compatible string */ 20728c2ecf20Sopenharmony_ci ret = of_property_read_string(dn, "compatible", &p); 20738c2ecf20Sopenharmony_ci if (!ret) 20748c2ecf20Sopenharmony_ci buf = string(buf, end, p, str_spec); 20758c2ecf20Sopenharmony_ci break; 20768c2ecf20Sopenharmony_ci case 'C': /* full compatible string */ 20778c2ecf20Sopenharmony_ci has_mult = false; 20788c2ecf20Sopenharmony_ci of_property_for_each_string(dn, "compatible", prop, p) { 20798c2ecf20Sopenharmony_ci if (has_mult) 20808c2ecf20Sopenharmony_ci buf = string_nocheck(buf, end, ",", str_spec); 20818c2ecf20Sopenharmony_ci buf = string_nocheck(buf, end, "\"", str_spec); 20828c2ecf20Sopenharmony_ci buf = string(buf, end, p, str_spec); 20838c2ecf20Sopenharmony_ci buf = string_nocheck(buf, end, "\"", str_spec); 20848c2ecf20Sopenharmony_ci 20858c2ecf20Sopenharmony_ci has_mult = true; 20868c2ecf20Sopenharmony_ci } 20878c2ecf20Sopenharmony_ci break; 20888c2ecf20Sopenharmony_ci default: 20898c2ecf20Sopenharmony_ci break; 20908c2ecf20Sopenharmony_ci } 20918c2ecf20Sopenharmony_ci } 20928c2ecf20Sopenharmony_ci 20938c2ecf20Sopenharmony_ci return widen_string(buf, buf - buf_start, end, spec); 20948c2ecf20Sopenharmony_ci} 20958c2ecf20Sopenharmony_ci 20968c2ecf20Sopenharmony_cistatic noinline_for_stack 20978c2ecf20Sopenharmony_cichar *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 20988c2ecf20Sopenharmony_ci struct printf_spec spec, const char *fmt) 20998c2ecf20Sopenharmony_ci{ 21008c2ecf20Sopenharmony_ci struct printf_spec str_spec = spec; 21018c2ecf20Sopenharmony_ci char *buf_start = buf; 21028c2ecf20Sopenharmony_ci 21038c2ecf20Sopenharmony_ci str_spec.field_width = -1; 21048c2ecf20Sopenharmony_ci 21058c2ecf20Sopenharmony_ci if (*fmt != 'w') 21068c2ecf20Sopenharmony_ci return error_string(buf, end, "(%pf?)", spec); 21078c2ecf20Sopenharmony_ci 21088c2ecf20Sopenharmony_ci if (check_pointer(&buf, end, fwnode, spec)) 21098c2ecf20Sopenharmony_ci return buf; 21108c2ecf20Sopenharmony_ci 21118c2ecf20Sopenharmony_ci fmt++; 21128c2ecf20Sopenharmony_ci 21138c2ecf20Sopenharmony_ci switch (*fmt) { 21148c2ecf20Sopenharmony_ci case 'P': /* name */ 21158c2ecf20Sopenharmony_ci buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 21168c2ecf20Sopenharmony_ci break; 21178c2ecf20Sopenharmony_ci case 'f': /* full_name */ 21188c2ecf20Sopenharmony_ci default: 21198c2ecf20Sopenharmony_ci buf = fwnode_full_name_string(fwnode, buf, end); 21208c2ecf20Sopenharmony_ci break; 21218c2ecf20Sopenharmony_ci } 21228c2ecf20Sopenharmony_ci 21238c2ecf20Sopenharmony_ci return widen_string(buf, buf - buf_start, end, spec); 21248c2ecf20Sopenharmony_ci} 21258c2ecf20Sopenharmony_ci 21268c2ecf20Sopenharmony_ci/* 21278c2ecf20Sopenharmony_ci * Show a '%p' thing. A kernel extension is that the '%p' is followed 21288c2ecf20Sopenharmony_ci * by an extra set of alphanumeric characters that are extended format 21298c2ecf20Sopenharmony_ci * specifiers. 21308c2ecf20Sopenharmony_ci * 21318c2ecf20Sopenharmony_ci * Please update scripts/checkpatch.pl when adding/removing conversion 21328c2ecf20Sopenharmony_ci * characters. (Search for "check for vsprintf extension"). 21338c2ecf20Sopenharmony_ci * 21348c2ecf20Sopenharmony_ci * Right now we handle: 21358c2ecf20Sopenharmony_ci * 21368c2ecf20Sopenharmony_ci * - 'S' For symbolic direct pointers (or function descriptors) with offset 21378c2ecf20Sopenharmony_ci * - 's' For symbolic direct pointers (or function descriptors) without offset 21388c2ecf20Sopenharmony_ci * - '[Ss]R' as above with __builtin_extract_return_addr() translation 21398c2ecf20Sopenharmony_ci * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 21408c2ecf20Sopenharmony_ci * %ps and %pS. Be careful when re-using these specifiers. 21418c2ecf20Sopenharmony_ci * - 'B' For backtraced symbolic direct pointers with offset 21428c2ecf20Sopenharmony_ci * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 21438c2ecf20Sopenharmony_ci * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 21448c2ecf20Sopenharmony_ci * - 'b[l]' For a bitmap, the number of bits is determined by the field 21458c2ecf20Sopenharmony_ci * width which must be explicitly specified either as part of the 21468c2ecf20Sopenharmony_ci * format string '%32b[l]' or through '%*b[l]', [l] selects 21478c2ecf20Sopenharmony_ci * range-list format instead of hex format 21488c2ecf20Sopenharmony_ci * - 'M' For a 6-byte MAC address, it prints the address in the 21498c2ecf20Sopenharmony_ci * usual colon-separated hex notation 21508c2ecf20Sopenharmony_ci * - 'm' For a 6-byte MAC address, it prints the hex address without colons 21518c2ecf20Sopenharmony_ci * - 'MF' For a 6-byte MAC FDDI address, it prints the address 21528c2ecf20Sopenharmony_ci * with a dash-separated hex notation 21538c2ecf20Sopenharmony_ci * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 21548c2ecf20Sopenharmony_ci * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 21558c2ecf20Sopenharmony_ci * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 21568c2ecf20Sopenharmony_ci * IPv6 uses colon separated network-order 16 bit hex with leading 0's 21578c2ecf20Sopenharmony_ci * [S][pfs] 21588c2ecf20Sopenharmony_ci * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 21598c2ecf20Sopenharmony_ci * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 21608c2ecf20Sopenharmony_ci * - 'i' [46] for 'raw' IPv4/IPv6 addresses 21618c2ecf20Sopenharmony_ci * IPv6 omits the colons (01020304...0f) 21628c2ecf20Sopenharmony_ci * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 21638c2ecf20Sopenharmony_ci * [S][pfs] 21648c2ecf20Sopenharmony_ci * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 21658c2ecf20Sopenharmony_ci * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 21668c2ecf20Sopenharmony_ci * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 21678c2ecf20Sopenharmony_ci * - 'I[6S]c' for IPv6 addresses printed as specified by 21688c2ecf20Sopenharmony_ci * https://tools.ietf.org/html/rfc5952 21698c2ecf20Sopenharmony_ci * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 21708c2ecf20Sopenharmony_ci * of the following flags (see string_escape_mem() for the 21718c2ecf20Sopenharmony_ci * details): 21728c2ecf20Sopenharmony_ci * a - ESCAPE_ANY 21738c2ecf20Sopenharmony_ci * c - ESCAPE_SPECIAL 21748c2ecf20Sopenharmony_ci * h - ESCAPE_HEX 21758c2ecf20Sopenharmony_ci * n - ESCAPE_NULL 21768c2ecf20Sopenharmony_ci * o - ESCAPE_OCTAL 21778c2ecf20Sopenharmony_ci * p - ESCAPE_NP 21788c2ecf20Sopenharmony_ci * s - ESCAPE_SPACE 21798c2ecf20Sopenharmony_ci * By default ESCAPE_ANY_NP is used. 21808c2ecf20Sopenharmony_ci * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 21818c2ecf20Sopenharmony_ci * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 21828c2ecf20Sopenharmony_ci * Options for %pU are: 21838c2ecf20Sopenharmony_ci * b big endian lower case hex (default) 21848c2ecf20Sopenharmony_ci * B big endian UPPER case hex 21858c2ecf20Sopenharmony_ci * l little endian lower case hex 21868c2ecf20Sopenharmony_ci * L little endian UPPER case hex 21878c2ecf20Sopenharmony_ci * big endian output byte order is: 21888c2ecf20Sopenharmony_ci * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 21898c2ecf20Sopenharmony_ci * little endian output byte order is: 21908c2ecf20Sopenharmony_ci * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 21918c2ecf20Sopenharmony_ci * - 'V' For a struct va_format which contains a format string * and va_list *, 21928c2ecf20Sopenharmony_ci * call vsnprintf(->format, *->va_list). 21938c2ecf20Sopenharmony_ci * Implements a "recursive vsnprintf". 21948c2ecf20Sopenharmony_ci * Do not use this feature without some mechanism to verify the 21958c2ecf20Sopenharmony_ci * correctness of the format string and va_list arguments. 21968c2ecf20Sopenharmony_ci * - 'K' For a kernel pointer that should be hidden from unprivileged users 21978c2ecf20Sopenharmony_ci * - 'NF' For a netdev_features_t 21988c2ecf20Sopenharmony_ci * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 21998c2ecf20Sopenharmony_ci * a certain separator (' ' by default): 22008c2ecf20Sopenharmony_ci * C colon 22018c2ecf20Sopenharmony_ci * D dash 22028c2ecf20Sopenharmony_ci * N no separator 22038c2ecf20Sopenharmony_ci * The maximum supported length is 64 bytes of the input. Consider 22048c2ecf20Sopenharmony_ci * to use print_hex_dump() for the larger input. 22058c2ecf20Sopenharmony_ci * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 22068c2ecf20Sopenharmony_ci * (default assumed to be phys_addr_t, passed by reference) 22078c2ecf20Sopenharmony_ci * - 'd[234]' For a dentry name (optionally 2-4 last components) 22088c2ecf20Sopenharmony_ci * - 'D[234]' Same as 'd' but for a struct file 22098c2ecf20Sopenharmony_ci * - 'g' For block_device name (gendisk + partition number) 22108c2ecf20Sopenharmony_ci * - 't[RT][dt][r]' For time and date as represented by: 22118c2ecf20Sopenharmony_ci * R struct rtc_time 22128c2ecf20Sopenharmony_ci * T time64_t 22138c2ecf20Sopenharmony_ci * - 'C' For a clock, it prints the name (Common Clock Framework) or address 22148c2ecf20Sopenharmony_ci * (legacy clock framework) of the clock 22158c2ecf20Sopenharmony_ci * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 22168c2ecf20Sopenharmony_ci * (legacy clock framework) of the clock 22178c2ecf20Sopenharmony_ci * - 'G' For flags to be printed as a collection of symbolic strings that would 22188c2ecf20Sopenharmony_ci * construct the specific value. Supported flags given by option: 22198c2ecf20Sopenharmony_ci * p page flags (see struct page) given as pointer to unsigned long 22208c2ecf20Sopenharmony_ci * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 22218c2ecf20Sopenharmony_ci * v vma flags (VM_*) given as pointer to unsigned long 22228c2ecf20Sopenharmony_ci * - 'OF[fnpPcCF]' For a device tree object 22238c2ecf20Sopenharmony_ci * Without any optional arguments prints the full_name 22248c2ecf20Sopenharmony_ci * f device node full_name 22258c2ecf20Sopenharmony_ci * n device node name 22268c2ecf20Sopenharmony_ci * p device node phandle 22278c2ecf20Sopenharmony_ci * P device node path spec (name + @unit) 22288c2ecf20Sopenharmony_ci * F device node flags 22298c2ecf20Sopenharmony_ci * c major compatible string 22308c2ecf20Sopenharmony_ci * C full compatible string 22318c2ecf20Sopenharmony_ci * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 22328c2ecf20Sopenharmony_ci * Without an option prints the full name of the node 22338c2ecf20Sopenharmony_ci * f full name 22348c2ecf20Sopenharmony_ci * P node name, including a possible unit address 22358c2ecf20Sopenharmony_ci * - 'x' For printing the address. Equivalent to "%lx". 22368c2ecf20Sopenharmony_ci * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 22378c2ecf20Sopenharmony_ci * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 22388c2ecf20Sopenharmony_ci * or user (u) memory to probe, and: 22398c2ecf20Sopenharmony_ci * s a string, equivalent to "%s" on direct vsnprintf() use 22408c2ecf20Sopenharmony_ci * 22418c2ecf20Sopenharmony_ci * ** When making changes please also update: 22428c2ecf20Sopenharmony_ci * Documentation/core-api/printk-formats.rst 22438c2ecf20Sopenharmony_ci * 22448c2ecf20Sopenharmony_ci * Note: The default behaviour (unadorned %p) is to hash the address, 22458c2ecf20Sopenharmony_ci * rendering it useful as a unique identifier. 22468c2ecf20Sopenharmony_ci */ 22478c2ecf20Sopenharmony_cistatic noinline_for_stack 22488c2ecf20Sopenharmony_cichar *pointer(const char *fmt, char *buf, char *end, void *ptr, 22498c2ecf20Sopenharmony_ci struct printf_spec spec) 22508c2ecf20Sopenharmony_ci{ 22518c2ecf20Sopenharmony_ci switch (*fmt) { 22528c2ecf20Sopenharmony_ci case 'S': 22538c2ecf20Sopenharmony_ci case 's': 22548c2ecf20Sopenharmony_ci ptr = dereference_symbol_descriptor(ptr); 22558c2ecf20Sopenharmony_ci /* fall through */ 22568c2ecf20Sopenharmony_ci case 'B': 22578c2ecf20Sopenharmony_ci return symbol_string(buf, end, ptr, spec, fmt); 22588c2ecf20Sopenharmony_ci case 'R': 22598c2ecf20Sopenharmony_ci case 'r': 22608c2ecf20Sopenharmony_ci return resource_string(buf, end, ptr, spec, fmt); 22618c2ecf20Sopenharmony_ci case 'h': 22628c2ecf20Sopenharmony_ci return hex_string(buf, end, ptr, spec, fmt); 22638c2ecf20Sopenharmony_ci case 'b': 22648c2ecf20Sopenharmony_ci switch (fmt[1]) { 22658c2ecf20Sopenharmony_ci case 'l': 22668c2ecf20Sopenharmony_ci return bitmap_list_string(buf, end, ptr, spec, fmt); 22678c2ecf20Sopenharmony_ci default: 22688c2ecf20Sopenharmony_ci return bitmap_string(buf, end, ptr, spec, fmt); 22698c2ecf20Sopenharmony_ci } 22708c2ecf20Sopenharmony_ci case 'M': /* Colon separated: 00:01:02:03:04:05 */ 22718c2ecf20Sopenharmony_ci case 'm': /* Contiguous: 000102030405 */ 22728c2ecf20Sopenharmony_ci /* [mM]F (FDDI) */ 22738c2ecf20Sopenharmony_ci /* [mM]R (Reverse order; Bluetooth) */ 22748c2ecf20Sopenharmony_ci return mac_address_string(buf, end, ptr, spec, fmt); 22758c2ecf20Sopenharmony_ci case 'I': /* Formatted IP supported 22768c2ecf20Sopenharmony_ci * 4: 1.2.3.4 22778c2ecf20Sopenharmony_ci * 6: 0001:0203:...:0708 22788c2ecf20Sopenharmony_ci * 6c: 1::708 or 1::1.2.3.4 22798c2ecf20Sopenharmony_ci */ 22808c2ecf20Sopenharmony_ci case 'i': /* Contiguous: 22818c2ecf20Sopenharmony_ci * 4: 001.002.003.004 22828c2ecf20Sopenharmony_ci * 6: 000102...0f 22838c2ecf20Sopenharmony_ci */ 22848c2ecf20Sopenharmony_ci return ip_addr_string(buf, end, ptr, spec, fmt); 22858c2ecf20Sopenharmony_ci case 'E': 22868c2ecf20Sopenharmony_ci return escaped_string(buf, end, ptr, spec, fmt); 22878c2ecf20Sopenharmony_ci case 'U': 22888c2ecf20Sopenharmony_ci return uuid_string(buf, end, ptr, spec, fmt); 22898c2ecf20Sopenharmony_ci case 'V': 22908c2ecf20Sopenharmony_ci return va_format(buf, end, ptr, spec, fmt); 22918c2ecf20Sopenharmony_ci case 'K': 22928c2ecf20Sopenharmony_ci return restricted_pointer(buf, end, ptr, spec); 22938c2ecf20Sopenharmony_ci case 'N': 22948c2ecf20Sopenharmony_ci return netdev_bits(buf, end, ptr, spec, fmt); 22958c2ecf20Sopenharmony_ci case 'a': 22968c2ecf20Sopenharmony_ci return address_val(buf, end, ptr, spec, fmt); 22978c2ecf20Sopenharmony_ci case 'd': 22988c2ecf20Sopenharmony_ci return dentry_name(buf, end, ptr, spec, fmt); 22998c2ecf20Sopenharmony_ci case 't': 23008c2ecf20Sopenharmony_ci return time_and_date(buf, end, ptr, spec, fmt); 23018c2ecf20Sopenharmony_ci case 'C': 23028c2ecf20Sopenharmony_ci return clock(buf, end, ptr, spec, fmt); 23038c2ecf20Sopenharmony_ci case 'D': 23048c2ecf20Sopenharmony_ci return file_dentry_name(buf, end, ptr, spec, fmt); 23058c2ecf20Sopenharmony_ci#ifdef CONFIG_BLOCK 23068c2ecf20Sopenharmony_ci case 'g': 23078c2ecf20Sopenharmony_ci return bdev_name(buf, end, ptr, spec, fmt); 23088c2ecf20Sopenharmony_ci#endif 23098c2ecf20Sopenharmony_ci 23108c2ecf20Sopenharmony_ci case 'G': 23118c2ecf20Sopenharmony_ci return flags_string(buf, end, ptr, spec, fmt); 23128c2ecf20Sopenharmony_ci case 'O': 23138c2ecf20Sopenharmony_ci return device_node_string(buf, end, ptr, spec, fmt + 1); 23148c2ecf20Sopenharmony_ci case 'f': 23158c2ecf20Sopenharmony_ci return fwnode_string(buf, end, ptr, spec, fmt + 1); 23168c2ecf20Sopenharmony_ci case 'x': 23178c2ecf20Sopenharmony_ci return pointer_string(buf, end, ptr, spec); 23188c2ecf20Sopenharmony_ci case 'e': 23198c2ecf20Sopenharmony_ci /* %pe with a non-ERR_PTR gets treated as plain %p */ 23208c2ecf20Sopenharmony_ci if (!IS_ERR(ptr)) 23218c2ecf20Sopenharmony_ci break; 23228c2ecf20Sopenharmony_ci return err_ptr(buf, end, ptr, spec); 23238c2ecf20Sopenharmony_ci case 'u': 23248c2ecf20Sopenharmony_ci case 'k': 23258c2ecf20Sopenharmony_ci switch (fmt[1]) { 23268c2ecf20Sopenharmony_ci case 's': 23278c2ecf20Sopenharmony_ci return string(buf, end, ptr, spec); 23288c2ecf20Sopenharmony_ci default: 23298c2ecf20Sopenharmony_ci return error_string(buf, end, "(einval)", spec); 23308c2ecf20Sopenharmony_ci } 23318c2ecf20Sopenharmony_ci } 23328c2ecf20Sopenharmony_ci 23338c2ecf20Sopenharmony_ci /* default is to _not_ leak addresses, hash before printing */ 23348c2ecf20Sopenharmony_ci return ptr_to_id(buf, end, ptr, spec); 23358c2ecf20Sopenharmony_ci} 23368c2ecf20Sopenharmony_ci 23378c2ecf20Sopenharmony_ci/* 23388c2ecf20Sopenharmony_ci * Helper function to decode printf style format. 23398c2ecf20Sopenharmony_ci * Each call decode a token from the format and return the 23408c2ecf20Sopenharmony_ci * number of characters read (or likely the delta where it wants 23418c2ecf20Sopenharmony_ci * to go on the next call). 23428c2ecf20Sopenharmony_ci * The decoded token is returned through the parameters 23438c2ecf20Sopenharmony_ci * 23448c2ecf20Sopenharmony_ci * 'h', 'l', or 'L' for integer fields 23458c2ecf20Sopenharmony_ci * 'z' support added 23/7/1999 S.H. 23468c2ecf20Sopenharmony_ci * 'z' changed to 'Z' --davidm 1/25/99 23478c2ecf20Sopenharmony_ci * 'Z' changed to 'z' --adobriyan 2017-01-25 23488c2ecf20Sopenharmony_ci * 't' added for ptrdiff_t 23498c2ecf20Sopenharmony_ci * 23508c2ecf20Sopenharmony_ci * @fmt: the format string 23518c2ecf20Sopenharmony_ci * @type of the token returned 23528c2ecf20Sopenharmony_ci * @flags: various flags such as +, -, # tokens.. 23538c2ecf20Sopenharmony_ci * @field_width: overwritten width 23548c2ecf20Sopenharmony_ci * @base: base of the number (octal, hex, ...) 23558c2ecf20Sopenharmony_ci * @precision: precision of a number 23568c2ecf20Sopenharmony_ci * @qualifier: qualifier of a number (long, size_t, ...) 23578c2ecf20Sopenharmony_ci */ 23588c2ecf20Sopenharmony_cistatic noinline_for_stack 23598c2ecf20Sopenharmony_ciint format_decode(const char *fmt, struct printf_spec *spec) 23608c2ecf20Sopenharmony_ci{ 23618c2ecf20Sopenharmony_ci const char *start = fmt; 23628c2ecf20Sopenharmony_ci char qualifier; 23638c2ecf20Sopenharmony_ci 23648c2ecf20Sopenharmony_ci /* we finished early by reading the field width */ 23658c2ecf20Sopenharmony_ci if (spec->type == FORMAT_TYPE_WIDTH) { 23668c2ecf20Sopenharmony_ci if (spec->field_width < 0) { 23678c2ecf20Sopenharmony_ci spec->field_width = -spec->field_width; 23688c2ecf20Sopenharmony_ci spec->flags |= LEFT; 23698c2ecf20Sopenharmony_ci } 23708c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_NONE; 23718c2ecf20Sopenharmony_ci goto precision; 23728c2ecf20Sopenharmony_ci } 23738c2ecf20Sopenharmony_ci 23748c2ecf20Sopenharmony_ci /* we finished early by reading the precision */ 23758c2ecf20Sopenharmony_ci if (spec->type == FORMAT_TYPE_PRECISION) { 23768c2ecf20Sopenharmony_ci if (spec->precision < 0) 23778c2ecf20Sopenharmony_ci spec->precision = 0; 23788c2ecf20Sopenharmony_ci 23798c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_NONE; 23808c2ecf20Sopenharmony_ci goto qualifier; 23818c2ecf20Sopenharmony_ci } 23828c2ecf20Sopenharmony_ci 23838c2ecf20Sopenharmony_ci /* By default */ 23848c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_NONE; 23858c2ecf20Sopenharmony_ci 23868c2ecf20Sopenharmony_ci for (; *fmt ; ++fmt) { 23878c2ecf20Sopenharmony_ci if (*fmt == '%') 23888c2ecf20Sopenharmony_ci break; 23898c2ecf20Sopenharmony_ci } 23908c2ecf20Sopenharmony_ci 23918c2ecf20Sopenharmony_ci /* Return the current non-format string */ 23928c2ecf20Sopenharmony_ci if (fmt != start || !*fmt) 23938c2ecf20Sopenharmony_ci return fmt - start; 23948c2ecf20Sopenharmony_ci 23958c2ecf20Sopenharmony_ci /* Process flags */ 23968c2ecf20Sopenharmony_ci spec->flags = 0; 23978c2ecf20Sopenharmony_ci 23988c2ecf20Sopenharmony_ci while (1) { /* this also skips first '%' */ 23998c2ecf20Sopenharmony_ci bool found = true; 24008c2ecf20Sopenharmony_ci 24018c2ecf20Sopenharmony_ci ++fmt; 24028c2ecf20Sopenharmony_ci 24038c2ecf20Sopenharmony_ci switch (*fmt) { 24048c2ecf20Sopenharmony_ci case '-': spec->flags |= LEFT; break; 24058c2ecf20Sopenharmony_ci case '+': spec->flags |= PLUS; break; 24068c2ecf20Sopenharmony_ci case ' ': spec->flags |= SPACE; break; 24078c2ecf20Sopenharmony_ci case '#': spec->flags |= SPECIAL; break; 24088c2ecf20Sopenharmony_ci case '0': spec->flags |= ZEROPAD; break; 24098c2ecf20Sopenharmony_ci default: found = false; 24108c2ecf20Sopenharmony_ci } 24118c2ecf20Sopenharmony_ci 24128c2ecf20Sopenharmony_ci if (!found) 24138c2ecf20Sopenharmony_ci break; 24148c2ecf20Sopenharmony_ci } 24158c2ecf20Sopenharmony_ci 24168c2ecf20Sopenharmony_ci /* get field width */ 24178c2ecf20Sopenharmony_ci spec->field_width = -1; 24188c2ecf20Sopenharmony_ci 24198c2ecf20Sopenharmony_ci if (isdigit(*fmt)) 24208c2ecf20Sopenharmony_ci spec->field_width = skip_atoi(&fmt); 24218c2ecf20Sopenharmony_ci else if (*fmt == '*') { 24228c2ecf20Sopenharmony_ci /* it's the next argument */ 24238c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_WIDTH; 24248c2ecf20Sopenharmony_ci return ++fmt - start; 24258c2ecf20Sopenharmony_ci } 24268c2ecf20Sopenharmony_ci 24278c2ecf20Sopenharmony_ciprecision: 24288c2ecf20Sopenharmony_ci /* get the precision */ 24298c2ecf20Sopenharmony_ci spec->precision = -1; 24308c2ecf20Sopenharmony_ci if (*fmt == '.') { 24318c2ecf20Sopenharmony_ci ++fmt; 24328c2ecf20Sopenharmony_ci if (isdigit(*fmt)) { 24338c2ecf20Sopenharmony_ci spec->precision = skip_atoi(&fmt); 24348c2ecf20Sopenharmony_ci if (spec->precision < 0) 24358c2ecf20Sopenharmony_ci spec->precision = 0; 24368c2ecf20Sopenharmony_ci } else if (*fmt == '*') { 24378c2ecf20Sopenharmony_ci /* it's the next argument */ 24388c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_PRECISION; 24398c2ecf20Sopenharmony_ci return ++fmt - start; 24408c2ecf20Sopenharmony_ci } 24418c2ecf20Sopenharmony_ci } 24428c2ecf20Sopenharmony_ci 24438c2ecf20Sopenharmony_ciqualifier: 24448c2ecf20Sopenharmony_ci /* get the conversion qualifier */ 24458c2ecf20Sopenharmony_ci qualifier = 0; 24468c2ecf20Sopenharmony_ci if (*fmt == 'h' || _tolower(*fmt) == 'l' || 24478c2ecf20Sopenharmony_ci *fmt == 'z' || *fmt == 't') { 24488c2ecf20Sopenharmony_ci qualifier = *fmt++; 24498c2ecf20Sopenharmony_ci if (unlikely(qualifier == *fmt)) { 24508c2ecf20Sopenharmony_ci if (qualifier == 'l') { 24518c2ecf20Sopenharmony_ci qualifier = 'L'; 24528c2ecf20Sopenharmony_ci ++fmt; 24538c2ecf20Sopenharmony_ci } else if (qualifier == 'h') { 24548c2ecf20Sopenharmony_ci qualifier = 'H'; 24558c2ecf20Sopenharmony_ci ++fmt; 24568c2ecf20Sopenharmony_ci } 24578c2ecf20Sopenharmony_ci } 24588c2ecf20Sopenharmony_ci } 24598c2ecf20Sopenharmony_ci 24608c2ecf20Sopenharmony_ci /* default base */ 24618c2ecf20Sopenharmony_ci spec->base = 10; 24628c2ecf20Sopenharmony_ci switch (*fmt) { 24638c2ecf20Sopenharmony_ci case 'c': 24648c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_CHAR; 24658c2ecf20Sopenharmony_ci return ++fmt - start; 24668c2ecf20Sopenharmony_ci 24678c2ecf20Sopenharmony_ci case 's': 24688c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_STR; 24698c2ecf20Sopenharmony_ci return ++fmt - start; 24708c2ecf20Sopenharmony_ci 24718c2ecf20Sopenharmony_ci case 'p': 24728c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_PTR; 24738c2ecf20Sopenharmony_ci return ++fmt - start; 24748c2ecf20Sopenharmony_ci 24758c2ecf20Sopenharmony_ci case '%': 24768c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_PERCENT_CHAR; 24778c2ecf20Sopenharmony_ci return ++fmt - start; 24788c2ecf20Sopenharmony_ci 24798c2ecf20Sopenharmony_ci /* integer number formats - set up the flags and "break" */ 24808c2ecf20Sopenharmony_ci case 'o': 24818c2ecf20Sopenharmony_ci spec->base = 8; 24828c2ecf20Sopenharmony_ci break; 24838c2ecf20Sopenharmony_ci 24848c2ecf20Sopenharmony_ci case 'x': 24858c2ecf20Sopenharmony_ci spec->flags |= SMALL; 24868c2ecf20Sopenharmony_ci /* fall through */ 24878c2ecf20Sopenharmony_ci 24888c2ecf20Sopenharmony_ci case 'X': 24898c2ecf20Sopenharmony_ci spec->base = 16; 24908c2ecf20Sopenharmony_ci break; 24918c2ecf20Sopenharmony_ci 24928c2ecf20Sopenharmony_ci case 'd': 24938c2ecf20Sopenharmony_ci case 'i': 24948c2ecf20Sopenharmony_ci spec->flags |= SIGN; 24958c2ecf20Sopenharmony_ci case 'u': 24968c2ecf20Sopenharmony_ci break; 24978c2ecf20Sopenharmony_ci 24988c2ecf20Sopenharmony_ci case 'n': 24998c2ecf20Sopenharmony_ci /* 25008c2ecf20Sopenharmony_ci * Since %n poses a greater security risk than 25018c2ecf20Sopenharmony_ci * utility, treat it as any other invalid or 25028c2ecf20Sopenharmony_ci * unsupported format specifier. 25038c2ecf20Sopenharmony_ci */ 25048c2ecf20Sopenharmony_ci /* fall through */ 25058c2ecf20Sopenharmony_ci 25068c2ecf20Sopenharmony_ci default: 25078c2ecf20Sopenharmony_ci WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 25088c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_INVALID; 25098c2ecf20Sopenharmony_ci return fmt - start; 25108c2ecf20Sopenharmony_ci } 25118c2ecf20Sopenharmony_ci 25128c2ecf20Sopenharmony_ci if (qualifier == 'L') 25138c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_LONG_LONG; 25148c2ecf20Sopenharmony_ci else if (qualifier == 'l') { 25158c2ecf20Sopenharmony_ci BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 25168c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 25178c2ecf20Sopenharmony_ci } else if (qualifier == 'z') { 25188c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_SIZE_T; 25198c2ecf20Sopenharmony_ci } else if (qualifier == 't') { 25208c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_PTRDIFF; 25218c2ecf20Sopenharmony_ci } else if (qualifier == 'H') { 25228c2ecf20Sopenharmony_ci BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 25238c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 25248c2ecf20Sopenharmony_ci } else if (qualifier == 'h') { 25258c2ecf20Sopenharmony_ci BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 25268c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 25278c2ecf20Sopenharmony_ci } else { 25288c2ecf20Sopenharmony_ci BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 25298c2ecf20Sopenharmony_ci spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 25308c2ecf20Sopenharmony_ci } 25318c2ecf20Sopenharmony_ci 25328c2ecf20Sopenharmony_ci return ++fmt - start; 25338c2ecf20Sopenharmony_ci} 25348c2ecf20Sopenharmony_ci 25358c2ecf20Sopenharmony_cistatic void 25368c2ecf20Sopenharmony_ciset_field_width(struct printf_spec *spec, int width) 25378c2ecf20Sopenharmony_ci{ 25388c2ecf20Sopenharmony_ci spec->field_width = width; 25398c2ecf20Sopenharmony_ci if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 25408c2ecf20Sopenharmony_ci spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 25418c2ecf20Sopenharmony_ci } 25428c2ecf20Sopenharmony_ci} 25438c2ecf20Sopenharmony_ci 25448c2ecf20Sopenharmony_cistatic void 25458c2ecf20Sopenharmony_ciset_precision(struct printf_spec *spec, int prec) 25468c2ecf20Sopenharmony_ci{ 25478c2ecf20Sopenharmony_ci spec->precision = prec; 25488c2ecf20Sopenharmony_ci if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 25498c2ecf20Sopenharmony_ci spec->precision = clamp(prec, 0, PRECISION_MAX); 25508c2ecf20Sopenharmony_ci } 25518c2ecf20Sopenharmony_ci} 25528c2ecf20Sopenharmony_ci 25538c2ecf20Sopenharmony_ci/** 25548c2ecf20Sopenharmony_ci * vsnprintf - Format a string and place it in a buffer 25558c2ecf20Sopenharmony_ci * @buf: The buffer to place the result into 25568c2ecf20Sopenharmony_ci * @size: The size of the buffer, including the trailing null space 25578c2ecf20Sopenharmony_ci * @fmt: The format string to use 25588c2ecf20Sopenharmony_ci * @args: Arguments for the format string 25598c2ecf20Sopenharmony_ci * 25608c2ecf20Sopenharmony_ci * This function generally follows C99 vsnprintf, but has some 25618c2ecf20Sopenharmony_ci * extensions and a few limitations: 25628c2ecf20Sopenharmony_ci * 25638c2ecf20Sopenharmony_ci * - ``%n`` is unsupported 25648c2ecf20Sopenharmony_ci * - ``%p*`` is handled by pointer() 25658c2ecf20Sopenharmony_ci * 25668c2ecf20Sopenharmony_ci * See pointer() or Documentation/core-api/printk-formats.rst for more 25678c2ecf20Sopenharmony_ci * extensive description. 25688c2ecf20Sopenharmony_ci * 25698c2ecf20Sopenharmony_ci * **Please update the documentation in both places when making changes** 25708c2ecf20Sopenharmony_ci * 25718c2ecf20Sopenharmony_ci * The return value is the number of characters which would 25728c2ecf20Sopenharmony_ci * be generated for the given input, excluding the trailing 25738c2ecf20Sopenharmony_ci * '\0', as per ISO C99. If you want to have the exact 25748c2ecf20Sopenharmony_ci * number of characters written into @buf as return value 25758c2ecf20Sopenharmony_ci * (not including the trailing '\0'), use vscnprintf(). If the 25768c2ecf20Sopenharmony_ci * return is greater than or equal to @size, the resulting 25778c2ecf20Sopenharmony_ci * string is truncated. 25788c2ecf20Sopenharmony_ci * 25798c2ecf20Sopenharmony_ci * If you're not already dealing with a va_list consider using snprintf(). 25808c2ecf20Sopenharmony_ci */ 25818c2ecf20Sopenharmony_ciint vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 25828c2ecf20Sopenharmony_ci{ 25838c2ecf20Sopenharmony_ci unsigned long long num; 25848c2ecf20Sopenharmony_ci char *str, *end; 25858c2ecf20Sopenharmony_ci struct printf_spec spec = {0}; 25868c2ecf20Sopenharmony_ci 25878c2ecf20Sopenharmony_ci /* Reject out-of-range values early. Large positive sizes are 25888c2ecf20Sopenharmony_ci used for unknown buffer sizes. */ 25898c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(size > INT_MAX)) 25908c2ecf20Sopenharmony_ci return 0; 25918c2ecf20Sopenharmony_ci 25928c2ecf20Sopenharmony_ci str = buf; 25938c2ecf20Sopenharmony_ci end = buf + size; 25948c2ecf20Sopenharmony_ci 25958c2ecf20Sopenharmony_ci /* Make sure end is always >= buf */ 25968c2ecf20Sopenharmony_ci if (end < buf) { 25978c2ecf20Sopenharmony_ci end = ((void *)-1); 25988c2ecf20Sopenharmony_ci size = end - buf; 25998c2ecf20Sopenharmony_ci } 26008c2ecf20Sopenharmony_ci 26018c2ecf20Sopenharmony_ci while (*fmt) { 26028c2ecf20Sopenharmony_ci const char *old_fmt = fmt; 26038c2ecf20Sopenharmony_ci int read = format_decode(fmt, &spec); 26048c2ecf20Sopenharmony_ci 26058c2ecf20Sopenharmony_ci fmt += read; 26068c2ecf20Sopenharmony_ci 26078c2ecf20Sopenharmony_ci switch (spec.type) { 26088c2ecf20Sopenharmony_ci case FORMAT_TYPE_NONE: { 26098c2ecf20Sopenharmony_ci int copy = read; 26108c2ecf20Sopenharmony_ci if (str < end) { 26118c2ecf20Sopenharmony_ci if (copy > end - str) 26128c2ecf20Sopenharmony_ci copy = end - str; 26138c2ecf20Sopenharmony_ci memcpy(str, old_fmt, copy); 26148c2ecf20Sopenharmony_ci } 26158c2ecf20Sopenharmony_ci str += read; 26168c2ecf20Sopenharmony_ci break; 26178c2ecf20Sopenharmony_ci } 26188c2ecf20Sopenharmony_ci 26198c2ecf20Sopenharmony_ci case FORMAT_TYPE_WIDTH: 26208c2ecf20Sopenharmony_ci set_field_width(&spec, va_arg(args, int)); 26218c2ecf20Sopenharmony_ci break; 26228c2ecf20Sopenharmony_ci 26238c2ecf20Sopenharmony_ci case FORMAT_TYPE_PRECISION: 26248c2ecf20Sopenharmony_ci set_precision(&spec, va_arg(args, int)); 26258c2ecf20Sopenharmony_ci break; 26268c2ecf20Sopenharmony_ci 26278c2ecf20Sopenharmony_ci case FORMAT_TYPE_CHAR: { 26288c2ecf20Sopenharmony_ci char c; 26298c2ecf20Sopenharmony_ci 26308c2ecf20Sopenharmony_ci if (!(spec.flags & LEFT)) { 26318c2ecf20Sopenharmony_ci while (--spec.field_width > 0) { 26328c2ecf20Sopenharmony_ci if (str < end) 26338c2ecf20Sopenharmony_ci *str = ' '; 26348c2ecf20Sopenharmony_ci ++str; 26358c2ecf20Sopenharmony_ci 26368c2ecf20Sopenharmony_ci } 26378c2ecf20Sopenharmony_ci } 26388c2ecf20Sopenharmony_ci c = (unsigned char) va_arg(args, int); 26398c2ecf20Sopenharmony_ci if (str < end) 26408c2ecf20Sopenharmony_ci *str = c; 26418c2ecf20Sopenharmony_ci ++str; 26428c2ecf20Sopenharmony_ci while (--spec.field_width > 0) { 26438c2ecf20Sopenharmony_ci if (str < end) 26448c2ecf20Sopenharmony_ci *str = ' '; 26458c2ecf20Sopenharmony_ci ++str; 26468c2ecf20Sopenharmony_ci } 26478c2ecf20Sopenharmony_ci break; 26488c2ecf20Sopenharmony_ci } 26498c2ecf20Sopenharmony_ci 26508c2ecf20Sopenharmony_ci case FORMAT_TYPE_STR: 26518c2ecf20Sopenharmony_ci str = string(str, end, va_arg(args, char *), spec); 26528c2ecf20Sopenharmony_ci break; 26538c2ecf20Sopenharmony_ci 26548c2ecf20Sopenharmony_ci case FORMAT_TYPE_PTR: 26558c2ecf20Sopenharmony_ci str = pointer(fmt, str, end, va_arg(args, void *), 26568c2ecf20Sopenharmony_ci spec); 26578c2ecf20Sopenharmony_ci while (isalnum(*fmt)) 26588c2ecf20Sopenharmony_ci fmt++; 26598c2ecf20Sopenharmony_ci break; 26608c2ecf20Sopenharmony_ci 26618c2ecf20Sopenharmony_ci case FORMAT_TYPE_PERCENT_CHAR: 26628c2ecf20Sopenharmony_ci if (str < end) 26638c2ecf20Sopenharmony_ci *str = '%'; 26648c2ecf20Sopenharmony_ci ++str; 26658c2ecf20Sopenharmony_ci break; 26668c2ecf20Sopenharmony_ci 26678c2ecf20Sopenharmony_ci case FORMAT_TYPE_INVALID: 26688c2ecf20Sopenharmony_ci /* 26698c2ecf20Sopenharmony_ci * Presumably the arguments passed gcc's type 26708c2ecf20Sopenharmony_ci * checking, but there is no safe or sane way 26718c2ecf20Sopenharmony_ci * for us to continue parsing the format and 26728c2ecf20Sopenharmony_ci * fetching from the va_list; the remaining 26738c2ecf20Sopenharmony_ci * specifiers and arguments would be out of 26748c2ecf20Sopenharmony_ci * sync. 26758c2ecf20Sopenharmony_ci */ 26768c2ecf20Sopenharmony_ci goto out; 26778c2ecf20Sopenharmony_ci 26788c2ecf20Sopenharmony_ci default: 26798c2ecf20Sopenharmony_ci switch (spec.type) { 26808c2ecf20Sopenharmony_ci case FORMAT_TYPE_LONG_LONG: 26818c2ecf20Sopenharmony_ci num = va_arg(args, long long); 26828c2ecf20Sopenharmony_ci break; 26838c2ecf20Sopenharmony_ci case FORMAT_TYPE_ULONG: 26848c2ecf20Sopenharmony_ci num = va_arg(args, unsigned long); 26858c2ecf20Sopenharmony_ci break; 26868c2ecf20Sopenharmony_ci case FORMAT_TYPE_LONG: 26878c2ecf20Sopenharmony_ci num = va_arg(args, long); 26888c2ecf20Sopenharmony_ci break; 26898c2ecf20Sopenharmony_ci case FORMAT_TYPE_SIZE_T: 26908c2ecf20Sopenharmony_ci if (spec.flags & SIGN) 26918c2ecf20Sopenharmony_ci num = va_arg(args, ssize_t); 26928c2ecf20Sopenharmony_ci else 26938c2ecf20Sopenharmony_ci num = va_arg(args, size_t); 26948c2ecf20Sopenharmony_ci break; 26958c2ecf20Sopenharmony_ci case FORMAT_TYPE_PTRDIFF: 26968c2ecf20Sopenharmony_ci num = va_arg(args, ptrdiff_t); 26978c2ecf20Sopenharmony_ci break; 26988c2ecf20Sopenharmony_ci case FORMAT_TYPE_UBYTE: 26998c2ecf20Sopenharmony_ci num = (unsigned char) va_arg(args, int); 27008c2ecf20Sopenharmony_ci break; 27018c2ecf20Sopenharmony_ci case FORMAT_TYPE_BYTE: 27028c2ecf20Sopenharmony_ci num = (signed char) va_arg(args, int); 27038c2ecf20Sopenharmony_ci break; 27048c2ecf20Sopenharmony_ci case FORMAT_TYPE_USHORT: 27058c2ecf20Sopenharmony_ci num = (unsigned short) va_arg(args, int); 27068c2ecf20Sopenharmony_ci break; 27078c2ecf20Sopenharmony_ci case FORMAT_TYPE_SHORT: 27088c2ecf20Sopenharmony_ci num = (short) va_arg(args, int); 27098c2ecf20Sopenharmony_ci break; 27108c2ecf20Sopenharmony_ci case FORMAT_TYPE_INT: 27118c2ecf20Sopenharmony_ci num = (int) va_arg(args, int); 27128c2ecf20Sopenharmony_ci break; 27138c2ecf20Sopenharmony_ci default: 27148c2ecf20Sopenharmony_ci num = va_arg(args, unsigned int); 27158c2ecf20Sopenharmony_ci } 27168c2ecf20Sopenharmony_ci 27178c2ecf20Sopenharmony_ci str = number(str, end, num, spec); 27188c2ecf20Sopenharmony_ci } 27198c2ecf20Sopenharmony_ci } 27208c2ecf20Sopenharmony_ci 27218c2ecf20Sopenharmony_ciout: 27228c2ecf20Sopenharmony_ci if (size > 0) { 27238c2ecf20Sopenharmony_ci if (str < end) 27248c2ecf20Sopenharmony_ci *str = '\0'; 27258c2ecf20Sopenharmony_ci else 27268c2ecf20Sopenharmony_ci end[-1] = '\0'; 27278c2ecf20Sopenharmony_ci } 27288c2ecf20Sopenharmony_ci 27298c2ecf20Sopenharmony_ci /* the trailing null byte doesn't count towards the total */ 27308c2ecf20Sopenharmony_ci return str-buf; 27318c2ecf20Sopenharmony_ci 27328c2ecf20Sopenharmony_ci} 27338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(vsnprintf); 27348c2ecf20Sopenharmony_ci 27358c2ecf20Sopenharmony_ci/** 27368c2ecf20Sopenharmony_ci * vscnprintf - Format a string and place it in a buffer 27378c2ecf20Sopenharmony_ci * @buf: The buffer to place the result into 27388c2ecf20Sopenharmony_ci * @size: The size of the buffer, including the trailing null space 27398c2ecf20Sopenharmony_ci * @fmt: The format string to use 27408c2ecf20Sopenharmony_ci * @args: Arguments for the format string 27418c2ecf20Sopenharmony_ci * 27428c2ecf20Sopenharmony_ci * The return value is the number of characters which have been written into 27438c2ecf20Sopenharmony_ci * the @buf not including the trailing '\0'. If @size is == 0 the function 27448c2ecf20Sopenharmony_ci * returns 0. 27458c2ecf20Sopenharmony_ci * 27468c2ecf20Sopenharmony_ci * If you're not already dealing with a va_list consider using scnprintf(). 27478c2ecf20Sopenharmony_ci * 27488c2ecf20Sopenharmony_ci * See the vsnprintf() documentation for format string extensions over C99. 27498c2ecf20Sopenharmony_ci */ 27508c2ecf20Sopenharmony_ciint vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 27518c2ecf20Sopenharmony_ci{ 27528c2ecf20Sopenharmony_ci int i; 27538c2ecf20Sopenharmony_ci 27548c2ecf20Sopenharmony_ci i = vsnprintf(buf, size, fmt, args); 27558c2ecf20Sopenharmony_ci 27568c2ecf20Sopenharmony_ci if (likely(i < size)) 27578c2ecf20Sopenharmony_ci return i; 27588c2ecf20Sopenharmony_ci if (size != 0) 27598c2ecf20Sopenharmony_ci return size - 1; 27608c2ecf20Sopenharmony_ci return 0; 27618c2ecf20Sopenharmony_ci} 27628c2ecf20Sopenharmony_ciEXPORT_SYMBOL(vscnprintf); 27638c2ecf20Sopenharmony_ci 27648c2ecf20Sopenharmony_ci/** 27658c2ecf20Sopenharmony_ci * snprintf - Format a string and place it in a buffer 27668c2ecf20Sopenharmony_ci * @buf: The buffer to place the result into 27678c2ecf20Sopenharmony_ci * @size: The size of the buffer, including the trailing null space 27688c2ecf20Sopenharmony_ci * @fmt: The format string to use 27698c2ecf20Sopenharmony_ci * @...: Arguments for the format string 27708c2ecf20Sopenharmony_ci * 27718c2ecf20Sopenharmony_ci * The return value is the number of characters which would be 27728c2ecf20Sopenharmony_ci * generated for the given input, excluding the trailing null, 27738c2ecf20Sopenharmony_ci * as per ISO C99. If the return is greater than or equal to 27748c2ecf20Sopenharmony_ci * @size, the resulting string is truncated. 27758c2ecf20Sopenharmony_ci * 27768c2ecf20Sopenharmony_ci * See the vsnprintf() documentation for format string extensions over C99. 27778c2ecf20Sopenharmony_ci */ 27788c2ecf20Sopenharmony_ciint snprintf(char *buf, size_t size, const char *fmt, ...) 27798c2ecf20Sopenharmony_ci{ 27808c2ecf20Sopenharmony_ci va_list args; 27818c2ecf20Sopenharmony_ci int i; 27828c2ecf20Sopenharmony_ci 27838c2ecf20Sopenharmony_ci va_start(args, fmt); 27848c2ecf20Sopenharmony_ci i = vsnprintf(buf, size, fmt, args); 27858c2ecf20Sopenharmony_ci va_end(args); 27868c2ecf20Sopenharmony_ci 27878c2ecf20Sopenharmony_ci return i; 27888c2ecf20Sopenharmony_ci} 27898c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snprintf); 27908c2ecf20Sopenharmony_ci 27918c2ecf20Sopenharmony_ci/** 27928c2ecf20Sopenharmony_ci * scnprintf - Format a string and place it in a buffer 27938c2ecf20Sopenharmony_ci * @buf: The buffer to place the result into 27948c2ecf20Sopenharmony_ci * @size: The size of the buffer, including the trailing null space 27958c2ecf20Sopenharmony_ci * @fmt: The format string to use 27968c2ecf20Sopenharmony_ci * @...: Arguments for the format string 27978c2ecf20Sopenharmony_ci * 27988c2ecf20Sopenharmony_ci * The return value is the number of characters written into @buf not including 27998c2ecf20Sopenharmony_ci * the trailing '\0'. If @size is == 0 the function returns 0. 28008c2ecf20Sopenharmony_ci */ 28018c2ecf20Sopenharmony_ci 28028c2ecf20Sopenharmony_ciint scnprintf(char *buf, size_t size, const char *fmt, ...) 28038c2ecf20Sopenharmony_ci{ 28048c2ecf20Sopenharmony_ci va_list args; 28058c2ecf20Sopenharmony_ci int i; 28068c2ecf20Sopenharmony_ci 28078c2ecf20Sopenharmony_ci va_start(args, fmt); 28088c2ecf20Sopenharmony_ci i = vscnprintf(buf, size, fmt, args); 28098c2ecf20Sopenharmony_ci va_end(args); 28108c2ecf20Sopenharmony_ci 28118c2ecf20Sopenharmony_ci return i; 28128c2ecf20Sopenharmony_ci} 28138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(scnprintf); 28148c2ecf20Sopenharmony_ci 28158c2ecf20Sopenharmony_ci/** 28168c2ecf20Sopenharmony_ci * vsprintf - Format a string and place it in a buffer 28178c2ecf20Sopenharmony_ci * @buf: The buffer to place the result into 28188c2ecf20Sopenharmony_ci * @fmt: The format string to use 28198c2ecf20Sopenharmony_ci * @args: Arguments for the format string 28208c2ecf20Sopenharmony_ci * 28218c2ecf20Sopenharmony_ci * The function returns the number of characters written 28228c2ecf20Sopenharmony_ci * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 28238c2ecf20Sopenharmony_ci * buffer overflows. 28248c2ecf20Sopenharmony_ci * 28258c2ecf20Sopenharmony_ci * If you're not already dealing with a va_list consider using sprintf(). 28268c2ecf20Sopenharmony_ci * 28278c2ecf20Sopenharmony_ci * See the vsnprintf() documentation for format string extensions over C99. 28288c2ecf20Sopenharmony_ci */ 28298c2ecf20Sopenharmony_ciint vsprintf(char *buf, const char *fmt, va_list args) 28308c2ecf20Sopenharmony_ci{ 28318c2ecf20Sopenharmony_ci return vsnprintf(buf, INT_MAX, fmt, args); 28328c2ecf20Sopenharmony_ci} 28338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(vsprintf); 28348c2ecf20Sopenharmony_ci 28358c2ecf20Sopenharmony_ci/** 28368c2ecf20Sopenharmony_ci * sprintf - Format a string and place it in a buffer 28378c2ecf20Sopenharmony_ci * @buf: The buffer to place the result into 28388c2ecf20Sopenharmony_ci * @fmt: The format string to use 28398c2ecf20Sopenharmony_ci * @...: Arguments for the format string 28408c2ecf20Sopenharmony_ci * 28418c2ecf20Sopenharmony_ci * The function returns the number of characters written 28428c2ecf20Sopenharmony_ci * into @buf. Use snprintf() or scnprintf() in order to avoid 28438c2ecf20Sopenharmony_ci * buffer overflows. 28448c2ecf20Sopenharmony_ci * 28458c2ecf20Sopenharmony_ci * See the vsnprintf() documentation for format string extensions over C99. 28468c2ecf20Sopenharmony_ci */ 28478c2ecf20Sopenharmony_ciint sprintf(char *buf, const char *fmt, ...) 28488c2ecf20Sopenharmony_ci{ 28498c2ecf20Sopenharmony_ci va_list args; 28508c2ecf20Sopenharmony_ci int i; 28518c2ecf20Sopenharmony_ci 28528c2ecf20Sopenharmony_ci va_start(args, fmt); 28538c2ecf20Sopenharmony_ci i = vsnprintf(buf, INT_MAX, fmt, args); 28548c2ecf20Sopenharmony_ci va_end(args); 28558c2ecf20Sopenharmony_ci 28568c2ecf20Sopenharmony_ci return i; 28578c2ecf20Sopenharmony_ci} 28588c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sprintf); 28598c2ecf20Sopenharmony_ci 28608c2ecf20Sopenharmony_ci#ifdef CONFIG_BINARY_PRINTF 28618c2ecf20Sopenharmony_ci/* 28628c2ecf20Sopenharmony_ci * bprintf service: 28638c2ecf20Sopenharmony_ci * vbin_printf() - VA arguments to binary data 28648c2ecf20Sopenharmony_ci * bstr_printf() - Binary data to text string 28658c2ecf20Sopenharmony_ci */ 28668c2ecf20Sopenharmony_ci 28678c2ecf20Sopenharmony_ci/** 28688c2ecf20Sopenharmony_ci * vbin_printf - Parse a format string and place args' binary value in a buffer 28698c2ecf20Sopenharmony_ci * @bin_buf: The buffer to place args' binary value 28708c2ecf20Sopenharmony_ci * @size: The size of the buffer(by words(32bits), not characters) 28718c2ecf20Sopenharmony_ci * @fmt: The format string to use 28728c2ecf20Sopenharmony_ci * @args: Arguments for the format string 28738c2ecf20Sopenharmony_ci * 28748c2ecf20Sopenharmony_ci * The format follows C99 vsnprintf, except %n is ignored, and its argument 28758c2ecf20Sopenharmony_ci * is skipped. 28768c2ecf20Sopenharmony_ci * 28778c2ecf20Sopenharmony_ci * The return value is the number of words(32bits) which would be generated for 28788c2ecf20Sopenharmony_ci * the given input. 28798c2ecf20Sopenharmony_ci * 28808c2ecf20Sopenharmony_ci * NOTE: 28818c2ecf20Sopenharmony_ci * If the return value is greater than @size, the resulting bin_buf is NOT 28828c2ecf20Sopenharmony_ci * valid for bstr_printf(). 28838c2ecf20Sopenharmony_ci */ 28848c2ecf20Sopenharmony_ciint vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 28858c2ecf20Sopenharmony_ci{ 28868c2ecf20Sopenharmony_ci struct printf_spec spec = {0}; 28878c2ecf20Sopenharmony_ci char *str, *end; 28888c2ecf20Sopenharmony_ci int width; 28898c2ecf20Sopenharmony_ci 28908c2ecf20Sopenharmony_ci str = (char *)bin_buf; 28918c2ecf20Sopenharmony_ci end = (char *)(bin_buf + size); 28928c2ecf20Sopenharmony_ci 28938c2ecf20Sopenharmony_ci#define save_arg(type) \ 28948c2ecf20Sopenharmony_ci({ \ 28958c2ecf20Sopenharmony_ci unsigned long long value; \ 28968c2ecf20Sopenharmony_ci if (sizeof(type) == 8) { \ 28978c2ecf20Sopenharmony_ci unsigned long long val8; \ 28988c2ecf20Sopenharmony_ci str = PTR_ALIGN(str, sizeof(u32)); \ 28998c2ecf20Sopenharmony_ci val8 = va_arg(args, unsigned long long); \ 29008c2ecf20Sopenharmony_ci if (str + sizeof(type) <= end) { \ 29018c2ecf20Sopenharmony_ci *(u32 *)str = *(u32 *)&val8; \ 29028c2ecf20Sopenharmony_ci *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 29038c2ecf20Sopenharmony_ci } \ 29048c2ecf20Sopenharmony_ci value = val8; \ 29058c2ecf20Sopenharmony_ci } else { \ 29068c2ecf20Sopenharmony_ci unsigned int val4; \ 29078c2ecf20Sopenharmony_ci str = PTR_ALIGN(str, sizeof(type)); \ 29088c2ecf20Sopenharmony_ci val4 = va_arg(args, int); \ 29098c2ecf20Sopenharmony_ci if (str + sizeof(type) <= end) \ 29108c2ecf20Sopenharmony_ci *(typeof(type) *)str = (type)(long)val4; \ 29118c2ecf20Sopenharmony_ci value = (unsigned long long)val4; \ 29128c2ecf20Sopenharmony_ci } \ 29138c2ecf20Sopenharmony_ci str += sizeof(type); \ 29148c2ecf20Sopenharmony_ci value; \ 29158c2ecf20Sopenharmony_ci}) 29168c2ecf20Sopenharmony_ci 29178c2ecf20Sopenharmony_ci while (*fmt) { 29188c2ecf20Sopenharmony_ci int read = format_decode(fmt, &spec); 29198c2ecf20Sopenharmony_ci 29208c2ecf20Sopenharmony_ci fmt += read; 29218c2ecf20Sopenharmony_ci 29228c2ecf20Sopenharmony_ci switch (spec.type) { 29238c2ecf20Sopenharmony_ci case FORMAT_TYPE_NONE: 29248c2ecf20Sopenharmony_ci case FORMAT_TYPE_PERCENT_CHAR: 29258c2ecf20Sopenharmony_ci break; 29268c2ecf20Sopenharmony_ci case FORMAT_TYPE_INVALID: 29278c2ecf20Sopenharmony_ci goto out; 29288c2ecf20Sopenharmony_ci 29298c2ecf20Sopenharmony_ci case FORMAT_TYPE_WIDTH: 29308c2ecf20Sopenharmony_ci case FORMAT_TYPE_PRECISION: 29318c2ecf20Sopenharmony_ci width = (int)save_arg(int); 29328c2ecf20Sopenharmony_ci /* Pointers may require the width */ 29338c2ecf20Sopenharmony_ci if (*fmt == 'p') 29348c2ecf20Sopenharmony_ci set_field_width(&spec, width); 29358c2ecf20Sopenharmony_ci break; 29368c2ecf20Sopenharmony_ci 29378c2ecf20Sopenharmony_ci case FORMAT_TYPE_CHAR: 29388c2ecf20Sopenharmony_ci save_arg(char); 29398c2ecf20Sopenharmony_ci break; 29408c2ecf20Sopenharmony_ci 29418c2ecf20Sopenharmony_ci case FORMAT_TYPE_STR: { 29428c2ecf20Sopenharmony_ci const char *save_str = va_arg(args, char *); 29438c2ecf20Sopenharmony_ci const char *err_msg; 29448c2ecf20Sopenharmony_ci size_t len; 29458c2ecf20Sopenharmony_ci 29468c2ecf20Sopenharmony_ci err_msg = check_pointer_msg(save_str); 29478c2ecf20Sopenharmony_ci if (err_msg) 29488c2ecf20Sopenharmony_ci save_str = err_msg; 29498c2ecf20Sopenharmony_ci 29508c2ecf20Sopenharmony_ci len = strlen(save_str) + 1; 29518c2ecf20Sopenharmony_ci if (str + len < end) 29528c2ecf20Sopenharmony_ci memcpy(str, save_str, len); 29538c2ecf20Sopenharmony_ci str += len; 29548c2ecf20Sopenharmony_ci break; 29558c2ecf20Sopenharmony_ci } 29568c2ecf20Sopenharmony_ci 29578c2ecf20Sopenharmony_ci case FORMAT_TYPE_PTR: 29588c2ecf20Sopenharmony_ci /* Dereferenced pointers must be done now */ 29598c2ecf20Sopenharmony_ci switch (*fmt) { 29608c2ecf20Sopenharmony_ci /* Dereference of functions is still OK */ 29618c2ecf20Sopenharmony_ci case 'S': 29628c2ecf20Sopenharmony_ci case 's': 29638c2ecf20Sopenharmony_ci case 'x': 29648c2ecf20Sopenharmony_ci case 'K': 29658c2ecf20Sopenharmony_ci case 'e': 29668c2ecf20Sopenharmony_ci save_arg(void *); 29678c2ecf20Sopenharmony_ci break; 29688c2ecf20Sopenharmony_ci default: 29698c2ecf20Sopenharmony_ci if (!isalnum(*fmt)) { 29708c2ecf20Sopenharmony_ci save_arg(void *); 29718c2ecf20Sopenharmony_ci break; 29728c2ecf20Sopenharmony_ci } 29738c2ecf20Sopenharmony_ci str = pointer(fmt, str, end, va_arg(args, void *), 29748c2ecf20Sopenharmony_ci spec); 29758c2ecf20Sopenharmony_ci if (str + 1 < end) 29768c2ecf20Sopenharmony_ci *str++ = '\0'; 29778c2ecf20Sopenharmony_ci else 29788c2ecf20Sopenharmony_ci end[-1] = '\0'; /* Must be nul terminated */ 29798c2ecf20Sopenharmony_ci } 29808c2ecf20Sopenharmony_ci /* skip all alphanumeric pointer suffixes */ 29818c2ecf20Sopenharmony_ci while (isalnum(*fmt)) 29828c2ecf20Sopenharmony_ci fmt++; 29838c2ecf20Sopenharmony_ci break; 29848c2ecf20Sopenharmony_ci 29858c2ecf20Sopenharmony_ci default: 29868c2ecf20Sopenharmony_ci switch (spec.type) { 29878c2ecf20Sopenharmony_ci 29888c2ecf20Sopenharmony_ci case FORMAT_TYPE_LONG_LONG: 29898c2ecf20Sopenharmony_ci save_arg(long long); 29908c2ecf20Sopenharmony_ci break; 29918c2ecf20Sopenharmony_ci case FORMAT_TYPE_ULONG: 29928c2ecf20Sopenharmony_ci case FORMAT_TYPE_LONG: 29938c2ecf20Sopenharmony_ci save_arg(unsigned long); 29948c2ecf20Sopenharmony_ci break; 29958c2ecf20Sopenharmony_ci case FORMAT_TYPE_SIZE_T: 29968c2ecf20Sopenharmony_ci save_arg(size_t); 29978c2ecf20Sopenharmony_ci break; 29988c2ecf20Sopenharmony_ci case FORMAT_TYPE_PTRDIFF: 29998c2ecf20Sopenharmony_ci save_arg(ptrdiff_t); 30008c2ecf20Sopenharmony_ci break; 30018c2ecf20Sopenharmony_ci case FORMAT_TYPE_UBYTE: 30028c2ecf20Sopenharmony_ci case FORMAT_TYPE_BYTE: 30038c2ecf20Sopenharmony_ci save_arg(char); 30048c2ecf20Sopenharmony_ci break; 30058c2ecf20Sopenharmony_ci case FORMAT_TYPE_USHORT: 30068c2ecf20Sopenharmony_ci case FORMAT_TYPE_SHORT: 30078c2ecf20Sopenharmony_ci save_arg(short); 30088c2ecf20Sopenharmony_ci break; 30098c2ecf20Sopenharmony_ci default: 30108c2ecf20Sopenharmony_ci save_arg(int); 30118c2ecf20Sopenharmony_ci } 30128c2ecf20Sopenharmony_ci } 30138c2ecf20Sopenharmony_ci } 30148c2ecf20Sopenharmony_ci 30158c2ecf20Sopenharmony_ciout: 30168c2ecf20Sopenharmony_ci return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 30178c2ecf20Sopenharmony_ci#undef save_arg 30188c2ecf20Sopenharmony_ci} 30198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(vbin_printf); 30208c2ecf20Sopenharmony_ci 30218c2ecf20Sopenharmony_ci/** 30228c2ecf20Sopenharmony_ci * bstr_printf - Format a string from binary arguments and place it in a buffer 30238c2ecf20Sopenharmony_ci * @buf: The buffer to place the result into 30248c2ecf20Sopenharmony_ci * @size: The size of the buffer, including the trailing null space 30258c2ecf20Sopenharmony_ci * @fmt: The format string to use 30268c2ecf20Sopenharmony_ci * @bin_buf: Binary arguments for the format string 30278c2ecf20Sopenharmony_ci * 30288c2ecf20Sopenharmony_ci * This function like C99 vsnprintf, but the difference is that vsnprintf gets 30298c2ecf20Sopenharmony_ci * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 30308c2ecf20Sopenharmony_ci * a binary buffer that generated by vbin_printf. 30318c2ecf20Sopenharmony_ci * 30328c2ecf20Sopenharmony_ci * The format follows C99 vsnprintf, but has some extensions: 30338c2ecf20Sopenharmony_ci * see vsnprintf comment for details. 30348c2ecf20Sopenharmony_ci * 30358c2ecf20Sopenharmony_ci * The return value is the number of characters which would 30368c2ecf20Sopenharmony_ci * be generated for the given input, excluding the trailing 30378c2ecf20Sopenharmony_ci * '\0', as per ISO C99. If you want to have the exact 30388c2ecf20Sopenharmony_ci * number of characters written into @buf as return value 30398c2ecf20Sopenharmony_ci * (not including the trailing '\0'), use vscnprintf(). If the 30408c2ecf20Sopenharmony_ci * return is greater than or equal to @size, the resulting 30418c2ecf20Sopenharmony_ci * string is truncated. 30428c2ecf20Sopenharmony_ci */ 30438c2ecf20Sopenharmony_ciint bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 30448c2ecf20Sopenharmony_ci{ 30458c2ecf20Sopenharmony_ci struct printf_spec spec = {0}; 30468c2ecf20Sopenharmony_ci char *str, *end; 30478c2ecf20Sopenharmony_ci const char *args = (const char *)bin_buf; 30488c2ecf20Sopenharmony_ci 30498c2ecf20Sopenharmony_ci if (WARN_ON_ONCE(size > INT_MAX)) 30508c2ecf20Sopenharmony_ci return 0; 30518c2ecf20Sopenharmony_ci 30528c2ecf20Sopenharmony_ci str = buf; 30538c2ecf20Sopenharmony_ci end = buf + size; 30548c2ecf20Sopenharmony_ci 30558c2ecf20Sopenharmony_ci#define get_arg(type) \ 30568c2ecf20Sopenharmony_ci({ \ 30578c2ecf20Sopenharmony_ci typeof(type) value; \ 30588c2ecf20Sopenharmony_ci if (sizeof(type) == 8) { \ 30598c2ecf20Sopenharmony_ci args = PTR_ALIGN(args, sizeof(u32)); \ 30608c2ecf20Sopenharmony_ci *(u32 *)&value = *(u32 *)args; \ 30618c2ecf20Sopenharmony_ci *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 30628c2ecf20Sopenharmony_ci } else { \ 30638c2ecf20Sopenharmony_ci args = PTR_ALIGN(args, sizeof(type)); \ 30648c2ecf20Sopenharmony_ci value = *(typeof(type) *)args; \ 30658c2ecf20Sopenharmony_ci } \ 30668c2ecf20Sopenharmony_ci args += sizeof(type); \ 30678c2ecf20Sopenharmony_ci value; \ 30688c2ecf20Sopenharmony_ci}) 30698c2ecf20Sopenharmony_ci 30708c2ecf20Sopenharmony_ci /* Make sure end is always >= buf */ 30718c2ecf20Sopenharmony_ci if (end < buf) { 30728c2ecf20Sopenharmony_ci end = ((void *)-1); 30738c2ecf20Sopenharmony_ci size = end - buf; 30748c2ecf20Sopenharmony_ci } 30758c2ecf20Sopenharmony_ci 30768c2ecf20Sopenharmony_ci while (*fmt) { 30778c2ecf20Sopenharmony_ci const char *old_fmt = fmt; 30788c2ecf20Sopenharmony_ci int read = format_decode(fmt, &spec); 30798c2ecf20Sopenharmony_ci 30808c2ecf20Sopenharmony_ci fmt += read; 30818c2ecf20Sopenharmony_ci 30828c2ecf20Sopenharmony_ci switch (spec.type) { 30838c2ecf20Sopenharmony_ci case FORMAT_TYPE_NONE: { 30848c2ecf20Sopenharmony_ci int copy = read; 30858c2ecf20Sopenharmony_ci if (str < end) { 30868c2ecf20Sopenharmony_ci if (copy > end - str) 30878c2ecf20Sopenharmony_ci copy = end - str; 30888c2ecf20Sopenharmony_ci memcpy(str, old_fmt, copy); 30898c2ecf20Sopenharmony_ci } 30908c2ecf20Sopenharmony_ci str += read; 30918c2ecf20Sopenharmony_ci break; 30928c2ecf20Sopenharmony_ci } 30938c2ecf20Sopenharmony_ci 30948c2ecf20Sopenharmony_ci case FORMAT_TYPE_WIDTH: 30958c2ecf20Sopenharmony_ci set_field_width(&spec, get_arg(int)); 30968c2ecf20Sopenharmony_ci break; 30978c2ecf20Sopenharmony_ci 30988c2ecf20Sopenharmony_ci case FORMAT_TYPE_PRECISION: 30998c2ecf20Sopenharmony_ci set_precision(&spec, get_arg(int)); 31008c2ecf20Sopenharmony_ci break; 31018c2ecf20Sopenharmony_ci 31028c2ecf20Sopenharmony_ci case FORMAT_TYPE_CHAR: { 31038c2ecf20Sopenharmony_ci char c; 31048c2ecf20Sopenharmony_ci 31058c2ecf20Sopenharmony_ci if (!(spec.flags & LEFT)) { 31068c2ecf20Sopenharmony_ci while (--spec.field_width > 0) { 31078c2ecf20Sopenharmony_ci if (str < end) 31088c2ecf20Sopenharmony_ci *str = ' '; 31098c2ecf20Sopenharmony_ci ++str; 31108c2ecf20Sopenharmony_ci } 31118c2ecf20Sopenharmony_ci } 31128c2ecf20Sopenharmony_ci c = (unsigned char) get_arg(char); 31138c2ecf20Sopenharmony_ci if (str < end) 31148c2ecf20Sopenharmony_ci *str = c; 31158c2ecf20Sopenharmony_ci ++str; 31168c2ecf20Sopenharmony_ci while (--spec.field_width > 0) { 31178c2ecf20Sopenharmony_ci if (str < end) 31188c2ecf20Sopenharmony_ci *str = ' '; 31198c2ecf20Sopenharmony_ci ++str; 31208c2ecf20Sopenharmony_ci } 31218c2ecf20Sopenharmony_ci break; 31228c2ecf20Sopenharmony_ci } 31238c2ecf20Sopenharmony_ci 31248c2ecf20Sopenharmony_ci case FORMAT_TYPE_STR: { 31258c2ecf20Sopenharmony_ci const char *str_arg = args; 31268c2ecf20Sopenharmony_ci args += strlen(str_arg) + 1; 31278c2ecf20Sopenharmony_ci str = string(str, end, (char *)str_arg, spec); 31288c2ecf20Sopenharmony_ci break; 31298c2ecf20Sopenharmony_ci } 31308c2ecf20Sopenharmony_ci 31318c2ecf20Sopenharmony_ci case FORMAT_TYPE_PTR: { 31328c2ecf20Sopenharmony_ci bool process = false; 31338c2ecf20Sopenharmony_ci int copy, len; 31348c2ecf20Sopenharmony_ci /* Non function dereferences were already done */ 31358c2ecf20Sopenharmony_ci switch (*fmt) { 31368c2ecf20Sopenharmony_ci case 'S': 31378c2ecf20Sopenharmony_ci case 's': 31388c2ecf20Sopenharmony_ci case 'x': 31398c2ecf20Sopenharmony_ci case 'K': 31408c2ecf20Sopenharmony_ci case 'e': 31418c2ecf20Sopenharmony_ci process = true; 31428c2ecf20Sopenharmony_ci break; 31438c2ecf20Sopenharmony_ci default: 31448c2ecf20Sopenharmony_ci if (!isalnum(*fmt)) { 31458c2ecf20Sopenharmony_ci process = true; 31468c2ecf20Sopenharmony_ci break; 31478c2ecf20Sopenharmony_ci } 31488c2ecf20Sopenharmony_ci /* Pointer dereference was already processed */ 31498c2ecf20Sopenharmony_ci if (str < end) { 31508c2ecf20Sopenharmony_ci len = copy = strlen(args); 31518c2ecf20Sopenharmony_ci if (copy > end - str) 31528c2ecf20Sopenharmony_ci copy = end - str; 31538c2ecf20Sopenharmony_ci memcpy(str, args, copy); 31548c2ecf20Sopenharmony_ci str += len; 31558c2ecf20Sopenharmony_ci args += len + 1; 31568c2ecf20Sopenharmony_ci } 31578c2ecf20Sopenharmony_ci } 31588c2ecf20Sopenharmony_ci if (process) 31598c2ecf20Sopenharmony_ci str = pointer(fmt, str, end, get_arg(void *), spec); 31608c2ecf20Sopenharmony_ci 31618c2ecf20Sopenharmony_ci while (isalnum(*fmt)) 31628c2ecf20Sopenharmony_ci fmt++; 31638c2ecf20Sopenharmony_ci break; 31648c2ecf20Sopenharmony_ci } 31658c2ecf20Sopenharmony_ci 31668c2ecf20Sopenharmony_ci case FORMAT_TYPE_PERCENT_CHAR: 31678c2ecf20Sopenharmony_ci if (str < end) 31688c2ecf20Sopenharmony_ci *str = '%'; 31698c2ecf20Sopenharmony_ci ++str; 31708c2ecf20Sopenharmony_ci break; 31718c2ecf20Sopenharmony_ci 31728c2ecf20Sopenharmony_ci case FORMAT_TYPE_INVALID: 31738c2ecf20Sopenharmony_ci goto out; 31748c2ecf20Sopenharmony_ci 31758c2ecf20Sopenharmony_ci default: { 31768c2ecf20Sopenharmony_ci unsigned long long num; 31778c2ecf20Sopenharmony_ci 31788c2ecf20Sopenharmony_ci switch (spec.type) { 31798c2ecf20Sopenharmony_ci 31808c2ecf20Sopenharmony_ci case FORMAT_TYPE_LONG_LONG: 31818c2ecf20Sopenharmony_ci num = get_arg(long long); 31828c2ecf20Sopenharmony_ci break; 31838c2ecf20Sopenharmony_ci case FORMAT_TYPE_ULONG: 31848c2ecf20Sopenharmony_ci case FORMAT_TYPE_LONG: 31858c2ecf20Sopenharmony_ci num = get_arg(unsigned long); 31868c2ecf20Sopenharmony_ci break; 31878c2ecf20Sopenharmony_ci case FORMAT_TYPE_SIZE_T: 31888c2ecf20Sopenharmony_ci num = get_arg(size_t); 31898c2ecf20Sopenharmony_ci break; 31908c2ecf20Sopenharmony_ci case FORMAT_TYPE_PTRDIFF: 31918c2ecf20Sopenharmony_ci num = get_arg(ptrdiff_t); 31928c2ecf20Sopenharmony_ci break; 31938c2ecf20Sopenharmony_ci case FORMAT_TYPE_UBYTE: 31948c2ecf20Sopenharmony_ci num = get_arg(unsigned char); 31958c2ecf20Sopenharmony_ci break; 31968c2ecf20Sopenharmony_ci case FORMAT_TYPE_BYTE: 31978c2ecf20Sopenharmony_ci num = get_arg(signed char); 31988c2ecf20Sopenharmony_ci break; 31998c2ecf20Sopenharmony_ci case FORMAT_TYPE_USHORT: 32008c2ecf20Sopenharmony_ci num = get_arg(unsigned short); 32018c2ecf20Sopenharmony_ci break; 32028c2ecf20Sopenharmony_ci case FORMAT_TYPE_SHORT: 32038c2ecf20Sopenharmony_ci num = get_arg(short); 32048c2ecf20Sopenharmony_ci break; 32058c2ecf20Sopenharmony_ci case FORMAT_TYPE_UINT: 32068c2ecf20Sopenharmony_ci num = get_arg(unsigned int); 32078c2ecf20Sopenharmony_ci break; 32088c2ecf20Sopenharmony_ci default: 32098c2ecf20Sopenharmony_ci num = get_arg(int); 32108c2ecf20Sopenharmony_ci } 32118c2ecf20Sopenharmony_ci 32128c2ecf20Sopenharmony_ci str = number(str, end, num, spec); 32138c2ecf20Sopenharmony_ci } /* default: */ 32148c2ecf20Sopenharmony_ci } /* switch(spec.type) */ 32158c2ecf20Sopenharmony_ci } /* while(*fmt) */ 32168c2ecf20Sopenharmony_ci 32178c2ecf20Sopenharmony_ciout: 32188c2ecf20Sopenharmony_ci if (size > 0) { 32198c2ecf20Sopenharmony_ci if (str < end) 32208c2ecf20Sopenharmony_ci *str = '\0'; 32218c2ecf20Sopenharmony_ci else 32228c2ecf20Sopenharmony_ci end[-1] = '\0'; 32238c2ecf20Sopenharmony_ci } 32248c2ecf20Sopenharmony_ci 32258c2ecf20Sopenharmony_ci#undef get_arg 32268c2ecf20Sopenharmony_ci 32278c2ecf20Sopenharmony_ci /* the trailing null byte doesn't count towards the total */ 32288c2ecf20Sopenharmony_ci return str - buf; 32298c2ecf20Sopenharmony_ci} 32308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bstr_printf); 32318c2ecf20Sopenharmony_ci 32328c2ecf20Sopenharmony_ci/** 32338c2ecf20Sopenharmony_ci * bprintf - Parse a format string and place args' binary value in a buffer 32348c2ecf20Sopenharmony_ci * @bin_buf: The buffer to place args' binary value 32358c2ecf20Sopenharmony_ci * @size: The size of the buffer(by words(32bits), not characters) 32368c2ecf20Sopenharmony_ci * @fmt: The format string to use 32378c2ecf20Sopenharmony_ci * @...: Arguments for the format string 32388c2ecf20Sopenharmony_ci * 32398c2ecf20Sopenharmony_ci * The function returns the number of words(u32) written 32408c2ecf20Sopenharmony_ci * into @bin_buf. 32418c2ecf20Sopenharmony_ci */ 32428c2ecf20Sopenharmony_ciint bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 32438c2ecf20Sopenharmony_ci{ 32448c2ecf20Sopenharmony_ci va_list args; 32458c2ecf20Sopenharmony_ci int ret; 32468c2ecf20Sopenharmony_ci 32478c2ecf20Sopenharmony_ci va_start(args, fmt); 32488c2ecf20Sopenharmony_ci ret = vbin_printf(bin_buf, size, fmt, args); 32498c2ecf20Sopenharmony_ci va_end(args); 32508c2ecf20Sopenharmony_ci 32518c2ecf20Sopenharmony_ci return ret; 32528c2ecf20Sopenharmony_ci} 32538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(bprintf); 32548c2ecf20Sopenharmony_ci 32558c2ecf20Sopenharmony_ci#endif /* CONFIG_BINARY_PRINTF */ 32568c2ecf20Sopenharmony_ci 32578c2ecf20Sopenharmony_ci/** 32588c2ecf20Sopenharmony_ci * vsscanf - Unformat a buffer into a list of arguments 32598c2ecf20Sopenharmony_ci * @buf: input buffer 32608c2ecf20Sopenharmony_ci * @fmt: format of buffer 32618c2ecf20Sopenharmony_ci * @args: arguments 32628c2ecf20Sopenharmony_ci */ 32638c2ecf20Sopenharmony_ciint vsscanf(const char *buf, const char *fmt, va_list args) 32648c2ecf20Sopenharmony_ci{ 32658c2ecf20Sopenharmony_ci const char *str = buf; 32668c2ecf20Sopenharmony_ci char *next; 32678c2ecf20Sopenharmony_ci char digit; 32688c2ecf20Sopenharmony_ci int num = 0; 32698c2ecf20Sopenharmony_ci u8 qualifier; 32708c2ecf20Sopenharmony_ci unsigned int base; 32718c2ecf20Sopenharmony_ci union { 32728c2ecf20Sopenharmony_ci long long s; 32738c2ecf20Sopenharmony_ci unsigned long long u; 32748c2ecf20Sopenharmony_ci } val; 32758c2ecf20Sopenharmony_ci s16 field_width; 32768c2ecf20Sopenharmony_ci bool is_sign; 32778c2ecf20Sopenharmony_ci 32788c2ecf20Sopenharmony_ci while (*fmt) { 32798c2ecf20Sopenharmony_ci /* skip any white space in format */ 32808c2ecf20Sopenharmony_ci /* white space in format matchs any amount of 32818c2ecf20Sopenharmony_ci * white space, including none, in the input. 32828c2ecf20Sopenharmony_ci */ 32838c2ecf20Sopenharmony_ci if (isspace(*fmt)) { 32848c2ecf20Sopenharmony_ci fmt = skip_spaces(++fmt); 32858c2ecf20Sopenharmony_ci str = skip_spaces(str); 32868c2ecf20Sopenharmony_ci } 32878c2ecf20Sopenharmony_ci 32888c2ecf20Sopenharmony_ci /* anything that is not a conversion must match exactly */ 32898c2ecf20Sopenharmony_ci if (*fmt != '%' && *fmt) { 32908c2ecf20Sopenharmony_ci if (*fmt++ != *str++) 32918c2ecf20Sopenharmony_ci break; 32928c2ecf20Sopenharmony_ci continue; 32938c2ecf20Sopenharmony_ci } 32948c2ecf20Sopenharmony_ci 32958c2ecf20Sopenharmony_ci if (!*fmt) 32968c2ecf20Sopenharmony_ci break; 32978c2ecf20Sopenharmony_ci ++fmt; 32988c2ecf20Sopenharmony_ci 32998c2ecf20Sopenharmony_ci /* skip this conversion. 33008c2ecf20Sopenharmony_ci * advance both strings to next white space 33018c2ecf20Sopenharmony_ci */ 33028c2ecf20Sopenharmony_ci if (*fmt == '*') { 33038c2ecf20Sopenharmony_ci if (!*str) 33048c2ecf20Sopenharmony_ci break; 33058c2ecf20Sopenharmony_ci while (!isspace(*fmt) && *fmt != '%' && *fmt) { 33068c2ecf20Sopenharmony_ci /* '%*[' not yet supported, invalid format */ 33078c2ecf20Sopenharmony_ci if (*fmt == '[') 33088c2ecf20Sopenharmony_ci return num; 33098c2ecf20Sopenharmony_ci fmt++; 33108c2ecf20Sopenharmony_ci } 33118c2ecf20Sopenharmony_ci while (!isspace(*str) && *str) 33128c2ecf20Sopenharmony_ci str++; 33138c2ecf20Sopenharmony_ci continue; 33148c2ecf20Sopenharmony_ci } 33158c2ecf20Sopenharmony_ci 33168c2ecf20Sopenharmony_ci /* get field width */ 33178c2ecf20Sopenharmony_ci field_width = -1; 33188c2ecf20Sopenharmony_ci if (isdigit(*fmt)) { 33198c2ecf20Sopenharmony_ci field_width = skip_atoi(&fmt); 33208c2ecf20Sopenharmony_ci if (field_width <= 0) 33218c2ecf20Sopenharmony_ci break; 33228c2ecf20Sopenharmony_ci } 33238c2ecf20Sopenharmony_ci 33248c2ecf20Sopenharmony_ci /* get conversion qualifier */ 33258c2ecf20Sopenharmony_ci qualifier = -1; 33268c2ecf20Sopenharmony_ci if (*fmt == 'h' || _tolower(*fmt) == 'l' || 33278c2ecf20Sopenharmony_ci *fmt == 'z') { 33288c2ecf20Sopenharmony_ci qualifier = *fmt++; 33298c2ecf20Sopenharmony_ci if (unlikely(qualifier == *fmt)) { 33308c2ecf20Sopenharmony_ci if (qualifier == 'h') { 33318c2ecf20Sopenharmony_ci qualifier = 'H'; 33328c2ecf20Sopenharmony_ci fmt++; 33338c2ecf20Sopenharmony_ci } else if (qualifier == 'l') { 33348c2ecf20Sopenharmony_ci qualifier = 'L'; 33358c2ecf20Sopenharmony_ci fmt++; 33368c2ecf20Sopenharmony_ci } 33378c2ecf20Sopenharmony_ci } 33388c2ecf20Sopenharmony_ci } 33398c2ecf20Sopenharmony_ci 33408c2ecf20Sopenharmony_ci if (!*fmt) 33418c2ecf20Sopenharmony_ci break; 33428c2ecf20Sopenharmony_ci 33438c2ecf20Sopenharmony_ci if (*fmt == 'n') { 33448c2ecf20Sopenharmony_ci /* return number of characters read so far */ 33458c2ecf20Sopenharmony_ci *va_arg(args, int *) = str - buf; 33468c2ecf20Sopenharmony_ci ++fmt; 33478c2ecf20Sopenharmony_ci continue; 33488c2ecf20Sopenharmony_ci } 33498c2ecf20Sopenharmony_ci 33508c2ecf20Sopenharmony_ci if (!*str) 33518c2ecf20Sopenharmony_ci break; 33528c2ecf20Sopenharmony_ci 33538c2ecf20Sopenharmony_ci base = 10; 33548c2ecf20Sopenharmony_ci is_sign = false; 33558c2ecf20Sopenharmony_ci 33568c2ecf20Sopenharmony_ci switch (*fmt++) { 33578c2ecf20Sopenharmony_ci case 'c': 33588c2ecf20Sopenharmony_ci { 33598c2ecf20Sopenharmony_ci char *s = (char *)va_arg(args, char*); 33608c2ecf20Sopenharmony_ci if (field_width == -1) 33618c2ecf20Sopenharmony_ci field_width = 1; 33628c2ecf20Sopenharmony_ci do { 33638c2ecf20Sopenharmony_ci *s++ = *str++; 33648c2ecf20Sopenharmony_ci } while (--field_width > 0 && *str); 33658c2ecf20Sopenharmony_ci num++; 33668c2ecf20Sopenharmony_ci } 33678c2ecf20Sopenharmony_ci continue; 33688c2ecf20Sopenharmony_ci case 's': 33698c2ecf20Sopenharmony_ci { 33708c2ecf20Sopenharmony_ci char *s = (char *)va_arg(args, char *); 33718c2ecf20Sopenharmony_ci if (field_width == -1) 33728c2ecf20Sopenharmony_ci field_width = SHRT_MAX; 33738c2ecf20Sopenharmony_ci /* first, skip leading white space in buffer */ 33748c2ecf20Sopenharmony_ci str = skip_spaces(str); 33758c2ecf20Sopenharmony_ci 33768c2ecf20Sopenharmony_ci /* now copy until next white space */ 33778c2ecf20Sopenharmony_ci while (*str && !isspace(*str) && field_width--) 33788c2ecf20Sopenharmony_ci *s++ = *str++; 33798c2ecf20Sopenharmony_ci *s = '\0'; 33808c2ecf20Sopenharmony_ci num++; 33818c2ecf20Sopenharmony_ci } 33828c2ecf20Sopenharmony_ci continue; 33838c2ecf20Sopenharmony_ci /* 33848c2ecf20Sopenharmony_ci * Warning: This implementation of the '[' conversion specifier 33858c2ecf20Sopenharmony_ci * deviates from its glibc counterpart in the following ways: 33868c2ecf20Sopenharmony_ci * (1) It does NOT support ranges i.e. '-' is NOT a special 33878c2ecf20Sopenharmony_ci * character 33888c2ecf20Sopenharmony_ci * (2) It cannot match the closing bracket ']' itself 33898c2ecf20Sopenharmony_ci * (3) A field width is required 33908c2ecf20Sopenharmony_ci * (4) '%*[' (discard matching input) is currently not supported 33918c2ecf20Sopenharmony_ci * 33928c2ecf20Sopenharmony_ci * Example usage: 33938c2ecf20Sopenharmony_ci * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 33948c2ecf20Sopenharmony_ci * buf1, buf2, buf3); 33958c2ecf20Sopenharmony_ci * if (ret < 3) 33968c2ecf20Sopenharmony_ci * // etc.. 33978c2ecf20Sopenharmony_ci */ 33988c2ecf20Sopenharmony_ci case '[': 33998c2ecf20Sopenharmony_ci { 34008c2ecf20Sopenharmony_ci char *s = (char *)va_arg(args, char *); 34018c2ecf20Sopenharmony_ci DECLARE_BITMAP(set, 256) = {0}; 34028c2ecf20Sopenharmony_ci unsigned int len = 0; 34038c2ecf20Sopenharmony_ci bool negate = (*fmt == '^'); 34048c2ecf20Sopenharmony_ci 34058c2ecf20Sopenharmony_ci /* field width is required */ 34068c2ecf20Sopenharmony_ci if (field_width == -1) 34078c2ecf20Sopenharmony_ci return num; 34088c2ecf20Sopenharmony_ci 34098c2ecf20Sopenharmony_ci if (negate) 34108c2ecf20Sopenharmony_ci ++fmt; 34118c2ecf20Sopenharmony_ci 34128c2ecf20Sopenharmony_ci for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 34138c2ecf20Sopenharmony_ci set_bit((u8)*fmt, set); 34148c2ecf20Sopenharmony_ci 34158c2ecf20Sopenharmony_ci /* no ']' or no character set found */ 34168c2ecf20Sopenharmony_ci if (!*fmt || !len) 34178c2ecf20Sopenharmony_ci return num; 34188c2ecf20Sopenharmony_ci ++fmt; 34198c2ecf20Sopenharmony_ci 34208c2ecf20Sopenharmony_ci if (negate) { 34218c2ecf20Sopenharmony_ci bitmap_complement(set, set, 256); 34228c2ecf20Sopenharmony_ci /* exclude null '\0' byte */ 34238c2ecf20Sopenharmony_ci clear_bit(0, set); 34248c2ecf20Sopenharmony_ci } 34258c2ecf20Sopenharmony_ci 34268c2ecf20Sopenharmony_ci /* match must be non-empty */ 34278c2ecf20Sopenharmony_ci if (!test_bit((u8)*str, set)) 34288c2ecf20Sopenharmony_ci return num; 34298c2ecf20Sopenharmony_ci 34308c2ecf20Sopenharmony_ci while (test_bit((u8)*str, set) && field_width--) 34318c2ecf20Sopenharmony_ci *s++ = *str++; 34328c2ecf20Sopenharmony_ci *s = '\0'; 34338c2ecf20Sopenharmony_ci ++num; 34348c2ecf20Sopenharmony_ci } 34358c2ecf20Sopenharmony_ci continue; 34368c2ecf20Sopenharmony_ci case 'o': 34378c2ecf20Sopenharmony_ci base = 8; 34388c2ecf20Sopenharmony_ci break; 34398c2ecf20Sopenharmony_ci case 'x': 34408c2ecf20Sopenharmony_ci case 'X': 34418c2ecf20Sopenharmony_ci base = 16; 34428c2ecf20Sopenharmony_ci break; 34438c2ecf20Sopenharmony_ci case 'i': 34448c2ecf20Sopenharmony_ci base = 0; 34458c2ecf20Sopenharmony_ci /* fall through */ 34468c2ecf20Sopenharmony_ci case 'd': 34478c2ecf20Sopenharmony_ci is_sign = true; 34488c2ecf20Sopenharmony_ci /* fall through */ 34498c2ecf20Sopenharmony_ci case 'u': 34508c2ecf20Sopenharmony_ci break; 34518c2ecf20Sopenharmony_ci case '%': 34528c2ecf20Sopenharmony_ci /* looking for '%' in str */ 34538c2ecf20Sopenharmony_ci if (*str++ != '%') 34548c2ecf20Sopenharmony_ci return num; 34558c2ecf20Sopenharmony_ci continue; 34568c2ecf20Sopenharmony_ci default: 34578c2ecf20Sopenharmony_ci /* invalid format; stop here */ 34588c2ecf20Sopenharmony_ci return num; 34598c2ecf20Sopenharmony_ci } 34608c2ecf20Sopenharmony_ci 34618c2ecf20Sopenharmony_ci /* have some sort of integer conversion. 34628c2ecf20Sopenharmony_ci * first, skip white space in buffer. 34638c2ecf20Sopenharmony_ci */ 34648c2ecf20Sopenharmony_ci str = skip_spaces(str); 34658c2ecf20Sopenharmony_ci 34668c2ecf20Sopenharmony_ci digit = *str; 34678c2ecf20Sopenharmony_ci if (is_sign && digit == '-') 34688c2ecf20Sopenharmony_ci digit = *(str + 1); 34698c2ecf20Sopenharmony_ci 34708c2ecf20Sopenharmony_ci if (!digit 34718c2ecf20Sopenharmony_ci || (base == 16 && !isxdigit(digit)) 34728c2ecf20Sopenharmony_ci || (base == 10 && !isdigit(digit)) 34738c2ecf20Sopenharmony_ci || (base == 8 && (!isdigit(digit) || digit > '7')) 34748c2ecf20Sopenharmony_ci || (base == 0 && !isdigit(digit))) 34758c2ecf20Sopenharmony_ci break; 34768c2ecf20Sopenharmony_ci 34778c2ecf20Sopenharmony_ci if (is_sign) 34788c2ecf20Sopenharmony_ci val.s = simple_strntoll(str, 34798c2ecf20Sopenharmony_ci field_width >= 0 ? field_width : INT_MAX, 34808c2ecf20Sopenharmony_ci &next, base); 34818c2ecf20Sopenharmony_ci else 34828c2ecf20Sopenharmony_ci val.u = simple_strntoull(str, 34838c2ecf20Sopenharmony_ci field_width >= 0 ? field_width : INT_MAX, 34848c2ecf20Sopenharmony_ci &next, base); 34858c2ecf20Sopenharmony_ci 34868c2ecf20Sopenharmony_ci switch (qualifier) { 34878c2ecf20Sopenharmony_ci case 'H': /* that's 'hh' in format */ 34888c2ecf20Sopenharmony_ci if (is_sign) 34898c2ecf20Sopenharmony_ci *va_arg(args, signed char *) = val.s; 34908c2ecf20Sopenharmony_ci else 34918c2ecf20Sopenharmony_ci *va_arg(args, unsigned char *) = val.u; 34928c2ecf20Sopenharmony_ci break; 34938c2ecf20Sopenharmony_ci case 'h': 34948c2ecf20Sopenharmony_ci if (is_sign) 34958c2ecf20Sopenharmony_ci *va_arg(args, short *) = val.s; 34968c2ecf20Sopenharmony_ci else 34978c2ecf20Sopenharmony_ci *va_arg(args, unsigned short *) = val.u; 34988c2ecf20Sopenharmony_ci break; 34998c2ecf20Sopenharmony_ci case 'l': 35008c2ecf20Sopenharmony_ci if (is_sign) 35018c2ecf20Sopenharmony_ci *va_arg(args, long *) = val.s; 35028c2ecf20Sopenharmony_ci else 35038c2ecf20Sopenharmony_ci *va_arg(args, unsigned long *) = val.u; 35048c2ecf20Sopenharmony_ci break; 35058c2ecf20Sopenharmony_ci case 'L': 35068c2ecf20Sopenharmony_ci if (is_sign) 35078c2ecf20Sopenharmony_ci *va_arg(args, long long *) = val.s; 35088c2ecf20Sopenharmony_ci else 35098c2ecf20Sopenharmony_ci *va_arg(args, unsigned long long *) = val.u; 35108c2ecf20Sopenharmony_ci break; 35118c2ecf20Sopenharmony_ci case 'z': 35128c2ecf20Sopenharmony_ci *va_arg(args, size_t *) = val.u; 35138c2ecf20Sopenharmony_ci break; 35148c2ecf20Sopenharmony_ci default: 35158c2ecf20Sopenharmony_ci if (is_sign) 35168c2ecf20Sopenharmony_ci *va_arg(args, int *) = val.s; 35178c2ecf20Sopenharmony_ci else 35188c2ecf20Sopenharmony_ci *va_arg(args, unsigned int *) = val.u; 35198c2ecf20Sopenharmony_ci break; 35208c2ecf20Sopenharmony_ci } 35218c2ecf20Sopenharmony_ci num++; 35228c2ecf20Sopenharmony_ci 35238c2ecf20Sopenharmony_ci if (!next) 35248c2ecf20Sopenharmony_ci break; 35258c2ecf20Sopenharmony_ci str = next; 35268c2ecf20Sopenharmony_ci } 35278c2ecf20Sopenharmony_ci 35288c2ecf20Sopenharmony_ci return num; 35298c2ecf20Sopenharmony_ci} 35308c2ecf20Sopenharmony_ciEXPORT_SYMBOL(vsscanf); 35318c2ecf20Sopenharmony_ci 35328c2ecf20Sopenharmony_ci/** 35338c2ecf20Sopenharmony_ci * sscanf - Unformat a buffer into a list of arguments 35348c2ecf20Sopenharmony_ci * @buf: input buffer 35358c2ecf20Sopenharmony_ci * @fmt: formatting of buffer 35368c2ecf20Sopenharmony_ci * @...: resulting arguments 35378c2ecf20Sopenharmony_ci */ 35388c2ecf20Sopenharmony_ciint sscanf(const char *buf, const char *fmt, ...) 35398c2ecf20Sopenharmony_ci{ 35408c2ecf20Sopenharmony_ci va_list args; 35418c2ecf20Sopenharmony_ci int i; 35428c2ecf20Sopenharmony_ci 35438c2ecf20Sopenharmony_ci va_start(args, fmt); 35448c2ecf20Sopenharmony_ci i = vsscanf(buf, fmt, args); 35458c2ecf20Sopenharmony_ci va_end(args); 35468c2ecf20Sopenharmony_ci 35478c2ecf20Sopenharmony_ci return i; 35488c2ecf20Sopenharmony_ci} 35498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(sscanf); 3550