vsprintf.c 18 KB

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