1 #include "qemu-common.h"
2 #include "qemu-option.h"
3 #include "qemu-config.h"
7 QemuOptsList qemu_drive_opts
= {
9 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
13 .type
= QEMU_OPT_NUMBER
,
17 .type
= QEMU_OPT_NUMBER
,
18 .help
= "unit number (i.e. lun for scsi)",
21 .type
= QEMU_OPT_STRING
,
22 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
25 .type
= QEMU_OPT_NUMBER
,
28 .type
= QEMU_OPT_NUMBER
,
29 .help
= "number of cylinders (ide disk geometry)",
32 .type
= QEMU_OPT_NUMBER
,
33 .help
= "number of heads (ide disk geometry)",
36 .type
= QEMU_OPT_NUMBER
,
37 .help
= "number of sectors (ide disk geometry)",
40 .type
= QEMU_OPT_STRING
,
41 .help
= "chs translation (auto, lba. none)",
44 .type
= QEMU_OPT_STRING
,
45 .help
= "media type (disk, cdrom)",
48 .type
= QEMU_OPT_BOOL
,
51 .type
= QEMU_OPT_STRING
,
55 .type
= QEMU_OPT_STRING
,
56 .help
= "host cache usage (none, writeback, writethrough)",
59 .type
= QEMU_OPT_STRING
,
60 .help
= "host AIO implementation (threads, native)",
63 .type
= QEMU_OPT_STRING
,
64 .help
= "disk format (raw, qcow2, ...)",
67 .type
= QEMU_OPT_STRING
,
70 .type
= QEMU_OPT_STRING
,
73 .type
= QEMU_OPT_STRING
,
76 .type
= QEMU_OPT_STRING
,
77 .help
= "pci address (virtio only)",
80 .type
= QEMU_OPT_BOOL
,
86 QemuOptsList qemu_chardev_opts
= {
88 .head
= QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts
.head
),
92 .type
= QEMU_OPT_STRING
,
95 .type
= QEMU_OPT_STRING
,
98 .type
= QEMU_OPT_STRING
,
101 .type
= QEMU_OPT_STRING
,
104 .type
= QEMU_OPT_STRING
,
107 .type
= QEMU_OPT_STRING
,
110 .type
= QEMU_OPT_NUMBER
,
113 .type
= QEMU_OPT_BOOL
,
116 .type
= QEMU_OPT_BOOL
,
119 .type
= QEMU_OPT_BOOL
,
122 .type
= QEMU_OPT_BOOL
,
125 .type
= QEMU_OPT_BOOL
,
128 .type
= QEMU_OPT_BOOL
,
131 .type
= QEMU_OPT_NUMBER
,
134 .type
= QEMU_OPT_NUMBER
,
137 .type
= QEMU_OPT_NUMBER
,
140 .type
= QEMU_OPT_NUMBER
,
143 .type
= QEMU_OPT_BOOL
,
146 .type
= QEMU_OPT_BOOL
,
148 { /* end if list */ }
152 QemuOptsList qemu_device_opts
= {
154 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
157 * no elements => accept any
158 * sanity checking will happen later
159 * when setting device properties
161 { /* end if list */ }
165 QemuOptsList qemu_netdev_opts
= {
167 .head
= QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts
.head
),
170 * no elements => accept any params
171 * validation will happen later
173 { /* end of list */ }
177 QemuOptsList qemu_net_opts
= {
179 .head
= QTAILQ_HEAD_INITIALIZER(qemu_net_opts
.head
),
182 * no elements => accept any params
183 * validation will happen later
185 { /* end of list */ }
189 QemuOptsList qemu_rtc_opts
= {
191 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
195 .type
= QEMU_OPT_STRING
,
198 .type
= QEMU_OPT_STRING
,
202 .type
= QEMU_OPT_STRING
,
205 { /* end if list */ }
209 QemuOptsList qemu_global_opts
= {
211 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
215 .type
= QEMU_OPT_STRING
,
218 .type
= QEMU_OPT_STRING
,
221 .type
= QEMU_OPT_STRING
,
223 { /* end if list */ }
227 QemuOptsList qemu_mon_opts
= {
229 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
233 .type
= QEMU_OPT_STRING
,
236 .type
= QEMU_OPT_STRING
,
239 .type
= QEMU_OPT_BOOL
,
241 { /* end if list */ }
245 QemuOptsList qemu_cpudef_opts
= {
247 .head
= QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts
.head
),
251 .type
= QEMU_OPT_STRING
,
254 .type
= QEMU_OPT_NUMBER
,
257 .type
= QEMU_OPT_STRING
,
260 .type
= QEMU_OPT_NUMBER
,
263 .type
= QEMU_OPT_NUMBER
,
266 .type
= QEMU_OPT_NUMBER
,
268 .name
= "feature_edx", /* cpuid 0000_0001.edx */
269 .type
= QEMU_OPT_STRING
,
271 .name
= "feature_ecx", /* cpuid 0000_0001.ecx */
272 .type
= QEMU_OPT_STRING
,
274 .name
= "extfeature_edx", /* cpuid 8000_0001.edx */
275 .type
= QEMU_OPT_STRING
,
277 .name
= "extfeature_ecx", /* cpuid 8000_0001.ecx */
278 .type
= QEMU_OPT_STRING
,
281 .type
= QEMU_OPT_NUMBER
,
284 .type
= QEMU_OPT_STRING
,
286 .name
= "vendor_override",
287 .type
= QEMU_OPT_NUMBER
,
289 { /* end of list */ }
293 static QemuOptsList
*lists
[] = {
306 static QemuOptsList
*find_list(const char *group
)
310 for (i
= 0; lists
[i
] != NULL
; i
++) {
311 if (strcmp(lists
[i
]->name
, group
) == 0)
314 if (lists
[i
] == NULL
) {
315 qemu_error("there is no option group \"%s\"\n", group
);
320 int qemu_set_option(const char *str
)
322 char group
[64], id
[64], arg
[64];
327 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
328 if (rc
< 3 || str
[offset
] != '=') {
329 qemu_error("can't parse: \"%s\"\n", str
);
333 list
= find_list(group
);
338 opts
= qemu_opts_find(list
, id
);
340 qemu_error("there is no %s \"%s\" defined\n",
345 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
351 int qemu_global_option(const char *str
)
353 char driver
[64], property
[64];
357 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
358 if (rc
< 2 || str
[offset
] != '=') {
359 qemu_error("can't parse: \"%s\"\n", str
);
363 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0);
364 qemu_opt_set(opts
, "driver", driver
);
365 qemu_opt_set(opts
, "property", property
);
366 qemu_opt_set(opts
, "value", str
+offset
+1);
370 static int qemu_add_one_global(QemuOpts
*opts
, void *opaque
)
374 g
= qemu_mallocz(sizeof(*g
));
375 g
->driver
= qemu_opt_get(opts
, "driver");
376 g
->property
= qemu_opt_get(opts
, "property");
377 g
->value
= qemu_opt_get(opts
, "value");
378 qdev_prop_register_global(g
);
382 void qemu_add_globals(void)
384 qemu_opts_foreach(&qemu_global_opts
, qemu_add_one_global
, NULL
, 0);
387 struct ConfigWriteData
{
392 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
394 struct ConfigWriteData
*data
= opaque
;
396 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
400 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
402 struct ConfigWriteData
*data
= opaque
;
403 const char *id
= qemu_opts_id(opts
);
406 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
408 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
410 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
411 fprintf(data
->fp
, "\n");
415 void qemu_config_write(FILE *fp
)
417 struct ConfigWriteData data
= { .fp
= fp
};
420 fprintf(fp
, "# qemu config file\n\n");
421 for (i
= 0; lists
[i
] != NULL
; i
++) {
422 data
.list
= lists
[i
];
423 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
427 int qemu_config_parse(FILE *fp
)
429 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
430 QemuOptsList
*list
= NULL
;
431 QemuOpts
*opts
= NULL
;
433 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
434 if (line
[0] == '\n') {
435 /* skip empty lines */
438 if (line
[0] == '#') {
442 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
444 list
= find_list(group
);
447 opts
= qemu_opts_create(list
, id
, 1);
450 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
451 /* group without id */
452 list
= find_list(group
);
455 opts
= qemu_opts_create(list
, NULL
, 0);
458 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
461 fprintf(stderr
, "no group defined\n");
464 if (qemu_opt_set(opts
, arg
, value
) != 0) {
465 fprintf(stderr
, "failed to set \"%s\" for %s\n",
471 fprintf(stderr
, "parse error: %s\n", line
);