vsprintf.c 19 KB

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