display_options.c 4.3 KB

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