display_options.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <config.h>
  24. #include <common.h>
  25. #include <version.h>
  26. #include <linux/ctype.h>
  27. #include <asm/io.h>
  28. int display_options (void)
  29. {
  30. #if defined(BUILD_TAG)
  31. printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
  32. #else
  33. printf ("\n\n%s\n\n", version_string);
  34. #endif
  35. return 0;
  36. }
  37. /*
  38. * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB",
  39. * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string
  40. * (like "\n")
  41. */
  42. void print_size(unsigned long long size, const char *s)
  43. {
  44. unsigned long m = 0, n;
  45. unsigned long long f;
  46. static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
  47. unsigned long d = 10 * ARRAY_SIZE(names);
  48. char c = 0;
  49. unsigned int i;
  50. for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
  51. if (size >> d) {
  52. c = names[i];
  53. break;
  54. }
  55. }
  56. if (!c) {
  57. printf("%llu Bytes%s", size, s);
  58. return;
  59. }
  60. n = size >> d;
  61. f = size & ((1ULL << d) - 1);
  62. /* If there's a remainder, deal with it */
  63. if (f) {
  64. m = (10ULL * f + (1ULL << (d - 1))) >> d;
  65. if (m >= 10) {
  66. m -= 10;
  67. n += 1;
  68. }
  69. }
  70. printf ("%lu", n);
  71. if (m) {
  72. printf (".%ld", m);
  73. }
  74. printf (" %ciB%s", c, s);
  75. }
  76. /*
  77. * Print data buffer in hex and ascii form to the terminal.
  78. *
  79. * data reads are buffered so that each memory address is only read once.
  80. * Useful when displaying the contents of volatile registers.
  81. *
  82. * parameters:
  83. * addr: Starting address to display at start of line
  84. * data: pointer to data buffer
  85. * width: data value width. May be 1, 2, or 4.
  86. * count: number of values to display
  87. * linelen: Number of values to print per line; specify 0 for default length
  88. */
  89. #define MAX_LINE_LENGTH_BYTES (64)
  90. #define DEFAULT_LINE_LENGTH_BYTES (16)
  91. int print_buffer(ulong addr, const void *data, uint width, uint count,
  92. uint linelen)
  93. {
  94. /* linebuf as a union causes proper alignment */
  95. union linebuf {
  96. uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
  97. uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
  98. uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
  99. } lb;
  100. int i;
  101. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  102. linelen = MAX_LINE_LENGTH_BYTES / width;
  103. if (linelen < 1)
  104. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  105. while (count) {
  106. uint thislinelen = linelen;
  107. printf("%08lx:", addr);
  108. /* check for overflow condition */
  109. if (count < thislinelen)
  110. thislinelen = count;
  111. /* Copy from memory into linebuf and print hex values */
  112. for (i = 0; i < thislinelen; i++) {
  113. uint32_t x;
  114. if (width == 4)
  115. x = lb.ui[i] = *(volatile uint32_t *)data;
  116. else if (width == 2)
  117. x = lb.us[i] = *(volatile uint16_t *)data;
  118. else
  119. x = lb.uc[i] = *(volatile uint8_t *)data;
  120. printf(" %0*x", width * 2, x);
  121. data += width;
  122. }
  123. while (thislinelen < linelen) {
  124. /* fill line with whitespace for nice ASCII print */
  125. for (i=0; i<width*2+1; i++)
  126. puts(" ");
  127. linelen--;
  128. }
  129. /* Print data in ASCII characters */
  130. for (i = 0; i < thislinelen * width; i++) {
  131. if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
  132. lb.uc[i] = '.';
  133. }
  134. lb.uc[i] = '\0';
  135. printf(" %s\n", lb.uc);
  136. /* update references */
  137. addr += thislinelen * width;
  138. count -= thislinelen;
  139. if (ctrlc())
  140. return -1;
  141. }
  142. return 0;
  143. }