xref: /openbmc/linux/drivers/acpi/acpica/utbuffer.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
288ec2860SBob Moore /******************************************************************************
388ec2860SBob Moore  *
488ec2860SBob Moore  * Module Name: utbuffer - Buffer dump routines
588ec2860SBob Moore  *
6*612c2932SBob Moore  * Copyright (C) 2000 - 2023, Intel Corp.
788ec2860SBob Moore  *
895857638SErik Schmauss  *****************************************************************************/
988ec2860SBob Moore 
1088ec2860SBob Moore #include <acpi/acpi.h>
1188ec2860SBob Moore #include "accommon.h"
1288ec2860SBob Moore 
1388ec2860SBob Moore #define _COMPONENT          ACPI_UTILITIES
1488ec2860SBob Moore ACPI_MODULE_NAME("utbuffer")
1588ec2860SBob Moore 
1688ec2860SBob Moore /*******************************************************************************
1788ec2860SBob Moore  *
1888ec2860SBob Moore  * FUNCTION:    acpi_ut_dump_buffer
1988ec2860SBob Moore  *
2088ec2860SBob Moore  * PARAMETERS:  buffer              - Buffer to dump
2188ec2860SBob Moore  *              count               - Amount to dump, in bytes
2288ec2860SBob Moore  *              display             - BYTE, WORD, DWORD, or QWORD display:
2388ec2860SBob Moore  *                                      DB_BYTE_DISPLAY
2488ec2860SBob Moore  *                                      DB_WORD_DISPLAY
2588ec2860SBob Moore  *                                      DB_DWORD_DISPLAY
2688ec2860SBob Moore  *                                      DB_QWORD_DISPLAY
2788ec2860SBob Moore  *              base_offset         - Beginning buffer offset (display only)
2888ec2860SBob Moore  *
2988ec2860SBob Moore  * RETURN:      None
3088ec2860SBob Moore  *
3188ec2860SBob Moore  * DESCRIPTION: Generic dump buffer in both hex and ascii.
3288ec2860SBob Moore  *
3388ec2860SBob Moore  ******************************************************************************/
acpi_ut_dump_buffer(u8 * buffer,u32 count,u32 display,u32 base_offset)3488ec2860SBob Moore void acpi_ut_dump_buffer(u8 *buffer, u32 count, u32 display, u32 base_offset)
3588ec2860SBob Moore {
3688ec2860SBob Moore 	u32 i = 0;
3788ec2860SBob Moore 	u32 j;
3888ec2860SBob Moore 	u32 temp32;
3988ec2860SBob Moore 	u8 buf_char;
40efcf9456SErik Schmauss 	u32 display_data_only = display & DB_DISPLAY_DATA_ONLY;
4188ec2860SBob Moore 
42efcf9456SErik Schmauss 	display &= ~DB_DISPLAY_DATA_ONLY;
4388ec2860SBob Moore 	if (!buffer) {
4488ec2860SBob Moore 		acpi_os_printf("Null Buffer Pointer in DumpBuffer!\n");
4588ec2860SBob Moore 		return;
4688ec2860SBob Moore 	}
4788ec2860SBob Moore 
4888ec2860SBob Moore 	if ((count < 4) || (count & 0x01)) {
4988ec2860SBob Moore 		display = DB_BYTE_DISPLAY;
5088ec2860SBob Moore 	}
5188ec2860SBob Moore 
5288ec2860SBob Moore 	/* Nasty little dump buffer routine! */
5388ec2860SBob Moore 
5488ec2860SBob Moore 	while (i < count) {
5588ec2860SBob Moore 
5688ec2860SBob Moore 		/* Print current offset */
5788ec2860SBob Moore 
58efcf9456SErik Schmauss 		if (!display_data_only) {
5957b758caSBob Moore 			acpi_os_printf("%8.4X: ", (base_offset + i));
60efcf9456SErik Schmauss 		}
6188ec2860SBob Moore 
6288ec2860SBob Moore 		/* Print 16 hex chars */
6388ec2860SBob Moore 
6488ec2860SBob Moore 		for (j = 0; j < 16;) {
6588ec2860SBob Moore 			if (i + j >= count) {
6688ec2860SBob Moore 
6788ec2860SBob Moore 				/* Dump fill spaces */
6888ec2860SBob Moore 
6988ec2860SBob Moore 				acpi_os_printf("%*s", ((display * 2) + 1), " ");
7088ec2860SBob Moore 				j += display;
7188ec2860SBob Moore 				continue;
7288ec2860SBob Moore 			}
7388ec2860SBob Moore 
7488ec2860SBob Moore 			switch (display) {
7588ec2860SBob Moore 			case DB_BYTE_DISPLAY:
7688ec2860SBob Moore 			default:	/* Default is BYTE display */
7788ec2860SBob Moore 
7888ec2860SBob Moore 				acpi_os_printf("%02X ",
7988ec2860SBob Moore 					       buffer[(acpi_size)i + j]);
8088ec2860SBob Moore 				break;
8188ec2860SBob Moore 
8288ec2860SBob Moore 			case DB_WORD_DISPLAY:
8388ec2860SBob Moore 
8488ec2860SBob Moore 				ACPI_MOVE_16_TO_32(&temp32,
8588ec2860SBob Moore 						   &buffer[(acpi_size)i + j]);
8688ec2860SBob Moore 				acpi_os_printf("%04X ", temp32);
8788ec2860SBob Moore 				break;
8888ec2860SBob Moore 
8988ec2860SBob Moore 			case DB_DWORD_DISPLAY:
9088ec2860SBob Moore 
9188ec2860SBob Moore 				ACPI_MOVE_32_TO_32(&temp32,
9288ec2860SBob Moore 						   &buffer[(acpi_size)i + j]);
9388ec2860SBob Moore 				acpi_os_printf("%08X ", temp32);
9488ec2860SBob Moore 				break;
9588ec2860SBob Moore 
9688ec2860SBob Moore 			case DB_QWORD_DISPLAY:
9788ec2860SBob Moore 
9888ec2860SBob Moore 				ACPI_MOVE_32_TO_32(&temp32,
9988ec2860SBob Moore 						   &buffer[(acpi_size)i + j]);
10088ec2860SBob Moore 				acpi_os_printf("%08X", temp32);
10188ec2860SBob Moore 
10288ec2860SBob Moore 				ACPI_MOVE_32_TO_32(&temp32,
10388ec2860SBob Moore 						   &buffer[(acpi_size)i + j +
10488ec2860SBob Moore 							   4]);
10588ec2860SBob Moore 				acpi_os_printf("%08X ", temp32);
10688ec2860SBob Moore 				break;
10788ec2860SBob Moore 			}
10888ec2860SBob Moore 
10988ec2860SBob Moore 			j += display;
11088ec2860SBob Moore 		}
11188ec2860SBob Moore 
11288ec2860SBob Moore 		/*
11388ec2860SBob Moore 		 * Print the ASCII equivalent characters but watch out for the bad
11488ec2860SBob Moore 		 * unprintable ones (printable chars are 0x20 through 0x7E)
11588ec2860SBob Moore 		 */
116efcf9456SErik Schmauss 		if (!display_data_only) {
11788ec2860SBob Moore 			acpi_os_printf(" ");
11888ec2860SBob Moore 			for (j = 0; j < 16; j++) {
11988ec2860SBob Moore 				if (i + j >= count) {
12088ec2860SBob Moore 					acpi_os_printf("\n");
12188ec2860SBob Moore 					return;
12288ec2860SBob Moore 				}
12388ec2860SBob Moore 
124d5a6f6b4SBob Moore 				/*
125d5a6f6b4SBob Moore 				 * Add comment characters so rest of line is ignored when
126d5a6f6b4SBob Moore 				 * compiled
127d5a6f6b4SBob Moore 				 */
128d5a6f6b4SBob Moore 				if (j == 0) {
129d5a6f6b4SBob Moore 					acpi_os_printf("// ");
130d5a6f6b4SBob Moore 				}
131d5a6f6b4SBob Moore 
13288ec2860SBob Moore 				buf_char = buffer[(acpi_size)i + j];
1334fa4616eSBob Moore 				if (isprint(buf_char)) {
13488ec2860SBob Moore 					acpi_os_printf("%c", buf_char);
13588ec2860SBob Moore 				} else {
13688ec2860SBob Moore 					acpi_os_printf(".");
13788ec2860SBob Moore 				}
13888ec2860SBob Moore 			}
13988ec2860SBob Moore 
14088ec2860SBob Moore 			/* Done with that line. */
14188ec2860SBob Moore 
14288ec2860SBob Moore 			acpi_os_printf("\n");
143efcf9456SErik Schmauss 		}
14488ec2860SBob Moore 		i += 16;
14588ec2860SBob Moore 	}
14688ec2860SBob Moore 
14788ec2860SBob Moore 	return;
14888ec2860SBob Moore }
14988ec2860SBob Moore 
15088ec2860SBob Moore /*******************************************************************************
15188ec2860SBob Moore  *
15288ec2860SBob Moore  * FUNCTION:    acpi_ut_debug_dump_buffer
15388ec2860SBob Moore  *
15488ec2860SBob Moore  * PARAMETERS:  buffer              - Buffer to dump
15588ec2860SBob Moore  *              count               - Amount to dump, in bytes
15688ec2860SBob Moore  *              display             - BYTE, WORD, DWORD, or QWORD display:
15788ec2860SBob Moore  *                                      DB_BYTE_DISPLAY
15888ec2860SBob Moore  *                                      DB_WORD_DISPLAY
15988ec2860SBob Moore  *                                      DB_DWORD_DISPLAY
16088ec2860SBob Moore  *                                      DB_QWORD_DISPLAY
16188ec2860SBob Moore  *              component_ID        - Caller's component ID
16288ec2860SBob Moore  *
16388ec2860SBob Moore  * RETURN:      None
16488ec2860SBob Moore  *
16588ec2860SBob Moore  * DESCRIPTION: Generic dump buffer in both hex and ascii.
16688ec2860SBob Moore  *
16788ec2860SBob Moore  ******************************************************************************/
16888ec2860SBob Moore 
16988ec2860SBob Moore void
acpi_ut_debug_dump_buffer(u8 * buffer,u32 count,u32 display,u32 component_id)17088ec2860SBob Moore acpi_ut_debug_dump_buffer(u8 *buffer, u32 count, u32 display, u32 component_id)
17188ec2860SBob Moore {
17288ec2860SBob Moore 
17388ec2860SBob Moore 	/* Only dump the buffer if tracing is enabled */
17488ec2860SBob Moore 
17588ec2860SBob Moore 	if (!((ACPI_LV_TABLES & acpi_dbg_level) &&
17688ec2860SBob Moore 	      (component_id & acpi_dbg_layer))) {
17788ec2860SBob Moore 		return;
17888ec2860SBob Moore 	}
17988ec2860SBob Moore 
18088ec2860SBob Moore 	acpi_ut_dump_buffer(buffer, count, display, 0);
18188ec2860SBob Moore }
182846d6ef4SLv Zheng 
183846d6ef4SLv Zheng #ifdef ACPI_APPLICATION
184846d6ef4SLv Zheng /*******************************************************************************
185846d6ef4SLv Zheng  *
186846d6ef4SLv Zheng  * FUNCTION:    acpi_ut_dump_buffer_to_file
187846d6ef4SLv Zheng  *
188846d6ef4SLv Zheng  * PARAMETERS:  file                - File descriptor
189846d6ef4SLv Zheng  *              buffer              - Buffer to dump
190846d6ef4SLv Zheng  *              count               - Amount to dump, in bytes
191846d6ef4SLv Zheng  *              display             - BYTE, WORD, DWORD, or QWORD display:
192846d6ef4SLv Zheng  *                                      DB_BYTE_DISPLAY
193846d6ef4SLv Zheng  *                                      DB_WORD_DISPLAY
194846d6ef4SLv Zheng  *                                      DB_DWORD_DISPLAY
195846d6ef4SLv Zheng  *                                      DB_QWORD_DISPLAY
196846d6ef4SLv Zheng  *              base_offset         - Beginning buffer offset (display only)
197846d6ef4SLv Zheng  *
198846d6ef4SLv Zheng  * RETURN:      None
199846d6ef4SLv Zheng  *
200846d6ef4SLv Zheng  * DESCRIPTION: Generic dump buffer in both hex and ascii to a file.
201846d6ef4SLv Zheng  *
202846d6ef4SLv Zheng  ******************************************************************************/
203846d6ef4SLv Zheng 
204846d6ef4SLv Zheng void
acpi_ut_dump_buffer_to_file(ACPI_FILE file,u8 * buffer,u32 count,u32 display,u32 base_offset)205846d6ef4SLv Zheng acpi_ut_dump_buffer_to_file(ACPI_FILE file,
206846d6ef4SLv Zheng 			    u8 *buffer, u32 count, u32 display, u32 base_offset)
207846d6ef4SLv Zheng {
208846d6ef4SLv Zheng 	u32 i = 0;
209846d6ef4SLv Zheng 	u32 j;
210846d6ef4SLv Zheng 	u32 temp32;
211846d6ef4SLv Zheng 	u8 buf_char;
212846d6ef4SLv Zheng 
213846d6ef4SLv Zheng 	if (!buffer) {
214f173a775SLv Zheng 		fprintf(file, "Null Buffer Pointer in DumpBuffer!\n");
215846d6ef4SLv Zheng 		return;
216846d6ef4SLv Zheng 	}
217846d6ef4SLv Zheng 
218846d6ef4SLv Zheng 	if ((count < 4) || (count & 0x01)) {
219846d6ef4SLv Zheng 		display = DB_BYTE_DISPLAY;
220846d6ef4SLv Zheng 	}
221846d6ef4SLv Zheng 
222846d6ef4SLv Zheng 	/* Nasty little dump buffer routine! */
223846d6ef4SLv Zheng 
224846d6ef4SLv Zheng 	while (i < count) {
225846d6ef4SLv Zheng 
226846d6ef4SLv Zheng 		/* Print current offset */
227846d6ef4SLv Zheng 
22857b758caSBob Moore 		fprintf(file, "%8.4X: ", (base_offset + i));
229846d6ef4SLv Zheng 
230846d6ef4SLv Zheng 		/* Print 16 hex chars */
231846d6ef4SLv Zheng 
232846d6ef4SLv Zheng 		for (j = 0; j < 16;) {
233846d6ef4SLv Zheng 			if (i + j >= count) {
234846d6ef4SLv Zheng 
235846d6ef4SLv Zheng 				/* Dump fill spaces */
236846d6ef4SLv Zheng 
237f173a775SLv Zheng 				fprintf(file, "%*s", ((display * 2) + 1), " ");
238846d6ef4SLv Zheng 				j += display;
239846d6ef4SLv Zheng 				continue;
240846d6ef4SLv Zheng 			}
241846d6ef4SLv Zheng 
242846d6ef4SLv Zheng 			switch (display) {
243846d6ef4SLv Zheng 			case DB_BYTE_DISPLAY:
244846d6ef4SLv Zheng 			default:	/* Default is BYTE display */
245846d6ef4SLv Zheng 
246f173a775SLv Zheng 				fprintf(file, "%02X ",
247846d6ef4SLv Zheng 					buffer[(acpi_size)i + j]);
248846d6ef4SLv Zheng 				break;
249846d6ef4SLv Zheng 
250846d6ef4SLv Zheng 			case DB_WORD_DISPLAY:
251846d6ef4SLv Zheng 
252846d6ef4SLv Zheng 				ACPI_MOVE_16_TO_32(&temp32,
253846d6ef4SLv Zheng 						   &buffer[(acpi_size)i + j]);
254f173a775SLv Zheng 				fprintf(file, "%04X ", temp32);
255846d6ef4SLv Zheng 				break;
256846d6ef4SLv Zheng 
257846d6ef4SLv Zheng 			case DB_DWORD_DISPLAY:
258846d6ef4SLv Zheng 
259846d6ef4SLv Zheng 				ACPI_MOVE_32_TO_32(&temp32,
260846d6ef4SLv Zheng 						   &buffer[(acpi_size)i + j]);
261f173a775SLv Zheng 				fprintf(file, "%08X ", temp32);
262846d6ef4SLv Zheng 				break;
263846d6ef4SLv Zheng 
264846d6ef4SLv Zheng 			case DB_QWORD_DISPLAY:
265846d6ef4SLv Zheng 
266846d6ef4SLv Zheng 				ACPI_MOVE_32_TO_32(&temp32,
267846d6ef4SLv Zheng 						   &buffer[(acpi_size)i + j]);
268f173a775SLv Zheng 				fprintf(file, "%08X", temp32);
269846d6ef4SLv Zheng 
270846d6ef4SLv Zheng 				ACPI_MOVE_32_TO_32(&temp32,
271846d6ef4SLv Zheng 						   &buffer[(acpi_size)i + j +
272846d6ef4SLv Zheng 							   4]);
273f173a775SLv Zheng 				fprintf(file, "%08X ", temp32);
274846d6ef4SLv Zheng 				break;
275846d6ef4SLv Zheng 			}
276846d6ef4SLv Zheng 
277846d6ef4SLv Zheng 			j += display;
278846d6ef4SLv Zheng 		}
279846d6ef4SLv Zheng 
280846d6ef4SLv Zheng 		/*
281846d6ef4SLv Zheng 		 * Print the ASCII equivalent characters but watch out for the bad
282846d6ef4SLv Zheng 		 * unprintable ones (printable chars are 0x20 through 0x7E)
283846d6ef4SLv Zheng 		 */
284f173a775SLv Zheng 		fprintf(file, " ");
285846d6ef4SLv Zheng 		for (j = 0; j < 16; j++) {
286846d6ef4SLv Zheng 			if (i + j >= count) {
287f173a775SLv Zheng 				fprintf(file, "\n");
288846d6ef4SLv Zheng 				return;
289846d6ef4SLv Zheng 			}
290846d6ef4SLv Zheng 
291846d6ef4SLv Zheng 			buf_char = buffer[(acpi_size)i + j];
2924fa4616eSBob Moore 			if (isprint(buf_char)) {
293f173a775SLv Zheng 				fprintf(file, "%c", buf_char);
294846d6ef4SLv Zheng 			} else {
295f173a775SLv Zheng 				fprintf(file, ".");
296846d6ef4SLv Zheng 			}
297846d6ef4SLv Zheng 		}
298846d6ef4SLv Zheng 
299846d6ef4SLv Zheng 		/* Done with that line. */
300846d6ef4SLv Zheng 
301f173a775SLv Zheng 		fprintf(file, "\n");
302846d6ef4SLv Zheng 		i += 16;
303846d6ef4SLv Zheng 	}
304846d6ef4SLv Zheng 
305846d6ef4SLv Zheng 	return;
306846d6ef4SLv Zheng }
307846d6ef4SLv Zheng #endif
308