tiny-printf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. static 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. static 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 == '-')
  192. ch = *(fmt++);
  193. if (ch == '0') {
  194. ch = *(fmt++);
  195. lz = 1;
  196. }
  197. if (ch >= '0' && ch <= '9') {
  198. width = 0;
  199. while (ch >= '0' && ch <= '9') {
  200. width = (width * 10) + ch - '0';
  201. ch = *fmt++;
  202. }
  203. }
  204. if (ch == 'l') {
  205. ch = *(fmt++);
  206. islong = true;
  207. }
  208. info->bf = buf;
  209. p = info->bf;
  210. info->zs = 0;
  211. switch (ch) {
  212. case '\0':
  213. goto abort;
  214. case 'u':
  215. case 'd':
  216. div = 1000000000;
  217. if (islong) {
  218. num = va_arg(va, unsigned long);
  219. if (sizeof(long) > 4)
  220. div *= div * 10;
  221. } else {
  222. num = va_arg(va, unsigned int);
  223. }
  224. if (ch == 'd') {
  225. if (islong && (long)num < 0) {
  226. num = -(long)num;
  227. out(info, '-');
  228. } else if (!islong && (int)num < 0) {
  229. num = -(int)num;
  230. out(info, '-');
  231. }
  232. }
  233. if (!num) {
  234. out_dgt(info, 0);
  235. } else {
  236. for (; div; div /= 10)
  237. div_out(info, &num, div);
  238. }
  239. break;
  240. case 'x':
  241. if (islong) {
  242. num = va_arg(va, unsigned long);
  243. div = 1UL << (sizeof(long) * 8 - 4);
  244. } else {
  245. num = va_arg(va, unsigned int);
  246. div = 0x10000000;
  247. }
  248. if (!num) {
  249. out_dgt(info, 0);
  250. } else {
  251. for (; div; div /= 0x10)
  252. div_out(info, &num, div);
  253. }
  254. break;
  255. case 'c':
  256. out(info, (char)(va_arg(va, int)));
  257. break;
  258. case 's':
  259. p = va_arg(va, char*);
  260. break;
  261. case 'p':
  262. pointer(info, fmt, va_arg(va, void *));
  263. while (isalnum(fmt[0]))
  264. fmt++;
  265. break;
  266. case '%':
  267. out(info, '%');
  268. default:
  269. break;
  270. }
  271. *info->bf = 0;
  272. info->bf = p;
  273. while (*info->bf++ && width > 0)
  274. width--;
  275. while (width-- > 0)
  276. info->putc(info, lz ? '0' : ' ');
  277. if (p) {
  278. while ((ch = *p++))
  279. info->putc(info, ch);
  280. }
  281. }
  282. }
  283. abort:
  284. return 0;
  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. static void putc_outstr(struct printf_info *info, char ch)
  304. {
  305. *info->outstr++ = ch;
  306. }
  307. int sprintf(char *buf, const char *fmt, ...)
  308. {
  309. struct printf_info info;
  310. va_list va;
  311. int ret;
  312. va_start(va, fmt);
  313. info.outstr = buf;
  314. info.putc = putc_outstr;
  315. ret = _vprintf(&info, fmt, va);
  316. va_end(va);
  317. *info.outstr = '\0';
  318. return ret;
  319. }
  320. /* Note that size is ignored */
  321. int snprintf(char *buf, size_t size, const char *fmt, ...)
  322. {
  323. struct printf_info info;
  324. va_list va;
  325. int ret;
  326. va_start(va, fmt);
  327. info.outstr = buf;
  328. info.putc = putc_outstr;
  329. ret = _vprintf(&info, fmt, va);
  330. va_end(va);
  331. *info.outstr = '\0';
  332. return ret;
  333. }
  334. void __assert_fail(const char *assertion, const char *file, unsigned line,
  335. const char *function)
  336. {
  337. /* This will not return */
  338. printf("%s:%u: %s: Assertion `%s' failed.", file, line, function,
  339. assertion);
  340. hang();
  341. }