vsprintf.c 17 KB

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