display_options.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <inttypes.h>
  10. #include <version.h>
  11. #include <linux/ctype.h>
  12. #include <asm/io.h>
  13. int display_options (void)
  14. {
  15. #if defined(BUILD_TAG)
  16. printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
  17. #else
  18. printf ("\n\n%s\n\n", version_string);
  19. #endif
  20. return 0;
  21. }
  22. void print_size(uint64_t size, const char *s)
  23. {
  24. unsigned long m = 0, n;
  25. uint64_t f;
  26. static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
  27. unsigned long d = 10 * ARRAY_SIZE(names);
  28. char c = 0;
  29. unsigned int i;
  30. for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
  31. if (size >> d) {
  32. c = names[i];
  33. break;
  34. }
  35. }
  36. if (!c) {
  37. printf("%" PRIu64 " Bytes%s", size, s);
  38. return;
  39. }
  40. n = size >> d;
  41. f = size & ((1ULL << d) - 1);
  42. /* If there's a remainder, deal with it */
  43. if (f) {
  44. m = (10ULL * f + (1ULL << (d - 1))) >> d;
  45. if (m >= 10) {
  46. m -= 10;
  47. n += 1;
  48. }
  49. }
  50. printf ("%lu", n);
  51. if (m) {
  52. printf (".%ld", m);
  53. }
  54. printf (" %ciB%s", c, s);
  55. }
  56. /*
  57. * Print data buffer in hex and ascii form to the terminal.
  58. *
  59. * data reads are buffered so that each memory address is only read once.
  60. * Useful when displaying the contents of volatile registers.
  61. *
  62. * parameters:
  63. * addr: Starting address to display at start of line
  64. * data: pointer to data buffer
  65. * width: data value width. May be 1, 2, or 4.
  66. * count: number of values to display
  67. * linelen: Number of values to print per line; specify 0 for default length
  68. */
  69. #define MAX_LINE_LENGTH_BYTES (64)
  70. #define DEFAULT_LINE_LENGTH_BYTES (16)
  71. int print_buffer(ulong addr, const void *data, uint width, uint count,
  72. uint linelen)
  73. {
  74. /* linebuf as a union causes proper alignment */
  75. union linebuf {
  76. #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
  77. uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
  78. #endif
  79. uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
  80. uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
  81. uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
  82. } lb;
  83. int i;
  84. #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
  85. uint64_t x;
  86. #else
  87. uint32_t x;
  88. #endif
  89. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  90. linelen = MAX_LINE_LENGTH_BYTES / width;
  91. if (linelen < 1)
  92. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  93. while (count) {
  94. uint thislinelen = linelen;
  95. printf("%08lx:", addr);
  96. /* check for overflow condition */
  97. if (count < thislinelen)
  98. thislinelen = count;
  99. /* Copy from memory into linebuf and print hex values */
  100. for (i = 0; i < thislinelen; i++) {
  101. if (width == 4)
  102. x = lb.ui[i] = *(volatile uint32_t *)data;
  103. #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
  104. else if (width == 8)
  105. x = lb.uq[i] = *(volatile uint64_t *)data;
  106. #endif
  107. else if (width == 2)
  108. x = lb.us[i] = *(volatile uint16_t *)data;
  109. else
  110. x = lb.uc[i] = *(volatile uint8_t *)data;
  111. #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
  112. printf(" %0*" PRIx64, width * 2, x);
  113. #else
  114. printf(" %0*x", width * 2, x);
  115. #endif
  116. data += width;
  117. }
  118. while (thislinelen < linelen) {
  119. /* fill line with whitespace for nice ASCII print */
  120. for (i=0; i<width*2+1; i++)
  121. puts(" ");
  122. linelen--;
  123. }
  124. /* Print data in ASCII characters */
  125. for (i = 0; i < thislinelen * width; i++) {
  126. if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
  127. lb.uc[i] = '.';
  128. }
  129. lb.uc[i] = '\0';
  130. printf(" %s\n", lb.uc);
  131. /* update references */
  132. addr += thislinelen * width;
  133. count -= thislinelen;
  134. if (ctrlc())
  135. return -1;
  136. }
  137. return 0;
  138. }