display_options.c 3.9 KB

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