display_options.c 3.6 KB

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