xref: /openbmc/linux/drivers/acpi/acpica/dbhistry.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
299575102SLv Zheng /******************************************************************************
399575102SLv Zheng  *
499575102SLv Zheng  * Module Name: dbhistry - debugger HISTORY command
599575102SLv Zheng  *
6*612c2932SBob Moore  * Copyright (C) 2000 - 2023, Intel Corp.
799575102SLv Zheng  *
895857638SErik Schmauss  *****************************************************************************/
999575102SLv Zheng 
1099575102SLv Zheng #include <acpi/acpi.h>
1199575102SLv Zheng #include "accommon.h"
1299575102SLv Zheng #include "acdebug.h"
1399575102SLv Zheng 
1499575102SLv Zheng #define _COMPONENT          ACPI_CA_DEBUGGER
1599575102SLv Zheng ACPI_MODULE_NAME("dbhistry")
1699575102SLv Zheng 
1799575102SLv Zheng #define HI_NO_HISTORY       0
1899575102SLv Zheng #define HI_RECORD_HISTORY   1
1999575102SLv Zheng #define HISTORY_SIZE        40
2099575102SLv Zheng typedef struct history_info {
2199575102SLv Zheng 	char *command;
2299575102SLv Zheng 	u32 cmd_num;
2399575102SLv Zheng 
2499575102SLv Zheng } HISTORY_INFO;
2599575102SLv Zheng 
2699575102SLv Zheng static HISTORY_INFO acpi_gbl_history_buffer[HISTORY_SIZE];
2799575102SLv Zheng static u16 acpi_gbl_lo_history = 0;
2899575102SLv Zheng static u16 acpi_gbl_num_history = 0;
2999575102SLv Zheng static u16 acpi_gbl_next_history_index = 0;
3099575102SLv Zheng 
3199575102SLv Zheng /*******************************************************************************
3299575102SLv Zheng  *
3399575102SLv Zheng  * FUNCTION:    acpi_db_add_to_history
3499575102SLv Zheng  *
3599575102SLv Zheng  * PARAMETERS:  command_line    - Command to add
3699575102SLv Zheng  *
3799575102SLv Zheng  * RETURN:      None
3899575102SLv Zheng  *
3999575102SLv Zheng  * DESCRIPTION: Add a command line to the history buffer.
4099575102SLv Zheng  *
4199575102SLv Zheng  ******************************************************************************/
4299575102SLv Zheng 
acpi_db_add_to_history(char * command_line)4399575102SLv Zheng void acpi_db_add_to_history(char *command_line)
4499575102SLv Zheng {
4599575102SLv Zheng 	u16 cmd_len;
4699575102SLv Zheng 	u16 buffer_len;
4799575102SLv Zheng 
4899575102SLv Zheng 	/* Put command into the next available slot */
4999575102SLv Zheng 
5099575102SLv Zheng 	cmd_len = (u16)strlen(command_line);
5199575102SLv Zheng 	if (!cmd_len) {
5299575102SLv Zheng 		return;
5399575102SLv Zheng 	}
5499575102SLv Zheng 
5599575102SLv Zheng 	if (acpi_gbl_history_buffer[acpi_gbl_next_history_index].command !=
5699575102SLv Zheng 	    NULL) {
5799575102SLv Zheng 		buffer_len =
5899575102SLv Zheng 		    (u16)
5999575102SLv Zheng 		    strlen(acpi_gbl_history_buffer[acpi_gbl_next_history_index].
6099575102SLv Zheng 			   command);
6199575102SLv Zheng 
6299575102SLv Zheng 		if (cmd_len > buffer_len) {
6399575102SLv Zheng 			acpi_os_free(acpi_gbl_history_buffer
6499575102SLv Zheng 				     [acpi_gbl_next_history_index].command);
6599575102SLv Zheng 			acpi_gbl_history_buffer[acpi_gbl_next_history_index].
6699575102SLv Zheng 			    command = acpi_os_allocate(cmd_len + 1);
6799575102SLv Zheng 		}
6899575102SLv Zheng 	} else {
6999575102SLv Zheng 		acpi_gbl_history_buffer[acpi_gbl_next_history_index].command =
7099575102SLv Zheng 		    acpi_os_allocate(cmd_len + 1);
7199575102SLv Zheng 	}
7299575102SLv Zheng 
7399575102SLv Zheng 	strcpy(acpi_gbl_history_buffer[acpi_gbl_next_history_index].command,
7499575102SLv Zheng 	       command_line);
7599575102SLv Zheng 
7699575102SLv Zheng 	acpi_gbl_history_buffer[acpi_gbl_next_history_index].cmd_num =
7799575102SLv Zheng 	    acpi_gbl_next_cmd_num;
7899575102SLv Zheng 
7999575102SLv Zheng 	/* Adjust indexes */
8099575102SLv Zheng 
8199575102SLv Zheng 	if ((acpi_gbl_num_history == HISTORY_SIZE) &&
8299575102SLv Zheng 	    (acpi_gbl_next_history_index == acpi_gbl_lo_history)) {
8399575102SLv Zheng 		acpi_gbl_lo_history++;
8499575102SLv Zheng 		if (acpi_gbl_lo_history >= HISTORY_SIZE) {
8599575102SLv Zheng 			acpi_gbl_lo_history = 0;
8699575102SLv Zheng 		}
8799575102SLv Zheng 	}
8899575102SLv Zheng 
8999575102SLv Zheng 	acpi_gbl_next_history_index++;
9099575102SLv Zheng 	if (acpi_gbl_next_history_index >= HISTORY_SIZE) {
9199575102SLv Zheng 		acpi_gbl_next_history_index = 0;
9299575102SLv Zheng 	}
9399575102SLv Zheng 
9499575102SLv Zheng 	acpi_gbl_next_cmd_num++;
9599575102SLv Zheng 	if (acpi_gbl_num_history < HISTORY_SIZE) {
9699575102SLv Zheng 		acpi_gbl_num_history++;
9799575102SLv Zheng 	}
9899575102SLv Zheng }
9999575102SLv Zheng 
10099575102SLv Zheng /*******************************************************************************
10199575102SLv Zheng  *
10299575102SLv Zheng  * FUNCTION:    acpi_db_display_history
10399575102SLv Zheng  *
10499575102SLv Zheng  * PARAMETERS:  None
10599575102SLv Zheng  *
10699575102SLv Zheng  * RETURN:      None
10799575102SLv Zheng  *
10899575102SLv Zheng  * DESCRIPTION: Display the contents of the history buffer
10999575102SLv Zheng  *
11099575102SLv Zheng  ******************************************************************************/
11199575102SLv Zheng 
acpi_db_display_history(void)11299575102SLv Zheng void acpi_db_display_history(void)
11399575102SLv Zheng {
11499575102SLv Zheng 	u32 i;
11599575102SLv Zheng 	u16 history_index;
11699575102SLv Zheng 
11799575102SLv Zheng 	history_index = acpi_gbl_lo_history;
11899575102SLv Zheng 
11999575102SLv Zheng 	/* Dump entire history buffer */
12099575102SLv Zheng 
12199575102SLv Zheng 	for (i = 0; i < acpi_gbl_num_history; i++) {
12299575102SLv Zheng 		if (acpi_gbl_history_buffer[history_index].command) {
123231ec06eSBob Moore 			acpi_os_printf("%3u %s\n",
12499575102SLv Zheng 				       acpi_gbl_history_buffer[history_index].
12599575102SLv Zheng 				       cmd_num,
12699575102SLv Zheng 				       acpi_gbl_history_buffer[history_index].
12799575102SLv Zheng 				       command);
12899575102SLv Zheng 		}
12999575102SLv Zheng 
13099575102SLv Zheng 		history_index++;
13199575102SLv Zheng 		if (history_index >= HISTORY_SIZE) {
13299575102SLv Zheng 			history_index = 0;
13399575102SLv Zheng 		}
13499575102SLv Zheng 	}
13599575102SLv Zheng }
13699575102SLv Zheng 
13799575102SLv Zheng /*******************************************************************************
13899575102SLv Zheng  *
13999575102SLv Zheng  * FUNCTION:    acpi_db_get_from_history
14099575102SLv Zheng  *
14199575102SLv Zheng  * PARAMETERS:  command_num_arg         - String containing the number of the
14299575102SLv Zheng  *                                        command to be retrieved
14399575102SLv Zheng  *
14499575102SLv Zheng  * RETURN:      Pointer to the retrieved command. Null on error.
14599575102SLv Zheng  *
14699575102SLv Zheng  * DESCRIPTION: Get a command from the history buffer
14799575102SLv Zheng  *
14899575102SLv Zheng  ******************************************************************************/
14999575102SLv Zheng 
acpi_db_get_from_history(char * command_num_arg)15099575102SLv Zheng char *acpi_db_get_from_history(char *command_num_arg)
15199575102SLv Zheng {
15299575102SLv Zheng 	u32 cmd_num;
15399575102SLv Zheng 
15499575102SLv Zheng 	if (command_num_arg == NULL) {
15599575102SLv Zheng 		cmd_num = acpi_gbl_next_cmd_num - 1;
15699575102SLv Zheng 	}
15799575102SLv Zheng 
15899575102SLv Zheng 	else {
15999575102SLv Zheng 		cmd_num = strtoul(command_num_arg, NULL, 0);
16099575102SLv Zheng 	}
16199575102SLv Zheng 
16299575102SLv Zheng 	return (acpi_db_get_history_by_index(cmd_num));
16399575102SLv Zheng }
16499575102SLv Zheng 
16599575102SLv Zheng /*******************************************************************************
16699575102SLv Zheng  *
16799575102SLv Zheng  * FUNCTION:    acpi_db_get_history_by_index
16899575102SLv Zheng  *
16999575102SLv Zheng  * PARAMETERS:  cmd_num             - Index of the desired history entry.
17099575102SLv Zheng  *                                    Values are 0...(acpi_gbl_next_cmd_num - 1)
17199575102SLv Zheng  *
17299575102SLv Zheng  * RETURN:      Pointer to the retrieved command. Null on error.
17399575102SLv Zheng  *
17499575102SLv Zheng  * DESCRIPTION: Get a command from the history buffer
17599575102SLv Zheng  *
17699575102SLv Zheng  ******************************************************************************/
17799575102SLv Zheng 
acpi_db_get_history_by_index(u32 cmd_num)17899575102SLv Zheng char *acpi_db_get_history_by_index(u32 cmd_num)
17999575102SLv Zheng {
18099575102SLv Zheng 	u32 i;
18199575102SLv Zheng 	u16 history_index;
18299575102SLv Zheng 
18399575102SLv Zheng 	/* Search history buffer */
18499575102SLv Zheng 
18599575102SLv Zheng 	history_index = acpi_gbl_lo_history;
18699575102SLv Zheng 	for (i = 0; i < acpi_gbl_num_history; i++) {
18799575102SLv Zheng 		if (acpi_gbl_history_buffer[history_index].cmd_num == cmd_num) {
18899575102SLv Zheng 
18999575102SLv Zheng 			/* Found the command, return it */
19099575102SLv Zheng 
19199575102SLv Zheng 			return (acpi_gbl_history_buffer[history_index].command);
19299575102SLv Zheng 		}
19399575102SLv Zheng 
19499575102SLv Zheng 		/* History buffer is circular */
19599575102SLv Zheng 
19699575102SLv Zheng 		history_index++;
19799575102SLv Zheng 		if (history_index >= HISTORY_SIZE) {
19899575102SLv Zheng 			history_index = 0;
19999575102SLv Zheng 		}
20099575102SLv Zheng 	}
20199575102SLv Zheng 
20299575102SLv Zheng 	acpi_os_printf("Invalid history number: %u\n", history_index);
20399575102SLv Zheng 	return (NULL);
20499575102SLv Zheng }
205