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