utbuffer.c 7.1 KB

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