display_options.c 5.0 KB

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