utbuffer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: utbuffer - Buffer dump routines
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #define _COMPONENT ACPI_UTILITIES
  12. ACPI_MODULE_NAME("utbuffer")
  13. /*******************************************************************************
  14. *
  15. * FUNCTION: acpi_ut_dump_buffer
  16. *
  17. * PARAMETERS: buffer - Buffer to dump
  18. * count - Amount to dump, in bytes
  19. * display - BYTE, WORD, DWORD, or QWORD display:
  20. * DB_BYTE_DISPLAY
  21. * DB_WORD_DISPLAY
  22. * DB_DWORD_DISPLAY
  23. * DB_QWORD_DISPLAY
  24. * base_offset - Beginning buffer offset (display only)
  25. *
  26. * RETURN: None
  27. *
  28. * DESCRIPTION: Generic dump buffer in both hex and ascii.
  29. *
  30. ******************************************************************************/
  31. void acpi_ut_dump_buffer(u8 *buffer, u32 count, u32 display, u32 base_offset)
  32. {
  33. u32 i = 0;
  34. u32 j;
  35. u32 temp32;
  36. u8 buf_char;
  37. if (!buffer) {
  38. acpi_os_printf("Null Buffer Pointer in DumpBuffer!\n");
  39. return;
  40. }
  41. if ((count < 4) || (count & 0x01)) {
  42. display = DB_BYTE_DISPLAY;
  43. }
  44. /* Nasty little dump buffer routine! */
  45. while (i < count) {
  46. /* Print current offset */
  47. acpi_os_printf("%8.4X: ", (base_offset + i));
  48. /* Print 16 hex chars */
  49. for (j = 0; j < 16;) {
  50. if (i + j >= count) {
  51. /* Dump fill spaces */
  52. acpi_os_printf("%*s", ((display * 2) + 1), " ");
  53. j += display;
  54. continue;
  55. }
  56. switch (display) {
  57. case DB_BYTE_DISPLAY:
  58. default: /* Default is BYTE display */
  59. acpi_os_printf("%02X ",
  60. buffer[(acpi_size)i + j]);
  61. break;
  62. case DB_WORD_DISPLAY:
  63. ACPI_MOVE_16_TO_32(&temp32,
  64. &buffer[(acpi_size)i + j]);
  65. acpi_os_printf("%04X ", temp32);
  66. break;
  67. case DB_DWORD_DISPLAY:
  68. ACPI_MOVE_32_TO_32(&temp32,
  69. &buffer[(acpi_size)i + j]);
  70. acpi_os_printf("%08X ", temp32);
  71. break;
  72. case DB_QWORD_DISPLAY:
  73. ACPI_MOVE_32_TO_32(&temp32,
  74. &buffer[(acpi_size)i + j]);
  75. acpi_os_printf("%08X", temp32);
  76. ACPI_MOVE_32_TO_32(&temp32,
  77. &buffer[(acpi_size)i + j +
  78. 4]);
  79. acpi_os_printf("%08X ", temp32);
  80. break;
  81. }
  82. j += display;
  83. }
  84. /*
  85. * Print the ASCII equivalent characters but watch out for the bad
  86. * unprintable ones (printable chars are 0x20 through 0x7E)
  87. */
  88. acpi_os_printf(" ");
  89. for (j = 0; j < 16; j++) {
  90. if (i + j >= count) {
  91. acpi_os_printf("\n");
  92. return;
  93. }
  94. /*
  95. * Add comment characters so rest of line is ignored when
  96. * compiled
  97. */
  98. if (j == 0) {
  99. acpi_os_printf("// ");
  100. }
  101. buf_char = buffer[(acpi_size)i + j];
  102. if (isprint(buf_char)) {
  103. acpi_os_printf("%c", buf_char);
  104. } else {
  105. acpi_os_printf(".");
  106. }
  107. }
  108. /* Done with that line. */
  109. acpi_os_printf("\n");
  110. i += 16;
  111. }
  112. return;
  113. }
  114. /*******************************************************************************
  115. *
  116. * FUNCTION: acpi_ut_debug_dump_buffer
  117. *
  118. * PARAMETERS: buffer - Buffer to dump
  119. * count - Amount to dump, in bytes
  120. * display - BYTE, WORD, DWORD, or QWORD display:
  121. * DB_BYTE_DISPLAY
  122. * DB_WORD_DISPLAY
  123. * DB_DWORD_DISPLAY
  124. * DB_QWORD_DISPLAY
  125. * component_ID - Caller's component ID
  126. *
  127. * RETURN: None
  128. *
  129. * DESCRIPTION: Generic dump buffer in both hex and ascii.
  130. *
  131. ******************************************************************************/
  132. void
  133. acpi_ut_debug_dump_buffer(u8 *buffer, u32 count, u32 display, u32 component_id)
  134. {
  135. /* Only dump the buffer if tracing is enabled */
  136. if (!((ACPI_LV_TABLES & acpi_dbg_level) &&
  137. (component_id & acpi_dbg_layer))) {
  138. return;
  139. }
  140. acpi_ut_dump_buffer(buffer, count, display, 0);
  141. }
  142. #ifdef ACPI_APPLICATION
  143. /*******************************************************************************
  144. *
  145. * FUNCTION: acpi_ut_dump_buffer_to_file
  146. *
  147. * PARAMETERS: file - File descriptor
  148. * buffer - Buffer to dump
  149. * count - Amount to dump, in bytes
  150. * display - BYTE, WORD, DWORD, or QWORD display:
  151. * DB_BYTE_DISPLAY
  152. * DB_WORD_DISPLAY
  153. * DB_DWORD_DISPLAY
  154. * DB_QWORD_DISPLAY
  155. * base_offset - Beginning buffer offset (display only)
  156. *
  157. * RETURN: None
  158. *
  159. * DESCRIPTION: Generic dump buffer in both hex and ascii to a file.
  160. *
  161. ******************************************************************************/
  162. void
  163. acpi_ut_dump_buffer_to_file(ACPI_FILE file,
  164. u8 *buffer, u32 count, u32 display, u32 base_offset)
  165. {
  166. u32 i = 0;
  167. u32 j;
  168. u32 temp32;
  169. u8 buf_char;
  170. if (!buffer) {
  171. fprintf(file, "Null Buffer Pointer in DumpBuffer!\n");
  172. return;
  173. }
  174. if ((count < 4) || (count & 0x01)) {
  175. display = DB_BYTE_DISPLAY;
  176. }
  177. /* Nasty little dump buffer routine! */
  178. while (i < count) {
  179. /* Print current offset */
  180. fprintf(file, "%8.4X: ", (base_offset + i));
  181. /* Print 16 hex chars */
  182. for (j = 0; j < 16;) {
  183. if (i + j >= count) {
  184. /* Dump fill spaces */
  185. fprintf(file, "%*s", ((display * 2) + 1), " ");
  186. j += display;
  187. continue;
  188. }
  189. switch (display) {
  190. case DB_BYTE_DISPLAY:
  191. default: /* Default is BYTE display */
  192. fprintf(file, "%02X ",
  193. buffer[(acpi_size)i + j]);
  194. break;
  195. case DB_WORD_DISPLAY:
  196. ACPI_MOVE_16_TO_32(&temp32,
  197. &buffer[(acpi_size)i + j]);
  198. fprintf(file, "%04X ", temp32);
  199. break;
  200. case DB_DWORD_DISPLAY:
  201. ACPI_MOVE_32_TO_32(&temp32,
  202. &buffer[(acpi_size)i + j]);
  203. fprintf(file, "%08X ", temp32);
  204. break;
  205. case DB_QWORD_DISPLAY:
  206. ACPI_MOVE_32_TO_32(&temp32,
  207. &buffer[(acpi_size)i + j]);
  208. fprintf(file, "%08X", temp32);
  209. ACPI_MOVE_32_TO_32(&temp32,
  210. &buffer[(acpi_size)i + j +
  211. 4]);
  212. fprintf(file, "%08X ", temp32);
  213. break;
  214. }
  215. j += display;
  216. }
  217. /*
  218. * Print the ASCII equivalent characters but watch out for the bad
  219. * unprintable ones (printable chars are 0x20 through 0x7E)
  220. */
  221. fprintf(file, " ");
  222. for (j = 0; j < 16; j++) {
  223. if (i + j >= count) {
  224. fprintf(file, "\n");
  225. return;
  226. }
  227. buf_char = buffer[(acpi_size)i + j];
  228. if (isprint(buf_char)) {
  229. fprintf(file, "%c", buf_char);
  230. } else {
  231. fprintf(file, ".");
  232. }
  233. }
  234. /* Done with that line. */
  235. fprintf(file, "\n");
  236. i += 16;
  237. }
  238. return;
  239. }
  240. #endif