display_options.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. printf("%llu Bytes%s", size, s);
  93. return;
  94. }
  95. n = size >> d;
  96. f = size & ((1ULL << d) - 1);
  97. /* If there's a remainder, deal with it */
  98. if (f) {
  99. m = (10ULL * f + (1ULL << (d - 1))) >> d;
  100. if (m >= 10) {
  101. m -= 10;
  102. n += 1;
  103. }
  104. }
  105. printf ("%lu", n);
  106. if (m) {
  107. printf (".%ld", m);
  108. }
  109. printf (" %ciB%s", c, s);
  110. }
  111. #define MAX_LINE_LENGTH_BYTES (64)
  112. #define DEFAULT_LINE_LENGTH_BYTES (16)
  113. int print_buffer(ulong addr, const void *data, uint width, uint count,
  114. uint linelen)
  115. {
  116. /* linebuf as a union causes proper alignment */
  117. union linebuf {
  118. uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
  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. ulong x;
  125. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  126. linelen = MAX_LINE_LENGTH_BYTES / width;
  127. if (linelen < 1)
  128. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  129. while (count) {
  130. uint thislinelen = linelen;
  131. printf("%08lx:", addr);
  132. /* check for overflow condition */
  133. if (count < thislinelen)
  134. thislinelen = count;
  135. /* Copy from memory into linebuf and print hex values */
  136. for (i = 0; i < thislinelen; i++) {
  137. if (width == 4)
  138. x = lb.ui[i] = *(volatile uint32_t *)data;
  139. else if (MEM_SUPPORT_64BIT_DATA && width == 8)
  140. x = lb.uq[i] = *(volatile ulong *)data;
  141. else if (width == 2)
  142. x = lb.us[i] = *(volatile uint16_t *)data;
  143. else
  144. x = lb.uc[i] = *(volatile uint8_t *)data;
  145. #if defined(CONFIG_SPL_BUILD)
  146. printf(" %x", (uint)x);
  147. #else
  148. printf(" %0*lx", width * 2, x);
  149. #endif
  150. data += width;
  151. }
  152. while (thislinelen < linelen) {
  153. /* fill line with whitespace for nice ASCII print */
  154. for (i=0; i<width*2+1; i++)
  155. puts(" ");
  156. linelen--;
  157. }
  158. /* Print data in ASCII characters */
  159. for (i = 0; i < thislinelen * width; i++) {
  160. if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
  161. lb.uc[i] = '.';
  162. }
  163. lb.uc[i] = '\0';
  164. printf(" %s\n", lb.uc);
  165. /* update references */
  166. addr += thislinelen * width;
  167. count -= thislinelen;
  168. #ifndef CONFIG_SPL_BUILD
  169. if (ctrlc())
  170. return -1;
  171. #endif
  172. }
  173. return 0;
  174. }