vsprintf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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. /*
  133. * Macro to add a new character to our output string, but only if it will
  134. * fit. The macro moves to the next character position in the output string.
  135. */
  136. #define ADDCH(str, ch) do { \
  137. if ((str) < end) \
  138. *(str) = (ch); \
  139. ++str; \
  140. } while (0)
  141. static char *number(char *buf, char *end, u64 num,
  142. int base, int size, int precision, int type)
  143. {
  144. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  145. static const char digits[16] = "0123456789ABCDEF";
  146. char tmp[66];
  147. char sign;
  148. char locase;
  149. int need_pfx = ((type & SPECIAL) && base != 10);
  150. int i;
  151. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  152. * produces same digits or (maybe lowercased) letters */
  153. locase = (type & SMALL);
  154. if (type & LEFT)
  155. type &= ~ZEROPAD;
  156. sign = 0;
  157. if (type & SIGN) {
  158. if ((s64) num < 0) {
  159. sign = '-';
  160. num = -(s64) num;
  161. size--;
  162. } else if (type & PLUS) {
  163. sign = '+';
  164. size--;
  165. } else if (type & SPACE) {
  166. sign = ' ';
  167. size--;
  168. }
  169. }
  170. if (need_pfx) {
  171. size--;
  172. if (base == 16)
  173. size--;
  174. }
  175. /* generate full string in tmp[], in reverse order */
  176. i = 0;
  177. if (num == 0)
  178. tmp[i++] = '0';
  179. /* Generic code, for any base:
  180. else do {
  181. tmp[i++] = (digits[do_div(num,base)] | locase);
  182. } while (num != 0);
  183. */
  184. else if (base != 10) { /* 8 or 16 */
  185. int mask = base - 1;
  186. int shift = 3;
  187. if (base == 16)
  188. shift = 4;
  189. do {
  190. tmp[i++] = (digits[((unsigned char)num) & mask]
  191. | locase);
  192. num >>= shift;
  193. } while (num);
  194. } else { /* base 10 */
  195. i = put_dec(tmp, num) - tmp;
  196. }
  197. /* printing 100 using %2d gives "100", not "00" */
  198. if (i > precision)
  199. precision = i;
  200. /* leading space padding */
  201. size -= precision;
  202. if (!(type & (ZEROPAD + LEFT))) {
  203. while (--size >= 0)
  204. ADDCH(buf, ' ');
  205. }
  206. /* sign */
  207. if (sign)
  208. ADDCH(buf, sign);
  209. /* "0x" / "0" prefix */
  210. if (need_pfx) {
  211. ADDCH(buf, '0');
  212. if (base == 16)
  213. ADDCH(buf, 'X' | locase);
  214. }
  215. /* zero or space padding */
  216. if (!(type & LEFT)) {
  217. char c = (type & ZEROPAD) ? '0' : ' ';
  218. while (--size >= 0)
  219. ADDCH(buf, c);
  220. }
  221. /* hmm even more zero padding? */
  222. while (i <= --precision)
  223. ADDCH(buf, '0');
  224. /* actual digits of result */
  225. while (--i >= 0)
  226. ADDCH(buf, tmp[i]);
  227. /* trailing space padding */
  228. while (--size >= 0)
  229. ADDCH(buf, ' ');
  230. return buf;
  231. }
  232. static char *string(char *buf, char *end, const char *s, int field_width,
  233. int precision, int flags)
  234. {
  235. int len, i;
  236. if (s == NULL)
  237. s = "<NULL>";
  238. len = strnlen(s, precision);
  239. if (!(flags & LEFT))
  240. while (len < field_width--)
  241. ADDCH(buf, ' ');
  242. for (i = 0; i < len; ++i)
  243. ADDCH(buf, *s++);
  244. while (len < field_width--)
  245. ADDCH(buf, ' ');
  246. return buf;
  247. }
  248. /* U-Boot uses UTF-16 strings in the EFI context only. */
  249. static __maybe_unused char *string16(char *buf, char *end, u16 *s,
  250. int field_width, int precision, int flags)
  251. {
  252. const u16 *str = s ? s : u"<NULL>";
  253. ssize_t i, len = utf16_strnlen(str, precision);
  254. if (!(flags & LEFT))
  255. for (; len < field_width; --field_width)
  256. ADDCH(buf, ' ');
  257. if (buf < end)
  258. *buf = 0;
  259. for (i = 0; i < len; ++i) {
  260. int slen = utf16_utf8_strnlen(str, 1);
  261. s32 s = utf16_get(&str);
  262. if (s < 0)
  263. s = '?';
  264. if (buf + slen < end) {
  265. utf8_put(s, &buf);
  266. if (buf < end)
  267. *buf = 0;
  268. } else {
  269. buf += slen;
  270. }
  271. }
  272. for (; len < field_width; --field_width)
  273. ADDCH(buf, ' ');
  274. return buf;
  275. }
  276. #if CONFIG_IS_ENABLED(EFI_DEVICE_PATH_TO_TEXT)
  277. static char *device_path_string(char *buf, char *end, void *dp, int field_width,
  278. int precision, int flags)
  279. {
  280. u16 *str;
  281. /* If dp == NULL output the string '<NULL>' */
  282. if (!dp)
  283. return string16(buf, end, dp, field_width, precision, flags);
  284. str = efi_dp_str((struct efi_device_path *)dp);
  285. if (!str)
  286. return ERR_PTR(-ENOMEM);
  287. buf = string16(buf, end, str, field_width, precision, flags);
  288. efi_free_pool(str);
  289. return buf;
  290. }
  291. #endif
  292. static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
  293. int precision, int flags)
  294. {
  295. /* (6 * 2 hex digits), 5 colons and trailing zero */
  296. char mac_addr[6 * 3];
  297. char *p = mac_addr;
  298. int i;
  299. for (i = 0; i < 6; i++) {
  300. p = hex_byte_pack(p, addr[i]);
  301. if (!(flags & SPECIAL) && i != 5)
  302. *p++ = ':';
  303. }
  304. *p = '\0';
  305. return string(buf, end, mac_addr, field_width, precision,
  306. flags & ~SPECIAL);
  307. }
  308. static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
  309. int precision, int flags)
  310. {
  311. /* (8 * 4 hex digits), 7 colons and trailing zero */
  312. char ip6_addr[8 * 5];
  313. char *p = ip6_addr;
  314. int i;
  315. for (i = 0; i < 8; i++) {
  316. p = hex_byte_pack(p, addr[2 * i]);
  317. p = hex_byte_pack(p, addr[2 * i + 1]);
  318. if (!(flags & SPECIAL) && i != 7)
  319. *p++ = ':';
  320. }
  321. *p = '\0';
  322. return string(buf, end, ip6_addr, field_width, precision,
  323. flags & ~SPECIAL);
  324. }
  325. static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
  326. int precision, int flags)
  327. {
  328. /* (4 * 3 decimal digits), 3 dots and trailing zero */
  329. char ip4_addr[4 * 4];
  330. char temp[3]; /* hold each IP quad in reverse order */
  331. char *p = ip4_addr;
  332. int i, digits;
  333. for (i = 0; i < 4; i++) {
  334. digits = put_dec_trunc(temp, addr[i]) - temp;
  335. /* reverse the digits in the quad */
  336. while (digits--)
  337. *p++ = temp[digits];
  338. if (i != 3)
  339. *p++ = '.';
  340. }
  341. *p = '\0';
  342. return string(buf, end, ip4_addr, field_width, precision,
  343. flags & ~SPECIAL);
  344. }
  345. #ifdef CONFIG_LIB_UUID
  346. /*
  347. * This works (roughly) the same way as Linux's.
  348. *
  349. * %pUb: 01020304-0506-0708-090a-0b0c0d0e0f10
  350. * %pUB: 01020304-0506-0708-090A-0B0C0D0E0F10
  351. * %pUl: 04030201-0605-0807-090a-0b0c0d0e0f10
  352. * %pUL: 04030201-0605-0807-090A-0B0C0D0E0F10
  353. * %pUs: GUID text representation if known or fallback to %pUl
  354. */
  355. static char *uuid_string(char *buf, char *end, u8 *addr, int field_width,
  356. int precision, int flags, const char *fmt)
  357. {
  358. char uuid[UUID_STR_LEN + 1];
  359. int str_format;
  360. const char *str;
  361. switch (*(++fmt)) {
  362. case 'L':
  363. str_format = UUID_STR_FORMAT_GUID | UUID_STR_UPPER_CASE;
  364. break;
  365. case 'l':
  366. str_format = UUID_STR_FORMAT_GUID;
  367. break;
  368. case 'B':
  369. str_format = UUID_STR_FORMAT_STD | UUID_STR_UPPER_CASE;
  370. break;
  371. case 's':
  372. str = uuid_guid_get_str(addr);
  373. if (str)
  374. return string(buf, end, str,
  375. field_width, precision, flags);
  376. str_format = UUID_STR_FORMAT_GUID;
  377. break;
  378. default:
  379. str_format = UUID_STR_FORMAT_STD;
  380. break;
  381. }
  382. if (addr)
  383. uuid_bin_to_str(addr, uuid, str_format);
  384. else
  385. strcpy(uuid, "<NULL>");
  386. return string(buf, end, uuid, field_width, precision, flags);
  387. }
  388. #endif
  389. /*
  390. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  391. * by an extra set of alphanumeric characters that are extended format
  392. * specifiers.
  393. *
  394. * Right now we handle:
  395. *
  396. * - 'M' For a 6-byte MAC address, it prints the address in the
  397. * usual colon-separated hex notation
  398. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  399. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  400. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  401. * currently the same
  402. *
  403. * Note: IPv6 support is currently if(0)'ed out. If you ever need
  404. * %pI6, please add an IPV6 Kconfig knob, make your code select or
  405. * depend on that, and change the 0 below to CONFIG_IS_ENABLED(IPV6).
  406. */
  407. static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  408. int field_width, int precision, int flags)
  409. {
  410. u64 num = (uintptr_t)ptr;
  411. /*
  412. * Being a boot loader, we explicitly allow pointers to
  413. * (physical) address null.
  414. */
  415. #if 0
  416. if (!ptr)
  417. return string(buf, end, "(null)", field_width, precision,
  418. flags);
  419. #endif
  420. switch (*fmt) {
  421. /* Device paths only exist in the EFI context. */
  422. #if CONFIG_IS_ENABLED(EFI_DEVICE_PATH_TO_TEXT) && !defined(API_BUILD)
  423. case 'D':
  424. return device_path_string(buf, end, ptr, field_width,
  425. precision, flags);
  426. #endif
  427. case 'a':
  428. flags |= SPECIAL | ZEROPAD;
  429. switch (fmt[1]) {
  430. case 'p':
  431. default:
  432. field_width = sizeof(phys_addr_t) * 2 + 2;
  433. num = *(phys_addr_t *)ptr;
  434. break;
  435. }
  436. break;
  437. case 'm':
  438. flags |= SPECIAL;
  439. /* Fallthrough */
  440. case 'M':
  441. return mac_address_string(buf, end, ptr, field_width,
  442. precision, flags);
  443. case 'i':
  444. flags |= SPECIAL;
  445. /* Fallthrough */
  446. case 'I':
  447. /* %pI6 currently unused */
  448. if (0 && fmt[1] == '6')
  449. return ip6_addr_string(buf, end, ptr, field_width,
  450. precision, flags);
  451. if (fmt[1] == '4')
  452. return ip4_addr_string(buf, end, ptr, field_width,
  453. precision, flags);
  454. flags &= ~SPECIAL;
  455. break;
  456. #ifdef CONFIG_LIB_UUID
  457. case 'U':
  458. return uuid_string(buf, end, ptr, field_width, precision,
  459. flags, fmt);
  460. #endif
  461. default:
  462. break;
  463. }
  464. flags |= SMALL;
  465. if (field_width == -1) {
  466. field_width = 2*sizeof(void *);
  467. flags |= ZEROPAD;
  468. }
  469. return number(buf, end, num, 16, field_width, precision, flags);
  470. }
  471. static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
  472. va_list args)
  473. {
  474. u64 num;
  475. int base;
  476. char *str;
  477. int flags; /* flags to number() */
  478. int field_width; /* width of output field */
  479. int precision; /* min. # of digits for integers; max
  480. number of chars for from string */
  481. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  482. /* 'z' support added 23/7/1999 S.H. */
  483. /* 'z' changed to 'Z' --davidm 1/25/99 */
  484. /* 't' added for ptrdiff_t */
  485. char *end = buf + size;
  486. /* Make sure end is always >= buf - do we want this in U-Boot? */
  487. if (end < buf) {
  488. end = ((void *)-1);
  489. size = end - buf;
  490. }
  491. str = buf;
  492. for (; *fmt ; ++fmt) {
  493. if (*fmt != '%') {
  494. ADDCH(str, *fmt);
  495. continue;
  496. }
  497. /* process flags */
  498. flags = 0;
  499. repeat:
  500. ++fmt; /* this also skips first '%' */
  501. switch (*fmt) {
  502. case '-':
  503. flags |= LEFT;
  504. goto repeat;
  505. case '+':
  506. flags |= PLUS;
  507. goto repeat;
  508. case ' ':
  509. flags |= SPACE;
  510. goto repeat;
  511. case '#':
  512. flags |= SPECIAL;
  513. goto repeat;
  514. case '0':
  515. flags |= ZEROPAD;
  516. goto repeat;
  517. }
  518. /* get field width */
  519. field_width = -1;
  520. if (is_digit(*fmt))
  521. field_width = skip_atoi(&fmt);
  522. else if (*fmt == '*') {
  523. ++fmt;
  524. /* it's the next argument */
  525. field_width = va_arg(args, int);
  526. if (field_width < 0) {
  527. field_width = -field_width;
  528. flags |= LEFT;
  529. }
  530. }
  531. /* get the precision */
  532. precision = -1;
  533. if (*fmt == '.') {
  534. ++fmt;
  535. if (is_digit(*fmt))
  536. precision = skip_atoi(&fmt);
  537. else if (*fmt == '*') {
  538. ++fmt;
  539. /* it's the next argument */
  540. precision = va_arg(args, int);
  541. }
  542. if (precision < 0)
  543. precision = 0;
  544. }
  545. /* get the conversion qualifier */
  546. qualifier = -1;
  547. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  548. *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
  549. qualifier = *fmt;
  550. ++fmt;
  551. if (qualifier == 'l' && *fmt == 'l') {
  552. qualifier = 'L';
  553. ++fmt;
  554. }
  555. }
  556. /* default base */
  557. base = 10;
  558. switch (*fmt) {
  559. case 'c':
  560. if (!(flags & LEFT)) {
  561. while (--field_width > 0)
  562. ADDCH(str, ' ');
  563. }
  564. ADDCH(str, (unsigned char) va_arg(args, int));
  565. while (--field_width > 0)
  566. ADDCH(str, ' ');
  567. continue;
  568. case 's':
  569. /* U-Boot uses UTF-16 strings in the EFI context only. */
  570. #if (CONFIG_IS_ENABLED(EFI_LOADER) || CONFIG_IS_ENABLED(EFI_APP)) && \
  571. !defined(API_BUILD)
  572. if (qualifier == 'l') {
  573. str = string16(str, end, va_arg(args, u16 *),
  574. field_width, precision, flags);
  575. } else
  576. #endif
  577. {
  578. str = string(str, end, va_arg(args, char *),
  579. field_width, precision, flags);
  580. }
  581. continue;
  582. case 'p':
  583. str = pointer(fmt + 1, str, end,
  584. va_arg(args, void *),
  585. field_width, precision, flags);
  586. if (IS_ERR(str))
  587. return PTR_ERR(str);
  588. /* Skip all alphanumeric pointer suffixes */
  589. while (isalnum(fmt[1]))
  590. fmt++;
  591. continue;
  592. case 'n':
  593. if (qualifier == 'l') {
  594. long *ip = va_arg(args, long *);
  595. *ip = (str - buf);
  596. } else {
  597. int *ip = va_arg(args, int *);
  598. *ip = (str - buf);
  599. }
  600. continue;
  601. case '%':
  602. ADDCH(str, '%');
  603. continue;
  604. /* integer number formats - set up the flags and "break" */
  605. case 'o':
  606. base = 8;
  607. break;
  608. case 'x':
  609. flags |= SMALL;
  610. case 'X':
  611. base = 16;
  612. break;
  613. case 'd':
  614. case 'i':
  615. flags |= SIGN;
  616. case 'u':
  617. break;
  618. default:
  619. ADDCH(str, '%');
  620. if (*fmt)
  621. ADDCH(str, *fmt);
  622. else
  623. --fmt;
  624. continue;
  625. }
  626. if (qualifier == 'L') /* "quad" for 64 bit variables */
  627. num = va_arg(args, unsigned long long);
  628. else if (qualifier == 'l') {
  629. num = va_arg(args, unsigned long);
  630. if (flags & SIGN)
  631. num = (signed long) num;
  632. } else if (qualifier == 'Z' || qualifier == 'z') {
  633. num = va_arg(args, size_t);
  634. } else if (qualifier == 't') {
  635. num = va_arg(args, ptrdiff_t);
  636. } else if (qualifier == 'h') {
  637. num = (unsigned short) va_arg(args, int);
  638. if (flags & SIGN)
  639. num = (signed short) num;
  640. } else {
  641. num = va_arg(args, unsigned int);
  642. if (flags & SIGN)
  643. num = (signed int) num;
  644. }
  645. str = number(str, end, num, base, field_width, precision,
  646. flags);
  647. }
  648. if (size > 0) {
  649. ADDCH(str, '\0');
  650. if (str > end)
  651. end[-1] = '\0';
  652. --str;
  653. }
  654. /* the trailing null byte doesn't count towards the total */
  655. return str - buf;
  656. }
  657. int vsnprintf(char *buf, size_t size, const char *fmt,
  658. va_list args)
  659. {
  660. return vsnprintf_internal(buf, size, fmt, args);
  661. }
  662. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  663. {
  664. int i;
  665. i = vsnprintf(buf, size, fmt, args);
  666. if (likely(i < size))
  667. return i;
  668. if (size != 0)
  669. return size - 1;
  670. return 0;
  671. }
  672. int snprintf(char *buf, size_t size, const char *fmt, ...)
  673. {
  674. va_list args;
  675. int i;
  676. va_start(args, fmt);
  677. i = vsnprintf(buf, size, fmt, args);
  678. va_end(args);
  679. return i;
  680. }
  681. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  682. {
  683. va_list args;
  684. int i;
  685. va_start(args, fmt);
  686. i = vscnprintf(buf, size, fmt, args);
  687. va_end(args);
  688. return i;
  689. }
  690. /**
  691. * Format a string and place it in a buffer (va_list version)
  692. *
  693. * @param buf The buffer to place the result into
  694. * @param fmt The format string to use
  695. * @param args Arguments for the format string
  696. *
  697. * The function returns the number of characters written
  698. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  699. * buffer overflows.
  700. *
  701. * If you're not already dealing with a va_list consider using sprintf().
  702. */
  703. int vsprintf(char *buf, const char *fmt, va_list args)
  704. {
  705. return vsnprintf_internal(buf, INT_MAX, fmt, args);
  706. }
  707. int sprintf(char *buf, const char *fmt, ...)
  708. {
  709. va_list args;
  710. int i;
  711. va_start(args, fmt);
  712. i = vsprintf(buf, fmt, args);
  713. va_end(args);
  714. return i;
  715. }
  716. #if CONFIG_IS_ENABLED(PRINTF)
  717. int printf(const char *fmt, ...)
  718. {
  719. va_list args;
  720. uint i;
  721. va_start(args, fmt);
  722. i = vprintf(fmt, args);
  723. va_end(args);
  724. return i;
  725. }
  726. int vprintf(const char *fmt, va_list args)
  727. {
  728. uint i;
  729. char printbuffer[CONFIG_SYS_PBSIZE];
  730. /*
  731. * For this to work, printbuffer must be larger than
  732. * anything we ever want to print.
  733. */
  734. i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
  735. /* Handle error */
  736. if (i <= 0)
  737. return i;
  738. /* Print the string */
  739. puts(printbuffer);
  740. return i;
  741. }
  742. #endif
  743. static char local_toa[22];
  744. char *simple_itoa(ulong i)
  745. {
  746. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  747. char *p = &local_toa[21];
  748. *p-- = '\0';
  749. do {
  750. *p-- = '0' + i % 10;
  751. i /= 10;
  752. } while (i > 0);
  753. return p + 1;
  754. }
  755. char *simple_xtoa(ulong num)
  756. {
  757. /* 16 digits plus nul terminator, good for 64-bit or smaller ints */
  758. char *p = &local_toa[17];
  759. *--p = '\0';
  760. do {
  761. p -= 2;
  762. hex_byte_pack(p, num & 0xff);
  763. num >>= 8;
  764. } while (num > 0);
  765. return p;
  766. }
  767. /* We don't seem to have %'d in U-Boot */
  768. void print_grouped_ull(unsigned long long int_val, int digits)
  769. {
  770. char str[21], *s;
  771. int grab = 3;
  772. digits = (digits + 2) / 3;
  773. sprintf(str, "%*llu", digits * 3, int_val);
  774. for (s = str; *s; s += grab) {
  775. if (s != str)
  776. putc(s[-1] != ' ' ? ',' : ' ');
  777. printf("%.*s", grab, s);
  778. grab = 3;
  779. }
  780. }
  781. bool str2off(const char *p, loff_t *num)
  782. {
  783. char *endptr;
  784. *num = simple_strtoull(p, &endptr, 16);
  785. return *p != '\0' && *endptr == '\0';
  786. }
  787. bool str2long(const char *p, ulong *num)
  788. {
  789. char *endptr;
  790. *num = hextoul(p, &endptr);
  791. return *p != '\0' && *endptr == '\0';
  792. }
  793. char *strmhz(char *buf, unsigned long hz)
  794. {
  795. long l, n;
  796. long m;
  797. n = DIV_ROUND_CLOSEST(hz, 1000) / 1000L;
  798. l = sprintf(buf, "%ld", n);
  799. hz -= n * 1000000L;
  800. m = DIV_ROUND_CLOSEST(hz, 1000L);
  801. if (m != 0)
  802. sprintf(buf + l, ".%03ld", m);
  803. return buf;
  804. }