Add -incoming help text
[qemu/cris-port.git] / util / qemu-config.c
blob30d6dcf526bf9da1d16651c09ef3e9efed920f91
1 #include "qemu-common.h"
2 #include "qemu/error-report.h"
3 #include "qemu/option.h"
4 #include "qemu/config-file.h"
5 #include "qapi/qmp/qerror.h"
6 #include "qapi/error.h"
7 #include "qmp-commands.h"
9 static QemuOptsList *vm_config_groups[32];
10 static QemuOptsList *drive_config_groups[4];
12 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
13 Error **errp)
15 int i;
17 for (i = 0; lists[i] != NULL; i++) {
18 if (strcmp(lists[i]->name, group) == 0)
19 break;
21 if (lists[i] == NULL) {
22 error_setg(errp, "There is no option group '%s'", group);
24 return lists[i];
27 QemuOptsList *qemu_find_opts(const char *group)
29 QemuOptsList *ret;
30 Error *local_err = NULL;
32 ret = find_list(vm_config_groups, group, &local_err);
33 if (local_err) {
34 error_report_err(local_err);
37 return ret;
40 QemuOpts *qemu_find_opts_singleton(const char *group)
42 QemuOptsList *list;
43 QemuOpts *opts;
45 list = qemu_find_opts(group);
46 assert(list);
47 opts = qemu_opts_find(list, NULL);
48 if (!opts) {
49 opts = qemu_opts_create(list, NULL, 0, &error_abort);
51 return opts;
54 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
56 CommandLineParameterInfoList *param_list = NULL, *entry;
57 CommandLineParameterInfo *info;
58 int i;
60 for (i = 0; desc[i].name != NULL; i++) {
61 info = g_malloc0(sizeof(*info));
62 info->name = g_strdup(desc[i].name);
64 switch (desc[i].type) {
65 case QEMU_OPT_STRING:
66 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
67 break;
68 case QEMU_OPT_BOOL:
69 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
70 break;
71 case QEMU_OPT_NUMBER:
72 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
73 break;
74 case QEMU_OPT_SIZE:
75 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
76 break;
79 if (desc[i].help) {
80 info->has_help = true;
81 info->help = g_strdup(desc[i].help);
83 if (desc[i].def_value_str) {
84 info->has_q_default = true;
85 info->q_default = g_strdup(desc[i].def_value_str);
88 entry = g_malloc0(sizeof(*entry));
89 entry->value = info;
90 entry->next = param_list;
91 param_list = entry;
94 return param_list;
97 /* remove repeated entry from the info list */
98 static void cleanup_infolist(CommandLineParameterInfoList *head)
100 CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
102 cur = head;
103 while (cur->next) {
104 pre_entry = head;
105 while (pre_entry != cur->next) {
106 if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
107 del_entry = cur->next;
108 cur->next = cur->next->next;
109 g_free(del_entry);
110 break;
112 pre_entry = pre_entry->next;
114 cur = cur->next;
118 /* merge the description items of two parameter infolists */
119 static void connect_infolist(CommandLineParameterInfoList *head,
120 CommandLineParameterInfoList *new)
122 CommandLineParameterInfoList *cur;
124 cur = head;
125 while (cur->next) {
126 cur = cur->next;
128 cur->next = new;
131 /* access all the local QemuOptsLists for drive option */
132 static CommandLineParameterInfoList *get_drive_infolist(void)
134 CommandLineParameterInfoList *head = NULL, *cur;
135 int i;
137 for (i = 0; drive_config_groups[i] != NULL; i++) {
138 if (!head) {
139 head = query_option_descs(drive_config_groups[i]->desc);
140 } else {
141 cur = query_option_descs(drive_config_groups[i]->desc);
142 connect_infolist(head, cur);
145 cleanup_infolist(head);
147 return head;
150 /* restore machine options that are now machine's properties */
151 static QemuOptsList machine_opts = {
152 .merge_lists = true,
153 .head = QTAILQ_HEAD_INITIALIZER(machine_opts.head),
154 .desc = {
156 .name = "type",
157 .type = QEMU_OPT_STRING,
158 .help = "emulated machine"
160 .name = "accel",
161 .type = QEMU_OPT_STRING,
162 .help = "accelerator list",
164 .name = "kernel_irqchip",
165 .type = QEMU_OPT_BOOL,
166 .help = "use KVM in-kernel irqchip",
168 .name = "kvm_shadow_mem",
169 .type = QEMU_OPT_SIZE,
170 .help = "KVM shadow MMU size",
172 .name = "kernel",
173 .type = QEMU_OPT_STRING,
174 .help = "Linux kernel image file",
176 .name = "initrd",
177 .type = QEMU_OPT_STRING,
178 .help = "Linux initial ramdisk file",
180 .name = "append",
181 .type = QEMU_OPT_STRING,
182 .help = "Linux kernel command line",
184 .name = "dtb",
185 .type = QEMU_OPT_STRING,
186 .help = "Linux kernel device tree file",
188 .name = "dumpdtb",
189 .type = QEMU_OPT_STRING,
190 .help = "Dump current dtb to a file and quit",
192 .name = "phandle_start",
193 .type = QEMU_OPT_NUMBER,
194 .help = "The first phandle ID we may generate dynamically",
196 .name = "dt_compatible",
197 .type = QEMU_OPT_STRING,
198 .help = "Overrides the \"compatible\" property of the dt root node",
200 .name = "dump-guest-core",
201 .type = QEMU_OPT_BOOL,
202 .help = "Include guest memory in a core dump",
204 .name = "mem-merge",
205 .type = QEMU_OPT_BOOL,
206 .help = "enable/disable memory merge support",
208 .name = "usb",
209 .type = QEMU_OPT_BOOL,
210 .help = "Set on/off to enable/disable usb",
212 .name = "firmware",
213 .type = QEMU_OPT_STRING,
214 .help = "firmware image",
216 .name = "iommu",
217 .type = QEMU_OPT_BOOL,
218 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
220 .name = "suppress-vmdesc",
221 .type = QEMU_OPT_BOOL,
222 .help = "Set on to disable self-describing migration",
224 { /* End of list */ }
228 CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option,
229 const char *option,
230 Error **errp)
232 CommandLineOptionInfoList *conf_list = NULL, *entry;
233 CommandLineOptionInfo *info;
234 int i;
236 for (i = 0; vm_config_groups[i] != NULL; i++) {
237 if (!has_option || !strcmp(option, vm_config_groups[i]->name)) {
238 info = g_malloc0(sizeof(*info));
239 info->option = g_strdup(vm_config_groups[i]->name);
240 if (!strcmp("drive", vm_config_groups[i]->name)) {
241 info->parameters = get_drive_infolist();
242 } else if (!strcmp("machine", vm_config_groups[i]->name)) {
243 info->parameters = query_option_descs(machine_opts.desc);
244 } else {
245 info->parameters =
246 query_option_descs(vm_config_groups[i]->desc);
248 entry = g_malloc0(sizeof(*entry));
249 entry->value = info;
250 entry->next = conf_list;
251 conf_list = entry;
255 if (conf_list == NULL) {
256 error_setg(errp, "invalid option name: %s", option);
259 return conf_list;
262 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
264 return find_list(vm_config_groups, group, errp);
267 void qemu_add_drive_opts(QemuOptsList *list)
269 int entries, i;
271 entries = ARRAY_SIZE(drive_config_groups);
272 entries--; /* keep list NULL terminated */
273 for (i = 0; i < entries; i++) {
274 if (drive_config_groups[i] == NULL) {
275 drive_config_groups[i] = list;
276 return;
279 fprintf(stderr, "ran out of space in drive_config_groups");
280 abort();
283 void qemu_add_opts(QemuOptsList *list)
285 int entries, i;
287 entries = ARRAY_SIZE(vm_config_groups);
288 entries--; /* keep list NULL terminated */
289 for (i = 0; i < entries; i++) {
290 if (vm_config_groups[i] == NULL) {
291 vm_config_groups[i] = list;
292 return;
295 fprintf(stderr, "ran out of space in vm_config_groups");
296 abort();
299 int qemu_set_option(const char *str)
301 Error *local_err = NULL;
302 char group[64], id[64], arg[64];
303 QemuOptsList *list;
304 QemuOpts *opts;
305 int rc, offset;
307 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
308 if (rc < 3 || str[offset] != '=') {
309 error_report("can't parse: \"%s\"", str);
310 return -1;
313 list = qemu_find_opts(group);
314 if (list == NULL) {
315 return -1;
318 opts = qemu_opts_find(list, id);
319 if (!opts) {
320 error_report("there is no %s \"%s\" defined",
321 list->name, id);
322 return -1;
325 qemu_opt_set(opts, arg, str + offset + 1, &local_err);
326 if (local_err) {
327 error_report_err(local_err);
328 return -1;
330 return 0;
333 struct ConfigWriteData {
334 QemuOptsList *list;
335 FILE *fp;
338 static int config_write_opt(const char *name, const char *value, void *opaque)
340 struct ConfigWriteData *data = opaque;
342 fprintf(data->fp, " %s = \"%s\"\n", name, value);
343 return 0;
346 static int config_write_opts(QemuOpts *opts, void *opaque)
348 struct ConfigWriteData *data = opaque;
349 const char *id = qemu_opts_id(opts);
351 if (id) {
352 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
353 } else {
354 fprintf(data->fp, "[%s]\n", data->list->name);
356 qemu_opt_foreach(opts, config_write_opt, data, 0);
357 fprintf(data->fp, "\n");
358 return 0;
361 void qemu_config_write(FILE *fp)
363 struct ConfigWriteData data = { .fp = fp };
364 QemuOptsList **lists = vm_config_groups;
365 int i;
367 fprintf(fp, "# qemu config file\n\n");
368 for (i = 0; lists[i] != NULL; i++) {
369 data.list = lists[i];
370 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
374 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
376 char line[1024], group[64], id[64], arg[64], value[1024];
377 Location loc;
378 QemuOptsList *list = NULL;
379 Error *local_err = NULL;
380 QemuOpts *opts = NULL;
381 int res = -1, lno = 0;
383 loc_push_none(&loc);
384 while (fgets(line, sizeof(line), fp) != NULL) {
385 loc_set_file(fname, ++lno);
386 if (line[0] == '\n') {
387 /* skip empty lines */
388 continue;
390 if (line[0] == '#') {
391 /* comment */
392 continue;
394 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
395 /* group with id */
396 list = find_list(lists, group, &local_err);
397 if (local_err) {
398 error_report_err(local_err);
399 goto out;
401 opts = qemu_opts_create(list, id, 1, NULL);
402 continue;
404 if (sscanf(line, "[%63[^]]]", group) == 1) {
405 /* group without id */
406 list = find_list(lists, group, &local_err);
407 if (local_err) {
408 error_report_err(local_err);
409 goto out;
411 opts = qemu_opts_create(list, NULL, 0, &error_abort);
412 continue;
414 value[0] = '\0';
415 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
416 sscanf(line, " %63s = \"\"", arg) == 1) {
417 /* arg = value */
418 if (opts == NULL) {
419 error_report("no group defined");
420 goto out;
422 qemu_opt_set(opts, arg, value, &local_err);
423 if (local_err) {
424 error_report_err(local_err);
425 goto out;
427 continue;
429 error_report("parse error");
430 goto out;
432 if (ferror(fp)) {
433 error_report("error reading file");
434 goto out;
436 res = 0;
437 out:
438 loc_pop(&loc);
439 return res;
442 int qemu_read_config_file(const char *filename)
444 FILE *f = fopen(filename, "r");
445 int ret;
447 if (f == NULL) {
448 return -errno;
451 ret = qemu_config_parse(f, vm_config_groups, filename);
452 fclose(f);
454 if (ret == 0) {
455 return 0;
456 } else {
457 return -EINVAL;
461 static void config_parse_qdict_section(QDict *options, QemuOptsList *opts,
462 Error **errp)
464 QemuOpts *subopts;
465 QDict *subqdict;
466 QList *list = NULL;
467 Error *local_err = NULL;
468 size_t orig_size, enum_size;
469 char *prefix;
471 prefix = g_strdup_printf("%s.", opts->name);
472 qdict_extract_subqdict(options, &subqdict, prefix);
473 g_free(prefix);
474 orig_size = qdict_size(subqdict);
475 if (!orig_size) {
476 goto out;
479 subopts = qemu_opts_create(opts, NULL, 0, &local_err);
480 if (local_err) {
481 error_propagate(errp, local_err);
482 goto out;
485 qemu_opts_absorb_qdict(subopts, subqdict, &local_err);
486 if (local_err) {
487 error_propagate(errp, local_err);
488 goto out;
491 enum_size = qdict_size(subqdict);
492 if (enum_size < orig_size && enum_size) {
493 error_setg(errp, "Unknown option '%s' for [%s]",
494 qdict_first(subqdict)->key, opts->name);
495 goto out;
498 if (enum_size) {
499 /* Multiple, enumerated sections */
500 QListEntry *list_entry;
501 unsigned i = 0;
503 /* Not required anymore */
504 qemu_opts_del(subopts);
506 qdict_array_split(subqdict, &list);
507 if (qdict_size(subqdict)) {
508 error_setg(errp, "Unused option '%s' for [%s]",
509 qdict_first(subqdict)->key, opts->name);
510 goto out;
513 QLIST_FOREACH_ENTRY(list, list_entry) {
514 QDict *section = qobject_to_qdict(qlist_entry_obj(list_entry));
515 char *opt_name;
517 if (!section) {
518 error_setg(errp, "[%s] section (index %u) does not consist of "
519 "keys", opts->name, i);
520 goto out;
523 opt_name = g_strdup_printf("%s.%u", opts->name, i++);
524 subopts = qemu_opts_create(opts, opt_name, 1, &local_err);
525 g_free(opt_name);
526 if (local_err) {
527 error_propagate(errp, local_err);
528 goto out;
531 qemu_opts_absorb_qdict(subopts, section, &local_err);
532 if (local_err) {
533 error_propagate(errp, local_err);
534 qemu_opts_del(subopts);
535 goto out;
538 if (qdict_size(section)) {
539 error_setg(errp, "[%s] section doesn't support the option '%s'",
540 opts->name, qdict_first(section)->key);
541 qemu_opts_del(subopts);
542 goto out;
547 out:
548 QDECREF(subqdict);
549 QDECREF(list);
552 void qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
553 Error **errp)
555 int i;
556 Error *local_err = NULL;
558 for (i = 0; lists[i]; i++) {
559 config_parse_qdict_section(options, lists[i], &local_err);
560 if (local_err) {
561 error_propagate(errp, local_err);
562 return;