darwin-user: Use GCC_FMT_ATTR (format checking)
[qemu/agraf.git] / qemu-config.c
blob52f18bef3194f2cbbdafea739482b3dcdef11941
1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
5 #include "sysemu.h"
6 #include "hw/qdev.h"
8 static QemuOptsList qemu_drive_opts = {
9 .name = "drive",
10 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
11 .desc = {
13 .name = "bus",
14 .type = QEMU_OPT_NUMBER,
15 .help = "bus number",
16 },{
17 .name = "unit",
18 .type = QEMU_OPT_NUMBER,
19 .help = "unit number (i.e. lun for scsi)",
20 },{
21 .name = "if",
22 .type = QEMU_OPT_STRING,
23 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
24 },{
25 .name = "index",
26 .type = QEMU_OPT_NUMBER,
27 },{
28 .name = "cyls",
29 .type = QEMU_OPT_NUMBER,
30 .help = "number of cylinders (ide disk geometry)",
31 },{
32 .name = "heads",
33 .type = QEMU_OPT_NUMBER,
34 .help = "number of heads (ide disk geometry)",
35 },{
36 .name = "secs",
37 .type = QEMU_OPT_NUMBER,
38 .help = "number of sectors (ide disk geometry)",
39 },{
40 .name = "trans",
41 .type = QEMU_OPT_STRING,
42 .help = "chs translation (auto, lba. none)",
43 },{
44 .name = "media",
45 .type = QEMU_OPT_STRING,
46 .help = "media type (disk, cdrom)",
47 },{
48 .name = "snapshot",
49 .type = QEMU_OPT_BOOL,
50 },{
51 .name = "file",
52 .type = QEMU_OPT_STRING,
53 .help = "disk image",
54 },{
55 .name = "cache",
56 .type = QEMU_OPT_STRING,
57 .help = "host cache usage (none, writeback, writethrough, unsafe)",
58 },{
59 .name = "aio",
60 .type = QEMU_OPT_STRING,
61 .help = "host AIO implementation (threads, native)",
62 },{
63 .name = "format",
64 .type = QEMU_OPT_STRING,
65 .help = "disk format (raw, qcow2, ...)",
66 },{
67 .name = "serial",
68 .type = QEMU_OPT_STRING,
69 },{
70 .name = "rerror",
71 .type = QEMU_OPT_STRING,
72 },{
73 .name = "werror",
74 .type = QEMU_OPT_STRING,
75 },{
76 .name = "addr",
77 .type = QEMU_OPT_STRING,
78 .help = "pci address (virtio only)",
79 },{
80 .name = "readonly",
81 .type = QEMU_OPT_BOOL,
83 { /* end of list */ }
87 static QemuOptsList qemu_chardev_opts = {
88 .name = "chardev",
89 .implied_opt_name = "backend",
90 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
91 .desc = {
93 .name = "backend",
94 .type = QEMU_OPT_STRING,
95 },{
96 .name = "path",
97 .type = QEMU_OPT_STRING,
98 },{
99 .name = "host",
100 .type = QEMU_OPT_STRING,
102 .name = "port",
103 .type = QEMU_OPT_STRING,
105 .name = "localaddr",
106 .type = QEMU_OPT_STRING,
108 .name = "localport",
109 .type = QEMU_OPT_STRING,
111 .name = "to",
112 .type = QEMU_OPT_NUMBER,
114 .name = "ipv4",
115 .type = QEMU_OPT_BOOL,
117 .name = "ipv6",
118 .type = QEMU_OPT_BOOL,
120 .name = "wait",
121 .type = QEMU_OPT_BOOL,
123 .name = "server",
124 .type = QEMU_OPT_BOOL,
126 .name = "delay",
127 .type = QEMU_OPT_BOOL,
129 .name = "telnet",
130 .type = QEMU_OPT_BOOL,
132 .name = "width",
133 .type = QEMU_OPT_NUMBER,
135 .name = "height",
136 .type = QEMU_OPT_NUMBER,
138 .name = "cols",
139 .type = QEMU_OPT_NUMBER,
141 .name = "rows",
142 .type = QEMU_OPT_NUMBER,
144 .name = "mux",
145 .type = QEMU_OPT_BOOL,
147 .name = "signal",
148 .type = QEMU_OPT_BOOL,
150 { /* end of list */ }
154 QemuOptsList qemu_fsdev_opts = {
155 .name = "fsdev",
156 .implied_opt_name = "fstype",
157 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
158 .desc = {
160 .name = "fstype",
161 .type = QEMU_OPT_STRING,
162 }, {
163 .name = "path",
164 .type = QEMU_OPT_STRING,
165 }, {
166 .name = "security_model",
167 .type = QEMU_OPT_STRING,
169 { /*End of list */ }
173 QemuOptsList qemu_virtfs_opts = {
174 .name = "virtfs",
175 .implied_opt_name = "fstype",
176 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
177 .desc = {
179 .name = "fstype",
180 .type = QEMU_OPT_STRING,
181 }, {
182 .name = "path",
183 .type = QEMU_OPT_STRING,
184 }, {
185 .name = "mount_tag",
186 .type = QEMU_OPT_STRING,
187 }, {
188 .name = "security_model",
189 .type = QEMU_OPT_STRING,
192 { /*End of list */ }
196 static QemuOptsList qemu_device_opts = {
197 .name = "device",
198 .implied_opt_name = "driver",
199 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
200 .desc = {
202 * no elements => accept any
203 * sanity checking will happen later
204 * when setting device properties
206 { /* end of list */ }
210 static QemuOptsList qemu_netdev_opts = {
211 .name = "netdev",
212 .implied_opt_name = "type",
213 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
214 .desc = {
216 * no elements => accept any params
217 * validation will happen later
219 { /* end of list */ }
223 static QemuOptsList qemu_net_opts = {
224 .name = "net",
225 .implied_opt_name = "type",
226 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
227 .desc = {
229 * no elements => accept any params
230 * validation will happen later
232 { /* end of list */ }
236 static QemuOptsList qemu_rtc_opts = {
237 .name = "rtc",
238 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
239 .desc = {
241 .name = "base",
242 .type = QEMU_OPT_STRING,
244 .name = "clock",
245 .type = QEMU_OPT_STRING,
247 .name = "driftfix",
248 .type = QEMU_OPT_STRING,
250 { /* end of list */ }
254 static QemuOptsList qemu_global_opts = {
255 .name = "global",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
257 .desc = {
259 .name = "driver",
260 .type = QEMU_OPT_STRING,
262 .name = "property",
263 .type = QEMU_OPT_STRING,
265 .name = "value",
266 .type = QEMU_OPT_STRING,
268 { /* end of list */ }
272 static QemuOptsList qemu_mon_opts = {
273 .name = "mon",
274 .implied_opt_name = "chardev",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
276 .desc = {
278 .name = "mode",
279 .type = QEMU_OPT_STRING,
281 .name = "chardev",
282 .type = QEMU_OPT_STRING,
284 .name = "default",
285 .type = QEMU_OPT_BOOL,
287 .name = "pretty",
288 .type = QEMU_OPT_BOOL,
290 { /* end of list */ }
294 #ifdef CONFIG_SIMPLE_TRACE
295 static QemuOptsList qemu_trace_opts = {
296 .name = "trace",
297 .implied_opt_name = "trace",
298 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
299 .desc = {
301 .name = "file",
302 .type = QEMU_OPT_STRING,
304 { /* end if list */ }
307 #endif
309 static QemuOptsList qemu_cpudef_opts = {
310 .name = "cpudef",
311 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
312 .desc = {
314 .name = "name",
315 .type = QEMU_OPT_STRING,
317 .name = "level",
318 .type = QEMU_OPT_NUMBER,
320 .name = "vendor",
321 .type = QEMU_OPT_STRING,
323 .name = "family",
324 .type = QEMU_OPT_NUMBER,
326 .name = "model",
327 .type = QEMU_OPT_NUMBER,
329 .name = "stepping",
330 .type = QEMU_OPT_NUMBER,
332 .name = "feature_edx", /* cpuid 0000_0001.edx */
333 .type = QEMU_OPT_STRING,
335 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
336 .type = QEMU_OPT_STRING,
338 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
339 .type = QEMU_OPT_STRING,
341 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
342 .type = QEMU_OPT_STRING,
344 .name = "xlevel",
345 .type = QEMU_OPT_NUMBER,
347 .name = "model_id",
348 .type = QEMU_OPT_STRING,
350 .name = "vendor_override",
351 .type = QEMU_OPT_NUMBER,
353 { /* end of list */ }
357 QemuOptsList qemu_spice_opts = {
358 .name = "spice",
359 .head = QTAILQ_HEAD_INITIALIZER(qemu_spice_opts.head),
360 .desc = {
362 .name = "port",
363 .type = QEMU_OPT_NUMBER,
365 .name = "tls-port",
366 .type = QEMU_OPT_NUMBER,
368 .name = "addr",
369 .type = QEMU_OPT_STRING,
371 .name = "ipv4",
372 .type = QEMU_OPT_BOOL,
374 .name = "ipv6",
375 .type = QEMU_OPT_BOOL,
377 .name = "password",
378 .type = QEMU_OPT_STRING,
380 .name = "disable-ticketing",
381 .type = QEMU_OPT_BOOL,
383 .name = "x509-dir",
384 .type = QEMU_OPT_STRING,
386 .name = "x509-key-file",
387 .type = QEMU_OPT_STRING,
389 .name = "x509-key-password",
390 .type = QEMU_OPT_STRING,
392 .name = "x509-cert-file",
393 .type = QEMU_OPT_STRING,
395 .name = "x509-cacert-file",
396 .type = QEMU_OPT_STRING,
398 .name = "x509-dh-key-file",
399 .type = QEMU_OPT_STRING,
401 .name = "tls-ciphers",
402 .type = QEMU_OPT_STRING,
404 .name = "tls-channel",
405 .type = QEMU_OPT_STRING,
407 .name = "plaintext-channel",
408 .type = QEMU_OPT_STRING,
410 .name = "image-compression",
411 .type = QEMU_OPT_STRING,
413 .name = "jpeg-wan-compression",
414 .type = QEMU_OPT_STRING,
416 .name = "zlib-glz-wan-compression",
417 .type = QEMU_OPT_STRING,
419 .name = "streaming-video",
420 .type = QEMU_OPT_STRING,
422 .name = "agent-mouse",
423 .type = QEMU_OPT_BOOL,
425 .name = "playback-compression",
426 .type = QEMU_OPT_BOOL,
428 { /* end if list */ }
432 static QemuOptsList *vm_config_groups[32] = {
433 &qemu_drive_opts,
434 &qemu_chardev_opts,
435 &qemu_device_opts,
436 &qemu_netdev_opts,
437 &qemu_net_opts,
438 &qemu_rtc_opts,
439 &qemu_global_opts,
440 &qemu_mon_opts,
441 &qemu_cpudef_opts,
442 #ifdef CONFIG_SIMPLE_TRACE
443 &qemu_trace_opts,
444 #endif
445 NULL,
448 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
450 int i;
452 for (i = 0; lists[i] != NULL; i++) {
453 if (strcmp(lists[i]->name, group) == 0)
454 break;
456 if (lists[i] == NULL) {
457 error_report("there is no option group \"%s\"", group);
459 return lists[i];
462 QemuOptsList *qemu_find_opts(const char *group)
464 return find_list(vm_config_groups, group);
467 void qemu_add_opts(QemuOptsList *list)
469 int entries, i;
471 entries = ARRAY_SIZE(vm_config_groups);
472 entries--; /* keep list NULL terminated */
473 for (i = 0; i < entries; i++) {
474 if (vm_config_groups[i] == NULL) {
475 vm_config_groups[i] = list;
476 return;
479 fprintf(stderr, "ran out of space in vm_config_groups");
480 abort();
483 int qemu_set_option(const char *str)
485 char group[64], id[64], arg[64];
486 QemuOptsList *list;
487 QemuOpts *opts;
488 int rc, offset;
490 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
491 if (rc < 3 || str[offset] != '=') {
492 error_report("can't parse: \"%s\"", str);
493 return -1;
496 list = qemu_find_opts(group);
497 if (list == NULL) {
498 return -1;
501 opts = qemu_opts_find(list, id);
502 if (!opts) {
503 error_report("there is no %s \"%s\" defined",
504 list->name, id);
505 return -1;
508 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
509 return -1;
511 return 0;
514 int qemu_global_option(const char *str)
516 char driver[64], property[64];
517 QemuOpts *opts;
518 int rc, offset;
520 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
521 if (rc < 2 || str[offset] != '=') {
522 error_report("can't parse: \"%s\"", str);
523 return -1;
526 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
527 qemu_opt_set(opts, "driver", driver);
528 qemu_opt_set(opts, "property", property);
529 qemu_opt_set(opts, "value", str+offset+1);
530 return 0;
533 struct ConfigWriteData {
534 QemuOptsList *list;
535 FILE *fp;
538 static int config_write_opt(const char *name, const char *value, void *opaque)
540 struct ConfigWriteData *data = opaque;
542 fprintf(data->fp, " %s = \"%s\"\n", name, value);
543 return 0;
546 static int config_write_opts(QemuOpts *opts, void *opaque)
548 struct ConfigWriteData *data = opaque;
549 const char *id = qemu_opts_id(opts);
551 if (id) {
552 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
553 } else {
554 fprintf(data->fp, "[%s]\n", data->list->name);
556 qemu_opt_foreach(opts, config_write_opt, data, 0);
557 fprintf(data->fp, "\n");
558 return 0;
561 void qemu_config_write(FILE *fp)
563 struct ConfigWriteData data = { .fp = fp };
564 QemuOptsList **lists = vm_config_groups;
565 int i;
567 fprintf(fp, "# qemu config file\n\n");
568 for (i = 0; lists[i] != NULL; i++) {
569 data.list = lists[i];
570 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
574 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
576 char line[1024], group[64], id[64], arg[64], value[1024];
577 Location loc;
578 QemuOptsList *list = NULL;
579 QemuOpts *opts = NULL;
580 int res = -1, lno = 0;
582 loc_push_none(&loc);
583 while (fgets(line, sizeof(line), fp) != NULL) {
584 loc_set_file(fname, ++lno);
585 if (line[0] == '\n') {
586 /* skip empty lines */
587 continue;
589 if (line[0] == '#') {
590 /* comment */
591 continue;
593 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
594 /* group with id */
595 list = find_list(lists, group);
596 if (list == NULL)
597 goto out;
598 opts = qemu_opts_create(list, id, 1);
599 continue;
601 if (sscanf(line, "[%63[^]]]", group) == 1) {
602 /* group without id */
603 list = find_list(lists, group);
604 if (list == NULL)
605 goto out;
606 opts = qemu_opts_create(list, NULL, 0);
607 continue;
609 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
610 /* arg = value */
611 if (opts == NULL) {
612 error_report("no group defined");
613 goto out;
615 if (qemu_opt_set(opts, arg, value) != 0) {
616 goto out;
618 continue;
620 error_report("parse error");
621 goto out;
623 if (ferror(fp)) {
624 error_report("error reading file");
625 goto out;
627 res = 0;
628 out:
629 loc_pop(&loc);
630 return res;
633 int qemu_read_config_file(const char *filename)
635 FILE *f = fopen(filename, "r");
636 int ret;
638 if (f == NULL) {
639 return -errno;
642 ret = qemu_config_parse(f, vm_config_groups, filename);
643 fclose(f);
645 if (ret == 0) {
646 return 0;
647 } else {
648 return -EINVAL;