1 #include "qemu-common.h"
2 #include "qemu-error.h"
3 #include "qemu-option.h"
4 #include "qemu-config.h"
8 QemuOptsList qemu_drive_opts
= {
10 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
14 .type
= QEMU_OPT_NUMBER
,
18 .type
= QEMU_OPT_NUMBER
,
19 .help
= "unit number (i.e. lun for scsi)",
22 .type
= QEMU_OPT_STRING
,
23 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
26 .type
= QEMU_OPT_NUMBER
,
29 .type
= QEMU_OPT_NUMBER
,
30 .help
= "number of cylinders (ide disk geometry)",
33 .type
= QEMU_OPT_NUMBER
,
34 .help
= "number of heads (ide disk geometry)",
37 .type
= QEMU_OPT_NUMBER
,
38 .help
= "number of sectors (ide disk geometry)",
41 .type
= QEMU_OPT_STRING
,
42 .help
= "chs translation (auto, lba. none)",
45 .type
= QEMU_OPT_STRING
,
46 .help
= "media type (disk, cdrom)",
49 .type
= QEMU_OPT_BOOL
,
52 .type
= QEMU_OPT_STRING
,
56 .type
= QEMU_OPT_STRING
,
57 .help
= "host cache usage (none, writeback, writethrough)",
60 .type
= QEMU_OPT_STRING
,
61 .help
= "host AIO implementation (threads, native)",
64 .type
= QEMU_OPT_STRING
,
65 .help
= "disk format (raw, qcow2, ...)",
68 .type
= QEMU_OPT_STRING
,
71 .type
= QEMU_OPT_STRING
,
74 .type
= QEMU_OPT_STRING
,
77 .type
= QEMU_OPT_STRING
,
78 .help
= "pci address (virtio only)",
81 .type
= QEMU_OPT_BOOL
,
87 QemuOptsList qemu_chardev_opts
= {
89 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
93 .type
= QEMU_OPT_STRING
,
96 .type
= QEMU_OPT_STRING
,
99 .type
= QEMU_OPT_STRING
,
102 .type
= QEMU_OPT_STRING
,
105 .type
= QEMU_OPT_STRING
,
108 .type
= QEMU_OPT_STRING
,
111 .type
= QEMU_OPT_NUMBER
,
114 .type
= QEMU_OPT_BOOL
,
117 .type
= QEMU_OPT_BOOL
,
120 .type
= QEMU_OPT_BOOL
,
123 .type
= QEMU_OPT_BOOL
,
126 .type
= QEMU_OPT_BOOL
,
129 .type
= QEMU_OPT_BOOL
,
132 .type
= QEMU_OPT_NUMBER
,
135 .type
= QEMU_OPT_NUMBER
,
138 .type
= QEMU_OPT_NUMBER
,
141 .type
= QEMU_OPT_NUMBER
,
144 .type
= QEMU_OPT_BOOL
,
147 .type
= QEMU_OPT_BOOL
,
149 { /* end if list */ }
153 QemuOptsList qemu_device_opts
= {
155 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
158 * no elements => accept any
159 * sanity checking will happen later
160 * when setting device properties
162 { /* end if list */ }
166 QemuOptsList qemu_netdev_opts
= {
168 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
171 * no elements => accept any params
172 * validation will happen later
174 { /* end of list */ }
178 QemuOptsList qemu_net_opts
= {
180 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
183 * no elements => accept any params
184 * validation will happen later
186 { /* end of list */ }
190 QemuOptsList qemu_rtc_opts
= {
192 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
196 .type
= QEMU_OPT_STRING
,
199 .type
= QEMU_OPT_STRING
,
203 .type
= QEMU_OPT_STRING
,
206 { /* end if list */ }
210 QemuOptsList qemu_global_opts
= {
212 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
216 .type
= QEMU_OPT_STRING
,
219 .type
= QEMU_OPT_STRING
,
222 .type
= QEMU_OPT_STRING
,
224 { /* end if list */ }
228 QemuOptsList qemu_mon_opts
= {
230 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
234 .type
= QEMU_OPT_STRING
,
237 .type
= QEMU_OPT_STRING
,
240 .type
= QEMU_OPT_BOOL
,
242 { /* end if list */ }
246 QemuOptsList qemu_cpudef_opts
= {
248 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
252 .type
= QEMU_OPT_STRING
,
255 .type
= QEMU_OPT_NUMBER
,
258 .type
= QEMU_OPT_STRING
,
261 .type
= QEMU_OPT_NUMBER
,
264 .type
= QEMU_OPT_NUMBER
,
267 .type
= QEMU_OPT_NUMBER
,
269 .name
= "feature_edx", /* cpuid 0000_0001.edx */
270 .type
= QEMU_OPT_STRING
,
272 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
273 .type
= QEMU_OPT_STRING
,
275 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
276 .type
= QEMU_OPT_STRING
,
278 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
279 .type
= QEMU_OPT_STRING
,
282 .type
= QEMU_OPT_NUMBER
,
285 .type
= QEMU_OPT_STRING
,
287 .name
= "vendor_override",
288 .type
= QEMU_OPT_NUMBER
,
290 { /* end of list */ }
294 static QemuOptsList
*lists
[] = {
307 static QemuOptsList
*find_list(const char *group
)
311 for (i
= 0; lists
[i
] != NULL
; i
++) {
312 if (strcmp(lists
[i
]->name
, group
) == 0)
315 if (lists
[i
] == NULL
) {
316 error_report("there is no option group \"%s\"", group
);
321 int qemu_set_option(const char *str
)
323 char group
[64], id
[64], arg
[64];
328 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
329 if (rc
< 3 || str
[offset
] != '=') {
330 error_report("can't parse: \"%s\"", str
);
334 list
= find_list(group
);
339 opts
= qemu_opts_find(list
, id
);
341 error_report("there is no %s \"%s\" defined",
346 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
352 int qemu_global_option(const char *str
)
354 char driver
[64], property
[64];
358 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
359 if (rc
< 2 || str
[offset
] != '=') {
360 error_report("can't parse: \"%s\"", str
);
364 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
365 qemu_opt_set(opts
, "driver", driver
);
366 qemu_opt_set(opts
, "property", property
);
367 qemu_opt_set(opts
, "value", str
+offset
+1);
371 static int qemu_add_one_global(QemuOpts
*opts
, void *opaque
)
375 g
= qemu_mallocz(sizeof(*g
));
376 g
->driver
= qemu_opt_get(opts
, "driver");
377 g
->property
= qemu_opt_get(opts
, "property");
378 g
->value
= qemu_opt_get(opts
, "value");
379 qdev_prop_register_global(g
);
383 void qemu_add_globals(void)
385 qemu_opts_foreach(&qemu_global_opts
, qemu_add_one_global
, NULL
, 0);
388 struct ConfigWriteData
{
393 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
395 struct ConfigWriteData
*data
= opaque
;
397 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
401 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
403 struct ConfigWriteData
*data
= opaque
;
404 const char *id
= qemu_opts_id(opts
);
407 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
409 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
411 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
412 fprintf(data
->fp
, "\n");
416 void qemu_config_write(FILE *fp
)
418 struct ConfigWriteData data
= { .fp
= fp
};
421 fprintf(fp
, "# qemu config file\n\n");
422 for (i
= 0; lists
[i
] != NULL
; i
++) {
423 data
.list
= lists
[i
];
424 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
428 int qemu_config_parse(FILE *fp
)
430 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
431 QemuOptsList
*list
= NULL
;
432 QemuOpts
*opts
= NULL
;
434 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
435 if (line
[0] == '\n') {
436 /* skip empty lines */
439 if (line
[0] == '#') {
443 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
445 list
= find_list(group
);
448 opts
= qemu_opts_create(list
, id
, 1);
451 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
452 /* group without id */
453 list
= find_list(group
);
456 opts
= qemu_opts_create(list
, NULL
, 0);
459 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
462 fprintf(stderr
, "no group defined\n");
465 if (qemu_opt_set(opts
, arg
, value
) != 0) {
466 fprintf(stderr
, "failed to set \"%s\" for %s\n",
472 fprintf(stderr
, "parse error: %s\n", line
);