vsprintf.c 17 KB

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