vsprintf.c 19 KB

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