vsprintf.c 20 KB

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