vsprintf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * linux/lib/vsprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * (C) Copyright 2000-2009
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. */
  8. /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
  9. /*
  10. * Wirzenius wrote this portably, Torvalds fucked it up :-)
  11. *
  12. * from hush: simple_itoa() was lifted from boa-0.93.15
  13. */
  14. #include <common.h>
  15. #include <charset.h>
  16. #include <efi_loader.h>
  17. #include <div64.h>
  18. #include <hexdump.h>
  19. #include <stdarg.h>
  20. #include <uuid.h>
  21. #include <vsprintf.h>
  22. #include <linux/ctype.h>
  23. #include <linux/err.h>
  24. #include <linux/types.h>
  25. #include <linux/string.h>
  26. /* we use this so that we can do without the ctype library */
  27. #define is_digit(c) ((c) >= '0' && (c) <= '9')
  28. static int skip_atoi(const char **s)
  29. {
  30. int i = 0;
  31. while (is_digit(**s))
  32. i = i * 10 + *((*s)++) - '0';
  33. return i;
  34. }
  35. /* Decimal conversion is by far the most typical, and is used
  36. * for /proc and /sys data. This directly impacts e.g. top performance
  37. * with many processes running. We optimize it for speed
  38. * using code from
  39. * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
  40. * (with permission from the author, Douglas W. Jones). */
  41. /* Formats correctly any integer in [0,99999].
  42. * Outputs from one to five digits depending on input.
  43. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
  44. static char *put_dec_trunc(char *buf, unsigned q)
  45. {
  46. unsigned d3, d2, d1, d0;
  47. d1 = (q>>4) & 0xf;
  48. d2 = (q>>8) & 0xf;
  49. d3 = (q>>12);
  50. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  51. q = (d0 * 0xcd) >> 11;
  52. d0 = d0 - 10*q;
  53. *buf++ = d0 + '0'; /* least significant digit */
  54. d1 = q + 9*d3 + 5*d2 + d1;
  55. if (d1 != 0) {
  56. q = (d1 * 0xcd) >> 11;
  57. d1 = d1 - 10*q;
  58. *buf++ = d1 + '0'; /* next digit */
  59. d2 = q + 2*d2;
  60. if ((d2 != 0) || (d3 != 0)) {
  61. q = (d2 * 0xd) >> 7;
  62. d2 = d2 - 10*q;
  63. *buf++ = d2 + '0'; /* next digit */
  64. d3 = q + 4*d3;
  65. if (d3 != 0) {
  66. q = (d3 * 0xcd) >> 11;
  67. d3 = d3 - 10*q;
  68. *buf++ = d3 + '0'; /* next digit */
  69. if (q != 0)
  70. *buf++ = q + '0'; /* most sign. digit */
  71. }
  72. }
  73. }
  74. return buf;
  75. }
  76. /* Same with if's removed. Always emits five digits */
  77. static char *put_dec_full(char *buf, unsigned q)
  78. {
  79. /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
  80. /* but anyway, gcc produces better code with full-sized ints */
  81. unsigned d3, d2, d1, d0;
  82. d1 = (q>>4) & 0xf;
  83. d2 = (q>>8) & 0xf;
  84. d3 = (q>>12);
  85. /*
  86. * Possible ways to approx. divide by 10
  87. * gcc -O2 replaces multiply with shifts and adds
  88. * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
  89. * (x * 0x67) >> 10: 1100111
  90. * (x * 0x34) >> 9: 110100 - same
  91. * (x * 0x1a) >> 8: 11010 - same
  92. * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
  93. */
  94. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  95. q = (d0 * 0xcd) >> 11;
  96. d0 = d0 - 10*q;
  97. *buf++ = d0 + '0';
  98. d1 = q + 9*d3 + 5*d2 + d1;
  99. q = (d1 * 0xcd) >> 11;
  100. d1 = d1 - 10*q;
  101. *buf++ = d1 + '0';
  102. d2 = q + 2*d2;
  103. q = (d2 * 0xd) >> 7;
  104. d2 = d2 - 10*q;
  105. *buf++ = d2 + '0';
  106. d3 = q + 4*d3;
  107. q = (d3 * 0xcd) >> 11; /* - shorter code */
  108. /* q = (d3 * 0x67) >> 10; - would also work */
  109. d3 = d3 - 10*q;
  110. *buf++ = d3 + '0';
  111. *buf++ = q + '0';
  112. return buf;
  113. }
  114. /* No inlining helps gcc to use registers better */
  115. static noinline char *put_dec(char *buf, uint64_t num)
  116. {
  117. while (1) {
  118. unsigned rem;
  119. if (num < 100000)
  120. return put_dec_trunc(buf, num);
  121. rem = do_div(num, 100000);
  122. buf = put_dec_full(buf, rem);
  123. }
  124. }
  125. #define ZEROPAD 1 /* pad with zero */
  126. #define SIGN 2 /* unsigned/signed long */
  127. #define PLUS 4 /* show plus */
  128. #define SPACE 8 /* space if plus */
  129. #define LEFT 16 /* left justified */
  130. #define SMALL 32 /* Must be 32 == 0x20 */
  131. #define SPECIAL 64 /* 0x */
  132. #define ERRSTR 128 /* %dE showing error string if enabled */
  133. /*
  134. * Macro to add a new character to our output string, but only if it will
  135. * fit. The macro moves to the next character position in the output string.
  136. */
  137. #define ADDCH(str, ch) do { \
  138. if ((str) < end) \
  139. *(str) = (ch); \
  140. ++str; \
  141. } while (0)
  142. static char *number(char *buf, char *end, u64 num,
  143. int base, int size, int precision, int type)
  144. {
  145. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  146. static const char digits[16] = "0123456789ABCDEF";
  147. char tmp[66];
  148. char sign;
  149. char locase;
  150. int need_pfx = ((type & SPECIAL) && base != 10);
  151. int i;
  152. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  153. * produces same digits or (maybe lowercased) letters */
  154. locase = (type & SMALL);
  155. if (type & LEFT)
  156. type &= ~ZEROPAD;
  157. sign = 0;
  158. if (type & SIGN) {
  159. if ((s64) num < 0) {
  160. sign = '-';
  161. num = -(s64) num;
  162. size--;
  163. } else if (type & PLUS) {
  164. sign = '+';
  165. size--;
  166. } else if (type & SPACE) {
  167. sign = ' ';
  168. size--;
  169. }
  170. }
  171. if (need_pfx) {
  172. size--;
  173. if (base == 16)
  174. size--;
  175. }
  176. /* generate full string in tmp[], in reverse order */
  177. i = 0;
  178. if (num == 0)
  179. tmp[i++] = '0';
  180. /* Generic code, for any base:
  181. else do {
  182. tmp[i++] = (digits[do_div(num,base)] | locase);
  183. } while (num != 0);
  184. */
  185. else if (base != 10) { /* 8 or 16 */
  186. int mask = base - 1;
  187. int shift = 3;
  188. if (base == 16)
  189. shift = 4;
  190. do {
  191. tmp[i++] = (digits[((unsigned char)num) & mask]
  192. | locase);
  193. num >>= shift;
  194. } while (num);
  195. } else { /* base 10 */
  196. i = put_dec(tmp, num) - tmp;
  197. }
  198. /* printing 100 using %2d gives "100", not "00" */
  199. if (i > precision)
  200. precision = i;
  201. /* leading space padding */
  202. size -= precision;
  203. if (!(type & (ZEROPAD + LEFT))) {
  204. while (--size >= 0)
  205. ADDCH(buf, ' ');
  206. }
  207. /* sign */
  208. if (sign)
  209. ADDCH(buf, sign);
  210. /* "0x" / "0" prefix */
  211. if (need_pfx) {
  212. ADDCH(buf, '0');
  213. if (base == 16)
  214. ADDCH(buf, 'X' | locase);
  215. }
  216. /* zero or space padding */
  217. if (!(type & LEFT)) {
  218. char c = (type & ZEROPAD) ? '0' : ' ';
  219. while (--size >= 0)
  220. ADDCH(buf, c);
  221. }
  222. /* hmm even more zero padding? */
  223. while (i <= --precision)
  224. ADDCH(buf, '0');
  225. /* actual digits of result */
  226. while (--i >= 0)
  227. ADDCH(buf, tmp[i]);
  228. /* trailing space padding */
  229. while (--size >= 0)
  230. ADDCH(buf, ' ');
  231. return buf;
  232. }
  233. static char *string(char *buf, char *end, const char *s, int field_width,
  234. int precision, int flags)
  235. {
  236. int len, i;
  237. if (s == NULL)
  238. s = "<NULL>";
  239. len = strnlen(s, precision);
  240. if (!(flags & LEFT))
  241. while (len < field_width--)
  242. ADDCH(buf, ' ');
  243. for (i = 0; i < len; ++i)
  244. ADDCH(buf, *s++);
  245. while (len < field_width--)
  246. ADDCH(buf, ' ');
  247. return buf;
  248. }
  249. /* U-Boot uses UTF-16 strings in the EFI context only. */
  250. static __maybe_unused char *string16(char *buf, char *end, u16 *s,
  251. int field_width, int precision, int flags)
  252. {
  253. const u16 *str = s ? s : u"<NULL>";
  254. ssize_t i, len = utf16_strnlen(str, precision);
  255. if (!(flags & LEFT))
  256. for (; len < field_width; --field_width)
  257. ADDCH(buf, ' ');
  258. if (buf < end)
  259. *buf = 0;
  260. for (i = 0; i < len; ++i) {
  261. int slen = utf16_utf8_strnlen(str, 1);
  262. s32 s = utf16_get(&str);
  263. if (s < 0)
  264. s = '?';
  265. if (buf + slen < end) {
  266. utf8_put(s, &buf);
  267. if (buf < end)
  268. *buf = 0;
  269. } else {
  270. buf += slen;
  271. }
  272. }
  273. for (; len < field_width; --field_width)
  274. ADDCH(buf, ' ');
  275. return buf;
  276. }
  277. #if CONFIG_IS_ENABLED(EFI_DEVICE_PATH_TO_TEXT)
  278. static char *device_path_string(char *buf, char *end, void *dp, int field_width,
  279. int precision, int flags)
  280. {
  281. u16 *str;
  282. /* If dp == NULL output the string '<NULL>' */
  283. if (!dp)
  284. return string16(buf, end, dp, field_width, precision, flags);
  285. str = efi_dp_str((struct efi_device_path *)dp);
  286. if (!str)
  287. return ERR_PTR(-ENOMEM);
  288. buf = string16(buf, end, str, field_width, precision, flags);
  289. efi_free_pool(str);
  290. return buf;
  291. }
  292. #endif
  293. static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
  294. int precision, int flags)
  295. {
  296. /* (6 * 2 hex digits), 5 colons and trailing zero */
  297. char mac_addr[6 * 3];
  298. char *p = mac_addr;
  299. int i;
  300. for (i = 0; i < 6; i++) {
  301. p = hex_byte_pack(p, addr[i]);
  302. if (!(flags & SPECIAL) && i != 5)
  303. *p++ = ':';
  304. }
  305. *p = '\0';
  306. return string(buf, end, mac_addr, field_width, precision,
  307. flags & ~SPECIAL);
  308. }
  309. static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
  310. int precision, int flags)
  311. {
  312. /* (8 * 4 hex digits), 7 colons and trailing zero */
  313. char ip6_addr[8 * 5];
  314. char *p = ip6_addr;
  315. int i;
  316. for (i = 0; i < 8; i++) {
  317. p = hex_byte_pack(p, addr[2 * i]);
  318. p = hex_byte_pack(p, addr[2 * i + 1]);
  319. if (!(flags & SPECIAL) && i != 7)
  320. *p++ = ':';
  321. }
  322. *p = '\0';
  323. return string(buf, end, ip6_addr, field_width, precision,
  324. flags & ~SPECIAL);
  325. }
  326. static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
  327. int precision, int flags)
  328. {
  329. /* (4 * 3 decimal digits), 3 dots and trailing zero */
  330. char ip4_addr[4 * 4];
  331. char temp[3]; /* hold each IP quad in reverse order */
  332. char *p = ip4_addr;
  333. int i, digits;
  334. for (i = 0; i < 4; i++) {
  335. digits = put_dec_trunc(temp, addr[i]) - temp;
  336. /* reverse the digits in the quad */
  337. while (digits--)
  338. *p++ = temp[digits];
  339. if (i != 3)
  340. *p++ = '.';
  341. }
  342. *p = '\0';
  343. return string(buf, end, ip4_addr, field_width, precision,
  344. flags & ~SPECIAL);
  345. }
  346. #ifdef CONFIG_LIB_UUID
  347. /*
  348. * This works (roughly) the same way as Linux's.
  349. *
  350. * %pUb: 01020304-0506-0708-090a-0b0c0d0e0f10
  351. * %pUB: 01020304-0506-0708-090A-0B0C0D0E0F10
  352. * %pUl: 04030201-0605-0807-090a-0b0c0d0e0f10
  353. * %pUL: 04030201-0605-0807-090A-0B0C0D0E0F10
  354. * %pUs: GUID text representation if known or fallback to %pUl
  355. */
  356. static char *uuid_string(char *buf, char *end, u8 *addr, int field_width,
  357. int precision, int flags, const char *fmt)
  358. {
  359. char uuid[UUID_STR_LEN + 1];
  360. int str_format;
  361. const char *str;
  362. switch (*(++fmt)) {
  363. case 'L':
  364. str_format = UUID_STR_FORMAT_GUID | UUID_STR_UPPER_CASE;
  365. break;
  366. case 'l':
  367. str_format = UUID_STR_FORMAT_GUID;
  368. break;
  369. case 'B':
  370. str_format = UUID_STR_FORMAT_STD | UUID_STR_UPPER_CASE;
  371. break;
  372. case 's':
  373. str = uuid_guid_get_str(addr);
  374. if (str)
  375. return string(buf, end, str,
  376. field_width, precision, flags);
  377. str_format = UUID_STR_FORMAT_GUID;
  378. break;
  379. default:
  380. str_format = UUID_STR_FORMAT_STD;
  381. break;
  382. }
  383. if (addr)
  384. uuid_bin_to_str(addr, uuid, str_format);
  385. else
  386. strcpy(uuid, "<NULL>");
  387. return string(buf, end, uuid, field_width, precision, flags);
  388. }
  389. #endif
  390. /*
  391. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  392. * by an extra set of alphanumeric characters that are extended format
  393. * specifiers.
  394. *
  395. * Right now we handle:
  396. *
  397. * - 'M' For a 6-byte MAC address, it prints the address in the
  398. * usual colon-separated hex notation
  399. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  400. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  401. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  402. * currently the same
  403. */
  404. static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  405. int field_width, int precision, int flags)
  406. {
  407. u64 num = (uintptr_t)ptr;
  408. /*
  409. * Being a boot loader, we explicitly allow pointers to
  410. * (physical) address null.
  411. */
  412. #if 0
  413. if (!ptr)
  414. return string(buf, end, "(null)", field_width, precision,
  415. flags);
  416. #endif
  417. switch (*fmt) {
  418. /* Device paths only exist in the EFI context. */
  419. #if CONFIG_IS_ENABLED(EFI_DEVICE_PATH_TO_TEXT) && !defined(API_BUILD)
  420. case 'D':
  421. return device_path_string(buf, end, ptr, field_width,
  422. precision, flags);
  423. #endif
  424. case 'a':
  425. flags |= SPECIAL | ZEROPAD;
  426. switch (fmt[1]) {
  427. case 'p':
  428. default:
  429. field_width = sizeof(phys_addr_t) * 2 + 2;
  430. num = *(phys_addr_t *)ptr;
  431. break;
  432. }
  433. break;
  434. case 'm':
  435. flags |= SPECIAL;
  436. /* Fallthrough */
  437. case 'M':
  438. return mac_address_string(buf, end, ptr, field_width,
  439. precision, flags);
  440. case 'i':
  441. flags |= SPECIAL;
  442. /* Fallthrough */
  443. case 'I':
  444. if (IS_ENABLED(CONFIG_IPV6) && fmt[1] == '6')
  445. return ip6_addr_string(buf, end, ptr, field_width,
  446. precision, flags);
  447. if (fmt[1] == '4')
  448. return ip4_addr_string(buf, end, ptr, field_width,
  449. precision, flags);
  450. flags &= ~SPECIAL;
  451. break;
  452. #ifdef CONFIG_LIB_UUID
  453. case 'U':
  454. return uuid_string(buf, end, ptr, field_width, precision,
  455. flags, fmt);
  456. #endif
  457. default:
  458. break;
  459. }
  460. flags |= SMALL;
  461. if (field_width == -1) {
  462. field_width = 2*sizeof(void *);
  463. flags |= ZEROPAD;
  464. }
  465. return number(buf, end, num, 16, field_width, precision, flags);
  466. }
  467. static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
  468. va_list args)
  469. {
  470. u64 num;
  471. int base;
  472. char *str;
  473. int flags; /* flags to number() */
  474. int field_width; /* width of output field */
  475. int precision; /* min. # of digits for integers; max
  476. number of chars for from string */
  477. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  478. /* 'z' support added 23/7/1999 S.H. */
  479. /* 'z' changed to 'Z' --davidm 1/25/99 */
  480. /* 't' added for ptrdiff_t */
  481. char *end = buf + size;
  482. /* Make sure end is always >= buf - do we want this in U-Boot? */
  483. if (end < buf) {
  484. end = ((void *)-1);
  485. size = end - buf;
  486. }
  487. str = buf;
  488. for (; *fmt ; ++fmt) {
  489. if (*fmt != '%') {
  490. ADDCH(str, *fmt);
  491. continue;
  492. }
  493. /* process flags */
  494. flags = 0;
  495. repeat:
  496. ++fmt; /* this also skips first '%' */
  497. switch (*fmt) {
  498. case '-':
  499. flags |= LEFT;
  500. goto repeat;
  501. case '+':
  502. flags |= PLUS;
  503. goto repeat;
  504. case ' ':
  505. flags |= SPACE;
  506. goto repeat;
  507. case '#':
  508. flags |= SPECIAL;
  509. goto repeat;
  510. case '0':
  511. flags |= ZEROPAD;
  512. goto repeat;
  513. }
  514. /* get field width */
  515. field_width = -1;
  516. if (is_digit(*fmt))
  517. field_width = skip_atoi(&fmt);
  518. else if (*fmt == '*') {
  519. ++fmt;
  520. /* it's the next argument */
  521. field_width = va_arg(args, int);
  522. if (field_width < 0) {
  523. field_width = -field_width;
  524. flags |= LEFT;
  525. }
  526. }
  527. /* get the precision */
  528. precision = -1;
  529. if (*fmt == '.') {
  530. ++fmt;
  531. if (is_digit(*fmt))
  532. precision = skip_atoi(&fmt);
  533. else if (*fmt == '*') {
  534. ++fmt;
  535. /* it's the next argument */
  536. precision = va_arg(args, int);
  537. }
  538. if (precision < 0)
  539. precision = 0;
  540. }
  541. /* get the conversion qualifier */
  542. qualifier = -1;
  543. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  544. *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
  545. qualifier = *fmt;
  546. ++fmt;
  547. if (qualifier == 'l' && *fmt == 'l') {
  548. qualifier = 'L';
  549. ++fmt;
  550. }
  551. }
  552. /* default base */
  553. base = 10;
  554. switch (*fmt) {
  555. case 'c':
  556. if (!(flags & LEFT)) {
  557. while (--field_width > 0)
  558. ADDCH(str, ' ');
  559. }
  560. ADDCH(str, (unsigned char) va_arg(args, int));
  561. while (--field_width > 0)
  562. ADDCH(str, ' ');
  563. continue;
  564. case 's':
  565. /* U-Boot uses UTF-16 strings in the EFI context only. */
  566. #if (CONFIG_IS_ENABLED(EFI_LOADER) || IS_ENABLED(CONFIG_EFI_APP)) && \
  567. !defined(API_BUILD)
  568. if (qualifier == 'l') {
  569. str = string16(str, end, va_arg(args, u16 *),
  570. field_width, precision, flags);
  571. } else
  572. #endif
  573. {
  574. str = string(str, end, va_arg(args, char *),
  575. field_width, precision, flags);
  576. }
  577. continue;
  578. case 'p':
  579. str = pointer(fmt + 1, str, end,
  580. va_arg(args, void *),
  581. field_width, precision, flags);
  582. if (IS_ERR(str))
  583. return PTR_ERR(str);
  584. /* Skip all alphanumeric pointer suffixes */
  585. while (isalnum(fmt[1]))
  586. fmt++;
  587. continue;
  588. case 'n':
  589. if (qualifier == 'l') {
  590. long *ip = va_arg(args, long *);
  591. *ip = (str - buf);
  592. } else {
  593. int *ip = va_arg(args, int *);
  594. *ip = (str - buf);
  595. }
  596. continue;
  597. case '%':
  598. ADDCH(str, '%');
  599. continue;
  600. /* integer number formats - set up the flags and "break" */
  601. case 'o':
  602. base = 8;
  603. break;
  604. case 'x':
  605. flags |= SMALL;
  606. /* fallthrough */
  607. case 'X':
  608. base = 16;
  609. break;
  610. case 'd':
  611. if (fmt[1] == 'E') {
  612. flags |= ERRSTR;
  613. fmt++;
  614. }
  615. /* fallthrough */
  616. case 'i':
  617. flags |= SIGN;
  618. /* fallthrough */
  619. case 'u':
  620. break;
  621. default:
  622. ADDCH(str, '%');
  623. if (*fmt)
  624. ADDCH(str, *fmt);
  625. else
  626. --fmt;
  627. continue;
  628. }
  629. if (qualifier == 'L') /* "quad" for 64 bit variables */
  630. num = va_arg(args, unsigned long long);
  631. else if (qualifier == 'l') {
  632. num = va_arg(args, unsigned long);
  633. if (flags & SIGN)
  634. num = (signed long) num;
  635. } else if (qualifier == 'Z' || qualifier == 'z') {
  636. num = va_arg(args, size_t);
  637. } else if (qualifier == 't') {
  638. num = va_arg(args, ptrdiff_t);
  639. } else if (qualifier == 'h') {
  640. num = (unsigned short) va_arg(args, int);
  641. if (flags & SIGN)
  642. num = (signed short) num;
  643. } else {
  644. num = va_arg(args, unsigned int);
  645. if (flags & SIGN)
  646. num = (signed int) num;
  647. }
  648. str = number(str, end, num, base, field_width, precision,
  649. flags);
  650. if (IS_ENABLED(CONFIG_ERRNO_STR) && (flags & ERRSTR)) {
  651. const char *p;
  652. ADDCH(str, ':');
  653. ADDCH(str, ' ');
  654. for (p = errno_str(num); *p; p++)
  655. ADDCH(str, *p);
  656. }
  657. }
  658. if (size > 0) {
  659. ADDCH(str, '\0');
  660. if (str > end)
  661. end[-1] = '\0';
  662. --str;
  663. }
  664. /* the trailing null byte doesn't count towards the total */
  665. return str - buf;
  666. }
  667. int vsnprintf(char *buf, size_t size, const char *fmt,
  668. va_list args)
  669. {
  670. return vsnprintf_internal(buf, size, fmt, args);
  671. }
  672. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  673. {
  674. int i;
  675. i = vsnprintf(buf, size, fmt, args);
  676. if (likely(i < size))
  677. return i;
  678. if (size != 0)
  679. return size - 1;
  680. return 0;
  681. }
  682. int snprintf(char *buf, size_t size, const char *fmt, ...)
  683. {
  684. va_list args;
  685. int i;
  686. va_start(args, fmt);
  687. i = vsnprintf(buf, size, fmt, args);
  688. va_end(args);
  689. return i;
  690. }
  691. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  692. {
  693. va_list args;
  694. int i;
  695. va_start(args, fmt);
  696. i = vscnprintf(buf, size, fmt, args);
  697. va_end(args);
  698. return i;
  699. }
  700. /**
  701. * Format a string and place it in a buffer (va_list version)
  702. *
  703. * @param buf The buffer to place the result into
  704. * @param fmt The format string to use
  705. * @param args Arguments for the format string
  706. *
  707. * The function returns the number of characters written
  708. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  709. * buffer overflows.
  710. *
  711. * If you're not already dealing with a va_list consider using sprintf().
  712. */
  713. int vsprintf(char *buf, const char *fmt, va_list args)
  714. {
  715. return vsnprintf_internal(buf, INT_MAX, fmt, args);
  716. }
  717. int sprintf(char *buf, const char *fmt, ...)
  718. {
  719. va_list args;
  720. int i;
  721. va_start(args, fmt);
  722. i = vsprintf(buf, fmt, args);
  723. va_end(args);
  724. return i;
  725. }
  726. #if CONFIG_IS_ENABLED(PRINTF)
  727. int printf(const char *fmt, ...)
  728. {
  729. va_list args;
  730. uint i;
  731. va_start(args, fmt);
  732. i = vprintf(fmt, args);
  733. va_end(args);
  734. return i;
  735. }
  736. int vprintf(const char *fmt, va_list args)
  737. {
  738. uint i;
  739. char printbuffer[CONFIG_SYS_PBSIZE];
  740. /*
  741. * For this to work, printbuffer must be larger than
  742. * anything we ever want to print.
  743. */
  744. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  745. /* Handle error */
  746. if (i <= 0)
  747. return i;
  748. /* Print the string */
  749. puts(printbuffer);
  750. return i;
  751. }
  752. #endif
  753. static char local_toa[22];
  754. char *simple_itoa(ulong i)
  755. {
  756. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  757. char *p = &local_toa[21];
  758. *p-- = '\0';
  759. do {
  760. *p-- = '0' + i % 10;
  761. i /= 10;
  762. } while (i > 0);
  763. return p + 1;
  764. }
  765. char *simple_xtoa(ulong num)
  766. {
  767. /* 16 digits plus nul terminator, good for 64-bit or smaller ints */
  768. char *p = &local_toa[17];
  769. *--p = '\0';
  770. do {
  771. p -= 2;
  772. hex_byte_pack(p, num & 0xff);
  773. num >>= 8;
  774. } while (num > 0);
  775. return p;
  776. }
  777. /* We don't seem to have %'d in U-Boot */
  778. void print_grouped_ull(unsigned long long int_val, int digits)
  779. {
  780. char str[21], *s;
  781. int grab = 3;
  782. digits = (digits + 2) / 3;
  783. sprintf(str, "%*llu", digits * 3, int_val);
  784. for (s = str; *s; s += grab) {
  785. if (s != str)
  786. putc(s[-1] != ' ' ? ',' : ' ');
  787. printf("%.*s", grab, s);
  788. grab = 3;
  789. }
  790. }
  791. bool str2off(const char *p, loff_t *num)
  792. {
  793. char *endptr;
  794. *num = simple_strtoull(p, &endptr, 16);
  795. return *p != '\0' && *endptr == '\0';
  796. }
  797. bool str2long(const char *p, ulong *num)
  798. {
  799. char *endptr;
  800. *num = hextoul(p, &endptr);
  801. return *p != '\0' && *endptr == '\0';
  802. }
  803. char *strmhz(char *buf, unsigned long hz)
  804. {
  805. long l, n;
  806. long m;
  807. n = DIV_ROUND_CLOSEST(hz, 1000) / 1000L;
  808. l = sprintf(buf, "%ld", n);
  809. hz -= n * 1000000L;
  810. m = DIV_ROUND_CLOSEST(hz, 1000L);
  811. if (m != 0)
  812. sprintf(buf + l, ".%03ld", m);
  813. return buf;
  814. }