Split off sysfs id retrieval
[qemu-kvm/amd-iommu.git] / qemu-config.c
blob2caf76c93745a9ca55c1c2d80b6b905665ab6a54
1 #include "qemu-common.h"
2 #include "qemu-option.h"
3 #include "qemu-config.h"
4 #include "sysemu.h"
5 #include "hw/qdev.h"
7 QemuOptsList qemu_drive_opts = {
8 .name = "drive",
9 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head),
10 .desc = {
12 .name = "bus",
13 .type = QEMU_OPT_NUMBER,
14 .help = "bus number",
15 },{
16 .name = "unit",
17 .type = QEMU_OPT_NUMBER,
18 .help = "unit number (i.e. lun for scsi)",
19 },{
20 .name = "if",
21 .type = QEMU_OPT_STRING,
22 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
23 },{
24 .name = "index",
25 .type = QEMU_OPT_NUMBER,
26 },{
27 .name = "cyls",
28 .type = QEMU_OPT_NUMBER,
29 .help = "number of cylinders (ide disk geometry)",
30 },{
31 .name = "heads",
32 .type = QEMU_OPT_NUMBER,
33 .help = "number of heads (ide disk geometry)",
34 },{
35 .name = "secs",
36 .type = QEMU_OPT_NUMBER,
37 .help = "number of sectors (ide disk geometry)",
38 },{
39 .name = "trans",
40 .type = QEMU_OPT_STRING,
41 .help = "chs translation (auto, lba. none)",
42 },{
43 .name = "media",
44 .type = QEMU_OPT_STRING,
45 .help = "media type (disk, cdrom)",
46 },{
47 .name = "snapshot",
48 .type = QEMU_OPT_BOOL,
49 },{
50 .name = "file",
51 .type = QEMU_OPT_STRING,
52 .help = "disk image",
53 },{
54 .name = "cache",
55 .type = QEMU_OPT_STRING,
56 .help = "host cache usage (none, writeback, writethrough)",
57 },{
58 .name = "aio",
59 .type = QEMU_OPT_STRING,
60 .help = "host AIO implementation (threads, native)",
61 },{
62 .name = "format",
63 .type = QEMU_OPT_STRING,
64 .help = "disk format (raw, qcow2, ...)",
65 },{
66 .name = "serial",
67 .type = QEMU_OPT_STRING,
68 },{
69 .name = "rerror",
70 .type = QEMU_OPT_STRING,
71 },{
72 .name = "werror",
73 .type = QEMU_OPT_STRING,
74 },{
75 .name = "addr",
76 .type = QEMU_OPT_STRING,
77 .help = "pci address (virtio only)",
78 },{
79 .name = "readonly",
80 .type = QEMU_OPT_BOOL,
81 },{
82 .name = "boot",
83 .type = QEMU_OPT_BOOL,
84 .help = "make this a boot drive",
86 { /* end if list */ }
90 QemuOptsList qemu_chardev_opts = {
91 .name = "chardev",
92 .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head),
93 .desc = {
95 .name = "backend",
96 .type = QEMU_OPT_STRING,
97 },{
98 .name = "path",
99 .type = QEMU_OPT_STRING,
101 .name = "host",
102 .type = QEMU_OPT_STRING,
104 .name = "port",
105 .type = QEMU_OPT_STRING,
107 .name = "localaddr",
108 .type = QEMU_OPT_STRING,
110 .name = "localport",
111 .type = QEMU_OPT_STRING,
113 .name = "to",
114 .type = QEMU_OPT_NUMBER,
116 .name = "ipv4",
117 .type = QEMU_OPT_BOOL,
119 .name = "ipv6",
120 .type = QEMU_OPT_BOOL,
122 .name = "wait",
123 .type = QEMU_OPT_BOOL,
125 .name = "server",
126 .type = QEMU_OPT_BOOL,
128 .name = "delay",
129 .type = QEMU_OPT_BOOL,
131 .name = "telnet",
132 .type = QEMU_OPT_BOOL,
134 .name = "width",
135 .type = QEMU_OPT_NUMBER,
137 .name = "height",
138 .type = QEMU_OPT_NUMBER,
140 .name = "cols",
141 .type = QEMU_OPT_NUMBER,
143 .name = "rows",
144 .type = QEMU_OPT_NUMBER,
146 .name = "mux",
147 .type = QEMU_OPT_BOOL,
149 .name = "signal",
150 .type = QEMU_OPT_BOOL,
152 { /* end if list */ }
156 QemuOptsList qemu_device_opts = {
157 .name = "device",
158 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
159 .desc = {
161 * no elements => accept any
162 * sanity checking will happen later
163 * when setting device properties
165 { /* end if list */ }
169 QemuOptsList qemu_netdev_opts = {
170 .name = "netdev",
171 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
172 .desc = {
174 * no elements => accept any params
175 * validation will happen later
177 { /* end of list */ }
181 QemuOptsList qemu_net_opts = {
182 .name = "net",
183 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
184 .desc = {
186 * no elements => accept any params
187 * validation will happen later
189 { /* end of list */ }
193 QemuOptsList qemu_rtc_opts = {
194 .name = "rtc",
195 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
196 .desc = {
198 .name = "base",
199 .type = QEMU_OPT_STRING,
201 .name = "clock",
202 .type = QEMU_OPT_STRING,
203 #ifdef TARGET_I386
205 .name = "driftfix",
206 .type = QEMU_OPT_STRING,
207 #endif
209 { /* end if list */ }
213 QemuOptsList qemu_global_opts = {
214 .name = "global",
215 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
216 .desc = {
218 .name = "driver",
219 .type = QEMU_OPT_STRING,
221 .name = "property",
222 .type = QEMU_OPT_STRING,
224 .name = "value",
225 .type = QEMU_OPT_STRING,
227 { /* end if list */ }
231 QemuOptsList qemu_mon_opts = {
232 .name = "mon",
233 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
234 .desc = {
236 .name = "mode",
237 .type = QEMU_OPT_STRING,
239 .name = "chardev",
240 .type = QEMU_OPT_STRING,
242 .name = "default",
243 .type = QEMU_OPT_BOOL,
245 { /* end if list */ }
249 static QemuOptsList *lists[] = {
250 &qemu_drive_opts,
251 &qemu_chardev_opts,
252 &qemu_device_opts,
253 &qemu_netdev_opts,
254 &qemu_net_opts,
255 &qemu_rtc_opts,
256 &qemu_global_opts,
257 &qemu_mon_opts,
258 NULL,
261 static QemuOptsList *find_list(const char *group)
263 int i;
265 for (i = 0; lists[i] != NULL; i++) {
266 if (strcmp(lists[i]->name, group) == 0)
267 break;
269 if (lists[i] == NULL) {
270 qemu_error("there is no option group \"%s\"\n", group);
272 return lists[i];
275 int qemu_set_option(const char *str)
277 char group[64], id[64], arg[64];
278 QemuOptsList *list;
279 QemuOpts *opts;
280 int rc, offset;
282 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
283 if (rc < 3 || str[offset] != '=') {
284 qemu_error("can't parse: \"%s\"\n", str);
285 return -1;
288 list = find_list(group);
289 if (list == NULL) {
290 return -1;
293 opts = qemu_opts_find(list, id);
294 if (!opts) {
295 qemu_error("there is no %s \"%s\" defined\n",
296 list->name, id);
297 return -1;
300 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
301 return -1;
303 return 0;
306 int qemu_global_option(const char *str)
308 char driver[64], property[64];
309 QemuOpts *opts;
310 int rc, offset;
312 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
313 if (rc < 2 || str[offset] != '=') {
314 qemu_error("can't parse: \"%s\"\n", str);
315 return -1;
318 opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
319 qemu_opt_set(opts, "driver", driver);
320 qemu_opt_set(opts, "property", property);
321 qemu_opt_set(opts, "value", str+offset+1);
322 return 0;
325 static int qemu_add_one_global(QemuOpts *opts, void *opaque)
327 GlobalProperty *g;
329 g = qemu_mallocz(sizeof(*g));
330 g->driver = qemu_opt_get(opts, "driver");
331 g->property = qemu_opt_get(opts, "property");
332 g->value = qemu_opt_get(opts, "value");
333 qdev_prop_register_global(g);
334 return 0;
337 void qemu_add_globals(void)
339 qemu_opts_foreach(&qemu_global_opts, qemu_add_one_global, NULL, 0);
342 struct ConfigWriteData {
343 QemuOptsList *list;
344 FILE *fp;
347 static int config_write_opt(const char *name, const char *value, void *opaque)
349 struct ConfigWriteData *data = opaque;
351 fprintf(data->fp, " %s = \"%s\"\n", name, value);
352 return 0;
355 static int config_write_opts(QemuOpts *opts, void *opaque)
357 struct ConfigWriteData *data = opaque;
358 const char *id = qemu_opts_id(opts);
360 if (id) {
361 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
362 } else {
363 fprintf(data->fp, "[%s]\n", data->list->name);
365 qemu_opt_foreach(opts, config_write_opt, data, 0);
366 fprintf(data->fp, "\n");
367 return 0;
370 void qemu_config_write(FILE *fp)
372 struct ConfigWriteData data = { .fp = fp };
373 int i;
375 fprintf(fp, "# qemu config file\n\n");
376 for (i = 0; lists[i] != NULL; i++) {
377 data.list = lists[i];
378 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
382 int qemu_config_parse(FILE *fp)
384 char line[1024], group[64], id[64], arg[64], value[1024];
385 QemuOptsList *list = NULL;
386 QemuOpts *opts = NULL;
388 while (fgets(line, sizeof(line), fp) != NULL) {
389 if (line[0] == '\n') {
390 /* skip empty lines */
391 continue;
393 if (line[0] == '#') {
394 /* comment */
395 continue;
397 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
398 /* group with id */
399 list = find_list(group);
400 if (list == NULL)
401 return -1;
402 opts = qemu_opts_create(list, id, 1);
403 continue;
405 if (sscanf(line, "[%63[^]]]", group) == 1) {
406 /* group without id */
407 list = find_list(group);
408 if (list == NULL)
409 return -1;
410 opts = qemu_opts_create(list, NULL, 0);
411 continue;
413 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
414 /* arg = value */
415 if (opts == NULL) {
416 fprintf(stderr, "no group defined\n");
417 return -1;
419 if (qemu_opt_set(opts, arg, value) != 0) {
420 fprintf(stderr, "failed to set \"%s\" for %s\n",
421 arg, group);
422 return -1;
424 continue;
426 fprintf(stderr, "parse error: %s\n", line);
427 return -1;
429 return 0;