hv: cleanup the shell cmd code.

Remove cmd register API. And use static pre-defined cmd array
instead.

Signed-off-by: Yin Fengwei <fengwei.yin@intel.com>
Acked-by: Eddie Dong <Eddie.dong@intel.com>
This commit is contained in:
Yin Fengwei 2018-05-16 10:46:58 +08:00 committed by lijinxia
parent edb26a7e17
commit 03a3fb0d2a
3 changed files with 127 additions and 337 deletions

View File

@ -266,27 +266,19 @@ static int shell_process(struct shell *p_shell)
struct shell_cmd *shell_find_cmd(struct shell *p_shell, const char *cmd_str) struct shell_cmd *shell_find_cmd(struct shell *p_shell, const char *cmd_str)
{ {
struct shell_cmd *p_cmd; uint32_t i;
bool is_found = false; struct shell_cmd *p_cmd = NULL;
struct list_head *pos;
p_cmd = NULL;
if (p_shell->cmd_count <= 0) if (p_shell->cmd_count <= 0)
return NULL; return NULL;
list_for_each(pos, &p_shell->cmd_list) { for (i = 0; i < p_shell->cmd_count; i++) {
p_cmd = list_entry(pos, struct shell_cmd, node); p_cmd = &p_shell->shell_cmd[i];
pr_dbg("shell: cmd in registered list is '%s' in %s", if (strcmp(p_cmd->str, cmd_str) == 0)
p_cmd->str, __func__);
if (strcmp(p_cmd->str, cmd_str) == 0) {
is_found = true;
break; break;
}
} }
if (!is_found) { if (i == p_shell->cmd_count) {
/* No commands in the list. */ /* No commands in the list. */
p_cmd = NULL; p_cmd = NULL;
} }
@ -360,7 +352,7 @@ int shell_process_cmd(struct shell *p_shell, char *p_input_line)
/* Determine if there is a command to process. */ /* Determine if there is a command to process. */
if (cmd_argc != 0) { if (cmd_argc != 0) {
/* See if command is in the registered command list. */ /* See if command is in cmds supported */
p_cmd = shell_find_cmd(p_shell, cmd_argv[0]); p_cmd = shell_find_cmd(p_shell, cmd_argv[0]);
if (p_cmd != NULL) { if (p_cmd != NULL) {
@ -412,7 +404,6 @@ int shell_cmd_help(struct shell *p_shell,
{ {
int status = 0; int status = 0;
int spaces = 0; int spaces = 0;
int i;
struct shell_cmd *p_cmd = NULL; struct shell_cmd *p_cmd = NULL;
char space_buf[MAX_INDENT_LEN + 1]; char space_buf[MAX_INDENT_LEN + 1];
@ -428,11 +419,11 @@ int shell_cmd_help(struct shell *p_shell,
/* No registered commands */ /* No registered commands */
shell_puts(p_shell, "NONE\r\n"); shell_puts(p_shell, "NONE\r\n");
} else { } else {
struct list_head *pos; int i = 0;
uint32_t j;
i = 0; for (j = 0; j < p_shell->cmd_count; j++) {
list_for_each(pos, &p_shell->cmd_list) { p_cmd = &p_shell->shell_cmd[j];
p_cmd = list_entry(pos, struct shell_cmd, node);
/* Check if we've filled the screen with info */ /* Check if we've filled the screen with info */
/* i + 1 used to avoid 0%SHELL_ROWS=0 */ /* i + 1 used to avoid 0%SHELL_ROWS=0 */
@ -1132,11 +1123,7 @@ int shell_construct(struct shell **p_shell)
/* Allocate memory for shell session */ /* Allocate memory for shell session */
*p_shell = (struct shell *) calloc(1, sizeof(**p_shell)); *p_shell = (struct shell *) calloc(1, sizeof(**p_shell));
if (*p_shell) { if (!(*p_shell)) {
/* Zero-initialize the service control block. */
INIT_LIST_HEAD(&(*p_shell)->cmd_list);
(*p_shell)->cmd_count = 0;
} else {
pr_err("Error: out of memory"); pr_err("Error: out of memory");
status = -ENOMEM; status = -ENOMEM;
} }

View File

@ -53,14 +53,15 @@ struct shell_io {
#define SHELL_STRING_MAX_LEN (CPU_PAGE_SIZE << 2) #define SHELL_STRING_MAX_LEN (CPU_PAGE_SIZE << 2)
/* Shell Control Block */ /* Shell Control Block */
struct shell_cmd;
struct shell { struct shell {
struct shell_io session_io; /* Session I/O information */ struct shell_io session_io; /* Session I/O information */
char input_line[2][SHELL_CMD_MAX_LEN + 1]; /* current & last */ char input_line[2][SHELL_CMD_MAX_LEN + 1]; /* current & last */
char name[SHELL_NAME_MAX_LEN]; /* Session name */ char name[SHELL_NAME_MAX_LEN]; /* Session name */
uint32_t input_line_len; /* Length of current input line */ uint32_t input_line_len; /* Length of current input line */
uint32_t input_line_active; /* Active input line index */ uint32_t input_line_active; /* Active input line index */
struct list_head cmd_list; /* List of registered commands */ struct shell_cmd *shell_cmd; /* cmds supported */
uint32_t cmd_count; /* Count of added commands */ uint32_t cmd_count; /* Count of cmds supported */
}; };
/* Shell Command Function */ /* Shell Command Function */
@ -68,7 +69,6 @@ typedef int (*shell_cmd_fn_t)(struct shell *, int, char **);
/* Shell Command */ /* Shell Command */
struct shell_cmd { struct shell_cmd {
struct list_head node; /* Linked list node */
char *str; /* Command string */ char *str; /* Command string */
char *cmd_param; /* Command parameter string */ char *cmd_param; /* Command parameter string */
char *help_str; /* Help text associated with the command */ char *help_str; /* Help text associated with the command */

View File

@ -38,113 +38,116 @@
/* Shell that uses serial I/O */ /* Shell that uses serial I/O */
static struct shell *serial_session; static struct shell *serial_session;
static int shell_register_cmd(struct shell *p_shell, static struct shell_cmd acrn_cmd[] = {
const char *cmd, {
const char *cmd_param, .str = SHELL_CMD_HELP,
const char *cmd_help_str, .cmd_param = SHELL_CMD_HELP_PARAM,
int (*cmd_fcn)(struct shell *, int, char **)) .help_str = SHELL_CMD_HELP_HELP,
{ .fcn = shell_cmd_help,
int status = 0; },
struct shell_cmd *p_cmd; {
uint32_t cmd_mem_size; .str = SHELL_CMD_VM_LIST,
.cmd_param = SHELL_CMD_VM_LIST_PARAM,
if ((p_shell == NULL) || (cmd == NULL) || .help_str = SHELL_CMD_VM_LIST_HELP,
(cmd_help_str == NULL) || (cmd_fcn == NULL)) { .fcn = shell_list_vm,
return -EINVAL; },
} {
.str = SHELL_CMD_VCPU_LIST,
/* Check if a duplicate command exists */ .cmd_param = SHELL_CMD_VCPU_LIST_PARAM,
p_cmd = shell_find_cmd(p_shell, cmd); .help_str = SHELL_CMD_VCPU_LIST_HELP,
if (p_cmd != NULL) { .fcn = shell_list_vcpu,
/* Requested command is already registered */ },
pr_err("Error: Command %s is already registered.", cmd); {
status = -EINVAL; .str = SHELL_CMD_VCPU_PAUSE,
goto exit; .cmd_param = SHELL_CMD_VCPU_PAUSE_PARAM,
} .help_str = SHELL_CMD_VCPU_PAUSE_HELP,
.fcn = shell_pause_vcpu,
/* Requested command is not already registered. So allocate enough },
* memory for the command structure and the command, parameter and the {
* help text strings along with the corresponding null terminating .str = SHELL_CMD_VCPU_RESUME,
* character/s. .cmd_param = SHELL_CMD_VCPU_RESUME_PARAM,
*/ .help_str = SHELL_CMD_VCPU_RESUME_HELP,
cmd_mem_size = sizeof(struct shell_cmd) .fcn = shell_resume_vcpu,
+ (strnlen_s(cmd, SHELL_CMD_MAX_LEN) + 1); },
{
/* If command takes any parameters, need to allocate memory for storing .str = SHELL_CMD_VCPU_DUMPREG,
* parameter string. .cmd_param = SHELL_CMD_VCPU_DUMPREG_PARAM,
*/ .help_str = SHELL_CMD_VCPU_DUMPREG_HELP,
if (cmd_param) .fcn = shell_vcpu_dumpreg,
cmd_mem_size += strnlen_s(cmd_param, SHELL_PARA_MAX_LEN) + 1; },
{
/* If help text is provided for command, need to allocate memory for .str = SHELL_CMD_VCPU_DUMPMEM,
* storing help string. .cmd_param = SHELL_CMD_VCPU_DUMPMEM_PARAM,
*/ .help_str = SHELL_CMD_VCPU_DUMPMEM_HELP,
if (cmd_help_str) .fcn = shell_vcpu_dumpmem,
cmd_mem_size += strnlen_s(cmd_help_str, SHELL_HELP_MAX_LEN) + 1; },
{
p_cmd = (struct shell_cmd *) calloc(1, cmd_mem_size); .str = SHELL_CMD_VM_CONSOLE,
if (p_cmd == NULL) { .cmd_param = SHELL_CMD_VM_CONSOLE_PARAM,
status = -ENOMEM; .help_str = SHELL_CMD_VM_CONSOLE_HELP,
goto exit; .fcn = shell_to_sos_console,
} },
{
/* The command structure, command string, it's parameter string and .str = SHELL_CMD_INTERRUPT,
* the associated help string are all stored in contiguous memory .cmd_param = SHELL_CMD_INTERRUPT_PARAM,
* locations. So the cmd string immediately follows the command .help_str = SHELL_CMD_INTERRUPT_HELP,
* structure.. .fcn = shell_show_cpu_int,
*/ },
p_cmd->str = (char *)p_cmd + sizeof(struct shell_cmd); {
strncpy_s(p_cmd->str, SHELL_CMD_MAX_LEN, cmd, SHELL_CMD_MAX_LEN); .str = SHELL_CMD_PTDEV,
.cmd_param = SHELL_CMD_PTDEV_PARAM,
/* Check if this command does take any parameters... */ .help_str = SHELL_CMD_PTDEV_HELP,
if (cmd_param) { .fcn = shell_show_ptdev_info,
/* The command parameter string immediately follows the command },
* string in memory. {
*/ .str = SHELL_CMD_REQ,
p_cmd->cmd_param = p_cmd->str .cmd_param = SHELL_CMD_REQ_PARAM,
+ (strnlen_s(cmd, SHELL_CMD_MAX_LEN) + 1); .help_str = SHELL_CMD_REQ_HELP,
strcpy_s(p_cmd->cmd_param, SHELL_PARA_MAX_LEN, cmd_param); .fcn = shell_show_req_info,
} },
{
/* Check if help string is provided for the command.. */ .str = SHELL_CMD_VIOAPIC,
if (cmd_help_str) { .cmd_param = SHELL_CMD_VIOAPIC_PARAM,
if (cmd_param) { .help_str = SHELL_CMD_VIOAPIC_HELP,
/* The command help string immediately follows the .fcn = shell_show_vioapic_info,
* parameter string in memory | cmd_structure | },
* cmd_str | param_str | help_str | {
*/ .str = SHELL_CMD_IOAPIC,
p_cmd->help_str = p_cmd->cmd_param + .cmd_param = SHELL_CMD_IOAPIC_PARAM,
(strnlen_s(cmd_param, SHELL_PARA_MAX_LEN) + 1); .help_str = SHELL_CMD_IOAPIC_HELP,
.fcn = shell_show_ioapic_info,
strcpy_s(p_cmd->help_str, },
SHELL_HELP_MAX_LEN, cmd_help_str); {
} else { .str = SHELL_CMD_VMEXIT,
/* No command parameter/s. Help string immediately .cmd_param = SHELL_CMD_VMEXIT_PARAM,
* follows the cmd string | cmd_structure | cmd_str | .help_str = SHELL_CMD_VMEXIT_HELP,
* help_str | .fcn = shell_show_vmexit_profile,
*/ },
p_cmd->help_str = p_cmd->str + {
(strnlen_s(cmd, SHELL_CMD_MAX_LEN) + 1); .str = SHELL_CMD_LOGDUMP,
.cmd_param = SHELL_CMD_LOGDUMP_PARAM,
strcpy_s(p_cmd->help_str, .help_str = SHELL_CMD_LOGDUMP_HELP,
SHELL_HELP_MAX_LEN, cmd_help_str); .fcn = shell_dump_logbuf,
} },
} {
.str = SHELL_CMD_GET_LOG_LVL,
/* Set the command function. */ .cmd_param = SHELL_CMD_GET_LOG_LVL_PARAM,
p_cmd->fcn = cmd_fcn; .help_str = SHELL_CMD_GET_LOG_LVL_HELP,
.fcn = shell_get_loglevel,
INIT_LIST_HEAD(&p_cmd->node); },
list_add(&p_cmd->node, &p_shell->cmd_list); {
.str = SHELL_CMD_SET_LOG_LVL,
/* Update command count. */ .cmd_param = SHELL_CMD_SET_LOG_LVL_PARAM,
p_shell->cmd_count++; .help_str = SHELL_CMD_SET_LOG_LVL_HELP,
.fcn = shell_set_loglevel,
status = 0; },
{
exit: .str = SHELL_CMD_CPUID,
return status; .cmd_param = SHELL_CMD_CPUID_PARAM,
} .help_str = SHELL_CMD_CPUID_HELP,
.fcn = shell_cpuid,
},
};
int shell_init(void) int shell_init(void)
{ {
@ -162,213 +165,13 @@ int shell_init(void)
serial_session->session_io.io_special = shell_special_serial; serial_session->session_io.io_special = shell_special_serial;
serial_session->session_io.io_echo_on = (bool)true; serial_session->session_io.io_echo_on = (bool)true;
serial_session->shell_cmd = acrn_cmd;
serial_session->cmd_count = ARRAY_SIZE(acrn_cmd);
/* Initialize the handler for the serial port that will be used /* Initialize the handler for the serial port that will be used
* for shell i/p and o/p * for shell i/p and o/p
*/ */
status = serial_session->session_io.io_init(serial_session); status = serial_session->session_io.io_init(serial_session);
/* Register command handlers for the shell commands that are available
* by default
*/
if (status == 0) {
status = shell_register_cmd(serial_session,
SHELL_CMD_HELP,
SHELL_CMD_HELP_PARAM,
SHELL_CMD_HELP_HELP,
shell_cmd_help);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_HELP);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VM_LIST,
SHELL_CMD_VM_LIST_PARAM,
SHELL_CMD_VM_LIST_HELP,
shell_list_vm);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VM_LIST);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VCPU_LIST,
SHELL_CMD_VCPU_LIST_PARAM,
SHELL_CMD_VCPU_LIST_HELP,
shell_list_vcpu);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VCPU_LIST);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VCPU_PAUSE,
SHELL_CMD_VCPU_PAUSE_PARAM,
SHELL_CMD_VCPU_PAUSE_HELP,
shell_pause_vcpu);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VCPU_PAUSE);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VCPU_RESUME,
SHELL_CMD_VCPU_RESUME_PARAM,
SHELL_CMD_VCPU_RESUME_HELP,
shell_resume_vcpu);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VCPU_RESUME);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VCPU_DUMPREG,
SHELL_CMD_VCPU_DUMPREG_PARAM,
SHELL_CMD_VCPU_DUMPREG_HELP,
shell_vcpu_dumpreg);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VCPU_DUMPREG);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VCPU_DUMPMEM,
SHELL_CMD_VCPU_DUMPMEM_PARAM,
SHELL_CMD_VCPU_DUMPMEM_HELP,
shell_vcpu_dumpmem);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VCPU_DUMPMEM);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VM_CONSOLE,
SHELL_CMD_VM_CONSOLE_PARAM,
SHELL_CMD_VM_CONSOLE_HELP,
shell_to_sos_console);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VM_CONSOLE);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_INTERRUPT,
SHELL_CMD_INTERRUPT_PARAM,
SHELL_CMD_INTERRUPT_HELP,
shell_show_cpu_int);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_INTERRUPT);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_PTDEV,
SHELL_CMD_PTDEV_PARAM,
SHELL_CMD_PTDEV_HELP,
shell_show_ptdev_info);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_PTDEV);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_REQ,
SHELL_CMD_REQ_PARAM,
SHELL_CMD_REQ_HELP,
shell_show_req_info);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_REQ);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VIOAPIC,
SHELL_CMD_VIOAPIC_PARAM,
SHELL_CMD_VIOAPIC_HELP,
shell_show_vioapic_info);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VIOAPIC);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_IOAPIC,
SHELL_CMD_IOAPIC_PARAM,
SHELL_CMD_IOAPIC_HELP,
shell_show_ioapic_info);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_IOAPIC);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_VMEXIT,
SHELL_CMD_VMEXIT_PARAM,
SHELL_CMD_VMEXIT_HELP,
shell_show_vmexit_profile);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_VMEXIT);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_LOGDUMP,
SHELL_CMD_LOGDUMP_PARAM,
SHELL_CMD_LOGDUMP_HELP,
shell_dump_logbuf);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_LOGDUMP);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_GET_LOG_LVL,
SHELL_CMD_GET_LOG_LVL_PARAM,
SHELL_CMD_GET_LOG_LVL_HELP,
shell_get_loglevel);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_GET_LOG_LVL);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_SET_LOG_LVL,
SHELL_CMD_SET_LOG_LVL_PARAM,
SHELL_CMD_SET_LOG_LVL_HELP,
shell_set_loglevel);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_SET_LOG_LVL);
}
status = shell_register_cmd(serial_session,
SHELL_CMD_CPUID,
SHELL_CMD_CPUID_PARAM,
SHELL_CMD_CPUID_HELP,
shell_cpuid);
if (status != 0) {
pr_err("Error: Command \"%s\" registration failed.",
SHELL_CMD_CPUID);
}
}
return status; return status;
} }