]>
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 | 7 | |
3329f07b | 8 | static QemuOptsList qemu_drive_opts = { |
7282a033 | 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, | |
016f5cf6 | 57 | .help = "host cache usage (none, writeback, writethrough, unsafe)", |
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 | 82 | }, |
26056e0c | 83 | { /* end of list */ } |
7282a033 GH |
84 | }, |
85 | }; | |
86 | ||
3329f07b | 87 | static QemuOptsList qemu_chardev_opts = { |
191bc01b | 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 | }, |
26056e0c | 150 | { /* end of list */ } |
191bc01b GH |
151 | }, |
152 | }; | |
153 | ||
74db920c GS |
154 | QemuOptsList qemu_fsdev_opts = { |
155 | .name = "fsdev", | |
156 | .implied_opt_name = "fstype", | |
157 | .head = QTAILQ_HEAD_INITIALIZER(qemu_fsdev_opts.head), | |
158 | .desc = { | |
159 | { | |
160 | .name = "fstype", | |
161 | .type = QEMU_OPT_STRING, | |
162 | }, { | |
163 | .name = "path", | |
164 | .type = QEMU_OPT_STRING, | |
9ce56db6 VJ |
165 | }, { |
166 | .name = "security_model", | |
167 | .type = QEMU_OPT_STRING, | |
74db920c GS |
168 | }, |
169 | { /*End of list */ } | |
170 | }, | |
171 | }; | |
74db920c | 172 | |
3d54abc7 GS |
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, | |
9ce56db6 VJ |
187 | }, { |
188 | .name = "security_model", | |
189 | .type = QEMU_OPT_STRING, | |
3d54abc7 GS |
190 | }, |
191 | ||
192 | { /*End of list */ } | |
193 | }, | |
194 | }; | |
3d54abc7 | 195 | |
3329f07b | 196 | static QemuOptsList qemu_device_opts = { |
f31d07d1 | 197 | .name = "device", |
8212c64f | 198 | .implied_opt_name = "driver", |
72cf2d4f | 199 | .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head), |
f31d07d1 GH |
200 | .desc = { |
201 | /* | |
202 | * no elements => accept any | |
203 | * sanity checking will happen later | |
204 | * when setting device properties | |
205 | */ | |
26056e0c | 206 | { /* end of list */ } |
f31d07d1 GH |
207 | }, |
208 | }; | |
209 | ||
3329f07b | 210 | static QemuOptsList qemu_netdev_opts = { |
a1ea458f | 211 | .name = "netdev", |
8212c64f | 212 | .implied_opt_name = "type", |
a1ea458f MM |
213 | .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head), |
214 | .desc = { | |
215 | /* | |
216 | * no elements => accept any params | |
217 | * validation will happen later | |
218 | */ | |
219 | { /* end of list */ } | |
220 | }, | |
221 | }; | |
222 | ||
3329f07b | 223 | static QemuOptsList qemu_net_opts = { |
8119b33d | 224 | .name = "net", |
8212c64f | 225 | .implied_opt_name = "type", |
8119b33d MM |
226 | .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head), |
227 | .desc = { | |
228 | /* | |
229 | * no elements => accept any params | |
230 | * validation will happen later | |
231 | */ | |
232 | { /* end of list */ } | |
233 | }, | |
234 | }; | |
235 | ||
3329f07b | 236 | static QemuOptsList qemu_rtc_opts = { |
1ed2fc1f JK |
237 | .name = "rtc", |
238 | .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head), | |
239 | .desc = { | |
240 | { | |
241 | .name = "base", | |
242 | .type = QEMU_OPT_STRING, | |
6875204c JK |
243 | },{ |
244 | .name = "clock", | |
245 | .type = QEMU_OPT_STRING, | |
1ed2fc1f JK |
246 | },{ |
247 | .name = "driftfix", | |
248 | .type = QEMU_OPT_STRING, | |
1ed2fc1f | 249 | }, |
26056e0c | 250 | { /* end of list */ } |
1ed2fc1f JK |
251 | }, |
252 | }; | |
253 | ||
3329f07b | 254 | static QemuOptsList qemu_global_opts = { |
d0fef6fb GH |
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 | }, | |
26056e0c | 268 | { /* end of list */ } |
d0fef6fb GH |
269 | }, |
270 | }; | |
271 | ||
3329f07b | 272 | static QemuOptsList qemu_mon_opts = { |
88589343 | 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 | }, | |
26056e0c | 287 | { /* end of list */ } |
88589343 GH |
288 | }, |
289 | }; | |
290 | ||
ab6540d5 PS |
291 | #ifdef CONFIG_SIMPLE_TRACE |
292 | static QemuOptsList qemu_trace_opts = { | |
293 | .name = "trace", | |
294 | .implied_opt_name = "trace", | |
295 | .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head), | |
296 | .desc = { | |
297 | { | |
298 | .name = "file", | |
299 | .type = QEMU_OPT_STRING, | |
300 | }, | |
301 | { /* end if list */ } | |
302 | }, | |
303 | }; | |
304 | #endif | |
305 | ||
3329f07b | 306 | static QemuOptsList qemu_cpudef_opts = { |
b5ec5ce0 | 307 | .name = "cpudef", |
308 | .head = QTAILQ_HEAD_INITIALIZER(qemu_cpudef_opts.head), | |
309 | .desc = { | |
310 | { | |
311 | .name = "name", | |
312 | .type = QEMU_OPT_STRING, | |
313 | },{ | |
314 | .name = "level", | |
315 | .type = QEMU_OPT_NUMBER, | |
316 | },{ | |
317 | .name = "vendor", | |
318 | .type = QEMU_OPT_STRING, | |
319 | },{ | |
320 | .name = "family", | |
321 | .type = QEMU_OPT_NUMBER, | |
322 | },{ | |
323 | .name = "model", | |
324 | .type = QEMU_OPT_NUMBER, | |
325 | },{ | |
326 | .name = "stepping", | |
327 | .type = QEMU_OPT_NUMBER, | |
328 | },{ | |
329 | .name = "feature_edx", /* cpuid 0000_0001.edx */ | |
330 | .type = QEMU_OPT_STRING, | |
331 | },{ | |
332 | .name = "feature_ecx", /* cpuid 0000_0001.ecx */ | |
333 | .type = QEMU_OPT_STRING, | |
334 | },{ | |
335 | .name = "extfeature_edx", /* cpuid 8000_0001.edx */ | |
336 | .type = QEMU_OPT_STRING, | |
337 | },{ | |
338 | .name = "extfeature_ecx", /* cpuid 8000_0001.ecx */ | |
339 | .type = QEMU_OPT_STRING, | |
340 | },{ | |
341 | .name = "xlevel", | |
342 | .type = QEMU_OPT_NUMBER, | |
343 | },{ | |
344 | .name = "model_id", | |
345 | .type = QEMU_OPT_STRING, | |
346 | },{ | |
347 | .name = "vendor_override", | |
348 | .type = QEMU_OPT_NUMBER, | |
349 | }, | |
350 | { /* end of list */ } | |
351 | }, | |
352 | }; | |
353 | ||
dfe795e7 | 354 | static QemuOptsList *vm_config_groups[32] = { |
d058fe03 | 355 | &qemu_drive_opts, |
191bc01b | 356 | &qemu_chardev_opts, |
f31d07d1 | 357 | &qemu_device_opts, |
a1ea458f | 358 | &qemu_netdev_opts, |
8119b33d | 359 | &qemu_net_opts, |
5fdfbf7e | 360 | &qemu_rtc_opts, |
d0fef6fb | 361 | &qemu_global_opts, |
88589343 | 362 | &qemu_mon_opts, |
b5ec5ce0 | 363 | &qemu_cpudef_opts, |
ab6540d5 PS |
364 | #ifdef CONFIG_SIMPLE_TRACE |
365 | &qemu_trace_opts, | |
366 | #endif | |
d058fe03 GH |
367 | NULL, |
368 | }; | |
369 | ||
490b648e | 370 | static QemuOptsList *find_list(QemuOptsList **lists, const char *group) |
ddc97855 GH |
371 | { |
372 | int i; | |
373 | ||
374 | for (i = 0; lists[i] != NULL; i++) { | |
375 | if (strcmp(lists[i]->name, group) == 0) | |
376 | break; | |
377 | } | |
378 | if (lists[i] == NULL) { | |
1ecda02b | 379 | error_report("there is no option group \"%s\"", group); |
ddc97855 GH |
380 | } |
381 | return lists[i]; | |
382 | } | |
383 | ||
490b648e KW |
384 | QemuOptsList *qemu_find_opts(const char *group) |
385 | { | |
386 | return find_list(vm_config_groups, group); | |
387 | } | |
388 | ||
dfe795e7 GH |
389 | void qemu_add_opts(QemuOptsList *list) |
390 | { | |
391 | int entries, i; | |
392 | ||
393 | entries = ARRAY_SIZE(vm_config_groups); | |
394 | entries--; /* keep list NULL terminated */ | |
395 | for (i = 0; i < entries; i++) { | |
396 | if (vm_config_groups[i] == NULL) { | |
397 | vm_config_groups[i] = list; | |
398 | return; | |
399 | } | |
400 | } | |
401 | fprintf(stderr, "ran out of space in vm_config_groups"); | |
402 | abort(); | |
403 | } | |
404 | ||
d058fe03 GH |
405 | int qemu_set_option(const char *str) |
406 | { | |
407 | char group[64], id[64], arg[64]; | |
ddc97855 | 408 | QemuOptsList *list; |
d058fe03 | 409 | QemuOpts *opts; |
ddc97855 | 410 | int rc, offset; |
d058fe03 GH |
411 | |
412 | rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset); | |
413 | if (rc < 3 || str[offset] != '=') { | |
1ecda02b | 414 | error_report("can't parse: \"%s\"", str); |
d058fe03 GH |
415 | return -1; |
416 | } | |
417 | ||
304329ee | 418 | list = qemu_find_opts(group); |
ddc97855 | 419 | if (list == NULL) { |
d058fe03 GH |
420 | return -1; |
421 | } | |
422 | ||
ddc97855 | 423 | opts = qemu_opts_find(list, id); |
d058fe03 | 424 | if (!opts) { |
1ecda02b MA |
425 | error_report("there is no %s \"%s\" defined", |
426 | list->name, id); | |
d058fe03 GH |
427 | return -1; |
428 | } | |
429 | ||
3df04ac3 | 430 | if (qemu_opt_set(opts, arg, str+offset+1) == -1) { |
d058fe03 GH |
431 | return -1; |
432 | } | |
433 | return 0; | |
434 | } | |
435 | ||
d0fef6fb GH |
436 | int qemu_global_option(const char *str) |
437 | { | |
438 | char driver[64], property[64]; | |
439 | QemuOpts *opts; | |
440 | int rc, offset; | |
441 | ||
442 | rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset); | |
443 | if (rc < 2 || str[offset] != '=') { | |
1ecda02b | 444 | error_report("can't parse: \"%s\"", str); |
d0fef6fb GH |
445 | return -1; |
446 | } | |
447 | ||
448 | opts = qemu_opts_create(&qemu_global_opts, NULL, 0); | |
449 | qemu_opt_set(opts, "driver", driver); | |
450 | qemu_opt_set(opts, "property", property); | |
451 | qemu_opt_set(opts, "value", str+offset+1); | |
452 | return 0; | |
453 | } | |
454 | ||
9d993394 GH |
455 | struct ConfigWriteData { |
456 | QemuOptsList *list; | |
457 | FILE *fp; | |
458 | }; | |
459 | ||
460 | static int config_write_opt(const char *name, const char *value, void *opaque) | |
461 | { | |
462 | struct ConfigWriteData *data = opaque; | |
463 | ||
464 | fprintf(data->fp, " %s = \"%s\"\n", name, value); | |
465 | return 0; | |
466 | } | |
467 | ||
468 | static int config_write_opts(QemuOpts *opts, void *opaque) | |
469 | { | |
470 | struct ConfigWriteData *data = opaque; | |
471 | const char *id = qemu_opts_id(opts); | |
472 | ||
473 | if (id) { | |
474 | fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id); | |
475 | } else { | |
476 | fprintf(data->fp, "[%s]\n", data->list->name); | |
477 | } | |
478 | qemu_opt_foreach(opts, config_write_opt, data, 0); | |
479 | fprintf(data->fp, "\n"); | |
480 | return 0; | |
481 | } | |
482 | ||
483 | void qemu_config_write(FILE *fp) | |
484 | { | |
485 | struct ConfigWriteData data = { .fp = fp }; | |
490b648e | 486 | QemuOptsList **lists = vm_config_groups; |
9d993394 GH |
487 | int i; |
488 | ||
489 | fprintf(fp, "# qemu config file\n\n"); | |
490 | for (i = 0; lists[i] != NULL; i++) { | |
491 | data.list = lists[i]; | |
492 | qemu_opts_foreach(data.list, config_write_opts, &data, 0); | |
493 | } | |
494 | } | |
42262ba8 | 495 | |
490b648e | 496 | int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) |
42262ba8 GH |
497 | { |
498 | char line[1024], group[64], id[64], arg[64], value[1024]; | |
cf5a65aa | 499 | Location loc; |
42262ba8 GH |
500 | QemuOptsList *list = NULL; |
501 | QemuOpts *opts = NULL; | |
cf5a65aa | 502 | int res = -1, lno = 0; |
42262ba8 | 503 | |
cf5a65aa | 504 | loc_push_none(&loc); |
42262ba8 | 505 | while (fgets(line, sizeof(line), fp) != NULL) { |
cf5a65aa | 506 | loc_set_file(fname, ++lno); |
42262ba8 GH |
507 | if (line[0] == '\n') { |
508 | /* skip empty lines */ | |
509 | continue; | |
510 | } | |
511 | if (line[0] == '#') { | |
512 | /* comment */ | |
513 | continue; | |
514 | } | |
515 | if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) { | |
516 | /* group with id */ | |
490b648e | 517 | list = find_list(lists, group); |
42262ba8 | 518 | if (list == NULL) |
cf5a65aa | 519 | goto out; |
42262ba8 GH |
520 | opts = qemu_opts_create(list, id, 1); |
521 | continue; | |
522 | } | |
523 | if (sscanf(line, "[%63[^]]]", group) == 1) { | |
524 | /* group without id */ | |
490b648e | 525 | list = find_list(lists, group); |
42262ba8 | 526 | if (list == NULL) |
cf5a65aa | 527 | goto out; |
42262ba8 GH |
528 | opts = qemu_opts_create(list, NULL, 0); |
529 | continue; | |
530 | } | |
531 | if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { | |
532 | /* arg = value */ | |
533 | if (opts == NULL) { | |
cf5a65aa MA |
534 | error_report("no group defined"); |
535 | goto out; | |
42262ba8 GH |
536 | } |
537 | if (qemu_opt_set(opts, arg, value) != 0) { | |
cf5a65aa | 538 | goto out; |
42262ba8 GH |
539 | } |
540 | continue; | |
541 | } | |
cf5a65aa MA |
542 | error_report("parse error"); |
543 | goto out; | |
42262ba8 | 544 | } |
ef82516d MA |
545 | if (ferror(fp)) { |
546 | error_report("error reading file"); | |
547 | goto out; | |
548 | } | |
cf5a65aa MA |
549 | res = 0; |
550 | out: | |
551 | loc_pop(&loc); | |
552 | return res; | |
42262ba8 | 553 | } |
dcfb0939 KW |
554 | |
555 | int qemu_read_config_file(const char *filename) | |
556 | { | |
557 | FILE *f = fopen(filename, "r"); | |
019e78ba KW |
558 | int ret; |
559 | ||
dcfb0939 KW |
560 | if (f == NULL) { |
561 | return -errno; | |
562 | } | |
563 | ||
019e78ba | 564 | ret = qemu_config_parse(f, vm_config_groups, filename); |
dcfb0939 KW |
565 | fclose(f); |
566 | ||
019e78ba KW |
567 | if (ret == 0) { |
568 | return 0; | |
569 | } else { | |
570 | return -EINVAL; | |
571 | } | |
dcfb0939 | 572 | } |