tiny-printf.c 7.2 KB

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