Merge commit '88c8e03f5ddca8c8448dbb99d9de07d87b03ddd6' into upstream-merge
[qemu-kvm/amd-iommu.git] / qemu-config.c
bloba8d9e06ea2c0dbf0ffbd25e2e1ce1642c6780cce
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 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)",
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,
82 },{
83 .name = "boot",
84 .type = QEMU_OPT_BOOL,
85 .help = "make this a boot drive",
87 { /* end if list */ }
91 QemuOptsList qemu_chardev_opts = {
92 .name = "chardev",
93 .implied_opt_name = "backend",
94 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
95 .desc = {
97 .name = "backend",
98 .type = QEMU_OPT_STRING,
99 },{
100 .name = "path",
101 .type = QEMU_OPT_STRING,
103 .name = "host",
104 .type = QEMU_OPT_STRING,
106 .name = "port",
107 .type = QEMU_OPT_STRING,
109 .name = "localaddr",
110 .type = QEMU_OPT_STRING,
112 .name = "localport",
113 .type = QEMU_OPT_STRING,
115 .name = "to",
116 .type = QEMU_OPT_NUMBER,
118 .name = "ipv4",
119 .type = QEMU_OPT_BOOL,
121 .name = "ipv6",
122 .type = QEMU_OPT_BOOL,
124 .name = "wait",
125 .type = QEMU_OPT_BOOL,
127 .name = "server",
128 .type = QEMU_OPT_BOOL,
130 .name = "delay",
131 .type = QEMU_OPT_BOOL,
133 .name = "telnet",
134 .type = QEMU_OPT_BOOL,
136 .name = "width",
137 .type = QEMU_OPT_NUMBER,
139 .name = "height",
140 .type = QEMU_OPT_NUMBER,
142 .name = "cols",
143 .type = QEMU_OPT_NUMBER,
145 .name = "rows",
146 .type = QEMU_OPT_NUMBER,
148 .name = "mux",
149 .type = QEMU_OPT_BOOL,
151 .name = "signal",
152 .type = QEMU_OPT_BOOL,
154 { /* end if list */ }
158 #ifdef CONFIG_LINUX
159 QemuOptsList qemu_fsdev_opts = {
160 .name = "fsdev",
161 .implied_opt_name = "fstype",
162 .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head),
163 .desc = {
165 .name = "fstype",
166 .type = QEMU_OPT_STRING,
167 }, {
168 .name = "path",
169 .type = QEMU_OPT_STRING,
171 { /*End of list */ }
174 #endif
176 #ifdef CONFIG_LINUX
177 QemuOptsList qemu_virtfs_opts = {
178 .name = "virtfs",
179 .implied_opt_name = "fstype",
180 .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head),
181 .desc = {
183 .name = "fstype",
184 .type = QEMU_OPT_STRING,
185 }, {
186 .name = "path",
187 .type = QEMU_OPT_STRING,
188 }, {
189 .name = "mount_tag",
190 .type = QEMU_OPT_STRING,
193 { /*End of list */ }
196 #endif
198 QemuOptsList qemu_device_opts = {
199 .name = "device",
200 .implied_opt_name = "driver",
201 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
202 .desc = {
204 * no elements => accept any
205 * sanity checking will happen later
206 * when setting device properties
208 { /* end if list */ }
212 QemuOptsList qemu_netdev_opts = {
213 .name = "netdev",
214 .implied_opt_name = "type",
215 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
216 .desc = {
218 * no elements => accept any params
219 * validation will happen later
221 { /* end of list */ }
225 QemuOptsList qemu_net_opts = {
226 .name = "net",
227 .implied_opt_name = "type",
228 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
229 .desc = {
231 * no elements => accept any params
232 * validation will happen later
234 { /* end of list */ }
238 QemuOptsList qemu_rtc_opts = {
239 .name = "rtc",
240 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
241 .desc = {
243 .name = "base",
244 .type = QEMU_OPT_STRING,
246 .name = "clock",
247 .type = QEMU_OPT_STRING,
248 #ifdef TARGET_I386
250 .name = "driftfix",
251 .type = QEMU_OPT_STRING,
252 #endif
254 { /* end if list */ }
258 QemuOptsList qemu_global_opts = {
259 .name = "global",
260 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
261 .desc = {
263 .name = "driver",
264 .type = QEMU_OPT_STRING,
266 .name = "property",
267 .type = QEMU_OPT_STRING,
269 .name = "value",
270 .type = QEMU_OPT_STRING,
272 { /* end if list */ }
276 QemuOptsList qemu_mon_opts = {
277 .name = "mon",
278 .implied_opt_name = "chardev",
279 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
280 .desc = {
282 .name = "mode",
283 .type = QEMU_OPT_STRING,
285 .name = "chardev",
286 .type = QEMU_OPT_STRING,
288 .name = "default",
289 .type = QEMU_OPT_BOOL,
291 { /* end if list */ }
295 QemuOptsList qemu_cpudef_opts = {
296 .name = "cpudef",
297 .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head),
298 .desc = {
300 .name = "name",
301 .type = QEMU_OPT_STRING,
303 .name = "level",
304 .type = QEMU_OPT_NUMBER,
306 .name = "vendor",
307 .type = QEMU_OPT_STRING,
309 .name = "family",
310 .type = QEMU_OPT_NUMBER,
312 .name = "model",
313 .type = QEMU_OPT_NUMBER,
315 .name = "stepping",
316 .type = QEMU_OPT_NUMBER,
318 .name = "feature_edx", /* cpuid 0000_0001.edx */
319 .type = QEMU_OPT_STRING,
321 .name = "feature_ecx", /* cpuid 0000_0001.ecx */
322 .type = QEMU_OPT_STRING,
324 .name = "extfeature_edx", /* cpuid 8000_0001.edx */
325 .type = QEMU_OPT_STRING,
327 .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */
328 .type = QEMU_OPT_STRING,
330 .name = "xlevel",
331 .type = QEMU_OPT_NUMBER,
333 .name = "model_id",
334 .type = QEMU_OPT_STRING,
336 .name = "vendor_override",
337 .type = QEMU_OPT_NUMBER,
339 { /* end of list */ }
343 static QemuOptsList *vm_config_groups[] = {
344 &qemu_drive_opts,
345 &qemu_chardev_opts,
346 &qemu_device_opts,
347 &qemu_netdev_opts,
348 &qemu_net_opts,
349 &qemu_rtc_opts,
350 &qemu_global_opts,
351 &qemu_mon_opts,
352 &qemu_cpudef_opts,
353 NULL,
356 static QemuOptsList *find_list(QemuOptsList **lists, const char *group)
358 int i;
360 for (i = 0; lists[i] != NULL; i++) {
361 if (strcmp(lists[i]->name, group) == 0)
362 break;
364 if (lists[i] == NULL) {
365 error_report("there is no option group \"%s\"", group);
367 return lists[i];
370 QemuOptsList *qemu_find_opts(const char *group)
372 return find_list(vm_config_groups, group);
375 int qemu_set_option(const char *str)
377 char group[64], id[64], arg[64];
378 QemuOptsList *list;
379 QemuOpts *opts;
380 int rc, offset;
382 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
383 if (rc < 3 || str[offset] != '=') {
384 error_report("can't parse: \"%s\"", str);
385 return -1;
388 list = qemu_find_opts(group);
389 if (list == NULL) {
390 return -1;
393 opts = qemu_opts_find(list, id);
394 if (!opts) {
395 error_report("there is no %s \"%s\" defined",
396 list->name, id);
397 return -1;
400 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
401 return -1;
403 return 0;
406 int qemu_global_option(const char *str)
408 char driver[64], property[64];
409 QemuOpts *opts;
410 int rc, offset;
412 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
413 if (rc < 2 || str[offset] != '=') {
414 error_report("can't parse: \"%s\"", str);
415 return -1;
418 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
419 qemu_opt_set(opts, "driver", driver);
420 qemu_opt_set(opts, "property", property);
421 qemu_opt_set(opts, "value", str+offset+1);
422 return 0;
425 struct ConfigWriteData {
426 QemuOptsList *list;
427 FILE *fp;
430 static int config_write_opt(const char *name, const char *value, void *opaque)
432 struct ConfigWriteData *data = opaque;
434 fprintf(data->fp, " %s = \"%s\"\n", name, value);
435 return 0;
438 static int config_write_opts(QemuOpts *opts, void *opaque)
440 struct ConfigWriteData *data = opaque;
441 const char *id = qemu_opts_id(opts);
443 if (id) {
444 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
445 } else {
446 fprintf(data->fp, "[%s]\n", data->list->name);
448 qemu_opt_foreach(opts, config_write_opt, data, 0);
449 fprintf(data->fp, "\n");
450 return 0;
453 void qemu_config_write(FILE *fp)
455 struct ConfigWriteData data = { .fp = fp };
456 QemuOptsList **lists = vm_config_groups;
457 int i;
459 fprintf(fp, "# qemu config file\n\n");
460 for (i = 0; lists[i] != NULL; i++) {
461 data.list = lists[i];
462 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
466 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
468 char line[1024], group[64], id[64], arg[64], value[1024];
469 Location loc;
470 QemuOptsList *list = NULL;
471 QemuOpts *opts = NULL;
472 int res = -1, lno = 0;
474 loc_push_none(&loc);
475 while (fgets(line, sizeof(line), fp) != NULL) {
476 loc_set_file(fname, ++lno);
477 if (line[0] == '\n') {
478 /* skip empty lines */
479 continue;
481 if (line[0] == '#') {
482 /* comment */
483 continue;
485 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
486 /* group with id */
487 list = find_list(lists, group);
488 if (list == NULL)
489 goto out;
490 opts = qemu_opts_create(list, id, 1);
491 continue;
493 if (sscanf(line, "[%63[^]]]", group) == 1) {
494 /* group without id */
495 list = find_list(lists, group);
496 if (list == NULL)
497 goto out;
498 opts = qemu_opts_create(list, NULL, 0);
499 continue;
501 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
502 /* arg = value */
503 if (opts == NULL) {
504 error_report("no group defined");
505 goto out;
507 if (qemu_opt_set(opts, arg, value) != 0) {
508 goto out;
510 continue;
512 error_report("parse error");
513 goto out;
515 if (ferror(fp)) {
516 error_report("error reading file");
517 goto out;
519 res = 0;
520 out:
521 loc_pop(&loc);
522 return res;
525 int qemu_read_config_file(const char *filename)
527 FILE *f = fopen(filename, "r");
528 if (f == NULL) {
529 return -errno;
532 if (qemu_config_parse(f, vm_config_groups, filename) != 0) {
533 return -EINVAL;
535 fclose(f);
537 return 0;