tiny-printf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: LGPL-2.1+
  2. /*
  3. * Tiny printf version for SPL
  4. *
  5. * Copied from:
  6. * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
  7. *
  8. * Copyright (C) 2004,2008 Kustaa Nyholm
  9. */
  10. #include <common.h>
  11. #include <stdarg.h>
  12. #include <serial.h>
  13. #include <linux/ctype.h>
  14. struct printf_info {
  15. char *bf; /* Digit buffer */
  16. char zs; /* non-zero if a digit has been written */
  17. char *outstr; /* Next output position for sprintf() */
  18. /* Output a character */
  19. void (*putc)(struct printf_info *info, char ch);
  20. };
  21. static void out(struct printf_info *info, char c)
  22. {
  23. *info->bf++ = c;
  24. }
  25. static void out_dgt(struct printf_info *info, char dgt)
  26. {
  27. out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
  28. info->zs = 1;
  29. }
  30. static void div_out(struct printf_info *info, unsigned long *num,
  31. unsigned long div)
  32. {
  33. unsigned char dgt = 0;
  34. while (*num >= div) {
  35. *num -= div;
  36. dgt++;
  37. }
  38. if (info->zs || dgt > 0)
  39. out_dgt(info, dgt);
  40. }
  41. #ifdef CONFIG_SPL_NET_SUPPORT
  42. static void string(struct printf_info *info, char *s)
  43. {
  44. char ch;
  45. while ((ch = *s++))
  46. out(info, ch);
  47. }
  48. static const char hex_asc[] = "0123456789abcdef";
  49. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  50. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  51. static inline char *pack_hex_byte(char *buf, u8 byte)
  52. {
  53. *buf++ = hex_asc_hi(byte);
  54. *buf++ = hex_asc_lo(byte);
  55. return buf;
  56. }
  57. static void mac_address_string(struct printf_info *info, u8 *addr,
  58. bool separator)
  59. {
  60. /* (6 * 2 hex digits), 5 colons and trailing zero */
  61. char mac_addr[6 * 3];
  62. char *p = mac_addr;
  63. int i;
  64. for (i = 0; i < 6; i++) {
  65. p = pack_hex_byte(p, addr[i]);
  66. if (separator && i != 5)
  67. *p++ = ':';
  68. }
  69. *p = '\0';
  70. string(info, mac_addr);
  71. }
  72. static char *put_dec_trunc(char *buf, unsigned int q)
  73. {
  74. unsigned int d3, d2, d1, d0;
  75. d1 = (q >> 4) & 0xf;
  76. d2 = (q >> 8) & 0xf;
  77. d3 = (q >> 12);
  78. d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
  79. q = (d0 * 0xcd) >> 11;
  80. d0 = d0 - 10 * q;
  81. *buf++ = d0 + '0'; /* least significant digit */
  82. d1 = q + 9 * d3 + 5 * d2 + d1;
  83. if (d1 != 0) {
  84. q = (d1 * 0xcd) >> 11;
  85. d1 = d1 - 10 * q;
  86. *buf++ = d1 + '0'; /* next digit */
  87. d2 = q + 2 * d2;
  88. if ((d2 != 0) || (d3 != 0)) {
  89. q = (d2 * 0xd) >> 7;
  90. d2 = d2 - 10 * q;
  91. *buf++ = d2 + '0'; /* next digit */
  92. d3 = q + 4 * d3;
  93. if (d3 != 0) {
  94. q = (d3 * 0xcd) >> 11;
  95. d3 = d3 - 10 * q;
  96. *buf++ = d3 + '0'; /* next digit */
  97. if (q != 0)
  98. *buf++ = q + '0'; /* most sign. digit */
  99. }
  100. }
  101. }
  102. return buf;
  103. }
  104. static void ip4_addr_string(struct printf_info *info, u8 *addr)
  105. {
  106. /* (4 * 3 decimal digits), 3 dots and trailing zero */
  107. char ip4_addr[4 * 4];
  108. char temp[3]; /* hold each IP quad in reverse order */
  109. char *p = ip4_addr;
  110. int i, digits;
  111. for (i = 0; i < 4; i++) {
  112. digits = put_dec_trunc(temp, addr[i]) - temp;
  113. /* reverse the digits in the quad */
  114. while (digits--)
  115. *p++ = temp[digits];
  116. if (i != 3)
  117. *p++ = '.';
  118. }
  119. *p = '\0';
  120. string(info, ip4_addr);
  121. }
  122. #endif
  123. /*
  124. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  125. * by an extra set of characters that are extended format
  126. * specifiers.
  127. *
  128. * Right now we handle:
  129. *
  130. * - 'M' For a 6-byte MAC address, it prints the address in the
  131. * usual colon-separated hex notation.
  132. * - 'm' Same as above except there is no colon-separator.
  133. * - 'I4'for IPv4 addresses printed in the usual way (dot-separated
  134. * decimal).
  135. */
  136. static void pointer(struct printf_info *info, const char *fmt, void *ptr)
  137. {
  138. #ifdef DEBUG
  139. unsigned long num = (uintptr_t)ptr;
  140. unsigned long div;
  141. #endif
  142. switch (*fmt) {
  143. #ifdef DEBUG
  144. case 'a':
  145. switch (fmt[1]) {
  146. case 'p':
  147. default:
  148. num = *(phys_addr_t *)ptr;
  149. break;
  150. }
  151. break;
  152. #endif
  153. #ifdef CONFIG_SPL_NET_SUPPORT
  154. case 'm':
  155. return mac_address_string(info, ptr, false);
  156. case 'M':
  157. return mac_address_string(info, ptr, true);
  158. case 'I':
  159. if (fmt[1] == '4')
  160. return ip4_addr_string(info, ptr);
  161. #endif
  162. default:
  163. break;
  164. }
  165. #ifdef DEBUG
  166. div = 1UL << (sizeof(long) * 8 - 4);
  167. for (; div; div /= 0x10)
  168. div_out(info, &num, div);
  169. #endif
  170. }
  171. static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
  172. {
  173. char ch;
  174. char *p;
  175. unsigned long num;
  176. char buf[12];
  177. unsigned long div;
  178. while ((ch = *(fmt++))) {
  179. if (ch != '%') {
  180. info->putc(info, ch);
  181. } else {
  182. bool lz = false;
  183. int width = 0;
  184. bool islong = false;
  185. ch = *(fmt++);
  186. if (ch == '-')
  187. ch = *(fmt++);
  188. if (ch == '0') {
  189. ch = *(fmt++);
  190. lz = 1;
  191. }
  192. if (ch >= '0' && ch <= '9') {
  193. width = 0;
  194. while (ch >= '0' && ch <= '9') {
  195. width = (width * 10) + ch - '0';
  196. ch = *fmt++;
  197. }
  198. }
  199. if (ch == 'l') {
  200. ch = *(fmt++);
  201. islong = true;
  202. }
  203. info->bf = buf;
  204. p = info->bf;
  205. info->zs = 0;
  206. switch (ch) {
  207. case '\0':
  208. goto abort;
  209. case 'u':
  210. case 'd':
  211. div = 1000000000;
  212. if (islong) {
  213. num = va_arg(va, unsigned long);
  214. if (sizeof(long) > 4)
  215. div *= div * 10;
  216. } else {
  217. num = va_arg(va, unsigned int);
  218. }
  219. if (ch == 'd') {
  220. if (islong && (long)num < 0) {
  221. num = -(long)num;
  222. out(info, '-');
  223. } else if (!islong && (int)num < 0) {
  224. num = -(int)num;
  225. out(info, '-');
  226. }
  227. }
  228. if (!num) {
  229. out_dgt(info, 0);
  230. } else {
  231. for (; div; div /= 10)
  232. div_out(info, &num, div);
  233. }
  234. break;
  235. case 'x':
  236. if (islong) {
  237. num = va_arg(va, unsigned long);
  238. div = 1UL << (sizeof(long) * 8 - 4);
  239. } else {
  240. num = va_arg(va, unsigned int);
  241. div = 0x10000000;
  242. }
  243. if (!num) {
  244. out_dgt(info, 0);
  245. } else {
  246. for (; div; div /= 0x10)
  247. div_out(info, &num, div);
  248. }
  249. break;
  250. case 'c':
  251. out(info, (char)(va_arg(va, int)));
  252. break;
  253. case 's':
  254. p = va_arg(va, char*);
  255. break;
  256. case 'p':
  257. pointer(info, fmt, va_arg(va, void *));
  258. while (isalnum(fmt[0]))
  259. fmt++;
  260. break;
  261. case '%':
  262. out(info, '%');
  263. default:
  264. break;
  265. }
  266. *info->bf = 0;
  267. info->bf = p;
  268. while (*info->bf++ && width > 0)
  269. width--;
  270. while (width-- > 0)
  271. info->putc(info, lz ? '0' : ' ');
  272. if (p) {
  273. while ((ch = *p++))
  274. info->putc(info, ch);
  275. }
  276. }
  277. }
  278. abort:
  279. return 0;
  280. }
  281. #if CONFIG_IS_ENABLED(PRINTF)
  282. static void putc_normal(struct printf_info *info, char ch)
  283. {
  284. putc(ch);
  285. }
  286. int vprintf(const char *fmt, va_list va)
  287. {
  288. struct printf_info info;
  289. info.putc = putc_normal;
  290. return _vprintf(&info, fmt, va);
  291. }
  292. int printf(const char *fmt, ...)
  293. {
  294. struct printf_info info;
  295. va_list va;
  296. int ret;
  297. info.putc = putc_normal;
  298. va_start(va, fmt);
  299. ret = _vprintf(&info, fmt, va);
  300. va_end(va);
  301. return ret;
  302. }
  303. #endif
  304. static void putc_outstr(struct printf_info *info, char ch)
  305. {
  306. *info->outstr++ = ch;
  307. }
  308. int sprintf(char *buf, const char *fmt, ...)
  309. {
  310. struct printf_info info;
  311. va_list va;
  312. int ret;
  313. va_start(va, fmt);
  314. info.outstr = buf;
  315. info.putc = putc_outstr;
  316. ret = _vprintf(&info, fmt, va);
  317. va_end(va);
  318. *info.outstr = '\0';
  319. return ret;
  320. }
  321. /* Note that size is ignored */
  322. int snprintf(char *buf, size_t size, const char *fmt, ...)
  323. {
  324. struct printf_info info;
  325. va_list va;
  326. int ret;
  327. va_start(va, fmt);
  328. info.outstr = buf;
  329. info.putc = putc_outstr;
  330. ret = _vprintf(&info, fmt, va);
  331. va_end(va);
  332. *info.outstr = '\0';
  333. return ret;
  334. }