| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | #include "qemu/osdep.h" |
| 3 | #include "qemu/target-info.h" |
| 4 | #include "qapi/error.h" |
| 5 | #include "qapi/qapi-commands-misc.h" |
| 6 | #include "qobject/qlist.h" |
| 7 | #include "qemu/option.h" |
| 8 | #include "qemu/config-file.h" |
| 9 | #include "hw/core/boards.h" |
| 10 | |
| 11 | static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc) |
| 12 | { |
| 13 | CommandLineParameterInfoList *param_list = NULL; |
| 14 | CommandLineParameterInfo *info; |
| 15 | int i; |
| 16 | |
| 17 | for (i = 0; desc[i].name != NULL; i++) { |
| 18 | info = g_malloc0(sizeof(*info)); |
| 19 | info->name = g_strdup(desc[i].name); |
| 20 | |
| 21 | switch (desc[i].type) { |
| 22 | case QEMU_OPT_STRING: |
| 23 | info->type = COMMAND_LINE_PARAMETER_TYPE_STRING; |
| 24 | break; |
| 25 | case QEMU_OPT_BOOL: |
| 26 | info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN; |
| 27 | break; |
| 28 | case QEMU_OPT_NUMBER: |
| 29 | info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER; |
| 30 | break; |
| 31 | case QEMU_OPT_SIZE: |
| 32 | info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE; |
| 33 | break; |
| 34 | } |
| 35 | |
| 36 | info->help = g_strdup(desc[i].help); |
| 37 | info->q_default = g_strdup(desc[i].def_value_str); |
| 38 | |
| 39 | QAPI_LIST_PREPEND(param_list, info); |
| 40 | } |
| 41 | |
| 42 | return param_list; |
| 43 | } |
| 44 | |
| 45 | /* remove repeated entry from the info list */ |
| 46 | static void cleanup_infolist(CommandLineParameterInfoList *head) |
| 47 | { |
| 48 | CommandLineParameterInfoList *pre_entry, *cur, *del_entry; |
| 49 | |
| 50 | cur = head; |
| 51 | while (cur->next) { |
| 52 | pre_entry = head; |
| 53 | while (pre_entry != cur->next) { |
| 54 | if (!strcmp(pre_entry->value->name, cur->next->value->name)) { |
| 55 | del_entry = cur->next; |
| 56 | cur->next = cur->next->next; |
| 57 | del_entry->next = NULL; |
| 58 | qapi_free_CommandLineParameterInfoList(del_entry); |
| 59 | break; |
| 60 | } |
| 61 | pre_entry = pre_entry->next; |
| 62 | } |
| 63 | cur = cur->next; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* merge the description items of two parameter infolists */ |
| 68 | static void connect_infolist(CommandLineParameterInfoList *head, |
| 69 | CommandLineParameterInfoList *new) |
| 70 | { |
| 71 | CommandLineParameterInfoList *cur; |
| 72 | |
| 73 | cur = head; |
| 74 | while (cur->next) { |
| 75 | cur = cur->next; |
| 76 | } |
| 77 | cur->next = new; |
| 78 | } |
| 79 | |
| 80 | /* access all the local QemuOptsLists for drive option */ |
| 81 | static CommandLineParameterInfoList *get_drive_infolist(void) |
| 82 | { |
| 83 | CommandLineParameterInfoList *head = NULL, *cur; |
| 84 | int i; |
| 85 | |
| 86 | for (i = 0; drive_config_groups[i] != NULL; i++) { |
| 87 | if (!head) { |
| 88 | head = query_option_descs(drive_config_groups[i]->desc); |
| 89 | } else { |
| 90 | cur = query_option_descs(drive_config_groups[i]->desc); |
| 91 | connect_infolist(head, cur); |
| 92 | } |
| 93 | } |
| 94 | cleanup_infolist(head); |
| 95 | |
| 96 | return head; |
| 97 | } |
| 98 | |
| 99 | static CommandLineParameterInfo *objprop_to_cmdline_prop(ObjectProperty *prop) |
| 100 | { |
| 101 | CommandLineParameterInfo *info; |
| 102 | |
| 103 | info = g_malloc0(sizeof(*info)); |
| 104 | info->name = g_strdup(prop->name); |
| 105 | |
| 106 | if (g_str_equal(prop->type, "bool") || g_str_equal(prop->type, "OnOffAuto")) { |
| 107 | info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN; |
| 108 | } else if (g_str_equal(prop->type, "int")) { |
| 109 | info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER; |
| 110 | } else if (g_str_equal(prop->type, "size")) { |
| 111 | info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE; |
| 112 | } else { |
| 113 | info->type = COMMAND_LINE_PARAMETER_TYPE_STRING; |
| 114 | } |
| 115 | |
| 116 | if (prop->description) { |
| 117 | info->help = g_strdup(prop->description); |
| 118 | } |
| 119 | |
| 120 | return info; |
| 121 | } |
| 122 | |
| 123 | static CommandLineParameterInfoList *query_all_machine_properties(void) |
| 124 | { |
| 125 | CommandLineParameterInfoList *params = NULL, *clpiter; |
| 126 | CommandLineParameterInfo *info; |
| 127 | GSList *machines, *curr_mach; |
| 128 | ObjectPropertyIterator op_iter; |
| 129 | ObjectProperty *prop; |
| 130 | bool is_new; |
| 131 | |
| 132 | machines = object_class_get_list(target_machine_typename(), false); |
| 133 | assert(machines); |
| 134 | |
| 135 | /* Loop over all machine classes */ |
| 136 | for (curr_mach = machines; curr_mach; curr_mach = curr_mach->next) { |
| 137 | object_class_property_iter_init(&op_iter, curr_mach->data); |
| 138 | /* ... and over the properties of each machine: */ |
| 139 | while ((prop = object_property_iter_next(&op_iter))) { |
| 140 | if (!prop->set) { |
| 141 | continue; |
| 142 | } |
| 143 | /* |
| 144 | * Check whether the property has already been put into the list |
| 145 | * (via another machine class) |
| 146 | */ |
| 147 | is_new = true; |
| 148 | for (clpiter = params; clpiter != NULL; clpiter = clpiter->next) { |
| 149 | if (g_str_equal(clpiter->value->name, prop->name)) { |
| 150 | is_new = false; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | /* If it hasn't been added before, add it now to the list */ |
| 155 | if (is_new) { |
| 156 | info = objprop_to_cmdline_prop(prop); |
| 157 | QAPI_LIST_PREPEND(params, info); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | g_slist_free(machines); |
| 163 | |
| 164 | /* Add entry for the "type" parameter */ |
| 165 | info = g_malloc0(sizeof(*info)); |
| 166 | info->name = g_strdup("type"); |
| 167 | info->type = COMMAND_LINE_PARAMETER_TYPE_STRING; |
| 168 | info->help = g_strdup("machine type"); |
| 169 | QAPI_LIST_PREPEND(params, info); |
| 170 | |
| 171 | return params; |
| 172 | } |
| 173 | |
| 174 | CommandLineOptionInfoList *qmp_query_command_line_options(const char *option, |
| 175 | Error **errp) |
| 176 | { |
| 177 | CommandLineOptionInfoList *conf_list = NULL; |
| 178 | CommandLineOptionInfo *info; |
| 179 | int i; |
| 180 | |
| 181 | for (i = 0; vm_config_groups[i] != NULL; i++) { |
| 182 | if (!option || !strcmp(option, vm_config_groups[i]->name)) { |
| 183 | info = g_malloc0(sizeof(*info)); |
| 184 | info->option = g_strdup(vm_config_groups[i]->name); |
| 185 | if (!strcmp("drive", vm_config_groups[i]->name)) { |
| 186 | info->parameters = get_drive_infolist(); |
| 187 | } else { |
| 188 | info->parameters = |
| 189 | query_option_descs(vm_config_groups[i]->desc); |
| 190 | } |
| 191 | QAPI_LIST_PREPEND(conf_list, info); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (!option || !strcmp(option, "machine")) { |
| 196 | info = g_malloc0(sizeof(*info)); |
| 197 | info->option = g_strdup("machine"); |
| 198 | info->parameters = query_all_machine_properties(); |
| 199 | QAPI_LIST_PREPEND(conf_list, info); |
| 200 | } |
| 201 | |
| 202 | if (conf_list == NULL) { |
| 203 | error_setg(errp, "invalid option name: %s", option); |
| 204 | } |
| 205 | |
| 206 | return conf_list; |
| 207 | } |