vsprintf.c 19 KB

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