display_options.c 3.7 KB

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