Merge commit 'eb47d7c5d96060040931c42773ee07e61e547af9' into upstream-merge
[qemu/qemu-dev-zwu.git] / qemu-config.c
blobefa892c5db085b72fac171738cfa8b108f6f0757
1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
5 #include "hw/qdev.h"
7 static QemuOptsList qemu_drive_opts = {
8 .name = "drive",
9 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
10 .desc = {
12 .name = "bus",
13 .type = QEMU_OPT_NUMBER,
14 .help = "bus number",
15 },{
16 .name = "unit",
17 .type = QEMU_OPT_NUMBER,
18 .help = "unit number (i.e. lun for scsi)",
19 },{
20 .name = "if",
21 .type = QEMU_OPT_STRING,
22 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
23 },{
24 .name = "index",
25 .type = QEMU_OPT_NUMBER,
26 },{
27 .name = "cyls",
28 .type = QEMU_OPT_NUMBER,
29 .help = "number of cylinders (ide disk geometry)",
30 },{
31 .name = "heads",
32 .type = QEMU_OPT_NUMBER,
33 .help = "number of heads (ide disk geometry)",
34 },{
35 .name = "secs",
36 .type = QEMU_OPT_NUMBER,
37 .help = "number of sectors (ide disk geometry)",
38 },{
39 .name = "trans",
40 .type = QEMU_OPT_STRING,
41 .help = "chs translation (auto, lba. none)",
42 },{
43 .name = "media",
44 .type = QEMU_OPT_STRING,
45 .help = "media type (disk, cdrom)",
46 },{
47 .name = "snapshot",
48 .type = QEMU_OPT_BOOL,
49 },{
50 .name = "file",
51 .type = QEMU_OPT_STRING,
52 .help = "disk image",
53 },{
54 .name = "cache",
55 .type = QEMU_OPT_STRING,
56 .help = "host cache usage (none, writeback, writethrough, unsafe)",
57 },{
58 .name = "aio",
59 .type = QEMU_OPT_STRING,
60 .help = "host AIO implementation (threads, native)",
61 },{
62 .name = "format",
63 .type = QEMU_OPT_STRING,
64 .help = "disk format (raw, qcow2, ...)",
65 },{
66 .name = "serial",
67 .type = QEMU_OPT_STRING,
68 },{
69 .name = "rerror",
70 .type = QEMU_OPT_STRING,
71 },{
72 .name = "werror",
73 .type = QEMU_OPT_STRING,
74 },{
75 .name = "addr",
76 .type = QEMU_OPT_STRING,
77 .help = "pci address (virtio only)",
78 },{
79 .name = "readonly",
80 .type = QEMU_OPT_BOOL,
81 },{
82 .name = "boot",
83 .type = QEMU_OPT_BOOL,
84 .help = "make this a boot drive",
86 { /* end of list */ }
90 static QemuOptsList qemu_chardev_opts = {
91 .name = "chardev",
92 .implied_opt_name = "backend",
93 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
94 .desc = {
96 .name = "backend",
97 .type = QEMU_OPT_STRING,
98 },{
99 .name = "path",
100 .type = QEMU_OPT_STRING,
102 .name = "host",
103 .type = QEMU_OPT_STRING,
105 .name = "port",
106 .type = QEMU_OPT_STRING,
108 .name = "localaddr",
109 .type = QEMU_OPT_STRING,
111 .name = "localport",
112 .type = QEMU_OPT_STRING,
114 .name = "to",
115 .type = QEMU_OPT_NUMBER,
117 .name = "ipv4",
118 .type = QEMU_OPT_BOOL,
120 .name = "ipv6",
121 .type = QEMU_OPT_BOOL,
123 .name = "wait",
124 .type = QEMU_OPT_BOOL,
126 .name = "server",
127 .type = QEMU_OPT_BOOL,
129 .name = "delay",
130 .type = QEMU_OPT_BOOL,
132 .name = "telnet",
133 .type = QEMU_OPT_BOOL,
135 .name = "width",
136 .type = QEMU_OPT_NUMBER,
138 .name = "height",
139 .type = QEMU_OPT_NUMBER,
141 .name = "cols",
142 .type = QEMU_OPT_NUMBER,
144 .name = "rows",
145 .type = QEMU_OPT_NUMBER,
147 .name = "mux",
148 .type = QEMU_OPT_BOOL,
150 .name = "signal",
151 .type = QEMU_OPT_BOOL,
153 .name = "name",
154 .type = QEMU_OPT_STRING,
156 .name = "debug",
157 .type = QEMU_OPT_NUMBER,
159 { /* end of list */ }
163 QemuOptsList qemu_fsdev_opts = {
164 .name = "fsdev",
165 .implied_opt_name = "fstype",
166 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
167 .desc = {
169 .name = "fstype",
170 .type = QEMU_OPT_STRING,
171 }, {
172 .name = "path",
173 .type = QEMU_OPT_STRING,
174 }, {
175 .name = "security_model",
176 .type = QEMU_OPT_STRING,
178 { /*End of list */ }
182 QemuOptsList qemu_virtfs_opts = {
183 .name = "virtfs",
184 .implied_opt_name = "fstype",
185 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
186 .desc = {
188 .name = "fstype",
189 .type = QEMU_OPT_STRING,
190 }, {
191 .name = "path",
192 .type = QEMU_OPT_STRING,
193 }, {
194 .name = "mount_tag",
195 .type = QEMU_OPT_STRING,
196 }, {
197 .name = "security_model",
198 .type = QEMU_OPT_STRING,
201 { /*End of list */ }
205 static QemuOptsList qemu_device_opts = {
206 .name = "device",
207 .implied_opt_name = "driver",
208 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
209 .desc = {
211 * no elements => accept any
212 * sanity checking will happen later
213 * when setting device properties
215 { /* end of list */ }
219 static QemuOptsList qemu_netdev_opts = {
220 .name = "netdev",
221 .implied_opt_name = "type",
222 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
223 .desc = {
225 * no elements => accept any params
226 * validation will happen later
228 { /* end of list */ }
232 static QemuOptsList qemu_net_opts = {
233 .name = "net",
234 .implied_opt_name = "type",
235 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
236 .desc = {
238 * no elements => accept any params
239 * validation will happen later
241 { /* end of list */ }
245 static QemuOptsList qemu_rtc_opts = {
246 .name = "rtc",
247 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
248 .desc = {
250 .name = "base",
251 .type = QEMU_OPT_STRING,
253 .name = "clock",
254 .type = QEMU_OPT_STRING,
256 .name = "driftfix",
257 .type = QEMU_OPT_STRING,
259 { /* end of list */ }
263 static QemuOptsList qemu_global_opts = {
264 .name = "global",
265 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
266 .desc = {
268 .name = "driver",
269 .type = QEMU_OPT_STRING,
271 .name = "property",
272 .type = QEMU_OPT_STRING,
274 .name = "value",
275 .type = QEMU_OPT_STRING,
277 { /* end of list */ }
281 static QemuOptsList qemu_mon_opts = {
282 .name = "mon",
283 .implied_opt_name = "chardev",
284 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
285 .desc = {
287 .name = "mode",
288 .type = QEMU_OPT_STRING,
290 .name = "chardev",
291 .type = QEMU_OPT_STRING,
293 .name = "default",
294 .type = QEMU_OPT_BOOL,
296 .name = "pretty",
297 .type = QEMU_OPT_BOOL,
299 { /* end of list */ }
303 #ifdef CONFIG_SIMPLE_TRACE
304 static QemuOptsList qemu_trace_opts = {
305 .name = "trace",
306 .implied_opt_name = "trace",
307 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
308 .desc = {
310 .name = "file",
311 .type = QEMU_OPT_STRING,
313 { /* end of list */ }
316 #endif
318 static QemuOptsList qemu_cpudef_opts = {
319 .name = "cpudef",
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
321 .desc = {
323 .name = "name",
324 .type = QEMU_OPT_STRING,
326 .name = "level",
327 .type = QEMU_OPT_NUMBER,
329 .name = "vendor",
330 .type = QEMU_OPT_STRING,
332 .name = "family",
333 .type = QEMU_OPT_NUMBER,
335 .name = "model",
336 .type = QEMU_OPT_NUMBER,
338 .name = "stepping",
339 .type = QEMU_OPT_NUMBER,
341 .name = "feature_edx", /* cpuid 0000_0001.edx */
342 .type = QEMU_OPT_STRING,
344 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
345 .type = QEMU_OPT_STRING,
347 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
348 .type = QEMU_OPT_STRING,
350 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
351 .type = QEMU_OPT_STRING,
353 .name = "xlevel",
354 .type = QEMU_OPT_NUMBER,
356 .name = "model_id",
357 .type = QEMU_OPT_STRING,
359 .name = "vendor_override",
360 .type = QEMU_OPT_NUMBER,
362 { /* end of list */ }
366 QemuOptsList qemu_spice_opts = {
367 .name = "spice",
368 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
369 .desc = {
371 .name = "port",
372 .type = QEMU_OPT_NUMBER,
374 .name = "tls-port",
375 .type = QEMU_OPT_NUMBER,
377 .name = "addr",
378 .type = QEMU_OPT_STRING,
380 .name = "ipv4",
381 .type = QEMU_OPT_BOOL,
383 .name = "ipv6",
384 .type = QEMU_OPT_BOOL,
386 .name = "password",
387 .type = QEMU_OPT_STRING,
389 .name = "disable-ticketing",
390 .type = QEMU_OPT_BOOL,
392 .name = "disable-copy-paste",
393 .type = QEMU_OPT_BOOL,
395 .name = "sasl",
396 .type = QEMU_OPT_BOOL,
398 .name = "x509-dir",
399 .type = QEMU_OPT_STRING,
401 .name = "x509-key-file",
402 .type = QEMU_OPT_STRING,
404 .name = "x509-key-password",
405 .type = QEMU_OPT_STRING,
407 .name = "x509-cert-file",
408 .type = QEMU_OPT_STRING,
410 .name = "x509-cacert-file",
411 .type = QEMU_OPT_STRING,
413 .name = "x509-dh-key-file",
414 .type = QEMU_OPT_STRING,
416 .name = "tls-ciphers",
417 .type = QEMU_OPT_STRING,
419 .name = "tls-channel",
420 .type = QEMU_OPT_STRING,
422 .name = "plaintext-channel",
423 .type = QEMU_OPT_STRING,
425 .name = "image-compression",
426 .type = QEMU_OPT_STRING,
428 .name = "jpeg-wan-compression",
429 .type = QEMU_OPT_STRING,
431 .name = "zlib-glz-wan-compression",
432 .type = QEMU_OPT_STRING,
434 .name = "streaming-video",
435 .type = QEMU_OPT_STRING,
437 .name = "agent-mouse",
438 .type = QEMU_OPT_BOOL,
440 .name = "playback-compression",
441 .type = QEMU_OPT_BOOL,
443 { /* end of list */ }
447 QemuOptsList qemu_option_rom_opts = {
448 .name = "option-rom",
449 .implied_opt_name = "romfile",
450 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
451 .desc = {
453 .name = "bootindex",
454 .type = QEMU_OPT_NUMBER,
455 }, {
456 .name = "romfile",
457 .type = QEMU_OPT_STRING,
459 { /* end of list */ }
463 static QemuOptsList qemu_machine_opts = {
464 .name = "machine",
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
466 .desc = {
468 .name = "accel",
469 .type = QEMU_OPT_STRING,
470 .help = "accelerator list",
472 { /* End of list */ }
476 static QemuOptsList *vm_config_groups[32] = {
477 &qemu_drive_opts,
478 &qemu_chardev_opts,
479 &qemu_device_opts,
480 &qemu_netdev_opts,
481 &qemu_net_opts,
482 &qemu_rtc_opts,
483 &qemu_global_opts,
484 &qemu_mon_opts,
485 &qemu_cpudef_opts,
486 #ifdef CONFIG_SIMPLE_TRACE
487 &qemu_trace_opts,
488 #endif
489 &qemu_option_rom_opts,
490 &qemu_machine_opts,
491 NULL,
494 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
496 int i;
498 for (i = 0; lists[i] != NULL; i++) {
499 if (strcmp(lists[i]->name, group) == 0)
500 break;
502 if (lists[i] == NULL) {
503 error_report("there is no option group \"%s\"", group);
505 return lists[i];
508 QemuOptsList *qemu_find_opts(const char *group)
510 return find_list(vm_config_groups, group);
513 void qemu_add_opts(QemuOptsList *list)
515 int entries, i;
517 entries = ARRAY_SIZE(vm_config_groups);
518 entries--; /* keep list NULL terminated */
519 for (i = 0; i < entries; i++) {
520 if (vm_config_groups[i] == NULL) {
521 vm_config_groups[i] = list;
522 return;
525 fprintf(stderr, "ran out of space in vm_config_groups");
526 abort();
529 int qemu_set_option(const char *str)
531 char group[64], id[64], arg[64];
532 QemuOptsList *list;
533 QemuOpts *opts;
534 int rc, offset;
536 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
537 if (rc < 3 || str[offset] != '=') {
538 error_report("can't parse: \"%s\"", str);
539 return -1;
542 list = qemu_find_opts(group);
543 if (list == NULL) {
544 return -1;
547 opts = qemu_opts_find(list, id);
548 if (!opts) {
549 error_report("there is no %s \"%s\" defined",
550 list->name, id);
551 return -1;
554 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
555 return -1;
557 return 0;
560 int qemu_global_option(const char *str)
562 char driver[64], property[64];
563 QemuOpts *opts;
564 int rc, offset;
566 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
567 if (rc < 2 || str[offset] != '=') {
568 error_report("can't parse: \"%s\"", str);
569 return -1;
572 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
573 qemu_opt_set(opts, "driver", driver);
574 qemu_opt_set(opts, "property", property);
575 qemu_opt_set(opts, "value", str+offset+1);
576 return 0;
579 struct ConfigWriteData {
580 QemuOptsList *list;
581 FILE *fp;
584 static int config_write_opt(const char *name, const char *value, void *opaque)
586 struct ConfigWriteData *data = opaque;
588 fprintf(data->fp, " %s = \"%s\"\n", name, value);
589 return 0;
592 static int config_write_opts(QemuOpts *opts, void *opaque)
594 struct ConfigWriteData *data = opaque;
595 const char *id = qemu_opts_id(opts);
597 if (id) {
598 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
599 } else {
600 fprintf(data->fp, "[%s]\n", data->list->name);
602 qemu_opt_foreach(opts, config_write_opt, data, 0);
603 fprintf(data->fp, "\n");
604 return 0;
607 void qemu_config_write(FILE *fp)
609 struct ConfigWriteData data = { .fp = fp };
610 QemuOptsList **lists = vm_config_groups;
611 int i;
613 fprintf(fp, "# qemu config file\n\n");
614 for (i = 0; lists[i] != NULL; i++) {
615 data.list = lists[i];
616 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
620 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
622 char line[1024], group[64], id[64], arg[64], value[1024];
623 Location loc;
624 QemuOptsList *list = NULL;
625 QemuOpts *opts = NULL;
626 int res = -1, lno = 0;
628 loc_push_none(&loc);
629 while (fgets(line, sizeof(line), fp) != NULL) {
630 loc_set_file(fname, ++lno);
631 if (line[0] == '\n') {
632 /* skip empty lines */
633 continue;
635 if (line[0] == '#') {
636 /* comment */
637 continue;
639 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
640 /* group with id */
641 list = find_list(lists, group);
642 if (list == NULL)
643 goto out;
644 opts = qemu_opts_create(list, id, 1);
645 continue;
647 if (sscanf(line, "[%63[^]]]", group) == 1) {
648 /* group without id */
649 list = find_list(lists, group);
650 if (list == NULL)
651 goto out;
652 opts = qemu_opts_create(list, NULL, 0);
653 continue;
655 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
656 /* arg = value */
657 if (opts == NULL) {
658 error_report("no group defined");
659 goto out;
661 if (qemu_opt_set(opts, arg, value) != 0) {
662 goto out;
664 continue;
666 error_report("parse error");
667 goto out;
669 if (ferror(fp)) {
670 error_report("error reading file");
671 goto out;
673 res = 0;
674 out:
675 loc_pop(&loc);
676 return res;
679 int qemu_read_config_file(const char *filename)
681 FILE *f = fopen(filename, "r");
682 int ret;
684 if (f == NULL) {
685 return -errno;
688 ret = qemu_config_parse(f, vm_config_groups, filename);
689 fclose(f);
691 if (ret == 0) {
692 return 0;
693 } else {
694 return -EINVAL;