hexdump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * lib/hexdump.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation. See README and COPYING for
  8. * more details.
  9. */
  10. #include <common.h>
  11. #include <hexdump.h>
  12. #include <mapmem.h>
  13. #include <linux/ctype.h>
  14. #include <linux/compat.h>
  15. #include <linux/log2.h>
  16. #include <asm/unaligned.h>
  17. #define MAX_LINE_LENGTH_BYTES 64
  18. const char hex_asc[] = "0123456789abcdef";
  19. const char hex_asc_upper[] = "0123456789ABCDEF";
  20. #if CONFIG_IS_ENABLED(HEXDUMP)
  21. int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
  22. char *linebuf, size_t linebuflen, bool ascii)
  23. {
  24. const u8 *ptr = buf;
  25. int ngroups;
  26. u8 ch;
  27. int j, lx = 0;
  28. int ascii_column;
  29. int ret;
  30. if (!rowsize)
  31. rowsize = 16;
  32. else
  33. rowsize = min(rowsize, MAX_LINE_LENGTH_BYTES);
  34. if (len > rowsize) /* limit to one line at a time */
  35. len = rowsize;
  36. if (!is_power_of_2(groupsize) || groupsize > 8)
  37. groupsize = 1;
  38. if ((len % groupsize) != 0) /* no mixed size output */
  39. groupsize = 1;
  40. ngroups = len / groupsize;
  41. ascii_column = rowsize * 2 + rowsize / groupsize + 1;
  42. if (!linebuflen)
  43. goto overflow1;
  44. if (!len)
  45. goto nil;
  46. if (groupsize == 8) {
  47. const u64 *ptr8 = buf;
  48. for (j = 0; j < ngroups; j++) {
  49. ret = snprintf(linebuf + lx, linebuflen - lx,
  50. "%s%16.16llx", j ? " " : "",
  51. get_unaligned(ptr8 + j));
  52. if (ret >= linebuflen - lx)
  53. goto overflow1;
  54. lx += ret;
  55. }
  56. } else if (groupsize == 4) {
  57. const u32 *ptr4 = buf;
  58. for (j = 0; j < ngroups; j++) {
  59. ret = snprintf(linebuf + lx, linebuflen - lx,
  60. "%s%8.8x", j ? " " : "",
  61. get_unaligned(ptr4 + j));
  62. if (ret >= linebuflen - lx)
  63. goto overflow1;
  64. lx += ret;
  65. }
  66. } else if (groupsize == 2) {
  67. const u16 *ptr2 = buf;
  68. for (j = 0; j < ngroups; j++) {
  69. ret = snprintf(linebuf + lx, linebuflen - lx,
  70. "%s%4.4x", j ? " " : "",
  71. get_unaligned(ptr2 + j));
  72. if (ret >= linebuflen - lx)
  73. goto overflow1;
  74. lx += ret;
  75. }
  76. } else {
  77. for (j = 0; j < len; j++) {
  78. if (linebuflen < lx + 2)
  79. goto overflow2;
  80. ch = ptr[j];
  81. linebuf[lx++] = hex_asc_hi(ch);
  82. if (linebuflen < lx + 2)
  83. goto overflow2;
  84. linebuf[lx++] = hex_asc_lo(ch);
  85. if (linebuflen < lx + 2)
  86. goto overflow2;
  87. linebuf[lx++] = ' ';
  88. }
  89. if (j)
  90. lx--;
  91. }
  92. if (!ascii)
  93. goto nil;
  94. while (lx < ascii_column) {
  95. if (linebuflen < lx + 2)
  96. goto overflow2;
  97. linebuf[lx++] = ' ';
  98. }
  99. for (j = 0; j < len; j++) {
  100. if (linebuflen < lx + 2)
  101. goto overflow2;
  102. ch = ptr[j];
  103. linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
  104. }
  105. nil:
  106. linebuf[lx] = '\0';
  107. return lx;
  108. overflow2:
  109. linebuf[lx++] = '\0';
  110. overflow1:
  111. return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
  112. }
  113. int print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
  114. int groupsize, const void *buf, size_t len, bool ascii)
  115. {
  116. const u8 *ptr = buf;
  117. int i, linelen, remaining = len;
  118. char linebuf[MAX_LINE_LENGTH_BYTES * 3 + 2 + MAX_LINE_LENGTH_BYTES + 1];
  119. if (!rowsize)
  120. rowsize = 16;
  121. else
  122. rowsize = min(rowsize, MAX_LINE_LENGTH_BYTES);
  123. for (i = 0; i < len; i += rowsize) {
  124. linelen = min(remaining, rowsize);
  125. remaining -= rowsize;
  126. hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  127. linebuf, sizeof(linebuf), ascii);
  128. switch (prefix_type) {
  129. case DUMP_PREFIX_ADDRESS:
  130. printf("%s%0*lx: %s\n", prefix_str,
  131. IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
  132. (ulong)map_to_sysmem(ptr) + i, linebuf);
  133. break;
  134. case DUMP_PREFIX_OFFSET:
  135. printf("%s%.8x: %s\n", prefix_str, i, linebuf);
  136. break;
  137. default:
  138. printf("%s%s\n", prefix_str, linebuf);
  139. break;
  140. }
  141. if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc())
  142. return -EINTR;
  143. }
  144. return 0;
  145. }
  146. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  147. const void *buf, size_t len)
  148. {
  149. print_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true);
  150. }
  151. #else
  152. /*
  153. * Some code in U-Boot copy-pasted from Linux kernel uses both
  154. * functions below so to keep stuff compilable we keep these stubs here.
  155. */
  156. int print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
  157. int groupsize, const void *buf, size_t len, bool ascii)
  158. {
  159. return -ENOSYS;
  160. }
  161. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  162. const void *buf, size_t len)
  163. {
  164. }
  165. #endif /* CONFIG_HEXDUMP */