display_options.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 kB", "xxx.y kB", "xxx MB", "xxx.y MB",
  39. * xxx GB, or xxx.y GB as needed; allow for optional trailing string
  40. * (like "\n")
  41. */
  42. void print_size (phys_size_t size, const char *s)
  43. {
  44. ulong m = 0, n;
  45. phys_size_t d = 1 << 30; /* 1 GB */
  46. char c = 'G';
  47. if (size < d) { /* try MB */
  48. c = 'M';
  49. d = 1 << 20;
  50. if (size < d) { /* print in kB */
  51. c = 'k';
  52. d = 1 << 10;
  53. }
  54. }
  55. n = size / d;
  56. /* If there's a remainder, deal with it */
  57. if(size % d) {
  58. m = (10 * (size - (n * d)) + (d / 2) ) / d;
  59. if (m >= 10) {
  60. m -= 10;
  61. n += 1;
  62. }
  63. }
  64. printf ("%2ld", n);
  65. if (m) {
  66. printf (".%ld", m);
  67. }
  68. printf (" %cB%s", c, s);
  69. }
  70. /*
  71. * Print data buffer in hex and ascii form to the terminal.
  72. *
  73. * data reads are buffered so that each memory address is only read once.
  74. * Useful when displaying the contents of volatile registers.
  75. *
  76. * parameters:
  77. * addr: Starting address to display at start of line
  78. * data: pointer to data buffer
  79. * width: data value width. May be 1, 2, or 4.
  80. * count: number of values to display
  81. * linelen: Number of values to print per line; specify 0 for default length
  82. */
  83. #define MAX_LINE_LENGTH_BYTES (64)
  84. #define DEFAULT_LINE_LENGTH_BYTES (16)
  85. int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
  86. {
  87. uint8_t linebuf[MAX_LINE_LENGTH_BYTES];
  88. uint32_t *uip = (void*)linebuf;
  89. uint16_t *usp = (void*)linebuf;
  90. uint8_t *ucp = (void*)linebuf;
  91. int i;
  92. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  93. linelen = MAX_LINE_LENGTH_BYTES / width;
  94. if (linelen < 1)
  95. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  96. while (count) {
  97. printf("%08lx:", addr);
  98. /* check for overflow condition */
  99. if (count < linelen)
  100. linelen = count;
  101. /* Copy from memory into linebuf and print hex values */
  102. for (i = 0; i < linelen; i++) {
  103. if (width == 4) {
  104. uip[i] = *(volatile uint32_t *)data;
  105. printf(" %08x", uip[i]);
  106. } else if (width == 2) {
  107. usp[i] = *(volatile uint16_t *)data;
  108. printf(" %04x", usp[i]);
  109. } else {
  110. ucp[i] = *(volatile uint8_t *)data;
  111. printf(" %02x", ucp[i]);
  112. }
  113. data += width;
  114. }
  115. /* Print data in ASCII characters */
  116. puts(" ");
  117. for (i = 0; i < linelen * width; i++)
  118. putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.');
  119. putc ('\n');
  120. /* update references */
  121. addr += linelen * width;
  122. count -= linelen;
  123. if (ctrlc())
  124. return -1;
  125. }
  126. return 0;
  127. }