hv: add input vm_id check for hv shell commands

For hv shell commands with vm_id option, need to check it is in the valid range [0, CONFIG_MAX_VM].
This commit checks the input vm_id, if it's not in the valid range, an error message will be given,
and vm_id will be assgined to default 0U.

Tracked-On: #2520
Signed-off-by: Yan, Like <like.yan@intel.com>
Acked-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Yan, Like 2019-03-29 13:00:50 +08:00 committed by ACRN System Integration
parent e35f27a525
commit ab62261a57
1 changed files with 19 additions and 3 deletions

View File

@ -238,6 +238,22 @@ static void shell_puts(const char *string_ptr)
SHELL_STRING_MAX_LEN));
}
static uint16_t sanitize_vmid(uint16_t vmid)
{
uint16_t sanitized_vmid = vmid;
char temp_str[TEMP_STR_SIZE];
if (vmid >= CONFIG_MAX_VM_NUM) {
snprintf(temp_str, MAX_STR_SIZE,
"VM ID given exceeds the MAX_VM_NUM(%u), using 0 instead\r\n",
CONFIG_MAX_VM_NUM);
shell_puts(temp_str);
sanitized_vmid = 0U;
}
return sanitized_vmid;
}
static void shell_handle_special_char(char ch)
{
switch (ch) {
@ -734,7 +750,7 @@ static int32_t shell_vcpu_dumpreg(int32_t argc, char **argv)
if (status < 0) {
goto out;
}
vm_id = (uint16_t)status;
vm_id = sanitize_vmid((uint16_t)status);
vcpu_id = (uint16_t)strtol_deci(argv[2]);
vm = get_vm_from_vmid(vm_id);
@ -829,7 +845,7 @@ static int32_t shell_to_sos_console(__unused int32_t argc, __unused char **argv)
struct acrn_vm_config *vm_config;
if (argc == 2U) {
guest_no = strtol_deci(argv[1]);
guest_no = sanitize_vmid(strtol_deci(argv[1]));
}
vuart_vmid = guest_no;
@ -1115,7 +1131,7 @@ static int32_t shell_show_vioapic_info(int32_t argc, char **argv)
}
ret = strtol_deci(argv[1]);
if (ret >= 0) {
vmid = (uint16_t) ret;
vmid = sanitize_vmid((uint16_t) ret);
get_vioapic_info(shell_log_buf, SHELL_LOG_BUF_SIZE, vmid);
shell_puts(shell_log_buf);
return 0;