display_options.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, void* data, uint width, uint count, uint linelen)
  92. {
  93. /* linebuf as a union causes proper alignment */
  94. union linebuf {
  95. uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
  96. uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
  97. uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
  98. } lb;
  99. int i;
  100. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  101. linelen = MAX_LINE_LENGTH_BYTES / width;
  102. if (linelen < 1)
  103. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  104. while (count) {
  105. printf("%08lx:", addr);
  106. /* check for overflow condition */
  107. if (count < linelen)
  108. linelen = count;
  109. /* Copy from memory into linebuf and print hex values */
  110. for (i = 0; i < linelen; i++) {
  111. uint32_t x;
  112. if (width == 4)
  113. x = lb.ui[i] = *(volatile uint32_t *)data;
  114. else if (width == 2)
  115. x = lb.us[i] = *(volatile uint16_t *)data;
  116. else
  117. x = lb.uc[i] = *(volatile uint8_t *)data;
  118. printf(" %0*x", width * 2, x);
  119. data += width;
  120. }
  121. /* Print data in ASCII characters */
  122. for (i = 0; i < linelen * width; i++) {
  123. if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
  124. lb.uc[i] = '.';
  125. }
  126. lb.uc[i] = '\0';
  127. printf(" %s\n", lb.uc);
  128. /* update references */
  129. addr += linelen * width;
  130. count -= linelen;
  131. if (ctrlc())
  132. return -1;
  133. }
  134. return 0;
  135. }