1 #include "qemu/osdep.h"
2 #include "block/qdict.h" /* for qdict_extract_subqdict() */
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-misc.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qapi/qmp/qlist.h"
7 #include "qemu/error-report.h"
8 #include "qemu/option.h"
9 #include "qemu/config-file.h"
10 #include "hw/boards.h"
12 static QemuOptsList *vm_config_groups[48];
13 static QemuOptsList *drive_config_groups[5];
15 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
20 qemu_load_module_for_opts(group);
21 for (i = 0; lists[i] != NULL; i++) {
22 if (strcmp(lists[i]->name, group) == 0)
25 if (lists[i] == NULL) {
26 error_setg(errp, "There is no option group '%s'", group);
31 QemuOptsList *qemu_find_opts(const char *group)
34 Error *local_err = NULL;
36 ret = find_list(vm_config_groups, group, &local_err);
38 error_report_err(local_err);
44 QemuOpts *qemu_find_opts_singleton(const char *group)
49 list = qemu_find_opts(group);
51 opts = qemu_opts_find(list, NULL);
53 opts = qemu_opts_create(list, NULL, 0, &error_abort);
58 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
60 CommandLineParameterInfoList *param_list = NULL;
61 CommandLineParameterInfo *info;
64 for (i = 0; desc[i].name != NULL; i++) {
65 info = g_malloc0(sizeof(*info));
66 info->name = g_strdup(desc[i].name);
68 switch (desc[i].type) {
70 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
73 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
76 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
79 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
83 info->help = g_strdup(desc[i].help);
84 info->q_default = g_strdup(desc[i].def_value_str);
86 QAPI_LIST_PREPEND(param_list, info);
92 /* remove repeated entry from the info list */
93 static void cleanup_infolist(CommandLineParameterInfoList *head)
95 CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
100 while (pre_entry != cur->next) {
101 if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
102 del_entry = cur->next;
103 cur->next = cur->next->next;
104 del_entry->next = NULL;
105 qapi_free_CommandLineParameterInfoList(del_entry);
108 pre_entry = pre_entry->next;
114 /* merge the description items of two parameter infolists */
115 static void connect_infolist(CommandLineParameterInfoList *head,
116 CommandLineParameterInfoList *new)
118 CommandLineParameterInfoList *cur;
127 /* access all the local QemuOptsLists for drive option */
128 static CommandLineParameterInfoList *get_drive_infolist(void)
130 CommandLineParameterInfoList *head = NULL, *cur;
133 for (i = 0; drive_config_groups[i] != NULL; i++) {
135 head = query_option_descs(drive_config_groups[i]->desc);
137 cur = query_option_descs(drive_config_groups[i]->desc);
138 connect_infolist(head, cur);
141 cleanup_infolist(head);
146 static CommandLineParameterInfo *objprop_to_cmdline_prop(ObjectProperty *prop)
148 CommandLineParameterInfo *info;
150 info = g_malloc0(sizeof(*info));
151 info->name = g_strdup(prop->name);
153 if (g_str_equal(prop->type, "bool") || g_str_equal(prop->type, "OnOffAuto")) {
154 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
155 } else if (g_str_equal(prop->type, "int")) {
156 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
157 } else if (g_str_equal(prop->type, "size")) {
158 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
160 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
163 if (prop->description) {
164 info->help = g_strdup(prop->description);
170 static CommandLineParameterInfoList *query_all_machine_properties(void)
172 CommandLineParameterInfoList *params = NULL, *clpiter;
173 CommandLineParameterInfo *info;
174 GSList *machines, *curr_mach;
175 ObjectPropertyIterator op_iter;
176 ObjectProperty *prop;
179 machines = object_class_get_list(TYPE_MACHINE, false);
182 /* Loop over all machine classes */
183 for (curr_mach = machines; curr_mach; curr_mach = curr_mach->next) {
184 object_class_property_iter_init(&op_iter, curr_mach->data);
185 /* ... and over the properties of each machine: */
186 while ((prop = object_property_iter_next(&op_iter))) {
191 * Check whether the property has already been put into the list
192 * (via another machine class)
195 for (clpiter = params; clpiter != NULL; clpiter = clpiter->next) {
196 if (g_str_equal(clpiter->value->name, prop->name)) {
201 /* If it hasn't been added before, add it now to the list */
203 info = objprop_to_cmdline_prop(prop);
204 QAPI_LIST_PREPEND(params, info);
209 g_slist_free(machines);
211 /* Add entry for the "type" parameter */
212 info = g_malloc0(sizeof(*info));
213 info->name = g_strdup("type");
214 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
215 info->help = g_strdup("machine type");
216 QAPI_LIST_PREPEND(params, info);
221 CommandLineOptionInfoList *qmp_query_command_line_options(const char *option,
224 CommandLineOptionInfoList *conf_list = NULL;
225 CommandLineOptionInfo *info;
228 for (i = 0; vm_config_groups[i] != NULL; i++) {
229 if (!option || !strcmp(option, vm_config_groups[i]->name)) {
230 info = g_malloc0(sizeof(*info));
231 info->option = g_strdup(vm_config_groups[i]->name);
232 if (!strcmp("drive", vm_config_groups[i]->name)) {
233 info->parameters = get_drive_infolist();
236 query_option_descs(vm_config_groups[i]->desc);
238 QAPI_LIST_PREPEND(conf_list, info);
242 if (!option || !strcmp(option, "machine")) {
243 info = g_malloc0(sizeof(*info));
244 info->option = g_strdup("machine");
245 info->parameters = query_all_machine_properties();
246 QAPI_LIST_PREPEND(conf_list, info);
249 if (conf_list == NULL) {
250 error_setg(errp, "invalid option name: %s", option);
256 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
258 return find_list(vm_config_groups, group, errp);
261 void qemu_add_drive_opts(QemuOptsList *list)
265 entries = ARRAY_SIZE(drive_config_groups);
266 entries--; /* keep list NULL terminated */
267 for (i = 0; i < entries; i++) {
268 if (drive_config_groups[i] == NULL) {
269 drive_config_groups[i] = list;
273 fprintf(stderr, "ran out of space in drive_config_groups");
277 void qemu_add_opts(QemuOptsList *list)
281 entries = ARRAY_SIZE(vm_config_groups);
282 entries--; /* keep list NULL terminated */
283 for (i = 0; i < entries; i++) {
284 if (vm_config_groups[i] == NULL) {
285 vm_config_groups[i] = list;
289 fprintf(stderr, "ran out of space in vm_config_groups");
293 /* Returns number of config groups on success, -errno on error */
294 static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque,
295 const char *fname, Error **errp)
298 char line[1024], prev_group[64], group[64], arg[64], value[1024];
301 int res = -EINVAL, lno = 0;
305 while (fgets(line, sizeof(line), fp) != NULL) {
307 if (line[0] == '\n') {
308 /* skip empty lines */
311 if (line[0] == '#') {
315 if (line[0] == '[') {
317 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, value) == 2) {
319 qdict_put_str(qdict, "id", value);
321 } else if (sscanf(line, "[%63[^]]]", group) == 1) {
327 cb(prev_group, prev, opaque, errp);
333 strcpy(prev_group, group);
337 loc_set_file(fname, lno);
339 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
340 sscanf(line, " %63s = \"\"", arg) == 1) {
343 error_setg(errp, "no group defined");
346 qdict_put_str(qdict, arg, value);
349 error_setg(errp, "parse error");
354 error_setg_errno(errp, errno, "Cannot read config file");
359 cb(group, qdict, opaque, errp);
364 qobject_unref(qdict);
368 void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
370 QemuOptsList **lists = opaque;
373 list = find_list(lists, group, errp);
378 qemu_opts_from_qdict(list, qdict, errp);
381 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp)
383 return qemu_config_foreach(fp, qemu_config_do_parse, lists, fname, errp);
386 int qemu_read_config_file(const char *filename, QEMUConfigCB *cb, Error **errp)
388 FILE *f = fopen(filename, "r");
392 error_setg_file_open(errp, errno, filename);
396 ret = qemu_config_foreach(f, cb, vm_config_groups, filename, errp);
401 static bool config_parse_qdict_section(QDict *options, QemuOptsList *opts,
405 g_autoptr(QDict) subqdict = NULL;
406 g_autoptr(QList) list = NULL;
407 size_t orig_size, enum_size;
410 prefix = g_strdup_printf("%s.", opts->name);
411 qdict_extract_subqdict(options, &subqdict, prefix);
413 orig_size = qdict_size(subqdict);
418 subopts = qemu_opts_create(opts, NULL, 0, errp);
423 if (!qemu_opts_absorb_qdict(subopts, subqdict, errp)) {
427 enum_size = qdict_size(subqdict);
428 if (enum_size < orig_size && enum_size) {
429 error_setg(errp, "Unknown option '%s' for [%s]",
430 qdict_first(subqdict)->key, opts->name);
435 /* Multiple, enumerated sections */
436 QListEntry *list_entry;
439 /* Not required anymore */
440 qemu_opts_del(subopts);
442 qdict_array_split(subqdict, &list);
443 if (qdict_size(subqdict)) {
444 error_setg(errp, "Unused option '%s' for [%s]",
445 qdict_first(subqdict)->key, opts->name);
449 QLIST_FOREACH_ENTRY(list, list_entry) {
450 QDict *section = qobject_to(QDict, qlist_entry_obj(list_entry));
454 error_setg(errp, "[%s] section (index %u) does not consist of "
455 "keys", opts->name, i);
459 opt_name = g_strdup_printf("%s.%u", opts->name, i++);
460 subopts = qemu_opts_create(opts, opt_name, 1, errp);
466 if (!qemu_opts_absorb_qdict(subopts, section, errp)) {
467 qemu_opts_del(subopts);
471 if (qdict_size(section)) {
472 error_setg(errp, "[%s] section doesn't support the option '%s'",
473 opts->name, qdict_first(section)->key);
474 qemu_opts_del(subopts);
483 bool qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
488 for (i = 0; lists[i]; i++) {
489 if (!config_parse_qdict_section(options, lists[i], errp)) {