display_options.c 3.4 KB

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