display_options.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <compiler.h>
  8. #include <console.h>
  9. #include <display_options.h>
  10. #include <div64.h>
  11. #include <version_string.h>
  12. #include <linux/ctype.h>
  13. #include <asm/io.h>
  14. char *display_options_get_banner_priv(bool newlines, const char *build_tag,
  15. char *buf, int size)
  16. {
  17. int len;
  18. len = snprintf(buf, size, "%s%s", newlines ? "\n\n" : "",
  19. version_string);
  20. if (build_tag && len < size)
  21. len += snprintf(buf + len, size - len, ", Build: %s",
  22. build_tag);
  23. if (len > size - 3)
  24. len = size - 3;
  25. if (len < 0)
  26. len = 0;
  27. snprintf(buf + len, size - len, "\n\n");
  28. return buf;
  29. }
  30. #ifndef BUILD_TAG
  31. #define BUILD_TAG NULL
  32. #endif
  33. char *display_options_get_banner(bool newlines, char *buf, int size)
  34. {
  35. return display_options_get_banner_priv(newlines, BUILD_TAG, buf, size);
  36. }
  37. int display_options(void)
  38. {
  39. char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
  40. display_options_get_banner(true, buf, sizeof(buf));
  41. printf("%s", buf);
  42. return 0;
  43. }
  44. void print_freq(uint64_t freq, const char *s)
  45. {
  46. unsigned long m = 0;
  47. uint32_t f;
  48. static const char names[] = {'G', 'M', 'k'};
  49. unsigned long d = 1e9;
  50. char c = 0;
  51. unsigned int i;
  52. for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) {
  53. if (freq >= d) {
  54. c = names[i];
  55. break;
  56. }
  57. }
  58. if (!c) {
  59. printf("%llu Hz%s", freq, s);
  60. return;
  61. }
  62. f = do_div(freq, d);
  63. /* If there's a remainder, show the first few digits */
  64. if (f) {
  65. m = f;
  66. while (m > 1000)
  67. m /= 10;
  68. while (m && !(m % 10))
  69. m /= 10;
  70. if (m >= 100)
  71. m = (m / 10) + (m % 100 >= 50);
  72. }
  73. printf("%lu", (unsigned long) freq);
  74. if (m)
  75. printf(".%ld", m);
  76. printf(" %cHz%s", c, s);
  77. }
  78. void print_size(uint64_t size, const char *s)
  79. {
  80. unsigned long m = 0, n;
  81. uint64_t f;
  82. static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
  83. unsigned long d = 10 * ARRAY_SIZE(names);
  84. char c = 0;
  85. unsigned int i;
  86. for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
  87. if (size >> d) {
  88. c = names[i];
  89. break;
  90. }
  91. }
  92. if (!c) {
  93. /*
  94. * SPL tiny-printf is not capable for printing uint64_t.
  95. * We have just checked that the size is small enought to fit
  96. * unsigned int safely.
  97. */
  98. printf("%u Bytes%s", (unsigned int)size, s);
  99. return;
  100. }
  101. n = size >> d;
  102. f = size & ((1ULL << d) - 1);
  103. /* If there's a remainder, deal with it */
  104. if (f) {
  105. m = (10ULL * f + (1ULL << (d - 1))) >> d;
  106. if (m >= 10) {
  107. m -= 10;
  108. n += 1;
  109. if (n == 1024 && i > 0) {
  110. n = 1;
  111. m = 0;
  112. c = names[i - 1];
  113. }
  114. }
  115. }
  116. printf ("%lu", n);
  117. if (m) {
  118. printf (".%ld", m);
  119. }
  120. printf (" %ciB%s", c, s);
  121. }
  122. #define MAX_LINE_LENGTH_BYTES 64
  123. #define DEFAULT_LINE_LENGTH_BYTES 16
  124. int hexdump_line(ulong addr, const void *data, uint width, uint count,
  125. uint linelen, char *out, int size)
  126. {
  127. /* linebuf as a union causes proper alignment */
  128. union linebuf {
  129. uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
  130. uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
  131. uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
  132. uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
  133. } lb;
  134. uint thislinelen;
  135. int i;
  136. ulong x;
  137. if (linelen * width > MAX_LINE_LENGTH_BYTES)
  138. linelen = MAX_LINE_LENGTH_BYTES / width;
  139. if (linelen < 1)
  140. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  141. /*
  142. * Check the size here so that we don't need to use snprintf(). This
  143. * helps to reduce code size
  144. */
  145. if (size < HEXDUMP_MAX_BUF_LENGTH(linelen * width))
  146. return -ENOSPC;
  147. thislinelen = linelen;
  148. out += sprintf(out, "%08lx:", addr);
  149. /* check for overflow condition */
  150. if (count < thislinelen)
  151. thislinelen = count;
  152. /* Copy from memory into linebuf and print hex values */
  153. for (i = 0; i < thislinelen; i++) {
  154. if (width == 4)
  155. x = lb.ui[i] = *(volatile uint32_t *)data;
  156. else if (MEM_SUPPORT_64BIT_DATA && width == 8)
  157. x = lb.uq[i] = *(volatile ulong *)data;
  158. else if (width == 2)
  159. x = lb.us[i] = *(volatile uint16_t *)data;
  160. else
  161. x = lb.uc[i] = *(volatile uint8_t *)data;
  162. if (CONFIG_IS_ENABLED(USE_TINY_PRINTF))
  163. out += sprintf(out, " %x", (uint)x);
  164. else
  165. out += sprintf(out, " %0*lx", width * 2, x);
  166. data += width;
  167. }
  168. /* fill line with whitespace for nice ASCII print */
  169. for (i = 0; i < (linelen - thislinelen) * (width * 2 + 1); i++)
  170. *out++ = ' ';
  171. /* Print data in ASCII characters */
  172. for (i = 0; i < thislinelen * width; i++) {
  173. if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
  174. lb.uc[i] = '.';
  175. }
  176. lb.uc[i] = '\0';
  177. out += sprintf(out, " %s", lb.uc);
  178. return thislinelen;
  179. }
  180. int print_buffer(ulong addr, const void *data, uint width, uint count,
  181. uint linelen)
  182. {
  183. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  184. linelen = MAX_LINE_LENGTH_BYTES / width;
  185. if (linelen < 1)
  186. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  187. while (count) {
  188. uint thislinelen;
  189. char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
  190. thislinelen = hexdump_line(addr, data, width, count, linelen,
  191. buf, sizeof(buf));
  192. assert(thislinelen >= 0);
  193. puts(buf);
  194. putc('\n');
  195. /* update references */
  196. data += thislinelen * width;
  197. addr += thislinelen * width;
  198. count -= thislinelen;
  199. if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc())
  200. return -EINTR;
  201. }
  202. return 0;
  203. }