]>
Commit | Line | Data |
---|---|---|
7282a033 | 1 | #include "qemu-common.h" |
2f792016 | 2 | #include "qemu-error.h" |
7282a033 GH |
3 | #include "qemu-option.h" |
4 | #include "qemu-config.h" | |
a861c453 | 5 | #include "sysemu.h" |
d0fef6fb | 6 | #include "hw/qdev.h" |
7282a033 GH |
7 | |
8 | QemuOptsList qemu_drive_opts = { | |
9 | .name = "drive", | |
72cf2d4f | 10 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), |
7282a033 GH |
11 | .desc = { |
12 | { | |
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)", | |
5c6c3a6c CH |
58 | },{ |
59 | .name = "aio", | |
60 | .type = QEMU_OPT_STRING, | |
61 | .help = "host AIO implementation (threads, native)", | |
7282a033 GH |
62 | },{ |
63 | .name = "format", | |
64 | .type = QEMU_OPT_STRING, | |
65 | .help = "disk format (raw, qcow2, ...)", | |
66 | },{ | |
67 | .name = "serial", | |
68 | .type = QEMU_OPT_STRING, | |
e9b2e818 KW |
69 | },{ |
70 | .name = "rerror", | |
71 | .type = QEMU_OPT_STRING, | |
7282a033 GH |
72 | },{ |
73 | .name = "werror", | |
74 | .type = QEMU_OPT_STRING, | |
75 | },{ | |
76 | .name = "addr", | |
77 | .type = QEMU_OPT_STRING, | |
78 | .help = "pci address (virtio only)", | |
59f2689d NS |
79 | },{ |
80 | .name = "readonly", | |
81 | .type = QEMU_OPT_BOOL, | |
7282a033 GH |
82 | }, |
83 | { /* end if list */ } | |
84 | }, | |
85 | }; | |
86 | ||
191bc01b GH |
87 | QemuOptsList qemu_chardev_opts = { |
88 | .name = "chardev", | |
8212c64f | 89 | .implied_opt_name = "backend", |
72cf2d4f | 90 | .head = QTAILQ_HEAD_INITIALIZER(qemu_chardev_opts.head), |
191bc01b | 91 | .desc = { |
7d31544f GH |
92 | { |
93 | .name = "backend", | |
94 | .type = QEMU_OPT_STRING, | |
95 | },{ | |
96 | .name = "path", | |
97 | .type = QEMU_OPT_STRING, | |
aeb2c47a GH |
98 | },{ |
99 | .name = "host", | |
100 | .type = QEMU_OPT_STRING, | |
101 | },{ | |
102 | .name = "port", | |
103 | .type = QEMU_OPT_STRING, | |
7e1b35b4 GH |
104 | },{ |
105 | .name = "localaddr", | |
106 | .type = QEMU_OPT_STRING, | |
107 | },{ | |
108 | .name = "localport", | |
109 | .type = QEMU_OPT_STRING, | |
aeb2c47a GH |
110 | },{ |
111 | .name = "to", | |
112 | .type = QEMU_OPT_NUMBER, | |
113 | },{ | |
114 | .name = "ipv4", | |
115 | .type = QEMU_OPT_BOOL, | |
116 | },{ | |
117 | .name = "ipv6", | |
118 | .type = QEMU_OPT_BOOL, | |
119 | },{ | |
120 | .name = "wait", | |
121 | .type = QEMU_OPT_BOOL, | |
122 | },{ | |
123 | .name = "server", | |
124 | .type = QEMU_OPT_BOOL, | |
125 | },{ | |
126 | .name = "delay", | |
127 | .type = QEMU_OPT_BOOL, | |
128 | },{ | |
129 | .name = "telnet", | |
130 | .type = QEMU_OPT_BOOL, | |
6ea314d9 GH |
131 | },{ |
132 | .name = "width", | |
133 | .type = QEMU_OPT_NUMBER, | |
134 | },{ | |
135 | .name = "height", | |
136 | .type = QEMU_OPT_NUMBER, | |
137 | },{ | |
138 | .name = "cols", | |
139 | .type = QEMU_OPT_NUMBER, | |
140 | },{ | |
141 | .name = "rows", | |
142 | .type = QEMU_OPT_NUMBER, | |
c845f401 GH |
143 | },{ |
144 | .name = "mux", | |
145 | .type = QEMU_OPT_BOOL, | |
5989020b KK |
146 | },{ |
147 | .name = "signal", | |
148 | .type = QEMU_OPT_BOOL, | |
7d31544f | 149 | }, |
191bc01b GH |
150 | { /* end if list */ } |
151 | }, | |
152 | }; | |
153 | ||
74db920c GS |
154 | #ifdef CONFIG_LINUX |
155 | QemuOptsList qemu_fsdev_opts = { | |
156 | .name = "fsdev", | |
157 | .implied_opt_name = "fstype", | |
158 | .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head), | |
159 | .desc = { | |
160 | { | |
161 | .name = "fstype", | |
162 | .type = QEMU_OPT_STRING, | |
163 | }, { | |
164 | .name = "path", | |
165 | .type = QEMU_OPT_STRING, | |
166 | }, | |
167 | { /*End of list */ } | |
168 | }, | |
169 | }; | |
170 | #endif | |
171 | ||
3d54abc7 GS |
172 | #ifdef CONFIG_LINUX |
173 | QemuOptsList qemu_virtfs_opts = { | |
174 | .name = "virtfs", | |
175 | .implied_opt_name = "fstype", | |
176 | .head = QTAILQ_HEAD_INITIALIZER(qemu_virtfs_opts.head), | |
177 | .desc = { | |
178 | { | |
179 | .name = "fstype", | |
180 | .type = QEMU_OPT_STRING, | |
181 | }, { | |
182 | .name = "path", | |
183 | .type = QEMU_OPT_STRING, | |
184 | }, { | |
185 | .name = "mount_tag", | |
186 | .type = QEMU_OPT_STRING, | |
187 | }, | |
188 | ||
189 | { /*End of list */ } | |
190 | }, | |
191 | }; | |
192 | #endif | |
193 | ||
f31d07d1 GH |
194 | QemuOptsList qemu_device_opts = { |
195 | .name = "device", | |
8212c64f | 196 | .implied_opt_name = "driver", |
72cf2d4f | 197 | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), |
f31d07d1 GH |
198 | .desc = { |
199 | /* | |
200 | * no elements => accept any | |
201 | * sanity checking will happen later | |
202 | * when setting device properties | |
203 | */ | |
204 | { /* end if list */ } | |
205 | }, | |
206 | }; | |
207 | ||
a1ea458f MM |
208 | QemuOptsList qemu_netdev_opts = { |
209 | .name = "netdev", | |
8212c64f | 210 | .implied_opt_name = "type", |
a1ea458f MM |
211 | .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), |
212 | .desc = { | |
213 | /* | |
214 | * no elements => accept any params | |
215 | * validation will happen later | |
216 | */ | |
217 | { /* end of list */ } | |
218 | }, | |
219 | }; | |
220 | ||
8119b33d MM |
221 | QemuOptsList qemu_net_opts = { |
222 | .name = "net", | |
8212c64f | 223 | .implied_opt_name = "type", |
8119b33d MM |
224 | .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), |
225 | .desc = { | |
226 | /* | |
227 | * no elements => accept any params | |
228 | * validation will happen later | |
229 | */ | |
230 | { /* end of list */ } | |
231 | }, | |
232 | }; | |
233 | ||
1ed2fc1f JK |
234 | QemuOptsList qemu_rtc_opts = { |
235 | .name = "rtc", | |
236 | .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head), | |
237 | .desc = { | |
238 | { | |
239 | .name = "base", | |
240 | .type = QEMU_OPT_STRING, | |
6875204c JK |
241 | },{ |
242 | .name = "clock", | |
243 | .type = QEMU_OPT_STRING, | |
1ed2fc1f JK |
244 | #ifdef TARGET_I386 |
245 | },{ | |
246 | .name = "driftfix", | |
247 | .type = QEMU_OPT_STRING, | |
248 | #endif | |
249 | }, | |
250 | { /* end if list */ } | |
251 | }, | |
252 | }; | |
253 | ||
d0fef6fb GH |
254 | QemuOptsList qemu_global_opts = { |
255 | .name = "global", | |
256 | .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head), | |
257 | .desc = { | |
258 | { | |
259 | .name = "driver", | |
260 | .type = QEMU_OPT_STRING, | |
261 | },{ | |
262 | .name = "property", | |
263 | .type = QEMU_OPT_STRING, | |
264 | },{ | |
265 | .name = "value", | |
266 | .type = QEMU_OPT_STRING, | |
267 | }, | |
268 | { /* end if list */ } | |
269 | }, | |
270 | }; | |
271 | ||
88589343 GH |
272 | QemuOptsList qemu_mon_opts = { |
273 | .name = "mon", | |
8212c64f | 274 | .implied_opt_name = "chardev", |
88589343 GH |
275 | .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head), |
276 | .desc = { | |
277 | { | |
278 | .name = "mode", | |
279 | .type = QEMU_OPT_STRING, | |
280 | },{ | |
281 | .name = "chardev", | |
282 | .type = QEMU_OPT_STRING, | |
283 | },{ | |
284 | .name = "default", | |
285 | .type = QEMU_OPT_BOOL, | |
286 | }, | |
287 | { /* end if list */ } | |
288 | }, | |
289 | }; | |
290 | ||
b5ec5ce0 | 291 | QemuOptsList qemu_cpudef_opts = { |
292 | .name = "cpudef", | |
293 | .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head), | |
294 | .desc = { | |
295 | { | |
296 | .name = "name", | |
297 | .type = QEMU_OPT_STRING, | |
298 | },{ | |
299 | .name = "level", | |
300 | .type = QEMU_OPT_NUMBER, | |
301 | },{ | |
302 | .name = "vendor", | |
303 | .type = QEMU_OPT_STRING, | |
304 | },{ | |
305 | .name = "family", | |
306 | .type = QEMU_OPT_NUMBER, | |
307 | },{ | |
308 | .name = "model", | |
309 | .type = QEMU_OPT_NUMBER, | |
310 | },{ | |
311 | .name = "stepping", | |
312 | .type = QEMU_OPT_NUMBER, | |
313 | },{ | |
314 | .name = "feature_edx", /* cpuid 0000_0001.edx */ | |
315 | .type = QEMU_OPT_STRING, | |
316 | },{ | |
317 | .name = "feature_ecx", /* cpuid 0000_0001.ecx */ | |
318 | .type = QEMU_OPT_STRING, | |
319 | },{ | |
320 | .name = "extfeature_edx", /* cpuid 8000_0001.edx */ | |
321 | .type = QEMU_OPT_STRING, | |
322 | },{ | |
323 | .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */ | |
324 | .type = QEMU_OPT_STRING, | |
325 | },{ | |
326 | .name = "xlevel", | |
327 | .type = QEMU_OPT_NUMBER, | |
328 | },{ | |
329 | .name = "model_id", | |
330 | .type = QEMU_OPT_STRING, | |
331 | },{ | |
332 | .name = "vendor_override", | |
333 | .type = QEMU_OPT_NUMBER, | |
334 | }, | |
335 | { /* end of list */ } | |
336 | }, | |
337 | }; | |
338 | ||
490b648e | 339 | static QemuOptsList *vm_config_groups[] = { |
d058fe03 | 340 | &qemu_drive_opts, |
191bc01b | 341 | &qemu_chardev_opts, |
f31d07d1 | 342 | &qemu_device_opts, |
a1ea458f | 343 | &qemu_netdev_opts, |
8119b33d | 344 | &qemu_net_opts, |
5fdfbf7e | 345 | &qemu_rtc_opts, |
d0fef6fb | 346 | &qemu_global_opts, |
88589343 | 347 | &qemu_mon_opts, |
b5ec5ce0 | 348 | &qemu_cpudef_opts, |
d058fe03 GH |
349 | NULL, |
350 | }; | |
351 | ||
490b648e | 352 | static QemuOptsList *find_list(QemuOptsList **lists, const char *group) |
ddc97855 GH |
353 | { |
354 | int i; | |
355 | ||
356 | for (i = 0; lists[i] != NULL; i++) { | |
357 | if (strcmp(lists[i]->name, group) == 0) | |
358 | break; | |
359 | } | |
360 | if (lists[i] == NULL) { | |
1ecda02b | 361 | error_report("there is no option group \"%s\"", group); |
ddc97855 GH |
362 | } |
363 | return lists[i]; | |
364 | } | |
365 | ||
490b648e KW |
366 | QemuOptsList *qemu_find_opts(const char *group) |
367 | { | |
368 | return find_list(vm_config_groups, group); | |
369 | } | |
370 | ||
d058fe03 GH |
371 | int qemu_set_option(const char *str) |
372 | { | |
373 | char group[64], id[64], arg[64]; | |
ddc97855 | 374 | QemuOptsList *list; |
d058fe03 | 375 | QemuOpts *opts; |
ddc97855 | 376 | int rc, offset; |
d058fe03 GH |
377 | |
378 | rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset); | |
379 | if (rc < 3 || str[offset] != '=') { | |
1ecda02b | 380 | error_report("can't parse: \"%s\"", str); |
d058fe03 GH |
381 | return -1; |
382 | } | |
383 | ||
304329ee | 384 | list = qemu_find_opts(group); |
ddc97855 | 385 | if (list == NULL) { |
d058fe03 GH |
386 | return -1; |
387 | } | |
388 | ||
ddc97855 | 389 | opts = qemu_opts_find(list, id); |
d058fe03 | 390 | if (!opts) { |
1ecda02b MA |
391 | error_report("there is no %s \"%s\" defined", |
392 | list->name, id); | |
d058fe03 GH |
393 | return -1; |
394 | } | |
395 | ||
3df04ac3 | 396 | if (qemu_opt_set(opts, arg, str+offset+1) == -1) { |
d058fe03 GH |
397 | return -1; |
398 | } | |
399 | return 0; | |
400 | } | |
401 | ||
d0fef6fb GH |
402 | int qemu_global_option(const char *str) |
403 | { | |
404 | char driver[64], property[64]; | |
405 | QemuOpts *opts; | |
406 | int rc, offset; | |
407 | ||
408 | rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset); | |
409 | if (rc < 2 || str[offset] != '=') { | |
1ecda02b | 410 | error_report("can't parse: \"%s\"", str); |
d0fef6fb GH |
411 | return -1; |
412 | } | |
413 | ||
414 | opts = qemu_opts_create(&qemu_global_opts, NULL, 0); | |
415 | qemu_opt_set(opts, "driver", driver); | |
416 | qemu_opt_set(opts, "property", property); | |
417 | qemu_opt_set(opts, "value", str+offset+1); | |
418 | return 0; | |
419 | } | |
420 | ||
9d993394 GH |
421 | struct ConfigWriteData { |
422 | QemuOptsList *list; | |
423 | FILE *fp; | |
424 | }; | |
425 | ||
426 | static int config_write_opt(const char *name, const char *value, void *opaque) | |
427 | { | |
428 | struct ConfigWriteData *data = opaque; | |
429 | ||
430 | fprintf(data->fp, " %s = \"%s\"\n", name, value); | |
431 | return 0; | |
432 | } | |
433 | ||
434 | static int config_write_opts(QemuOpts *opts, void *opaque) | |
435 | { | |
436 | struct ConfigWriteData *data = opaque; | |
437 | const char *id = qemu_opts_id(opts); | |
438 | ||
439 | if (id) { | |
440 | fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id); | |
441 | } else { | |
442 | fprintf(data->fp, "[%s]\n", data->list->name); | |
443 | } | |
444 | qemu_opt_foreach(opts, config_write_opt, data, 0); | |
445 | fprintf(data->fp, "\n"); | |
446 | return 0; | |
447 | } | |
448 | ||
449 | void qemu_config_write(FILE *fp) | |
450 | { | |
451 | struct ConfigWriteData data = { .fp = fp }; | |
490b648e | 452 | QemuOptsList **lists = vm_config_groups; |
9d993394 GH |
453 | int i; |
454 | ||
455 | fprintf(fp, "# qemu config file\n\n"); | |
456 | for (i = 0; lists[i] != NULL; i++) { | |
457 | data.list = lists[i]; | |
458 | qemu_opts_foreach(data.list, config_write_opts, &data, 0); | |
459 | } | |
460 | } | |
42262ba8 | 461 | |
490b648e | 462 | int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) |
42262ba8 GH |
463 | { |
464 | char line[1024], group[64], id[64], arg[64], value[1024]; | |
cf5a65aa | 465 | Location loc; |
42262ba8 GH |
466 | QemuOptsList *list = NULL; |
467 | QemuOpts *opts = NULL; | |
cf5a65aa | 468 | int res = -1, lno = 0; |
42262ba8 | 469 | |
cf5a65aa | 470 | loc_push_none(&loc); |
42262ba8 | 471 | while (fgets(line, sizeof(line), fp) != NULL) { |
cf5a65aa | 472 | loc_set_file(fname, ++lno); |
42262ba8 GH |
473 | if (line[0] == '\n') { |
474 | /* skip empty lines */ | |
475 | continue; | |
476 | } | |
477 | if (line[0] == '#') { | |
478 | /* comment */ | |
479 | continue; | |
480 | } | |
481 | if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) { | |
482 | /* group with id */ | |
490b648e | 483 | list = find_list(lists, group); |
42262ba8 | 484 | if (list == NULL) |
cf5a65aa | 485 | goto out; |
42262ba8 GH |
486 | opts = qemu_opts_create(list, id, 1); |
487 | continue; | |
488 | } | |
489 | if (sscanf(line, "[%63[^]]]", group) == 1) { | |
490 | /* group without id */ | |
490b648e | 491 | list = find_list(lists, group); |
42262ba8 | 492 | if (list == NULL) |
cf5a65aa | 493 | goto out; |
42262ba8 GH |
494 | opts = qemu_opts_create(list, NULL, 0); |
495 | continue; | |
496 | } | |
497 | if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { | |
498 | /* arg = value */ | |
499 | if (opts == NULL) { | |
cf5a65aa MA |
500 | error_report("no group defined"); |
501 | goto out; | |
42262ba8 GH |
502 | } |
503 | if (qemu_opt_set(opts, arg, value) != 0) { | |
cf5a65aa | 504 | goto out; |
42262ba8 GH |
505 | } |
506 | continue; | |
507 | } | |
cf5a65aa MA |
508 | error_report("parse error"); |
509 | goto out; | |
42262ba8 | 510 | } |
ef82516d MA |
511 | if (ferror(fp)) { |
512 | error_report("error reading file"); | |
513 | goto out; | |
514 | } | |
cf5a65aa MA |
515 | res = 0; |
516 | out: | |
517 | loc_pop(&loc); | |
518 | return res; | |
42262ba8 | 519 | } |
dcfb0939 KW |
520 | |
521 | int qemu_read_config_file(const char *filename) | |
522 | { | |
523 | FILE *f = fopen(filename, "r"); | |
524 | if (f == NULL) { | |
525 | return -errno; | |
526 | } | |
527 | ||
490b648e | 528 | if (qemu_config_parse(f, vm_config_groups, filename) != 0) { |
dcfb0939 KW |
529 | return -EINVAL; |
530 | } | |
531 | fclose(f); | |
532 | ||
533 | return 0; | |
534 | } |