display_options.c 4.4 KB

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