display_options.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <linux/ctype.h>
  26. #include <asm/io.h>
  27. int display_options (void)
  28. {
  29. extern char version_string[];
  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. static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
  46. unsigned long long d = 1ULL << (10 * ARRAY_SIZE(names));
  47. char c = 0;
  48. unsigned int i;
  49. for (i = 0; i < ARRAY_SIZE(names); i++, d >>= 10) {
  50. if (size >= d) {
  51. c = names[i];
  52. break;
  53. }
  54. }
  55. if (!c) {
  56. printf("%llu Bytes%s", size, s);
  57. return;
  58. }
  59. n = size / d;
  60. /* If there's a remainder, deal with it */
  61. if(size % d) {
  62. m = (10 * (size - (n * d)) + (d / 2) ) / d;
  63. if (m >= 10) {
  64. m -= 10;
  65. n += 1;
  66. }
  67. }
  68. printf ("%lu", n);
  69. if (m) {
  70. printf (".%ld", m);
  71. }
  72. printf (" %ciB%s", c, s);
  73. }
  74. /*
  75. * Print data buffer in hex and ascii form to the terminal.
  76. *
  77. * data reads are buffered so that each memory address is only read once.
  78. * Useful when displaying the contents of volatile registers.
  79. *
  80. * parameters:
  81. * addr: Starting address to display at start of line
  82. * data: pointer to data buffer
  83. * width: data value width. May be 1, 2, or 4.
  84. * count: number of values to display
  85. * linelen: Number of values to print per line; specify 0 for default length
  86. */
  87. #define MAX_LINE_LENGTH_BYTES (64)
  88. #define DEFAULT_LINE_LENGTH_BYTES (16)
  89. int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
  90. {
  91. uint8_t linebuf[MAX_LINE_LENGTH_BYTES];
  92. uint32_t *uip = (void*)linebuf;
  93. uint16_t *usp = (void*)linebuf;
  94. uint8_t *ucp = (void*)linebuf;
  95. int i;
  96. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  97. linelen = MAX_LINE_LENGTH_BYTES / width;
  98. if (linelen < 1)
  99. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  100. while (count) {
  101. printf("%08lx:", addr);
  102. /* check for overflow condition */
  103. if (count < linelen)
  104. linelen = count;
  105. /* Copy from memory into linebuf and print hex values */
  106. for (i = 0; i < linelen; i++) {
  107. if (width == 4) {
  108. uip[i] = *(volatile uint32_t *)data;
  109. printf(" %08x", uip[i]);
  110. } else if (width == 2) {
  111. usp[i] = *(volatile uint16_t *)data;
  112. printf(" %04x", usp[i]);
  113. } else {
  114. ucp[i] = *(volatile uint8_t *)data;
  115. printf(" %02x", ucp[i]);
  116. }
  117. data += width;
  118. }
  119. /* Print data in ASCII characters */
  120. puts(" ");
  121. for (i = 0; i < linelen * width; i++)
  122. putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.');
  123. putc ('\n');
  124. /* update references */
  125. addr += linelen * width;
  126. count -= linelen;
  127. if (ctrlc())
  128. return -1;
  129. }
  130. return 0;
  131. }