display_options.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <config.h>
  8. #include <common.h>
  9. #include <version.h>
  10. #include <linux/ctype.h>
  11. #include <asm/io.h>
  12. int display_options (void)
  13. {
  14. #if defined(BUILD_TAG)
  15. printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
  16. #else
  17. printf ("\n\n%s\n\n", version_string);
  18. #endif
  19. return 0;
  20. }
  21. /*
  22. * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB",
  23. * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string
  24. * (like "\n")
  25. */
  26. void print_size(unsigned long long size, const char *s)
  27. {
  28. unsigned long m = 0, n;
  29. unsigned long long f;
  30. static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
  31. unsigned long d = 10 * ARRAY_SIZE(names);
  32. char c = 0;
  33. unsigned int i;
  34. for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
  35. if (size >> d) {
  36. c = names[i];
  37. break;
  38. }
  39. }
  40. if (!c) {
  41. printf("%llu Bytes%s", size, s);
  42. return;
  43. }
  44. n = size >> d;
  45. f = size & ((1ULL << d) - 1);
  46. /* If there's a remainder, deal with it */
  47. if (f) {
  48. m = (10ULL * f + (1ULL << (d - 1))) >> d;
  49. if (m >= 10) {
  50. m -= 10;
  51. n += 1;
  52. }
  53. }
  54. printf ("%lu", n);
  55. if (m) {
  56. printf (".%ld", m);
  57. }
  58. printf (" %ciB%s", c, s);
  59. }
  60. /*
  61. * Print data buffer in hex and ascii form to the terminal.
  62. *
  63. * data reads are buffered so that each memory address is only read once.
  64. * Useful when displaying the contents of volatile registers.
  65. *
  66. * parameters:
  67. * addr: Starting address to display at start of line
  68. * data: pointer to data buffer
  69. * width: data value width. May be 1, 2, or 4.
  70. * count: number of values to display
  71. * linelen: Number of values to print per line; specify 0 for default length
  72. */
  73. #define MAX_LINE_LENGTH_BYTES (64)
  74. #define DEFAULT_LINE_LENGTH_BYTES (16)
  75. int print_buffer(ulong addr, const void *data, uint width, uint count,
  76. uint linelen)
  77. {
  78. /* linebuf as a union causes proper alignment */
  79. union linebuf {
  80. uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
  81. uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
  82. uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
  83. } lb;
  84. int i;
  85. if (linelen*width > MAX_LINE_LENGTH_BYTES)
  86. linelen = MAX_LINE_LENGTH_BYTES / width;
  87. if (linelen < 1)
  88. linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  89. while (count) {
  90. uint thislinelen = linelen;
  91. printf("%08lx:", addr);
  92. /* check for overflow condition */
  93. if (count < thislinelen)
  94. thislinelen = count;
  95. /* Copy from memory into linebuf and print hex values */
  96. for (i = 0; i < thislinelen; i++) {
  97. uint32_t x;
  98. if (width == 4)
  99. x = lb.ui[i] = *(volatile uint32_t *)data;
  100. else if (width == 2)
  101. x = lb.us[i] = *(volatile uint16_t *)data;
  102. else
  103. x = lb.uc[i] = *(volatile uint8_t *)data;
  104. printf(" %0*x", width * 2, x);
  105. data += width;
  106. }
  107. while (thislinelen < linelen) {
  108. /* fill line with whitespace for nice ASCII print */
  109. for (i=0; i<width*2+1; i++)
  110. puts(" ");
  111. linelen--;
  112. }
  113. /* Print data in ASCII characters */
  114. for (i = 0; i < thislinelen * width; i++) {
  115. if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
  116. lb.uc[i] = '.';
  117. }
  118. lb.uc[i] = '\0';
  119. printf(" %s\n", lb.uc);
  120. /* update references */
  121. addr += thislinelen * width;
  122. count -= thislinelen;
  123. if (ctrlc())
  124. return -1;
  125. }
  126. return 0;
  127. }