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 .implied_opt_name
= "backend",
90 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
94 .type
= QEMU_OPT_STRING
,
97 .type
= QEMU_OPT_STRING
,
100 .type
= QEMU_OPT_STRING
,
103 .type
= QEMU_OPT_STRING
,
106 .type
= QEMU_OPT_STRING
,
109 .type
= QEMU_OPT_STRING
,
112 .type
= QEMU_OPT_NUMBER
,
115 .type
= QEMU_OPT_BOOL
,
118 .type
= QEMU_OPT_BOOL
,
121 .type
= QEMU_OPT_BOOL
,
124 .type
= QEMU_OPT_BOOL
,
127 .type
= QEMU_OPT_BOOL
,
130 .type
= QEMU_OPT_BOOL
,
133 .type
= QEMU_OPT_NUMBER
,
136 .type
= QEMU_OPT_NUMBER
,
139 .type
= QEMU_OPT_NUMBER
,
142 .type
= QEMU_OPT_NUMBER
,
145 .type
= QEMU_OPT_BOOL
,
148 .type
= QEMU_OPT_BOOL
,
150 { /* end if list */ }
154 QemuOptsList qemu_device_opts
= {
156 .implied_opt_name
= "driver",
157 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
160 * no elements => accept any
161 * sanity checking will happen later
162 * when setting device properties
164 { /* end if list */ }
168 QemuOptsList qemu_netdev_opts
= {
170 .implied_opt_name
= "type",
171 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
174 * no elements => accept any params
175 * validation will happen later
177 { /* end of list */ }
181 QemuOptsList qemu_net_opts
= {
183 .implied_opt_name
= "type",
184 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
187 * no elements => accept any params
188 * validation will happen later
190 { /* end of list */ }
194 QemuOptsList qemu_rtc_opts
= {
196 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
200 .type
= QEMU_OPT_STRING
,
203 .type
= QEMU_OPT_STRING
,
207 .type
= QEMU_OPT_STRING
,
210 { /* end if list */ }
214 QemuOptsList qemu_global_opts
= {
216 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
220 .type
= QEMU_OPT_STRING
,
223 .type
= QEMU_OPT_STRING
,
226 .type
= QEMU_OPT_STRING
,
228 { /* end if list */ }
232 QemuOptsList qemu_mon_opts
= {
234 .implied_opt_name
= "chardev",
235 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
239 .type
= QEMU_OPT_STRING
,
242 .type
= QEMU_OPT_STRING
,
245 .type
= QEMU_OPT_BOOL
,
247 { /* end if list */ }
251 QemuOptsList qemu_cpudef_opts
= {
253 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
257 .type
= QEMU_OPT_STRING
,
260 .type
= QEMU_OPT_NUMBER
,
263 .type
= QEMU_OPT_STRING
,
266 .type
= QEMU_OPT_NUMBER
,
269 .type
= QEMU_OPT_NUMBER
,
272 .type
= QEMU_OPT_NUMBER
,
274 .name
= "feature_edx", /* cpuid 0000_0001.edx */
275 .type
= QEMU_OPT_STRING
,
277 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
278 .type
= QEMU_OPT_STRING
,
280 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
281 .type
= QEMU_OPT_STRING
,
283 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
284 .type
= QEMU_OPT_STRING
,
287 .type
= QEMU_OPT_NUMBER
,
290 .type
= QEMU_OPT_STRING
,
292 .name
= "vendor_override",
293 .type
= QEMU_OPT_NUMBER
,
295 { /* end of list */ }
299 static QemuOptsList
*lists
[] = {
312 QemuOptsList
*qemu_find_opts(const char *group
)
316 for (i
= 0; lists
[i
] != NULL
; i
++) {
317 if (strcmp(lists
[i
]->name
, group
) == 0)
320 if (lists
[i
] == NULL
) {
321 error_report("there is no option group \"%s\"", group
);
326 int qemu_set_option(const char *str
)
328 char group
[64], id
[64], arg
[64];
333 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
334 if (rc
< 3 || str
[offset
] != '=') {
335 error_report("can't parse: \"%s\"", str
);
339 list
= qemu_find_opts(group
);
344 opts
= qemu_opts_find(list
, id
);
346 error_report("there is no %s \"%s\" defined",
351 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
357 int qemu_global_option(const char *str
)
359 char driver
[64], property
[64];
363 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
364 if (rc
< 2 || str
[offset
] != '=') {
365 error_report("can't parse: \"%s\"", str
);
369 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
370 qemu_opt_set(opts
, "driver", driver
);
371 qemu_opt_set(opts
, "property", property
);
372 qemu_opt_set(opts
, "value", str
+offset
+1);
376 static int qemu_add_one_global(QemuOpts
*opts
, void *opaque
)
380 g
= qemu_mallocz(sizeof(*g
));
381 g
->driver
= qemu_opt_get(opts
, "driver");
382 g
->property
= qemu_opt_get(opts
, "property");
383 g
->value
= qemu_opt_get(opts
, "value");
384 qdev_prop_register_global(g
);
388 void qemu_add_globals(void)
390 qemu_opts_foreach(&qemu_global_opts
, qemu_add_one_global
, NULL
, 0);
393 struct ConfigWriteData
{
398 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
400 struct ConfigWriteData
*data
= opaque
;
402 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
406 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
408 struct ConfigWriteData
*data
= opaque
;
409 const char *id
= qemu_opts_id(opts
);
412 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
414 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
416 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
417 fprintf(data
->fp
, "\n");
421 void qemu_config_write(FILE *fp
)
423 struct ConfigWriteData data
= { .fp
= fp
};
426 fprintf(fp
, "# qemu config file\n\n");
427 for (i
= 0; lists
[i
] != NULL
; i
++) {
428 data
.list
= lists
[i
];
429 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
433 int qemu_config_parse(FILE *fp
, const char *fname
)
435 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
437 QemuOptsList
*list
= NULL
;
438 QemuOpts
*opts
= NULL
;
439 int res
= -1, lno
= 0;
442 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
443 loc_set_file(fname
, ++lno
);
444 if (line
[0] == '\n') {
445 /* skip empty lines */
448 if (line
[0] == '#') {
452 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
454 list
= qemu_find_opts(group
);
457 opts
= qemu_opts_create(list
, id
, 1);
460 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
461 /* group without id */
462 list
= qemu_find_opts(group
);
465 opts
= qemu_opts_create(list
, NULL
, 0);
468 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
471 error_report("no group defined");
474 if (qemu_opt_set(opts
, arg
, value
) != 0) {
479 error_report("parse error");
483 error_report("error reading file");